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