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  const 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 template <typename T>
480 struct EventMemberImplObjTraits;
481 
490 template <typename T>
492 {
497  static T &GetReference (Ptr<T> p) {
498  return *PeekPointer (p);
499  }
500 };
501 
502 
503 
504 } // namespace ns3
505 
506 
507 namespace ns3 {
508 
509 /*************************************************
510  * friend non-member function implementations
511  ************************************************/
512 
513 template <typename T>
515 {
516  return Ptr<T> (new T (), false);
517 }
518 
519 template <typename T, typename T1>
520 Ptr<T> Create (T1 a1)
521 {
522  return Ptr<T> (new T (a1), false);
523 }
524 
525 template <typename T, typename T1, typename T2>
526 Ptr<T> Create (T1 a1, T2 a2)
527 {
528  return Ptr<T> (new T (a1, a2), false);
529 }
530 
531 template <typename T, typename T1, typename T2, typename T3>
532 Ptr<T> Create (T1 a1, T2 a2, T3 a3)
533 {
534  return Ptr<T> (new T (a1, a2, a3), false);
535 }
536 
537 template <typename T, typename T1, typename T2, typename T3, typename T4>
538 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4)
539 {
540  return Ptr<T> (new T (a1, a2, a3, a4), false);
541 }
542 
543 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
544 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
545 {
546  return Ptr<T> (new T (a1, a2, a3, a4, a5), false);
547 }
548 
549 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
550 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
551 {
552  return Ptr<T> (new T (a1, a2, a3, a4, a5, a6), false);
553 }
554 
555 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
556 Ptr<T> Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
557 {
558  return Ptr<T> (new T (a1, a2, a3, a4, a5, a6, a7), false);
559 }
560 
561 template <typename U>
562 U * PeekPointer (const Ptr<U> &p)
563 {
564  return p.m_ptr;
565 }
566 
567 template <typename U>
568 U * GetPointer (const Ptr<U> &p)
569 {
570  p.Acquire ();
571  return p.m_ptr;
572 }
573 
574 template <typename T>
575 std::ostream &operator << (std::ostream &os, const Ptr<T> &p)
576 {
577  os << PeekPointer (p);
578  return os;
579 }
580 
581 template <typename T1, typename T2>
582 bool
583 operator == (Ptr<T1> const &lhs, T2 const *rhs)
584 {
585  return PeekPointer (lhs) == rhs;
586 }
587 
588 template <typename T1, typename T2>
589 bool
590 operator == (T1 const *lhs, Ptr<T2> &rhs)
591 {
592  return lhs == PeekPointer (rhs);
593 }
594 
595 template <typename T1, typename T2>
596 bool
597 operator != (Ptr<T1> const &lhs, T2 const *rhs)
598 {
599  return PeekPointer (lhs) != rhs;
600 }
601 
602 template <typename T1, typename T2>
603 bool
604 operator != (T1 const *lhs, Ptr<T2> &rhs)
605 {
606  return lhs != PeekPointer (rhs);
607 }
608 
609 template <typename T1, typename T2>
610 bool
611 operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
612 {
613  return PeekPointer (lhs) == PeekPointer (rhs);
614 }
615 
616 template <typename T1, typename T2>
617 bool
618 operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
619 {
620  return PeekPointer (lhs) != PeekPointer (rhs);
621 }
622 
623 template <typename T>
624 bool operator < (const Ptr<T> &lhs, const Ptr<T> &rhs)
625 {
626  return PeekPointer<T> (lhs) < PeekPointer<T> (rhs);
627 }
628 
629 template <typename T>
630 bool operator <= (const Ptr<T> &lhs, const Ptr<T> &rhs)
631 {
632  return PeekPointer<T> (lhs) <= PeekPointer<T> (rhs);
633 }
634 
635 template <typename T>
636 bool operator > (const Ptr<T> &lhs, const Ptr<T> &rhs)
637 {
638  return PeekPointer<T> (lhs) > PeekPointer<T> (rhs);
639 }
640 
641 template <typename T>
642 bool operator >= (const Ptr<T> &lhs, const Ptr<T> &rhs)
643 {
644  return PeekPointer<T> (lhs) >= PeekPointer<T> (rhs);
645 }
646 
656 template <typename T1, typename T2>
657 Ptr<T1>
659 {
660  return Ptr<T1> (const_cast<T1 *> (PeekPointer (p)));
661 }
662 
663 template <typename T1, typename T2>
664 Ptr<T1>
666 {
667  return Ptr<T1> (dynamic_cast<T1 *> (PeekPointer (p)));
668 }
669 
670 template <typename T1, typename T2>
671 Ptr<T1>
673 {
674  return Ptr<T1> (static_cast<T1 *> (PeekPointer (p)));
675 }
685 template <typename T>
687 {
688  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
689  return p;
690 }
691 
692 template <typename T>
693 Ptr<T> Copy (Ptr<const T> object)
694 {
695  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
696  return p;
697 }
700 /****************************************************
701  * Member method implementations.
702  ***************************************************/
703 
704 template <typename T>
705 void
706 Ptr<T>::Acquire (void) const
707 {
708  if (m_ptr != 0)
709  {
710  m_ptr->Ref ();
711  }
712 }
713 
714 template <typename T>
716  : m_ptr (0)
717 {
718 }
719 
720 template <typename T>
721 Ptr<T>::Ptr (T *ptr)
722  : m_ptr (ptr)
723 {
724  Acquire ();
725 }
726 
727 template <typename T>
728 Ptr<T>::Ptr (T *ptr, bool ref)
729  : m_ptr (ptr)
730 {
731  if (ref)
732  {
733  Acquire ();
734  }
735 }
736 
737 template <typename T>
738 Ptr<T>::Ptr (Ptr const&o)
739  : m_ptr (PeekPointer (o))
740 {
741  Acquire ();
742 }
743 template <typename T>
744 template <typename U>
745 Ptr<T>::Ptr (Ptr<U> const &o)
746  : m_ptr (PeekPointer (o))
747 {
748  Acquire ();
749 }
750 
751 template <typename T>
753 {
754  if (m_ptr != 0)
755  {
756  m_ptr->Unref ();
757  }
758 }
759 
760 template <typename T>
761 Ptr<T> &
763 {
764  if (&o == this)
765  {
766  return *this;
767  }
768  if (m_ptr != 0)
769  {
770  m_ptr->Unref ();
771  }
772  m_ptr = o.m_ptr;
773  Acquire ();
774  return *this;
775 }
776 
777 template <typename T>
778 T *
780 {
781  return m_ptr;
782 }
783 
784 template <typename T>
785 T *
787 {
788  return m_ptr;
789 }
790 
791 template <typename T>
792 const T &
794 {
795  return *m_ptr;
796 }
797 
798 template <typename T>
799 T &
801 {
802  return *m_ptr;
803 }
804 
805 template <typename T>
806 bool
808 {
809  return m_ptr == 0;
810 }
811 
812 template <typename T>
814 {
815  if (m_ptr == 0)
816  {
817  return 0;
818  }
819  static Tester test;
820  return &test;
821 }
822 
823 
824 } // namespace ns3
825 
826 #endif /* PTR_H */
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Ptr()
Create an empty smart pointer.
Definition: ptr.h:715
bool operator!()
Test for NULL pointer.
Definition: ptr.h:807
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:562
Helper for the MakeEvent functions which take a class method.
Definition: make-event.h:280
T * operator->() const
An rvalue member access.
Definition: ptr.h:786
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
~Ptr()
Destructor.
Definition: ptr.h:752
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:568
const T & operator*() const
A const dereference.
Definition: ptr.h:793
Ptr< T > & operator=(Ptr const &o)
Assignment operator by referencing the same underlying object.
Definition: ptr.h:762
Definition of assertion macros NS_ASSERT() and NS_ASSERT_MSG().
Trait class to convert a pointer into a reference, used by MemPtrCallBackImpl.
Definition: callback.h:113
Ptr< T1 > StaticCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:672
Ptr< T1 > DynamicCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:665
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:514
static T & GetReference(Ptr< T > p)
Definition: ptr.h:497
friend U * GetPointer(const Ptr< U > &p)
Get a permanent pointer to the underlying object.
Definition: ptr.h:568
Ptr< T1 > ConstCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:658
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.cc:95
friend U * PeekPointer(const Ptr< U > &p)
Get a temporary pointer to the underlying object.
Definition: ptr.h:562
void Acquire(void) const
Mark this as a a reference by incrementing the reference count.
Definition: ptr.h:706
Ptr< T1 > const_pointer_cast(Ptr< T2 > const &p)
Return a copy of p with its stored pointer const casted from T2 to T1.
void test(void)
Example use of ns3::SystemThread.
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:686