00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
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
00197
template <
class T>
00198 inline void deepCopyField(
PP<T>& field, CopiesMap& copies)
00199 {
00200
if (field) {
00201
00202
00203
00204
00205
00206
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 }
00254
00255
#endif
00256
00257