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


Generated on Thu Dec 20 2012 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.2