00001 // Module: Log4CPLUS 00002 // File: pointer.h 00003 // Created: 6/2001 00004 // Author: Tad E. Smith 00005 // 00006 // 00007 // Copyright 2001-2009 Tad E. Smith 00008 // 00009 // Licensed under the Apache License, Version 2.0 (the "License"); 00010 // you may not use this file except in compliance with the License. 00011 // You may obtain a copy of the License at 00012 // 00013 // http://www.apache.org/licenses/LICENSE-2.0 00014 // 00015 // Unless required by applicable law or agreed to in writing, software 00016 // distributed under the License is distributed on an "AS IS" BASIS, 00017 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 // See the License for the specific language governing permissions and 00019 // limitations under the License. 00020 00021 // 00022 // Note: Some of this code uses ideas from "More Effective C++" by Scott 00023 // Myers, Addison Wesley Longmain, Inc., (c) 1996, Chapter 29, pp. 183-213 00024 // 00025 00028 #ifndef _LOG4CPLUS_HELPERS_POINTERS_HEADER_ 00029 #define _LOG4CPLUS_HELPERS_POINTERS_HEADER_ 00030 00031 #include "dcmtk/oflog/config.h" 00032 //#include <memory> 00033 //#include <stdexcept> 00034 //#include <string> 00035 //#include <algorithm> 00036 //#include <cassert> 00037 00038 #define INCLUDE_CASSERT 00039 #include "dcmtk/ofstd/ofstdinc.h" 00040 00041 namespace log4cplus { 00042 namespace helpers { 00043 00044 /****************************************************************************** 00045 * Class SharedObject (from pp. 204-205) * 00046 ******************************************************************************/ 00047 00048 class LOG4CPLUS_EXPORT SharedObject 00049 { 00050 public: 00051 void addReference() const; 00052 void removeReference() const; 00053 00054 protected: 00055 // Ctor 00056 SharedObject() 00057 : access_mutex(LOG4CPLUS_MUTEX_CREATE) 00058 , count(0) 00059 { } 00060 00061 SharedObject(const SharedObject&) 00062 : access_mutex(LOG4CPLUS_MUTEX_CREATE) 00063 , count(0) 00064 { } 00065 00066 // Dtor 00067 virtual ~SharedObject(); 00068 00069 // Operators 00070 SharedObject& operator=(const SharedObject&) { return *this; } 00071 00072 public: 00073 LOG4CPLUS_MUTEX_PTR_DECLARE access_mutex; 00074 00075 private: 00076 mutable int count; 00077 }; 00078 00079 00080 /****************************************************************************** 00081 * Template Class SharedObjectPtr (from pp. 203, 206) * 00082 ******************************************************************************/ 00083 template<class T> 00084 class SharedObjectPtr 00085 { 00086 public: 00087 // Ctor 00088 explicit 00089 SharedObjectPtr(T* realPtr = 0) 00090 : pointee(realPtr) 00091 { 00092 addref (); 00093 } 00094 00095 SharedObjectPtr(const SharedObjectPtr& rhs) 00096 : pointee(rhs.pointee) 00097 { 00098 addref (); 00099 } 00100 00101 // Dtor 00102 ~SharedObjectPtr() 00103 { 00104 if (pointee) 00105 pointee->removeReference(); 00106 } 00107 00108 // Operators 00109 bool operator==(const SharedObjectPtr& rhs) const { return (pointee == rhs.pointee); } 00110 bool operator!=(const SharedObjectPtr& rhs) const { return (pointee != rhs.pointee); } 00111 bool operator==(const T* rhs) const { return (pointee == rhs); } 00112 bool operator!=(const T* rhs) const { return (pointee != rhs); } 00113 T* operator->() const {assert (pointee); return pointee; } 00114 T& operator*() const {assert (pointee); return *pointee; } 00115 00116 SharedObjectPtr& operator=(const SharedObjectPtr& rhs) 00117 { 00118 return this->operator = (rhs.pointee); 00119 } 00120 00121 SharedObjectPtr& operator=(T* rhs) 00122 { 00123 SharedObjectPtr<T> (rhs).swap (*this); 00124 return *this; 00125 } 00126 00127 // Methods 00128 T* get() const { return pointee; } 00129 00130 void swap (SharedObjectPtr & other) throw () 00131 { 00132 T* tmp = pointee; 00133 pointee = other.pointee; 00134 other.pointee = tmp; 00135 //STD_NAMESPACE swap (pointee, other.pointee); 00136 } 00137 00138 typedef T * (SharedObjectPtr:: * unspec_bool_type) () const; 00139 operator unspec_bool_type () const 00140 { 00141 return pointee ? &SharedObjectPtr::get : 0; 00142 } 00143 00144 bool operator ! () const 00145 { 00146 return ! pointee; 00147 } 00148 00149 private: 00150 // Methods 00151 void addref() const 00152 { 00153 if (pointee) 00154 pointee->addReference(); 00155 } 00156 00157 // Data 00158 T* pointee; 00159 }; 00160 00161 } // end namespace helpers 00162 } // end namespace log4cplus 00163 00164 00165 #endif // _LOG4CPLUS_HELPERS_POINTERS_HEADER_