A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ptr.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #ifndef PTR_H
22 #define PTR_H
23 
24 #include <iostream>
25 #include <stdint.h>
26 #include "assert.h"
27 
28 namespace ns3 {
29 
59 template <typename T>
60 class Ptr
61 {
62 private:
63  T *m_ptr;
64  class Tester {
65 private:
66  void operator delete (void *);
67  };
68  friend class Ptr<const T>;
69  template <typename U>
70  friend U *GetPointer (const Ptr<U> &p);
71  template <typename U>
72  friend U *PeekPointer (const Ptr<U> &p);
73 
74  inline void Acquire (void) const;
75 public:
79  Ptr ();
90  Ptr (T *ptr);
99  Ptr (T *ptr, bool ref);
100  Ptr (Ptr const&o);
101  // allow conversions from T to T const.
102  template <typename U>
103  Ptr (Ptr<U> const &o);
104  ~Ptr ();
105  Ptr<T> &operator = (Ptr const& o);
106 
107  T *operator -> () const;
108  T *operator -> ();
109  const T &operator * () const;
110  T &operator * ();
111  // allow if (!sp)
112  bool operator! ();
113  // allow if (sp)
114  // disable delete sp
115  operator Tester * () const;
116 };
117 
118 template <typename T>
119 Ptr<T> Create (void);
120 
121 template <typename T, typename T1>
122 Ptr<T> Create (T1 a1);
123 
124 template <typename T, typename T1, typename T2>
125 Ptr<T> Create (T1 a1, T2 a2);
126 
127 template <typename T, typename T1, typename T2, typename T3>
128 Ptr<T> Create (T1 a1, T2 a2, T3 a3);
129 
130 template <typename T, typename T1, typename T2, typename T3, typename T4>
131 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4);
132 
133 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
134 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5);
135 
136 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
137 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6);
138 
139 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
140 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7);
141 
151 template <typename T>
152 T * PeekPointer (const Ptr<T> &p);
153 
163 template <typename T>
164 T * GetPointer (const Ptr<T> &p);
165 
166 template <typename T>
167 std::ostream &operator << (std::ostream &, const Ptr<T> &p);
168 
169 
170 // allow if (sp == 0)
171 template <typename T1, typename T2>
172 bool operator == (Ptr<T1> const &lhs, T2 const *rhs);
173 
174 // allow if (0 == sp)
175 template <typename T1, typename T2>
176 bool operator == (T1 const *lhs, Ptr<T2> &rhs);
177 
178 // allow if (sp != 0)
179 template <typename T1, typename T2>
180 bool operator != (Ptr<T1> const &lhs, T2 const *rhs);
181 
182 // allow if (0 != sp)
183 template <typename T1, typename T2>
184 bool operator != (T1 const *lhs, Ptr<T2> &rhs);
185 
186 // allow if (sp0 == sp1)
187 template <typename T1, typename T2>
188 bool operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
189 
190 // allow if (sp0 != sp1)
191 template <typename T1, typename T2>
192 bool operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
193 
194 template <typename T1, typename T2>
196 
197 template <typename T>
198 struct CallbackTraits;
199 
200 template <typename T>
201 struct CallbackTraits<Ptr<T> >
202 {
203  static T & GetReference (Ptr<T> const p)
204  {
205  return *PeekPointer (p);
206  }
207 };
208 
209 template <typename T>
210 struct EventMemberImplObjTraits;
211 
212 template <typename T>
214 {
215  static T &GetReference (Ptr<T> p) {
216  return *PeekPointer (p);
217  }
218 };
219 
220 
221 
222 } // namespace ns3
223 
224 
225 namespace ns3 {
226 
227 /*************************************************
228  * friend non-member function implementations
229  ************************************************/
230 
231 template <typename T>
233 {
234  return Ptr<T> (new T (), false);
235 }
236 
237 template <typename T, typename T1>
238 Ptr<T> Create (T1 a1)
239 {
240  return Ptr<T> (new T (a1), false);
241 }
242 
243 template <typename T, typename T1, typename T2>
244 Ptr<T> Create (T1 a1, T2 a2)
245 {
246  return Ptr<T> (new T (a1, a2), false);
247 }
248 
249 template <typename T, typename T1, typename T2, typename T3>
250 Ptr<T> Create (T1 a1, T2 a2, T3 a3)
251 {
252  return Ptr<T> (new T (a1, a2, a3), false);
253 }
254 
255 template <typename T, typename T1, typename T2, typename T3, typename T4>
256 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4)
257 {
258  return Ptr<T> (new T (a1, a2, a3, a4), false);
259 }
260 
261 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
262 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
263 {
264  return Ptr<T> (new T (a1, a2, a3, a4, a5), false);
265 }
266 
267 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
268 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
269 {
270  return Ptr<T> (new T (a1, a2, a3, a4, a5, a6), false);
271 }
272 
273 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
274 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
275 {
276  return Ptr<T> (new T (a1, a2, a3, a4, a5, a6, a7), false);
277 }
278 
279 template <typename T>
280 T * PeekPointer (const Ptr<T> &p)
281 {
282  return p.m_ptr;
283 }
284 
285 template <typename T>
286 T * GetPointer (const Ptr<T> &p)
287 {
288  p.Acquire ();
289  return p.m_ptr;
290 }
291 
292 template <typename T>
293 std::ostream &operator << (std::ostream &os, const Ptr<T> &p)
294 {
295  os << PeekPointer (p);
296  return os;
297 }
298 
299 template <typename T1, typename T2>
300 bool
301 operator == (Ptr<T1> const &lhs, T2 const *rhs)
302 {
303  return PeekPointer (lhs) == rhs;
304 }
305 
306 template <typename T1, typename T2>
307 bool
308 operator == (T1 const *lhs, Ptr<T2> &rhs)
309 {
310  return lhs == PeekPointer (rhs);
311 }
312 
313 template <typename T1, typename T2>
314 bool
315 operator != (Ptr<T1> const &lhs, T2 const *rhs)
316 {
317  return PeekPointer (lhs) != rhs;
318 }
319 
320 template <typename T1, typename T2>
321 bool
322 operator != (T1 const *lhs, Ptr<T2> &rhs)
323 {
324  return lhs != PeekPointer (rhs);
325 }
326 
327 template <typename T1, typename T2>
328 bool
329 operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
330 {
331  return PeekPointer (lhs) == PeekPointer (rhs);
332 }
333 
334 template <typename T1, typename T2>
335 bool
336 operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
337 {
338  return PeekPointer (lhs) != PeekPointer (rhs);
339 }
340 
341 template <typename T>
342 bool operator < (const Ptr<T> &lhs, const Ptr<T> &rhs)
343 {
344  return PeekPointer<T> (lhs) < PeekPointer<T> (rhs);
345 }
346 
347 template <typename T>
348 bool operator <= (const Ptr<T> &lhs, const Ptr<T> &rhs)
349 {
350  return PeekPointer<T> (lhs) <= PeekPointer<T> (rhs);
351 }
352 
353 template <typename T>
354 bool operator > (const Ptr<T> &lhs, const Ptr<T> &rhs)
355 {
356  return PeekPointer<T> (lhs) > PeekPointer<T> (rhs);
357 }
358 
359 template <typename T>
360 bool operator >= (const Ptr<T> &lhs, const Ptr<T> &rhs)
361 {
362  return PeekPointer<T> (lhs) >= PeekPointer<T> (rhs);
363 }
364 
365 template <typename T1, typename T2>
366 Ptr<T1>
368 {
369  return Ptr<T1> (const_cast<T1 *> (PeekPointer (p)));
370 }
371 
372 template <typename T1, typename T2>
373 Ptr<T1>
375 {
376  return Ptr<T1> (dynamic_cast<T1 *> (PeekPointer (p)));
377 }
378 
379 template <typename T1, typename T2>
380 Ptr<T1>
382 {
383  return Ptr<T1> (static_cast<T1 *> (PeekPointer (p)));
384 }
385 
386 
387 template <typename T>
389 {
390  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
391  return p;
392 }
393 
394 template <typename T>
395 Ptr<T> Copy (Ptr<const T> object)
396 {
397  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
398  return p;
399 }
400 
401 /****************************************************
402  * Member method implementations.
403  ***************************************************/
404 
405 template <typename T>
406 void
407 Ptr<T>::Acquire (void) const
408 {
409  if (m_ptr != 0)
410  {
411  m_ptr->Ref ();
412  }
413 }
414 
415 template <typename T>
417  : m_ptr (0)
418 {
419 }
420 
421 template <typename T>
422 Ptr<T>::Ptr (T *ptr)
423  : m_ptr (ptr)
424 {
425  Acquire ();
426 }
427 
428 template <typename T>
429 Ptr<T>::Ptr (T *ptr, bool ref)
430  : m_ptr (ptr)
431 {
432  if (ref)
433  {
434  Acquire ();
435  }
436 }
437 
438 template <typename T>
439 Ptr<T>::Ptr (Ptr const&o)
440  : m_ptr (PeekPointer (o))
441 {
442  Acquire ();
443 }
444 template <typename T>
445 template <typename U>
446 Ptr<T>::Ptr (Ptr<U> const &o)
447  : m_ptr (PeekPointer (o))
448 {
449  Acquire ();
450 }
451 
452 template <typename T>
454 {
455  if (m_ptr != 0)
456  {
457  m_ptr->Unref ();
458  }
459 }
460 
461 template <typename T>
462 Ptr<T> &
464 {
465  if (&o == this)
466  {
467  return *this;
468  }
469  if (m_ptr != 0)
470  {
471  m_ptr->Unref ();
472  }
473  m_ptr = o.m_ptr;
474  Acquire ();
475  return *this;
476 }
477 
478 template <typename T>
479 T *
481 {
482  return m_ptr;
483 }
484 
485 template <typename T>
486 T *
488 {
489  return m_ptr;
490 }
491 
492 template <typename T>
493 const T &
495 {
496  return *m_ptr;
497 }
498 
499 template <typename T>
500 T &
502 {
503  return *m_ptr;
504 }
505 
506 template <typename T>
507 bool
509 {
510  return m_ptr == 0;
511 }
512 
513 template <typename T>
515 {
516  if (m_ptr == 0)
517  {
518  return 0;
519  }
520  static Tester test;
521  return &test;
522 }
523 
524 
525 } // namespace ns3
526 
527 #endif /* PTR_H */
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
Ptr()
Create an empty smart pointer.
Definition: ptr.h:416
bool operator!()
Definition: ptr.h:508
T * GetPointer(const Ptr< T > &p)
Definition: ptr.h:286
T * operator->() const
Definition: ptr.h:487
static T & GetReference(Ptr< T > const p)
Definition: ptr.h:203
bool operator>=(const int64x64_t &lhs, const int64x64_t &rhs)
Greater or equal operator.
Definition: int64x64.h:154
~Ptr()
Definition: ptr.h:453
const T & operator*() const
Definition: ptr.h:494
Ptr< T > Create(void)
Definition: ptr.h:232
Ptr< T > & operator=(Ptr const &o)
Definition: ptr.h:463
Trait class to convert a pointer into a reference, used by MemPtrCallBackImpl.
Definition: callback.h:52
T * PeekPointer(const Ptr< T > &p)
Definition: ptr.h:280
Ptr< T1 > StaticCast(Ptr< T2 > const &p)
Definition: ptr.h:381
Ptr< T1 > DynamicCast(Ptr< T2 > const &p)
Definition: ptr.h:374
bool operator!=(Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > a, Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > b)
Inequality test.
Definition: callback.h:1217
T * m_ptr
Definition: ptr.h:63
static T & GetReference(Ptr< T > p)
Definition: ptr.h:215
friend U * GetPointer(const Ptr< U > &p)
Ptr< T1 > ConstCast(Ptr< T2 > const &p)
Definition: ptr.h:367
bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:335
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.cc:89
friend U * PeekPointer(const Ptr< U > &p)
void Acquire(void) const
Definition: ptr.h:407
Ptr< T1 > const_pointer_cast(Ptr< T2 > const &p)
void test(void)
Ptr< T > Copy(Ptr< T > object)
Definition: ptr.h:388