A Discrete-Event Network Simulator
API
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 
34 namespace ns3 {
35 
72 template <typename T>
73 class Ptr
74 {
75 private:
76 
78  T *m_ptr;
79 
81  class Tester
82  {
83  private:
85  void operator delete (void *);
86  };
87 
89  friend class Ptr<const T>;
90 
102  template <typename U>
103  friend U * GetPointer (const Ptr<U> &p);
115  template <typename U>
116  friend U * PeekPointer (const Ptr<U> &p);
117 
119  inline void Acquire (void) const;
120 
121 public:
123  Ptr ();
134  Ptr (T *ptr);
143  Ptr (T *ptr, bool ref);
149  Ptr (Ptr const&o);
156  template <typename U>
157  Ptr (Ptr<U> const &o);
159  ~Ptr ();
166  Ptr<T> &operator = (Ptr const& o);
171  T *operator -> () const;
176  T *operator -> ();
181  T &operator * () const;
186  T &operator * ();
197  bool operator! ();
208  operator Tester * () const;
209 };
210 
227 template <typename T,
228  typename... Ts>
229 Ptr<T> Create (Ts... args);
230 
241 template <typename T>
242 std::ostream &operator << (std::ostream &os, const Ptr<T> &p);
243 
265 template <typename T1, typename T2>
266 bool operator == (Ptr<T1> const &lhs, T2 const *rhs);
267 
268 template <typename T1, typename T2>
269 bool operator == (T1 const *lhs, Ptr<T2> &rhs);
270 
271 template <typename T1, typename T2>
272 bool operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
296 template <typename T1, typename T2>
297 bool operator != (Ptr<T1> const &lhs, T2 const *rhs);
298 
299 template <typename T1, typename T2>
300 bool operator != (T1 const *lhs, Ptr<T2> &rhs);
301 
302 template <typename T1, typename T2>
303 bool operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
317 template <typename T>
318 bool operator < (const Ptr<T> &lhs, const Ptr<T> &rhs);
319 template <typename T>
320 bool operator <= (const Ptr<T> &lhs, const Ptr<T> &rhs);
321 template <typename T>
322 bool operator > (const Ptr<T> &lhs, const Ptr<T> &rhs);
323 template <typename T>
324 bool operator >= (const Ptr<T> &lhs, const Ptr<T> &rhs);
336 template <typename T1, typename T2>
338 
339 // Duplicate of struct CallbackTraits<T> as defined in callback.h
340 template <typename T>
341 struct CallbackTraits;
342 
353 template <typename T>
354 struct CallbackTraits<Ptr<T> >
355 {
360  static T & GetReference (Ptr<T> const p)
361  {
362  return *PeekPointer (p);
363  }
364 };
365 
366 // Duplicate of struct EventMemberImplObjTraits<T> as defined in make-event.h
367 // We repeat it here to declare a specialization on Ptr<T>
368 // without making this header dependent on make-event.h
369 template <typename T>
370 struct EventMemberImplObjTraits;
371 
380 template <typename T>
382 {
387  static T & GetReference (Ptr<T> p)
388  {
389  return *PeekPointer (p);
390  }
391 };
392 
393 
394 
395 } // namespace ns3
396 
397 
398 namespace ns3 {
399 
400 /*************************************************
401  * friend non-member function implementations
402  ************************************************/
403 
404 template <typename T, typename... Ts>
405 Ptr<T> Create (Ts... args)
406 {
407  return Ptr<T> (new T (args...), false);
408 }
409 
410 template <typename U>
411 U * PeekPointer (const Ptr<U> &p)
412 {
413  return p.m_ptr;
414 }
415 
416 template <typename U>
417 U * GetPointer (const Ptr<U> &p)
418 {
419  p.Acquire ();
420  return p.m_ptr;
421 }
422 
423 template <typename T>
424 std::ostream &operator << (std::ostream &os, const Ptr<T> &p)
425 {
426  os << PeekPointer (p);
427  return os;
428 }
429 
430 template <typename T1, typename T2>
431 bool
432 operator == (Ptr<T1> const &lhs, T2 const *rhs)
433 {
434  return PeekPointer (lhs) == rhs;
435 }
436 
437 template <typename T1, typename T2>
438 bool
439 operator == (T1 const *lhs, Ptr<T2> &rhs)
440 {
441  return lhs == PeekPointer (rhs);
442 }
443 
444 template <typename T1, typename T2>
445 bool
446 operator != (Ptr<T1> const &lhs, T2 const *rhs)
447 {
448  return PeekPointer (lhs) != rhs;
449 }
450 
451 template <typename T1, typename T2>
452 bool
453 operator != (T1 const *lhs, Ptr<T2> &rhs)
454 {
455  return lhs != PeekPointer (rhs);
456 }
457 
458 template <typename T1, typename T2>
459 bool
460 operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
461 {
462  return PeekPointer (lhs) == PeekPointer (rhs);
463 }
464 
465 template <typename T1, typename T2>
466 bool
467 operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
468 {
469  return PeekPointer (lhs) != PeekPointer (rhs);
470 }
471 
472 template <typename T>
473 bool operator < (const Ptr<T> &lhs, const Ptr<T> &rhs)
474 {
475  return PeekPointer<T> (lhs) < PeekPointer<T> (rhs);
476 }
477 
478 template <typename T>
479 bool operator <= (const Ptr<T> &lhs, const Ptr<T> &rhs)
480 {
481  return PeekPointer<T> (lhs) <= PeekPointer<T> (rhs);
482 }
483 
484 template <typename T>
485 bool operator > (const Ptr<T> &lhs, const Ptr<T> &rhs)
486 {
487  return PeekPointer<T> (lhs) > PeekPointer<T> (rhs);
488 }
489 
490 template <typename T>
491 bool operator >= (const Ptr<T> &lhs, const Ptr<T> &rhs)
492 {
493  return PeekPointer<T> (lhs) >= PeekPointer<T> (rhs);
494 }
495 
505 template <typename T1, typename T2>
506 Ptr<T1>
508 {
509  return Ptr<T1> (const_cast<T1 *> (PeekPointer (p)));
510 }
511 
512 template <typename T1, typename T2>
513 Ptr<T1>
515 {
516  return Ptr<T1> (dynamic_cast<T1 *> (PeekPointer (p)));
517 }
518 
519 template <typename T1, typename T2>
520 Ptr<T1>
522 {
523  return Ptr<T1> (static_cast<T1 *> (PeekPointer (p)));
524 }
535 template <typename T>
537 {
538  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
539  return p;
540 }
541 
542 template <typename T>
543 Ptr<T> Copy (Ptr<const T> object)
544 {
545  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
546  return p;
547 }
550 /****************************************************
551  * Member method implementations.
552  ***************************************************/
553 
554 template <typename T>
555 void
556 Ptr<T>::Acquire (void) const
557 {
558  if (m_ptr != 0)
559  {
560  m_ptr->Ref ();
561  }
562 }
563 
564 template <typename T>
566  : m_ptr (0)
567 {}
568 
569 template <typename T>
570 Ptr<T>::Ptr (T *ptr)
571  : m_ptr (ptr)
572 {
573  Acquire ();
574 }
575 
576 template <typename T>
577 Ptr<T>::Ptr (T *ptr, bool ref)
578  : m_ptr (ptr)
579 {
580  if (ref)
581  {
582  Acquire ();
583  }
584 }
585 
586 template <typename T>
587 Ptr<T>::Ptr (Ptr const&o)
588  : m_ptr (PeekPointer (o))
589 {
590  Acquire ();
591 }
592 template <typename T>
593 template <typename U>
594 Ptr<T>::Ptr (Ptr<U> const &o)
595  : m_ptr (PeekPointer (o))
596 {
597  Acquire ();
598 }
599 
600 template <typename T>
602 {
603  if (m_ptr != 0)
604  {
605  m_ptr->Unref ();
606  }
607 }
608 
609 template <typename T>
610 Ptr<T> &
612 {
613  if (&o == this)
614  {
615  return *this;
616  }
617  if (m_ptr != 0)
618  {
619  m_ptr->Unref ();
620  }
621  m_ptr = o.m_ptr;
622  Acquire ();
623  return *this;
624 }
625 
626 template <typename T>
627 T *
629 {
630  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
631  return m_ptr;
632 }
633 
634 template <typename T>
635 T *
637 {
638  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
639  return m_ptr;
640 }
641 
642 template <typename T>
643 T &
645 {
646  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
647  return *m_ptr;
648 }
649 
650 template <typename T>
651 T &
653 {
654  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
655  return *m_ptr;
656 }
657 
658 template <typename T>
659 bool
661 {
662  return m_ptr == 0;
663 }
664 
665 template <typename T>
666 Ptr<T>::operator Tester * () const
667 {
668  if (m_ptr == 0)
669  {
670  return 0;
671  }
672  static Tester test;
673  return &test;
674 }
675 
676 
677 } // namespace ns3
678 
679 #endif /* PTR_H */
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Ptr()
Create an empty smart pointer.
Definition: ptr.h:565
bool operator!()
Test for NULL pointer.
Definition: ptr.h:660
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:411
Helper for the MakeEvent functions which take a class method.
Definition: make-event.h:333
static T & GetReference(Ptr< T > const p)
Definition: ptr.h:360
bool operator>=(const int64x64_t &lhs, const int64x64_t &rhs)
Greater or equal operator.
Definition: int64x64.h:166
T * operator->() const
An rvalue member access.
Definition: ptr.h:636
~Ptr()
Destructor.
Definition: ptr.h:601
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:117
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:417
Ptr< T > & operator=(Ptr const &o)
Assignment operator by referencing the same underlying object.
Definition: ptr.h:611
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
Trait class to convert a pointer into a reference, used by MemPtrCallBackImpl.
Definition: callback.h:80
T & operator*() const
A const dereference.
Definition: ptr.h:644
Ptr< T1 > StaticCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:521
Ptr< T1 > DynamicCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:514
Ptr< T > Create(Ts... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr...
Definition: ptr.h:405
Helper to test for null pointer.
Definition: ptr.h:81
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:1606
Every class exported by the ns3 library is enclosed in the ns3 namespace.
T * m_ptr
The pointer.
Definition: ptr.h:78
void Acquire(void) const
Mark this as a a reference by incrementing the reference count.
Definition: ptr.h:556
static T & GetReference(Ptr< T > p)
Definition: ptr.h:387
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:88
friend U * GetPointer(const Ptr< U > &p)
Get a permanent pointer to the underlying object.
Definition: ptr.h:417
Ptr< T1 > ConstCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:507
bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:400
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:142
friend U * PeekPointer(const Ptr< U > &p)
Get a temporary pointer to the underlying object.
Definition: ptr.h:411
Ptr< T1 > const_pointer_cast(Ptr< T2 > const &p)
Return a copy of p with its stored pointer const casted from T2 to T1.
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:536
int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:480