1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
// This is mul/mbl/mbl_cloneable_ptr.h
#ifndef mbl_cloneable_ptr_h
#define mbl_cloneable_ptr_h
//:
// \file
#include <vsl/vsl_binary_loader.h>
#include <vcl_cassert.h>
//=======================================================================
//: Cunning pointer for objects that can be cloned.
// Used to record base class pointers to objects
// When copied, the object pointed to gets cloned.
// When written or read to/from binary streams,
// suitable polymorphic I/O is invoked.
//
// \code
// vcl_auto_ptr<T> inst = get_from_some_factory_function();
// mbl_cloneable_ptr<T> long_term_store;
// long_term_store = inst.release();
// \endcode
template <class BaseClass>
class mbl_cloneable_ptr
{
BaseClass* ptr_;
public:
//: Default constructor (zeros pointer)
mbl_cloneable_ptr() : ptr_(0) {}
//: Delete object pointed to and set pointer to zero
void deleteObject() { delete ptr_; ptr_=0; }
//: Destructor
~mbl_cloneable_ptr() { deleteObject(); }
//: Copy constructor
mbl_cloneable_ptr(const mbl_cloneable_ptr<BaseClass>& p) : ptr_(0) { *this = p; }
//: Construct from pointer, making a clone of r.
mbl_cloneable_ptr(const BaseClass& r) : ptr_(r.clone()) { assert(ptr_); }
//: Constructor from pointer, taking ownership of *p.
mbl_cloneable_ptr(BaseClass* p) : ptr_(p) { assert(ptr_); }
//: Copy operator
mbl_cloneable_ptr<BaseClass>& operator=(const mbl_cloneable_ptr<BaseClass>& p)
{
if (this==&p) return *this;
deleteObject(); if (p.ptr_!=0) ptr_=p.ptr_->clone();
return *this;
}
//: Copy operator - takes clone of p
mbl_cloneable_ptr<BaseClass>& operator=(const BaseClass& p)
{
if (ptr_==&p) return *this;
deleteObject();
ptr_= p.clone();
return *this;
}
//: Copy operator - takes responsibility for *p
// Sets internal pointer to p, and takes responsibility
// for deleting *p
mbl_cloneable_ptr<BaseClass>& operator=(BaseClass* p)
{
if (ptr_==p) return *this;
deleteObject();
ptr_= p;
return *this;
}
//: Return true if pointer defined
bool isDefined() const { return ptr_!=0; }
//: Make object behave like pointer to BaseClass
const BaseClass* operator->() const { return ptr_; }
//: Make object behave like pointer to BaseClass
BaseClass* operator->() { return ptr_; }
//: Return actual pointer
const BaseClass* ptr() const { return ptr_; }
//: Return actual pointer
BaseClass* ptr() { return ptr_; }
//: Return wrapped pointer and give up ownership
BaseClass* release()
{ BaseClass* p = ptr_; ptr_=0; return p; }
//: Cast to allow object to look like thing pointed to
operator BaseClass&() { assert(ptr_!=0); return *ptr_; }
//: Dereferencing the pointer
BaseClass &operator * () { return *ptr_; }
//: Dereferencing the pointer
const BaseClass &operator * () const { return *ptr_; }
//: Cast to allow object to look like thing pointed to
operator const BaseClass&() const { assert(ptr_!=0); return *ptr_; }
//: Save to binary stream
void b_write(vsl_b_ostream& bfs) const
{
vsl_b_write(bfs,ptr_);
}
//: Load from binary stream
void b_read(vsl_b_istream& bfs)
{
deleteObject();
vsl_b_read(bfs,ptr_);
}
};
template <class BaseClass>
void vsl_b_write(vsl_b_ostream& bfs, const mbl_cloneable_ptr<BaseClass>& p)
{ p.b_write(bfs); }
template <class BaseClass>
void vsl_b_read(vsl_b_istream& bfs, mbl_cloneable_ptr<BaseClass>& p)
{ p.b_read(bfs); }
//=======================================================================
//: Cunning non-zero pointer for objects that can be cloned.
// The pointer is guaranteed to always point to something.
// Used to record base class pointers to objects
// When copied, the object pointed to gets cloned.
// When written or read to/from binary streams,
// suitable polymorphic I/O is invoked.
//
// To take ownership of the contents of a vcl_auto_ptr<T> use
// \code
// vcl_auto_ptr<T> inst = get_from_some_factory_function();
// mbl_cloneable_nzptr<T> long_term_store(inst.release());
// \endcode
template <class BaseClass>
class mbl_cloneable_nzptr
{
BaseClass* ptr_;
public:
//: Destructor
~mbl_cloneable_nzptr() { delete ptr_; }
//: Copy constructor
// There is no default constructor.
mbl_cloneable_nzptr(const mbl_cloneable_nzptr<BaseClass>& cp):
ptr_(cp.ptr_->clone()) { assert(ptr_); }
//: Construct from pointer, making a clone of r.
// There is no default constructor.
mbl_cloneable_nzptr(const BaseClass& r) : ptr_(r.clone()) { assert(ptr_); }
//: Constructor from pointer, taking ownership of *p.
// There is no default constructor.
mbl_cloneable_nzptr(BaseClass* p) : ptr_(p) { assert(ptr_); }
//: Copy operator
mbl_cloneable_nzptr<BaseClass>& operator=(const mbl_cloneable_nzptr<BaseClass>& cp)
{
if (this==&cp) return *this;
BaseClass * tmp=cp.ptr_->clone();
assert(tmp);
delete ptr_;
ptr_ = tmp;
return *this;
}
//: Copy operator - takes clone of r
mbl_cloneable_nzptr<BaseClass>& operator=(const BaseClass& r)
{
if (ptr_==&r) return *this;
BaseClass * tmp=r.clone(); // Do it in this order, in case clone throws an exception.
assert(tmp);
delete ptr_;
ptr_= tmp;
return *this;
}
//: Copy operator - takes responsibility for *p
// Sets internal pointer to p, and takes responsibility
// for deleting *p
mbl_cloneable_nzptr<BaseClass>& operator=(BaseClass* p)
{
assert(p);
if (ptr_==p) return *this;
delete ptr_;
ptr_= p;
return *this;
}
//: Return true.
bool isDefined() const { return true; }
//: Make object behave like pointer to BaseClass
const BaseClass* operator->() const { return ptr_; }
//: Make object behave like pointer to BaseClass
BaseClass* operator->() { return ptr_; }
//: Return actual pointer
const BaseClass* ptr() const { return ptr_; }
//: Return actual pointer
BaseClass* ptr() { return ptr_; }
//: Return and give up ownership of wrapped pointer, while taking ownership a new pointer.
BaseClass* replace(BaseClass* p)
{ BaseClass* old = ptr_; ptr_=p; return old; }
//: Cast to allow object to look like thing pointed to
operator BaseClass&() { return *ptr_; }
//: Cast to allow object to look like thing pointed to
operator const BaseClass&() const { return *ptr_; }
//: Dereferencing the pointer
BaseClass &operator * () { return *ptr_; }
//: Dereferencing the pointer
const BaseClass &operator * () const { return *ptr_; }
//: Save to binary stream
void b_write(vsl_b_ostream& bfs) const
{
vsl_b_write(bfs,ptr_);
}
//: Load from binary stream
void b_read(vsl_b_istream& bfs)
{
delete ptr_;
ptr_ = 0;
vsl_b_read(bfs,ptr_);
}
};
template <class BaseClass>
void vsl_b_write(vsl_b_ostream& bfs, const mbl_cloneable_nzptr<BaseClass>& p)
{ p.b_write(bfs); }
template <class BaseClass>
void vsl_b_read(vsl_b_istream& bfs, mbl_cloneable_nzptr<BaseClass>& p)
{ p.b_read(bfs); }
#define MBL_CLONEABLE_PTR_INSTANTIATE(T) /* nothing */
#if 0 // was:
VCL_DEFINE_SPECIALIZATION class mbl_cloneable_ptr<T >; \
VCL_DEFINE_SPECIALIZATION void vsl_b_write(vsl_b_ostream& bfs, const mbl_cloneable_ptr<T >& p);\
VCL_DEFINE_SPECIALIZATION void vsl_b_read(vsl_b_istream& bfs, mbl_cloneable_ptr<T >& p);\
VCL_DEFINE_SPECIALIZATION class mbl_cloneable_nzptr<T >; \
VCL_DEFINE_SPECIALIZATION void vsl_b_write(vsl_b_ostream& bfs, const mbl_cloneable_nzptr<T >& p);\
VCL_DEFINE_SPECIALIZATION void vsl_b_read(vsl_b_istream& bfs, mbl_cloneable_nzptr<T >& p)
#endif // 0
#endif // mbl_cloneable_ptr_h
|