SVN maintainance: set eol-style to native
[reactos.git] / irc / ArchBlackmann / Reli.h
1 // Reli.h
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
5
6 #ifndef __RELI_H
7 #define __RELI_H
8
9 ////////////////////////////////////////////////////////////////////////////////
10 // Assert
11
12 #undef Assert
13 #ifdef NDEBUG
14 #define Assert(exp) ((void)0)
15 #else
16 void _wassert (char* szExpr, char* szFile, int line);
17 #define Assert(exp) (void)( (exp) || (_wassert(#exp, __FILE__, __LINE__), 0) )
18 #endif /* NDEBUG */
19
20 ////////////////////////////////////////////////////////////////////////////////
21 // Swap
22
23 template <class T>
24 void Swap(T a,T b)
25 {
26 T t = a;
27 a = b;
28 b = t;
29 }
30
31 ////////////////////////////////////////////////////////////////////////////////
32 // Uncopyable - base class disabling copy ctors
33 class Uncopyable
34 {
35 public:
36 Uncopyable(){} // need a default ctor
37 private:
38 Uncopyable ( const Uncopyable& );
39 const Uncopyable& operator = ( const Uncopyable& );
40 };
41
42 ////////////////////////////////////////////////////////////////////////////////
43 // SPtr - Smart Pointer's must be passed by reference or const reference
44
45 template <class T>
46 class SPtr : public Uncopyable
47 {
48 public:
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; } }
56 protected:
57 SPtr (): _p (0) {}
58 explicit SPtr (T* p): _p (p) {}
59 T * _p;
60 private:
61 operator T* () { return _p; }
62 };
63
64 #define DECLARE_SPTR(cls,init,init2) \
65 class S##cls : public SPtr<cls> \
66 { \
67 public: \
68 S##cls ( cls* p ) : SPtr<cls>(p) {} \
69 explicit S##cls init : SPtr<cls> (new cls init2) {} \
70 };
71 /* Example Usage of DECLARE_SPTR:
72 class MyClass
73 {
74 public: // can be protected
75 MyClass ( int i )
76 {
77 ...
78 }
79 ...
80 }; DECLARE_SPTR(MyClass,(int i),(i))
81 SMyClass ptr(i);
82 */
83 #define DECLARE_SPTRV(cls) typedef SPtr<cls> S##cls;
84 /* Example Usage of DECLARE_SPTRV:
85 class MyAbstractClass
86 {
87 public: // can be protected
88 MyAbstractClass ( int i )
89 {
90 ...
91 }
92 void MyPureVirtFunc() = 0;
93 ...
94 }; DECLARE_SPTRV(MyAbstractClass)
95 SMyAbstractClass ptr ( new MySubClass(i) );
96 */
97
98 #define DECLARE_PTR(cls,init,init2) \
99 class Ptr : public SPtr<cls> \
100 { \
101 Ptr(cls* p) : SPtr<cls> ( p ) \
102 { \
103 } \
104 Ptr init : SPtr<cls> ( new cls init2 ) {} \
105 };
106 /* Example Usage of DECLARE_PTR:
107 class MyClass
108 {
109 DECLARE_PTR(MyClass,(int i),(i))
110 public: // can be protected
111 MyClass ( int i )
112 {
113 ...
114 }
115 void MyPureVirtFunc() = 0;
116 ...
117 };
118 MyClass::Ptr ptr ( i );
119 */
120
121 #define DECLARE_PTRV(cls) \
122 class Ptr : public SPtr<cls> \
123 { \
124 Ptr(cls* p) : SPtr<cls> ( p ) \
125 { \
126 } \
127 };
128 /* Example Usage of DECLARE_PTRV:
129 class MyAbstractClass
130 {
131 DECLARE_PTRV(MyAbstractClass)
132 public: // can be protected
133 MyAbstractClass ( int i )
134 {
135 ...
136 }
137 void MyPureVirtFunc() = 0;
138 ...
139 };
140 MyAbstractClass::Ptr ptr ( new MySubClass(i) );
141 */
142
143 #endif//__RELI_H