changed copyright years in source files
[fegdk.git] / core / code / system / f_helpers.h
blob1f8114cd10f0eccbdb96a2d1c9c579317bf908b9
1 /*
2 fegdk: FE Game Development Kit
3 Copyright (C) 2001-2008 Alexey "waker" Yakovenko
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 Alexey Yakovenko
20 waker@users.sourceforge.net
23 #ifndef __F_HELPERS_H
24 #define __F_HELPERS_H
26 #include <assert.h>
28 namespace fe {
30 class genericCallback
32 public:
33 virtual void call (void *data) = 0;
36 // this class is used to store/access objects of different types in one list
37 class virtualDtorObject
39 public:
40 virtual ~virtualDtorObject (void) {}
43 template <class T1, class T2>
44 inline const T1 checked_cast (T2 src)
46 assert (src);
47 assert (dynamic_cast <T1> (src));
48 return static_cast <T1> (src);
51 template <class T> class heapBuffer
54 // first buffer byte is a refc
56 private:
58 unsigned char* mpBuffer;
60 public:
62 heapBuffer (int cnt)
64 assert (cnt);
65 mpBuffer = new unsigned char[cnt * sizeof (T) + 1];
66 *mpBuffer = 1;
69 heapBuffer (const heapBuffer &b)
71 mpBuffer = b.mpBuffer;
72 mpBuffer[0]++;
75 ~heapBuffer ()
77 if (--mpBuffer[0] == 0)
78 delete[] mpBuffer;
81 operator T* ()
83 return (T*) (mpBuffer + 1);
88 template <class T> class noAddrefReleaseOnSmartRefcPtr : public T
90 private:
91 noAddrefReleaseOnSmartRefcPtr (void) : T (NULL) {}
92 virtual void addRef () = 0;
93 virtual void release () = 0;
96 template <class T> class smartPtr
99 private:
101 T* p;
103 public:
105 smartPtr (void)
107 p = NULL;
110 smartPtr (T *lp)
112 if ( (p = lp) != NULL)
113 p->addRef ();
116 smartPtr (const smartPtr<T>& lp)
118 if ( (p = lp.p) != NULL)
119 p->addRef ();
122 ~smartPtr ()
124 if (p)
125 p->release ();
128 operator T* () const
130 return p;
133 /* T& operator* () const
135 assert (p != NULL);
136 return *p;
139 T** operator& ()
141 assert (p == NULL);
142 return &p;
145 noAddrefReleaseOnSmartRefcPtr<T>* operator-> () const
147 // assert (p!=NULL);
148 return (noAddrefReleaseOnSmartRefcPtr<T>*)p;
151 T* operator= (T* lp)
153 T* pp = p;
154 if ( (p = lp) != NULL)
155 p->addRef ();
156 if (pp)
157 pp->release ();
158 return p;
161 T* operator= (const smartPtr<T>& lp)
163 T* pp = p;
164 if ( (p = lp.p) != NULL)
165 p->addRef ();
166 if (pp)
167 pp->release ();
168 return p;
171 template <class N> smartPtr<N> dynamicCast (void) const
173 return p ? smartPtr<N> (checked_cast <N*> (p)) : smartPtr<N> (NULL);
178 template <class T> class autoPtr
181 private:
183 T* mpObject;
185 public:
187 autoPtr (T *o)
189 assert (o);
190 mpObject = o;
193 ~autoPtr ()
195 delete mpObject;
198 operator T* ()
200 return mpObject;
203 operator const T* () const
205 return mpObject;
213 #endif // __F_HELPERS_H