source: trunk/include/cppbase/bs_refptr.h@ 352

Last change on this file since 352 was 250, checked in by umoeller, 23 years ago

Build updates, moved files from warpin.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 2.6 KB
Line 
1
2/*
3 *@@sourcefile bs_refptr.h:
4 * template definition for REFPOINTER.b
5 */
6
7
8/*
9 *@@ REFPOINTER:
10 * generic pointer class template, which allows us to
11 * to store pointers in STL containers and still have
12 * operators work right.
13 *
14 * The problem with pointers and the STL is that the
15 * operators such as "<" are not invoked on the object
16 * directly, but on the pointer, so that sorting etc.
17 * occurs according to pointer values.
18 *
19 * It doesn't really make that much sense to sort items
20 * according to storage addresses in memory... so
21 * instead of storing "real" pointers, you can store
22 * instances of this class template, which supports
23 * the "*", "->", "==", "<" operators.
24 *
25 * Declare your list like this:
26 + list< REFPOINTER<Class> > ClassesList;
27 *
28 * Mind the spaces in "> >", because otherwise the
29 * compiler considers this an ">>" operator. There
30 * are no asterices (*) anywhere.
31 *
32 * Use the "*" or "->" operators on the REFPOINTER
33 * to get the actual class instance.
34 */
35
36template <class T>
37class REFPOINTER
38{
39 private:
40 T* ptr;
41
42 public:
43 // default constructor
44 inline REFPOINTER()
45 { ptr = 0; }
46
47 // destructor
48 inline ~REFPOINTER()
49 { }
50
51 // constructor from object
52 inline REFPOINTER(T& t)
53 { ptr = &t; }
54
55 // copy constructor 1
56 inline REFPOINTER(const REFPOINTER<T>& X)
57 { ptr = X.ptr; }
58
59 // copy constructor 2
60 inline REFPOINTER(const void* p)
61 { ptr = (T*)p; }
62
63 // dereference
64 inline T& operator*()
65 const
66 { return *ptr; }
67
68 inline T* operator->()
69 const
70 { return ptr; }
71
72 // conversion to pointer to class T
73 inline operator T*()
74 { return ptr; }
75
76 // () operator
77 inline operator bool()
78 const
79 { return ptr != 0; }
80
81 // ! operator
82 inline bool operator!()
83 const
84 { return ptr == 0; }
85
86 // comparison
87 inline friend bool operator == (const REFPOINTER<T>& left,
88 const REFPOINTER<T>& right)
89 {
90 return *(left.ptr) == *(right.ptr);
91 }
92
93 inline friend bool operator < (const REFPOINTER<T>& left,
94 const REFPOINTER<T>& right)
95 {
96 return *(left.ptr) < *(right.ptr);
97 }
98};
99
100
Note: See TracBrowser for help on using the repository browser.