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 
101  template <typename U>
102  friend U * GetPointer (const Ptr<U> &p);
113  template <typename U>
114  friend U * PeekPointer (const Ptr<U> &p);
115 
117  inline void Acquire (void) const;
118 
119 public:
121  Ptr ();
132  Ptr (T *ptr);
141  Ptr (T *ptr, bool ref);
147  Ptr (Ptr const&o);
154  template <typename U>
155  Ptr (Ptr<U> const &o);
157  ~Ptr ();
164  Ptr<T> &operator = (Ptr const& o);
169  T *operator -> () const;
174  T *operator -> ();
179  T &operator * () const;
184  T &operator * ();
195  bool operator! ();
206  operator Tester * () const;
207 };
208 
223 template <typename T>
224 Ptr<T> Create (void);
225 
232 template <typename T,
233  typename T1>
234 Ptr<T> Create (T1 a1);
235 
244 template <typename T,
245  typename T1, typename T2>
246 Ptr<T> Create (T1 a1, T2 a2);
247 
258 template <typename T,
259  typename T1, typename T2,
260  typename T3>
261 Ptr<T> Create (T1 a1, T2 a2, T3 a3);
262 
275 template <typename T,
276  typename T1, typename T2,
277  typename T3, typename T4>
278 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4);
279 
294 template <typename T,
295  typename T1, typename T2,
296  typename T3, typename T4,
297  typename T5>
298 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5);
299 
316 template <typename T,
317  typename T1, typename T2,
318  typename T3, typename T4,
319  typename T5, typename T6>
320 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6);
321 
340 template <typename T,
341  typename T1, typename T2,
342  typename T3, typename T4,
343  typename T5, typename T6,
344  typename T7>
345 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7);
355 template <typename T>
356 std::ostream &operator << (std::ostream &os, const Ptr<T> &p);
357 
379 template <typename T1, typename T2>
380 bool operator == (Ptr<T1> const &lhs, T2 const *rhs);
381 
382 template <typename T1, typename T2>
383 bool operator == (T1 const *lhs, Ptr<T2> &rhs);
384 
385 template <typename T1, typename T2>
386 bool operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
410 template <typename T1, typename T2>
411 bool operator != (Ptr<T1> const &lhs, T2 const *rhs);
412 
413 template <typename T1, typename T2>
414 bool operator != (T1 const *lhs, Ptr<T2> &rhs);
415 
416 template <typename T1, typename T2>
417 bool operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
430 template <typename T>
431 bool operator < (const Ptr<T> &lhs, const Ptr<T> &rhs);
432 template <typename T>
433 bool operator <= (const Ptr<T> &lhs, const Ptr<T> &rhs);
434 template <typename T>
435 bool operator > (const Ptr<T> &lhs, const Ptr<T> &rhs);
436 template <typename T>
437 bool operator >= (const Ptr<T> &lhs, const Ptr<T> &rhs);
449 template <typename T1, typename T2>
451 
452 // Duplicate of struct CallbackTraits<T> as defined in callback.h
453 template <typename T>
454 struct CallbackTraits;
455 
466 template <typename T>
467 struct CallbackTraits<Ptr<T> >
468 {
473  static T & GetReference (Ptr<T> const p)
474  {
475  return *PeekPointer (p);
476  }
477 };
478 
479 // Duplicate of struct EventMemberImplObjTraits<T> as defined in make-event.h
480 // We repeat it here to declare a specialization on Ptr<T>
481 // without making this header dependent on make-event.h
482 template <typename T>
483 struct EventMemberImplObjTraits;
484 
493 template <typename T>
495 {
500  static T & GetReference (Ptr<T> p)
501  {
502  return *PeekPointer (p);
503  }
504 };
505 
506 
507 
508 } // namespace ns3
509 
510 
511 namespace ns3 {
512 
513 /*************************************************
514  * friend non-member function implementations
515  ************************************************/
516 
517 template <typename T>
519 {
520  return Ptr<T> (new T (), false);
521 }
522 
523 template <typename T, typename T1>
524 Ptr<T> Create (T1 a1)
525 {
526  return Ptr<T> (new T (a1), false);
527 }
528 
529 template <typename T, typename T1, typename T2>
530 Ptr<T> Create (T1 a1, T2 a2)
531 {
532  return Ptr<T> (new T (a1, a2), false);
533 }
534 
535 template <typename T, typename T1, typename T2, typename T3>
536 Ptr<T> Create (T1 a1, T2 a2, T3 a3)
537 {
538  return Ptr<T> (new T (a1, a2, a3), false);
539 }
540 
541 template <typename T, typename T1, typename T2, typename T3, typename T4>
542 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4)
543 {
544  return Ptr<T> (new T (a1, a2, a3, a4), false);
545 }
546 
547 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
548 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
549 {
550  return Ptr<T> (new T (a1, a2, a3, a4, a5), false);
551 }
552 
553 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
554 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
555 {
556  return Ptr<T> (new T (a1, a2, a3, a4, a5, a6), false);
557 }
558 
559 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
560 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
561 {
562  return Ptr<T> (new T (a1, a2, a3, a4, a5, a6, a7), false);
563 }
564 
565 template <typename U>
566 U * PeekPointer (const Ptr<U> &p)
567 {
568  return p.m_ptr;
569 }
570 
571 template <typename U>
572 U * GetPointer (const Ptr<U> &p)
573 {
574  p.Acquire ();
575  return p.m_ptr;
576 }
577 
578 template <typename T>
579 std::ostream &operator << (std::ostream &os, const Ptr<T> &p)
580 {
581  os << PeekPointer (p);
582  return os;
583 }
584 
585 template <typename T1, typename T2>
586 bool
587 operator == (Ptr<T1> const &lhs, T2 const *rhs)
588 {
589  return PeekPointer (lhs) == rhs;
590 }
591 
592 template <typename T1, typename T2>
593 bool
594 operator == (T1 const *lhs, Ptr<T2> &rhs)
595 {
596  return lhs == PeekPointer (rhs);
597 }
598 
599 template <typename T1, typename T2>
600 bool
601 operator != (Ptr<T1> const &lhs, T2 const *rhs)
602 {
603  return PeekPointer (lhs) != rhs;
604 }
605 
606 template <typename T1, typename T2>
607 bool
608 operator != (T1 const *lhs, Ptr<T2> &rhs)
609 {
610  return lhs != PeekPointer (rhs);
611 }
612 
613 template <typename T1, typename T2>
614 bool
615 operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
616 {
617  return PeekPointer (lhs) == PeekPointer (rhs);
618 }
619 
620 template <typename T1, typename T2>
621 bool
622 operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
623 {
624  return PeekPointer (lhs) != PeekPointer (rhs);
625 }
626 
627 template <typename T>
628 bool operator < (const Ptr<T> &lhs, const Ptr<T> &rhs)
629 {
630  return PeekPointer<T> (lhs) < PeekPointer<T> (rhs);
631 }
632 
633 template <typename T>
634 bool operator <= (const Ptr<T> &lhs, const Ptr<T> &rhs)
635 {
636  return PeekPointer<T> (lhs) <= PeekPointer<T> (rhs);
637 }
638 
639 template <typename T>
640 bool operator > (const Ptr<T> &lhs, const Ptr<T> &rhs)
641 {
642  return PeekPointer<T> (lhs) > PeekPointer<T> (rhs);
643 }
644 
645 template <typename T>
646 bool operator >= (const Ptr<T> &lhs, const Ptr<T> &rhs)
647 {
648  return PeekPointer<T> (lhs) >= PeekPointer<T> (rhs);
649 }
650 
660 template <typename T1, typename T2>
661 Ptr<T1>
663 {
664  return Ptr<T1> (const_cast<T1 *> (PeekPointer (p)));
665 }
666 
667 template <typename T1, typename T2>
668 Ptr<T1>
670 {
671  return Ptr<T1> (dynamic_cast<T1 *> (PeekPointer (p)));
672 }
673 
674 template <typename T1, typename T2>
675 Ptr<T1>
677 {
678  return Ptr<T1> (static_cast<T1 *> (PeekPointer (p)));
679 }
689 template <typename T>
691 {
692  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
693  return p;
694 }
695 
696 template <typename T>
697 Ptr<T> Copy (Ptr<const T> object)
698 {
699  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
700  return p;
701 }
704 /****************************************************
705  * Member method implementations.
706  ***************************************************/
707 
708 template <typename T>
709 void
710 Ptr<T>::Acquire (void) const
711 {
712  if (m_ptr != 0)
713  {
714  m_ptr->Ref ();
715  }
716 }
717 
718 template <typename T>
720  : m_ptr (0)
721 {}
722 
723 template <typename T>
724 Ptr<T>::Ptr (T *ptr)
725  : m_ptr (ptr)
726 {
727  Acquire ();
728 }
729 
730 template <typename T>
731 Ptr<T>::Ptr (T *ptr, bool ref)
732  : m_ptr (ptr)
733 {
734  if (ref)
735  {
736  Acquire ();
737  }
738 }
739 
740 template <typename T>
741 Ptr<T>::Ptr (Ptr const&o)
742  : m_ptr (PeekPointer (o))
743 {
744  Acquire ();
745 }
746 template <typename T>
747 template <typename U>
748 Ptr<T>::Ptr (Ptr<U> const &o)
749  : m_ptr (PeekPointer (o))
750 {
751  Acquire ();
752 }
753 
754 template <typename T>
756 {
757  if (m_ptr != 0)
758  {
759  m_ptr->Unref ();
760  }
761 }
762 
763 template <typename T>
764 Ptr<T> &
766 {
767  if (&o == this)
768  {
769  return *this;
770  }
771  if (m_ptr != 0)
772  {
773  m_ptr->Unref ();
774  }
775  m_ptr = o.m_ptr;
776  Acquire ();
777  return *this;
778 }
779 
780 template <typename T>
781 T *
783 {
784  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
785  return m_ptr;
786 }
787 
788 template <typename T>
789 T *
791 {
792  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
793  return m_ptr;
794 }
795 
796 template <typename T>
797 T &
799 {
800  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
801  return *m_ptr;
802 }
803 
804 template <typename T>
805 T &
807 {
808  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
809  return *m_ptr;
810 }
811 
812 template <typename T>
813 bool
815 {
816  return m_ptr == 0;
817 }
818 
819 template <typename T>
820 Ptr<T>::operator Tester * () const
821 {
822  if (m_ptr == 0)
823  {
824  return 0;
825  }
826  static Tester test;
827  return &test;
828 }
829 
830 
831 } // namespace ns3
832 
833 #endif /* PTR_H */
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Ptr()
Create an empty smart pointer.
Definition: ptr.h:719
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
bool operator!()
Test for NULL pointer.
Definition: ptr.h:814
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:566
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:473
bool operator>=(const int64x64_t &lhs, const int64x64_t &rhs)
Greater or equal operator.
Definition: int64x64.h:145
T * operator->() const
An rvalue member access.
Definition: ptr.h:790
~Ptr()
Destructor.
Definition: ptr.h:755
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:108
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:572
Ptr< T > & operator=(Ptr const &o)
Assignment operator by referencing the same underlying object.
Definition: ptr.h:765
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
Trait class to convert a pointer into a reference, used by MemPtrCallBackImpl.
Definition: callback.h:113
T & operator*() const
A const dereference.
Definition: ptr.h:798
Ptr< T1 > StaticCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:676
Ptr< T1 > DynamicCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:669
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:1592
Every class exported by the ns3 library is enclosed in the ns3 namespace.
T * m_ptr
The pointer.
Definition: ptr.h:78
Ptr< T > Create(void)
Create class instances by constructors with varying numbers of arguments and return them by Ptr...
Definition: ptr.h:518
void Acquire(void) const
Mark this as a a reference by incrementing the reference count.
Definition: ptr.h:710
static T & GetReference(Ptr< T > p)
Definition: ptr.h:500
friend U * GetPointer(const Ptr< U > &p)
Get a permanent pointer to the underlying object.
Definition: ptr.h:572
Ptr< T1 > ConstCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:662
bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:368
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:137
friend U * PeekPointer(const Ptr< U > &p)
Get a temporary pointer to the underlying object.
Definition: ptr.h:566
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:690
int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:430