Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

PP.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 00038 00039 /* ******************************************************* 00040 * $Id: PP.h,v 1.7 2004/07/27 16:35:32 tihocan Exp $ 00041 * AUTHORS: Pascal Vincent & Yoshua Bengio 00042 * This file is part of the PLearn library. 00043 ******************************************************* */ 00044 00045 00048 #ifndef SMART_POINTER_INC 00049 #define SMART_POINTER_INC 00050 00051 #include <typeinfo> 00052 #include "TypeTraits.h" 00053 #include "CopiesMap.h" 00054 #include "plerror.h" 00055 00056 namespace PLearn { 00057 using namespace std; 00058 00059 class PPointable 00060 { 00061 private: 00062 int refcount; 00063 00064 public: 00065 inline PPointable() 00066 :refcount(0) 00067 {} 00068 00069 inline PPointable(const PPointable& other) 00070 :refcount(0) 00071 {} 00072 00073 inline void ref() const 00074 { const_cast<PPointable*>(this)->refcount++; } 00075 00076 inline void unref() const 00077 { 00078 const_cast<PPointable*>(this)->refcount--; 00079 if(refcount==0) 00080 delete this; 00081 } 00082 00083 inline int usage() const 00084 { return refcount; } 00085 00086 virtual ~PPointable() {} 00087 }; 00088 00089 template<class T> 00090 class PP 00091 { 00092 protected: 00093 T* ptr; 00094 00095 public: 00096 00098 inline PP<T>() 00099 :ptr(0) 00100 {} 00101 00103 inline PP<T>(const T* the_ptr) 00104 { 00105 ptr = const_cast<T*>(the_ptr); 00106 if(ptr) 00107 ptr->ref(); 00108 } 00109 00111 inline PP<T>(const PP<T>& other) 00112 { 00113 ptr = const_cast<T*>((T*)other); 00114 if(ptr) 00115 ptr->ref(); 00116 } 00117 00119 template <class U> 00120 explicit PP<T>(const PP<U>& other) 00121 { 00122 if(other.isNull()) 00123 ptr = 0; 00124 else 00125 { 00128 ptr = static_cast<T*>((U*)other); 00129 00135 ptr = dynamic_cast<T*>((U*)other); 00136 00141 if (!ptr) 00142 PLERROR("In PP constructor from smart pointer " 00143 "of other class (constructing %s from %s)", 00144 typeid(T).name(),typeid(U).name()); 00145 ptr->ref(); 00146 } 00147 } 00148 00149 inline bool isNull() const 00150 { return ptr==0; } 00151 00152 inline bool isNotNull() const 00153 { return ptr!=0; } 00154 00156 inline operator T*() const 00157 { return ptr; } 00158 00160 inline T* operator->() const 00161 { return ptr; } 00162 00164 inline T& operator*() const 00165 { return *ptr; } 00166 00168 inline PP<T>& operator=(const T* otherptr) 00169 { 00170 if(otherptr!=ptr) 00171 { 00172 if(ptr) 00173 ptr->unref(); 00174 ptr = const_cast<T*>(otherptr); 00175 if(ptr) 00176 ptr->ref(); 00177 } 00178 return *this; 00179 } 00180 00182 inline PP<T>& operator=(const PP<T>& other) 00183 { return operator=((T*)other); } 00184 00185 inline ~PP() 00186 { 00187 if(ptr) 00188 ptr->unref(); 00189 } 00190 00191 }; 00192 00194 // deepCopyField // 00197 template <class T> 00198 inline void deepCopyField(PP<T>& field, CopiesMap& copies) 00199 { 00200 if (field) { 00201 // Check the usage of the object pointed by 'field': it should be > 1, 00202 // because 'field' is a shallow copy. However, it *could* happen that it 00203 // is only 1, if the object that pointed to the same object has been deleted. 00204 // Since this is usually not wanted, we display a warning if this happens. 00205 // Indeed, there is a risk of this causing trouble, because the 'copies' map 00206 // may contain invalid mappings refering to now deleted objects. 00207 if (field->usage() == 1) 00208 PLWARNING("In deepCopyField(PP<T>& field, ...) - The usage() of the underlying object is only 1, this is unusual"); 00209 field = field->deepCopy(copies); 00210 } 00211 } 00212 00214 template<class T> 00215 T* deepCopy(PP<T> source, CopiesMap& copies) 00216 { return deepCopy((T*)source, copies); } 00217 00219 00223 template<class T> 00224 inline T* deepCopy(PP<T> source) 00225 { 00226 CopiesMap copies; 00227 return deepCopy(source, copies); 00228 } 00229 00230 00231 template<class T> 00232 class TypeTraits< PP<T> > 00233 { 00234 public: 00235 static string name() 00236 { return string("PP< ") + TypeTraits<T>::name()+" >"; } 00237 00238 static inline unsigned char little_endian_typecode() 00239 { return 0xFF; } 00240 00241 static inline unsigned char big_endian_typecode() 00242 { return 0xFF; } 00243 }; 00244 00245 template <class A, class B> 00246 class MultiMap : public PPointable 00247 { 00248 public: 00249 multimap<A,B> map; 00250 }; 00251 00252 00253 } // end of namespace PLearn 00254 00255 #endif 00256 00257

Generated on Tue Aug 17 16:02:36 2004 for PLearn by doxygen 1.3.7