/work/dcmtk-install/include/dcmtk/oflog/helpers/pointer.h
Line | Count | Source |
1 | | // -*- C++ -*- |
2 | | // Module: Log4CPLUS |
3 | | // File: pointer.h |
4 | | // Created: 6/2001 |
5 | | // Author: Tad E. Smith |
6 | | // |
7 | | // |
8 | | // Copyright 2001-2010 Tad E. Smith |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
11 | | // you may not use this file except in compliance with the License. |
12 | | // You may obtain a copy of the License at |
13 | | // |
14 | | // http://www.apache.org/licenses/LICENSE-2.0 |
15 | | // |
16 | | // Unless required by applicable law or agreed to in writing, software |
17 | | // distributed under the License is distributed on an "AS IS" BASIS, |
18 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
19 | | // See the License for the specific language governing permissions and |
20 | | // limitations under the License. |
21 | | |
22 | | // |
23 | | // Note: Some of this code uses ideas from "More Effective C++" by Scott |
24 | | // Myers, Addison Wesley Longmain, Inc., (c) 1996, Chapter 29, pp. 183-213 |
25 | | // |
26 | | |
27 | | /** @file */ |
28 | | |
29 | | #ifndef DCMTK_LOG4CPLUS_HELPERS_POINTERS_HEADER_ |
30 | | #define DCMTK_LOG4CPLUS_HELPERS_POINTERS_HEADER_ |
31 | | |
32 | | #include "dcmtk/oflog/config.h" |
33 | | |
34 | | #if defined (DCMTK_LOG4CPLUS_HAVE_PRAGMA_ONCE) |
35 | | #pragma once |
36 | | #endif |
37 | | |
38 | | #include "dcmtk/oflog/thread/syncprim.h" |
39 | | #include <algorithm> |
40 | | #include <cassert> |
41 | | #if ! defined (DCMTK_LOG4CPLUS_SINGLE_THREADED) \ |
42 | | && defined (DCMTK_LOG4CPLUS_HAVE_CXX11_ATOMICS) |
43 | | #include <atomic> |
44 | | #endif |
45 | | |
46 | | |
47 | | namespace dcmtk { |
48 | | namespace log4cplus { |
49 | | namespace helpers { |
50 | | |
51 | | /****************************************************************************** |
52 | | * Class SharedObject (from pp. 204-205) * |
53 | | ******************************************************************************/ |
54 | | |
55 | | class DCMTK_LOG4CPLUS_EXPORT SharedObject |
56 | | { |
57 | | public: |
58 | | void addReference() const; |
59 | | void removeReference() const; |
60 | | |
61 | | protected: |
62 | | // Ctor |
63 | | SharedObject() |
64 | | : access_mutex() |
65 | | , count(0) |
66 | | { } |
67 | | |
68 | | SharedObject(const SharedObject&) |
69 | | : access_mutex() |
70 | | , count(0) |
71 | 0 | { } |
72 | | |
73 | | // Dtor |
74 | | virtual ~SharedObject(); |
75 | | |
76 | | // Operators |
77 | 0 | SharedObject& operator=(const SharedObject&) { return *this; } |
78 | | |
79 | | public: |
80 | | thread::Mutex access_mutex; |
81 | | |
82 | | private: |
83 | | #if defined (DCMTK_LOG4CPLUS_SINGLE_THREADED) |
84 | | typedef unsigned count_type; |
85 | | #elif defined (DCMTK_LOG4CPLUS_HAVE_CXX11_ATOMICS) |
86 | | typedef STD_NAMESPACE atomic<unsigned> count_type; |
87 | | #elif defined (_WIN32) || defined (__CYGWIN__) |
88 | | typedef long count_type; |
89 | | #else |
90 | | typedef unsigned count_type; |
91 | | #endif |
92 | | mutable count_type count; |
93 | | }; |
94 | | |
95 | | |
96 | | /****************************************************************************** |
97 | | * Template Class SharedObjectPtr (from pp. 203, 206) * |
98 | | ******************************************************************************/ |
99 | | template<class T> |
100 | | class SharedObjectPtr |
101 | | { |
102 | | public: |
103 | | // Ctor |
104 | | explicit |
105 | | SharedObjectPtr(T* realPtr = 0) |
106 | | : pointee(realPtr) |
107 | | { |
108 | | addref (); |
109 | | } |
110 | | |
111 | | SharedObjectPtr(const SharedObjectPtr& rhs) |
112 | | : pointee(rhs.pointee) |
113 | | { |
114 | | addref (); |
115 | | } |
116 | | |
117 | | #if defined (DCMTK_LOG4CPLUS_HAVE_RVALUE_REFS) |
118 | | SharedObjectPtr(SharedObjectPtr && rhs) |
119 | | : pointee (STD_NAMESPACE move (rhs.pointee)) |
120 | | { |
121 | | rhs.pointee = 0; |
122 | | } |
123 | | |
124 | | SharedObjectPtr & operator = (SharedObjectPtr && rhs) |
125 | | { |
126 | | rhs.swap (*this); |
127 | | return *this; |
128 | | } |
129 | | #endif |
130 | | |
131 | | // Dtor |
132 | | ~SharedObjectPtr() |
133 | | { |
134 | | if (pointee) |
135 | | pointee->removeReference(); |
136 | | } |
137 | | |
138 | | // Operators |
139 | | bool operator==(const SharedObjectPtr& rhs) const { return (pointee == rhs.pointee); } |
140 | | bool operator!=(const SharedObjectPtr& rhs) const { return (pointee != rhs.pointee); } |
141 | | bool operator==(const T* rhs) const { return (pointee == rhs); } |
142 | | bool operator!=(const T* rhs) const { return (pointee != rhs); } |
143 | | T* operator->() const {assert (pointee); return pointee; } |
144 | | T& operator*() const {assert (pointee); return *pointee; } |
145 | | |
146 | | SharedObjectPtr& operator=(const SharedObjectPtr& rhs) |
147 | | { |
148 | | return this->operator = (rhs.pointee); |
149 | | } |
150 | | |
151 | | SharedObjectPtr& operator=(T* rhs) |
152 | | { |
153 | | SharedObjectPtr<T> (rhs).swap (*this); |
154 | | return *this; |
155 | | } |
156 | | |
157 | | // Methods |
158 | | T* get() const { return pointee; } |
159 | | |
160 | | void swap (SharedObjectPtr & other) throw () |
161 | | { |
162 | | T* tmp = pointee; |
163 | | pointee = other.pointee; |
164 | | other.pointee = tmp; |
165 | | } |
166 | | |
167 | | typedef T * (SharedObjectPtr:: * unspec_bool_type) () const; |
168 | | operator unspec_bool_type () const |
169 | | { |
170 | | return pointee ? &SharedObjectPtr::get : 0; |
171 | | } |
172 | | |
173 | | bool operator ! () const |
174 | | { |
175 | | return ! pointee; |
176 | | } |
177 | | |
178 | | private: |
179 | | // Methods |
180 | | void addref() const |
181 | | { |
182 | | if (pointee) |
183 | | pointee->addReference(); |
184 | | } |
185 | | |
186 | | // Data |
187 | | T* pointee; |
188 | | }; |
189 | | |
190 | | } // end namespace helpers |
191 | | } // end namespace log4cplus |
192 | | } // end namespace dcmtk |
193 | | |
194 | | |
195 | | #endif // DCMTK_LOG4CPLUS_HELPERS_POINTERS_HEADER_ |