Mercurial > projects > hoofbaby
comparison deps/Platinum/ThirdParty/Neptune/Source/Core/NptReferences.h @ 0:3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
author | fraserofthenight |
---|---|
date | Mon, 06 Jul 2009 08:06:28 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3425707ddbf6 |
---|---|
1 /***************************************************************** | |
2 | | |
3 | Neptune - References | |
4 | | |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC. | |
6 | All rights reserved. | |
7 | | |
8 | Redistribution and use in source and binary forms, with or without | |
9 | modification, are permitted provided that the following conditions are met: | |
10 | * Redistributions of source code must retain the above copyright | |
11 | notice, this list of conditions and the following disclaimer. | |
12 | * Redistributions in binary form must reproduce the above copyright | |
13 | notice, this list of conditions and the following disclaimer in the | |
14 | documentation and/or other materials provided with the distribution. | |
15 | * Neither the name of Axiomatic Systems nor the | |
16 | names of its contributors may be used to endorse or promote products | |
17 | derived from this software without specific prior written permission. | |
18 | | |
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY | |
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY | |
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | | |
30 ****************************************************************/ | |
31 | |
32 #ifndef _NPT_REFERENCES_H_ | |
33 #define _NPT_REFERENCES_H_ | |
34 | |
35 /*---------------------------------------------------------------------- | |
36 | includes | |
37 +---------------------------------------------------------------------*/ | |
38 #include "NptConstants.h" | |
39 | |
40 /*---------------------------------------------------------------------- | |
41 | NPT_Reference | |
42 +---------------------------------------------------------------------*/ | |
43 template <typename T> | |
44 class NPT_Reference | |
45 { | |
46 public: | |
47 // constructors and destructor | |
48 NPT_Reference() : m_Object(NULL), m_Counter(NULL) {} | |
49 explicit NPT_Reference(T* object) : | |
50 m_Object(object), m_Counter(object?new NPT_Cardinal(1):NULL) {} | |
51 | |
52 NPT_Reference(const NPT_Reference<T>& ref) : | |
53 m_Object(ref.m_Object), m_Counter(ref.m_Counter) { | |
54 if (m_Counter) ++(*m_Counter); | |
55 } | |
56 | |
57 // this methods should be private, but this causes a problem on some | |
58 // compilers, because we need this function in order to implement | |
59 // the cast operator operator NPT_Reference<U>() below, which would | |
60 // have to be marked as a friend, and friend declarations with the | |
61 // same class name confuses some compilers | |
62 NPT_Reference(T* object, NPT_Cardinal* counter) : | |
63 m_Object(object), m_Counter(counter) { | |
64 if (m_Counter) ++(*m_Counter); | |
65 } | |
66 | |
67 ~NPT_Reference() { | |
68 Release(); | |
69 } | |
70 | |
71 // overloaded operators | |
72 NPT_Reference<T>& operator=(const NPT_Reference<T>& ref) { | |
73 if (this != &ref) { | |
74 Release(); | |
75 m_Object = ref.m_Object; | |
76 m_Counter = ref.m_Counter; | |
77 if (m_Counter) ++(*m_Counter); | |
78 } | |
79 return *this; | |
80 } | |
81 NPT_Reference<T>& operator=(T* object) { | |
82 Release(); | |
83 m_Object = object; | |
84 m_Counter = object?new NPT_Cardinal(1):NULL; | |
85 return *this; | |
86 } | |
87 T& operator*() const { return *m_Object; } | |
88 T* operator->() const { return m_Object; } | |
89 | |
90 bool operator==(const NPT_Reference<T>& ref) { | |
91 return m_Object == ref.m_Object; | |
92 } | |
93 bool operator!=(const NPT_Reference<T>& ref) { | |
94 return m_Object != ref.m_Object; | |
95 } | |
96 | |
97 // overloaded cast operators | |
98 template <typename U> operator NPT_Reference<U>() { | |
99 return NPT_Reference<U>(m_Object, m_Counter); | |
100 } | |
101 | |
102 // methods | |
103 /** | |
104 * Returns the naked pointer value. | |
105 */ | |
106 T* AsPointer() const { return m_Object; } | |
107 | |
108 /** | |
109 * Returns the reference counter value. | |
110 */ | |
111 NPT_Cardinal GetCounter() const { return *m_Counter; } | |
112 | |
113 /** | |
114 * Returns wether this references a NULL object. | |
115 */ | |
116 bool IsNull() const { return m_Object == NULL; } | |
117 | |
118 /** | |
119 * Detach the reference from the shared object. | |
120 * The reference count is decremented, but the object is not deleted if the | |
121 * reference count becomes 0. | |
122 * After the method returns, this reference does not point to any shared object. | |
123 */ | |
124 void Detach() { | |
125 if (m_Counter && --(*m_Counter) == 0) { | |
126 delete m_Counter; | |
127 } | |
128 m_Counter = NULL; | |
129 m_Object = NULL; | |
130 } | |
131 | |
132 private: | |
133 // methods | |
134 void Release() { | |
135 if (m_Counter && --(*m_Counter) == 0) { | |
136 delete m_Counter; m_Counter = NULL; | |
137 delete m_Object; m_Object = NULL; | |
138 } | |
139 } | |
140 | |
141 // members | |
142 T* m_Object; | |
143 NPT_Cardinal* m_Counter; | |
144 }; | |
145 | |
146 #endif // _NPT_REFERENCES_H_ |