1 /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */ |
|
2 /* |
|
3 * Copyright (c) 2005,2006 INRIA |
|
4 * All rights reserved. |
|
5 * |
|
6 * This program is free software; you can redistribute it and/or modify |
|
7 * it under the terms of the GNU General Public License version 2 as |
|
8 * published by the Free Software Foundation; |
|
9 * |
|
10 * This program 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 |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 * |
|
19 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
|
20 */ |
|
21 |
|
22 #ifndef REF_PTR_H |
|
23 #define REF_PTR_H |
|
24 |
|
25 namespace ns3 { |
|
26 |
|
27 template <typename T> |
|
28 class RefPtr { |
|
29 private: |
|
30 T *m_env; |
|
31 public: |
|
32 RefPtr () |
|
33 : m_env (0) |
|
34 {} |
|
35 |
|
36 RefPtr (T *env) |
|
37 : m_env (env) |
|
38 {} |
|
39 RefPtr (RefPtr const&o) |
|
40 : m_env (o.m_env) |
|
41 { |
|
42 if (m_env != 0) { |
|
43 m_env->m_count++; |
|
44 } |
|
45 } |
|
46 // allow conversions from T to T const. |
|
47 template <typename U> |
|
48 RefPtr (RefPtr<U> const &o) |
|
49 : m_env (o.peek ()) |
|
50 { |
|
51 if (m_env != 0) { |
|
52 m_env->m_count++; |
|
53 } |
|
54 } |
|
55 ~RefPtr () |
|
56 { |
|
57 if (m_env != 0) { |
|
58 m_env->m_count--; |
|
59 if (m_env->m_count == 0) { |
|
60 m_env->destroy (); |
|
61 } |
|
62 } |
|
63 } |
|
64 RefPtr &operator = (RefPtr const& o) |
|
65 { |
|
66 if (o.m_env != 0) { |
|
67 o.m_env->m_count++; |
|
68 } |
|
69 if (m_env != 0) { |
|
70 m_env->m_count--; |
|
71 if (m_env->m_count == 0) { |
|
72 m_env->destroy (); |
|
73 } |
|
74 } |
|
75 m_env = o.m_env; |
|
76 return *this; |
|
77 } |
|
78 T *operator -> () |
|
79 { |
|
80 return m_env; |
|
81 } |
|
82 T *operator -> () const |
|
83 { |
|
84 return m_env; |
|
85 } |
|
86 // allow if (!sp) |
|
87 bool operator! () |
|
88 { |
|
89 return m_env == 0; |
|
90 } |
|
91 private: |
|
92 class Tester { |
|
93 private: |
|
94 void operator delete (void *); |
|
95 }; |
|
96 public: |
|
97 // allow if (sp) |
|
98 operator Tester * () const |
|
99 { |
|
100 if (m_env == 0) { |
|
101 return 0; |
|
102 } |
|
103 static Tester test; |
|
104 return &test; |
|
105 } |
|
106 // allow if (sp == 0) |
|
107 inline friend bool operator == (RefPtr const &lhs, T const *rhs) |
|
108 { |
|
109 return lhs.m_env == rhs; |
|
110 } |
|
111 // allow if (0 == sp) |
|
112 inline friend bool operator == (T const *lhs, RefPtr &rhs) |
|
113 { |
|
114 return lhs == rhs.m_env; |
|
115 } |
|
116 // allow if (sp != 0) |
|
117 inline friend bool operator != (RefPtr const &lhs, T const *rhs) |
|
118 { |
|
119 return lhs.m_env != rhs; |
|
120 } |
|
121 // allow if (0 != sp) |
|
122 inline friend bool operator != (T const *lhs, RefPtr &rhs) |
|
123 { |
|
124 return lhs != rhs.m_env; |
|
125 } |
|
126 |
|
127 T *peek (void) |
|
128 { |
|
129 return m_env; |
|
130 } |
|
131 T *peek (void) const |
|
132 { |
|
133 return m_env; |
|
134 } |
|
135 }; |
|
136 |
|
137 }; // namespace ns3 |
|
138 |
|
139 #endif /* REF_PTR_H */ |
|