2 // lots of code here is (c) Bartosz Milewski, 1996, www.relisoft.com
3 // The rest is (C) 2002-2004 Royce Mitchell III
4 // and released under the LGPL & BSD licenses
9 ////////////////////////////////////////////////////////////////////////////////
14 #define Assert(exp) ((void)0)
16 void _wassert (char* szExpr
, char* szFile
, int line
);
17 #define Assert(exp) (void)( (exp) || (_wassert(#exp, __FILE__, __LINE__), 0) )
20 ////////////////////////////////////////////////////////////////////////////////
31 ////////////////////////////////////////////////////////////////////////////////
32 // Uncopyable - base class disabling copy ctors
36 Uncopyable(){} // need a default ctor
38 Uncopyable ( const Uncopyable
& );
39 const Uncopyable
& operator = ( const Uncopyable
& );
42 ////////////////////////////////////////////////////////////////////////////////
43 // SPtr - Smart Pointer's must be passed by reference or const reference
46 class SPtr
: public Uncopyable
49 virtual ~SPtr () { Destroy(); }
50 T
* operator->() { return _p
; }
51 T
const * operator->() const { return _p
; }
52 operator T
&() { Assert(_p
); return *_p
; }
53 operator const T
&() const { Assert(_p
); return *_p
; }
54 void Acquire ( SPtr
<T
>& t
) { Destroy(); Swap(_p
,t
._p
); }
55 void Destroy() { if ( _p
) { delete _p
; _p
= 0; } }
58 explicit SPtr (T
* p
): _p (p
) {}
61 operator T
* () { return _p
; }
64 #define DECLARE_SPTR(cls,init,init2) \
65 class S##cls : public SPtr<cls> \
68 S##cls ( cls* p ) : SPtr<cls>(p) {} \
69 explicit S##cls init : SPtr<cls> (new cls init2) {} \
71 /* Example Usage of DECLARE_SPTR:
74 public: // can be protected
80 }; DECLARE_SPTR(MyClass,(int i),(i))
83 #define DECLARE_SPTRV(cls) typedef SPtr<cls> S##cls;
84 /* Example Usage of DECLARE_SPTRV:
87 public: // can be protected
88 MyAbstractClass ( int i )
92 void MyPureVirtFunc() = 0;
94 }; DECLARE_SPTRV(MyAbstractClass)
95 SMyAbstractClass ptr ( new MySubClass(i) );
98 #define DECLARE_PTR(cls,init,init2) \
99 class Ptr : public SPtr<cls> \
101 Ptr(cls* p) : SPtr<cls> ( p ) \
104 Ptr init : SPtr<cls> ( new cls init2 ) {} \
106 /* Example Usage of DECLARE_PTR:
109 DECLARE_PTR(MyClass,(int i),(i))
110 public: // can be protected
115 void MyPureVirtFunc() = 0;
118 MyClass::Ptr ptr ( i );
121 #define DECLARE_PTRV(cls) \
122 class Ptr : public SPtr<cls> \
124 Ptr(cls* p) : SPtr<cls> ( p ) \
128 /* Example Usage of DECLARE_PTRV:
129 class MyAbstractClass
131 DECLARE_PTRV(MyAbstractClass)
132 public: // can be protected
133 MyAbstractClass ( int i )
137 void MyPureVirtFunc() = 0;
140 MyAbstractClass::Ptr ptr ( new MySubClass(i) );