initial
[prop.git] / include / AD / memory / ref.h
blob9579e94cbb0b567a24f2e182673631010469468a
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef reference_counted_objects_h
26 #define reference_counted_objects_h
28 //////////////////////////////////////////////////////////////////////////////
29 // Reference counted objects base class.
30 //////////////////////////////////////////////////////////////////////////////
31 class RefObj {
32 int _ref_count; // reference count
33 public:
34 //////////////////////////////////////////////////////////////////////////
35 // Constructor and destructor
36 //////////////////////////////////////////////////////////////////////////
37 inline RefObj() : _ref_count(1) {}
38 virtual inline ~RefObj() {}
40 //////////////////////////////////////////////////////////////////////////
41 // Check if the pointer is actually valid.
42 //////////////////////////////////////////////////////////////////////////
43 inline static int _boxed(void * obj) { return (unsigned long)obj >= 4096; }
45 //////////////////////////////////////////////////////////////////////////
46 // Reference counting primitives.
47 //////////////////////////////////////////////////////////////////////////
48 inline static void _inc(RefObj * obj)
49 { if (RefObj::_boxed(obj)) ++obj->_ref_count; }
50 inline static void _dec(RefObj * obj)
51 { if (RefObj::_boxed(obj) && --obj->_ref_count == 0) delete obj; }
54 //////////////////////////////////////////////////////////////////////////////
55 // Ref<T> handles the reference counting of a pointer to class |T|.
56 // It is a smart pointer.
57 //////////////////////////////////////////////////////////////////////////////
59 template <class T>
60 class Ref {
61 const T& operator * () const; // for safety, no deferencing is allowed.
63 T * obj; // actual pointer to object (must be derived from RefObj)
65 public:
66 ///////////////////////////////////////////////////////////////////////
67 // Constructors and destructor.
68 ///////////////////////////////////////////////////////////////////////
69 inline Ref(T * x = 0) : obj(x) {}
70 inline Ref(Ref& r) : obj(r.obj) { RefObj::_inc(obj); }
71 inline ~Ref() { RefObj::_dec(obj); }
73 ///////////////////////////////////////////////////////////////////////
74 // Conversions and pinning.
75 // Pinning increments the reference on an object and returns
76 // its raw pointer. This raw pointer is assumed to be single
77 // threaded and placed into some other Ref<T> type.
78 ///////////////////////////////////////////////////////////////////////
79 inline operator int () const { return (int)obj; }
80 inline T * _pin () { RefObj::_inc(obj); return obj; }
81 inline T * _raw () { return obj; }
83 ///////////////////////////////////////////////////////////////////////
84 // Assignments.
85 ///////////////////////////////////////////////////////////////////////
86 inline Ref& operator = (Ref& r)
87 { if (obj != r.obj) {
88 RefObj::_inc(r.obj);
89 RefObj::_dec(obj);
90 obj = r.obj;
92 return *this;
95 inline Ref& operator = (T * x)
96 { if (obj != x) { RefObj::_dec(obj); obj = x; }
97 return *this;
100 ///////////////////////////////////////////////////////////////////////
101 // Dereferencing operators.
102 ///////////////////////////////////////////////////////////////////////
103 inline const T * operator -> () const { return obj; }
104 inline T * operator -> () { return obj; }
107 #endif