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;
181  T &operator * () const;
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<const T> &rhs);
321 template <typename T>
322 bool operator< (const Ptr<const T> &lhs, const Ptr<T> &rhs);
323 template <typename T>
324 bool operator<= (const Ptr<T> &lhs, const Ptr<T> &rhs);
325 template <typename T>
326 bool operator > (const Ptr<T> &lhs, const Ptr<T> &rhs);
327 template <typename T>
328 bool operator >= (const Ptr<T> &lhs, const Ptr<T> &rhs);
340 template <typename T1, typename T2>
342 
343 // Duplicate of struct CallbackTraits<T> as defined in callback.h
344 template <typename T>
345 struct CallbackTraits;
346 
357 template <typename T>
358 struct CallbackTraits<Ptr<T> >
359 {
364  static T & GetReference (Ptr<T> const p)
365  {
366  return *PeekPointer (p);
367  }
368 };
369 
370 // Duplicate of struct EventMemberImplObjTraits<T> as defined in make-event.h
371 // We repeat it here to declare a specialization on Ptr<T>
372 // without making this header dependent on make-event.h
373 template <typename T>
374 struct EventMemberImplObjTraits;
375 
384 template <typename T>
386 {
391  static T & GetReference (Ptr<T> p)
392  {
393  return *PeekPointer (p);
394  }
395 };
396 
397 
398 
399 } // namespace ns3
400 
401 
402 namespace ns3 {
403 
404 /*************************************************
405  * friend non-member function implementations
406  ************************************************/
407 
408 template <typename T, typename... Ts>
409 Ptr<T> Create (Ts&&... args)
410 {
411  return Ptr<T> (new T (std::forward<Ts> (args)...), false);
412 }
413 
414 template <typename U>
415 U * PeekPointer (const Ptr<U> &p)
416 {
417  return p.m_ptr;
418 }
419 
420 template <typename U>
421 U * GetPointer (const Ptr<U> &p)
422 {
423  p.Acquire ();
424  return p.m_ptr;
425 }
426 
427 template <typename T>
428 std::ostream &operator << (std::ostream &os, const Ptr<T> &p)
429 {
430  os << PeekPointer (p);
431  return os;
432 }
433 
434 template <typename T1, typename T2>
435 bool
436 operator == (Ptr<T1> const &lhs, T2 const *rhs)
437 {
438  return PeekPointer (lhs) == rhs;
439 }
440 
441 template <typename T1, typename T2>
442 bool
443 operator == (T1 const *lhs, Ptr<T2> &rhs)
444 {
445  return lhs == PeekPointer (rhs);
446 }
447 
448 template <typename T1, typename T2>
449 bool
450 operator != (Ptr<T1> const &lhs, T2 const *rhs)
451 {
452  return PeekPointer (lhs) != rhs;
453 }
454 
455 template <typename T1, typename T2>
456 bool
457 operator != (T1 const *lhs, Ptr<T2> &rhs)
458 {
459  return lhs != PeekPointer (rhs);
460 }
461 
462 template <typename T1, typename T2>
463 bool
464 operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
465 {
466  return PeekPointer (lhs) == PeekPointer (rhs);
467 }
468 
469 template <typename T1, typename T2>
470 bool
471 operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
472 {
473  return PeekPointer (lhs) != PeekPointer (rhs);
474 }
475 
476 template <typename T>
477 bool operator < (const Ptr<T> &lhs, const Ptr<T> &rhs)
478 {
479  return PeekPointer<T> (lhs) < PeekPointer<T> (rhs);
480 }
481 
482 template <typename T>
483 bool
484 operator< (const Ptr<T> &lhs, const Ptr<const T> &rhs)
485 {
486  return PeekPointer<T> (lhs) < PeekPointer<const T> (rhs);
487 }
488 
489 template <typename T>
490 bool
491 operator< (const Ptr<const T> &lhs, const Ptr<T> &rhs)
492 {
493  return PeekPointer<const T> (lhs) < PeekPointer<T> (rhs);
494 }
495 
496 template <typename T>
497 bool
498 operator<= (const Ptr<T> &lhs, const Ptr<T> &rhs)
499 {
500  return PeekPointer<T> (lhs) <= PeekPointer<T> (rhs);
501 }
502 
503 template <typename T>
504 bool operator > (const Ptr<T> &lhs, const Ptr<T> &rhs)
505 {
506  return PeekPointer<T> (lhs) > PeekPointer<T> (rhs);
507 }
508 
509 template <typename T>
510 bool operator >= (const Ptr<T> &lhs, const Ptr<T> &rhs)
511 {
512  return PeekPointer<T> (lhs) >= PeekPointer<T> (rhs);
513 }
514 
524 template <typename T1, typename T2>
525 Ptr<T1>
527 {
528  return Ptr<T1> (const_cast<T1 *> (PeekPointer (p)));
529 }
530 
531 template <typename T1, typename T2>
532 Ptr<T1>
534 {
535  return Ptr<T1> (dynamic_cast<T1 *> (PeekPointer (p)));
536 }
537 
538 template <typename T1, typename T2>
539 Ptr<T1>
541 {
542  return Ptr<T1> (static_cast<T1 *> (PeekPointer (p)));
543 }
554 template <typename T>
556 {
557  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
558  return p;
559 }
560 
561 template <typename T>
562 Ptr<T> Copy (Ptr<const T> object)
563 {
564  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
565  return p;
566 }
569 /****************************************************
570  * Member method implementations.
571  ***************************************************/
572 
573 template <typename T>
574 void
575 Ptr<T>::Acquire (void) const
576 {
577  if (m_ptr != 0)
578  {
579  m_ptr->Ref ();
580  }
581 }
582 
583 template <typename T>
585  : m_ptr (0)
586 {}
587 
588 template <typename T>
589 Ptr<T>::Ptr (T *ptr)
590  : m_ptr (ptr)
591 {
592  Acquire ();
593 }
594 
595 template <typename T>
596 Ptr<T>::Ptr (T *ptr, bool ref)
597  : m_ptr (ptr)
598 {
599  if (ref)
600  {
601  Acquire ();
602  }
603 }
604 
605 template <typename T>
606 Ptr<T>::Ptr (Ptr const&o)
607  : m_ptr (PeekPointer (o))
608 {
609  Acquire ();
610 }
611 template <typename T>
612 template <typename U>
613 Ptr<T>::Ptr (Ptr<U> const &o)
614  : m_ptr (PeekPointer (o))
615 {
616  Acquire ();
617 }
618 
619 template <typename T>
621 {
622  if (m_ptr != 0)
623  {
624  m_ptr->Unref ();
625  }
626 }
627 
628 template <typename T>
629 Ptr<T> &
631 {
632  if (&o == this)
633  {
634  return *this;
635  }
636  if (m_ptr != 0)
637  {
638  m_ptr->Unref ();
639  }
640  m_ptr = o.m_ptr;
641  Acquire ();
642  return *this;
643 }
644 
645 template <typename T>
646 T *
648 {
649  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
650  return m_ptr;
651 }
652 
653 template <typename T>
654 T *
656 {
657  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
658  return m_ptr;
659 }
660 
661 template <typename T>
662 T &
664 {
665  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
666  return *m_ptr;
667 }
668 
669 template <typename T>
670 T &
672 {
673  NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
674  return *m_ptr;
675 }
676 
677 template <typename T>
678 bool
680 {
681  return m_ptr == 0;
682 }
683 
684 template <typename T>
686 {
687  if (m_ptr == 0)
688  {
689  return 0;
690  }
691  static Tester test;
692  return &test;
693 }
694 
695 
696 } // namespace ns3
697 
698 
699 /****************************************************
700  * Global Functions (outside namespace ns3)
701  ***************************************************/
702 
716 template<class T>
717 struct
718 std::hash<ns3::Ptr<T>>
719 {
725  std::size_t
726  operator () (ns3::Ptr<T> p) const
727  {
728  return std::hash<const T *> () (ns3::PeekPointer (p));
729  }
730 };
731 
732 
733 #endif /* PTR_H */
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
Helper to test for null pointer.
Definition: ptr.h:82
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
Ptr(T *ptr)
Create a smart pointer which points to the object pointed to by the input raw pointer ptr.
Definition: ptr.h:589
friend U * GetPointer(const Ptr< U > &p)
Get a permanent pointer to the underlying object.
Definition: ptr.h:421
Ptr()
Create an empty smart pointer.
Definition: ptr.h:584
bool operator!()
Test for NULL pointer.
Definition: ptr.h:679
friend U * PeekPointer(const Ptr< U > &p)
Get a temporary pointer to the underlying object.
Definition: ptr.h:415
T & operator*() const
A const dereference.
Definition: ptr.h:663
Ptr(Ptr const &o)
Copy by referencing the same underlying object.
Definition: ptr.h:606
Ptr(T *ptr, bool ref)
Create a smart pointer which points to the object pointed to by the input raw pointer ptr.
Definition: ptr.h:596
void Acquire(void) const
Mark this as a a reference by incrementing the reference count.
Definition: ptr.h:575
~Ptr()
Destructor.
Definition: ptr.h:620
Ptr< T > & operator=(Ptr const &o)
Assignment operator by referencing the same underlying object.
Definition: ptr.h:630
T * m_ptr
The pointer.
Definition: ptr.h:78
Ptr(Ptr< U > const &o)
Copy, removing const qualifier.
Definition: ptr.h:613
T * operator->() const
An rvalue member access.
Definition: ptr.h:655
#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
bool operator>=(const int64x64_t &lhs, const int64x64_t &rhs)
Greater or equal operator.
Definition: int64x64.h:166
bool operator<=(const int64x64_t &lhs, const int64x64_t &rhs)
Less or equal operator.
Definition: int64x64.h:155
bool operator>(const Length &left, const Length &right)
Check if left has a value greater than right.
Definition: length.cc:413
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition: ptr.h:409
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:533
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:158
Ptr< T1 > StaticCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:540
Ptr< T1 > ConstCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:526
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:176
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:1612
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:421
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
Ptr< T1 > const_pointer_cast(Ptr< T2 > const &p)
Return a copy of p with its stored pointer const casted from T2 to T1.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:415
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:555
static T & GetReference(Ptr< T > const p)
Definition: ptr.h:364
Trait class to convert a pointer into a reference, used by MemPtrCallBackImpl.
Definition: callback.h:80
static T & GetReference(Ptr< T > p)
Definition: ptr.h:391
Helper for the MakeEvent functions which take a class method.
Definition: make-event.h:344