A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ptr.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19
20#ifndef PTR_H
21#define PTR_H
22
23#include "assert.h"
24#include "deprecated.h"
25
26#include <iostream>
27#include <stdint.h>
28
35namespace ns3
36{
37
76template <typename T>
77class Ptr
78{
79 private:
82
90 // Don't deprecate the class because the warning fires
91 // every time ptr.h is merely included, masking the real uses of Tester
92 // Leave the macro here so we can find this later to actually remove it.
93 class /* NS_DEPRECATED_3_37 ("see operator bool") */ Tester
94 {
95 public:
96 // Delete operator delete to avoid misuse
97 void operator delete(void*) = delete;
98 };
99
101 friend class Ptr<const T>;
102
114 template <typename U>
115 friend U* GetPointer(const Ptr<U>& p);
127 template <typename U>
128 friend U* PeekPointer(const Ptr<U>& p);
129
131 inline void Acquire() const;
132
133 public:
146 Ptr(T* ptr);
155 Ptr(T* ptr, bool ref);
161 Ptr(const Ptr& o);
168 template <typename U>
169 Ptr(const Ptr<U>& o);
178 Ptr<T>& operator=(const Ptr& o);
183 T* operator->() const;
193 T& operator*() const;
199
215 NS_DEPRECATED_3_37("see operator bool")
216 operator Tester*() const;
263 explicit operator bool() const;
264};
265
282template <typename T, typename... Ts>
283Ptr<T> Create(Ts&&... args);
284
295template <typename T>
296std::ostream& operator<<(std::ostream& os, const Ptr<T>& p);
297
319template <typename T1, typename T2>
320bool operator==(const Ptr<T1>& lhs, T2 const* rhs);
321
322template <typename T1, typename T2>
323bool operator==(T1 const* lhs, Ptr<T2>& rhs);
324
325template <typename T1, typename T2>
326bool operator==(const Ptr<T1>& lhs, const Ptr<T2>& rhs);
334template <typename T1, typename T2>
335std::enable_if_t<std::is_same_v<T2, std::nullptr_t>, bool> operator==(const Ptr<T1>& lhs, T2 rhs);
336
358template <typename T1, typename T2>
359bool operator!=(const Ptr<T1>& lhs, T2 const* rhs);
360
361template <typename T1, typename T2>
362bool operator!=(T1 const* lhs, Ptr<T2>& rhs);
363
364template <typename T1, typename T2>
365bool operator!=(const Ptr<T1>& lhs, const Ptr<T2>& rhs);
373template <typename T1, typename T2>
374std::enable_if_t<std::is_same_v<T2, std::nullptr_t>, bool> operator!=(const Ptr<T1>& lhs, T2 rhs);
375
386template <typename T>
387bool operator<(const Ptr<T>& lhs, const Ptr<T>& rhs);
388template <typename T>
389bool operator<(const Ptr<T>& lhs, const Ptr<const T>& rhs);
390template <typename T>
391bool operator<(const Ptr<const T>& lhs, const Ptr<T>& rhs);
392template <typename T>
393bool operator<=(const Ptr<T>& lhs, const Ptr<T>& rhs);
394template <typename T>
395bool operator>(const Ptr<T>& lhs, const Ptr<T>& rhs);
396template <typename T>
397bool operator>=(const Ptr<T>& lhs, const Ptr<T>& rhs);
409template <typename T1, typename T2>
410Ptr<T1> const_pointer_cast(const Ptr<T2>& p);
411
412// Duplicate of struct CallbackTraits<T> as defined in callback.h
413template <typename T>
415
426template <typename T>
428{
433 static T& GetReference(const Ptr<T> p)
434 {
435 return *PeekPointer(p);
436 }
437};
438
439// Duplicate of struct EventMemberImplObjTraits<T> as defined in make-event.h
440// We repeat it here to declare a specialization on Ptr<T>
441// without making this header dependent on make-event.h
442template <typename T>
443struct EventMemberImplObjTraits;
444
453template <typename T>
455{
460 static T& GetReference(Ptr<T> p)
461 {
462 return *PeekPointer(p);
463 }
464};
465
466} // namespace ns3
467
468namespace ns3
469{
470
471/*************************************************
472 * friend non-member function implementations
473 ************************************************/
474
475template <typename T, typename... Ts>
476Ptr<T>
477Create(Ts&&... args)
478{
479 return Ptr<T>(new T(std::forward<Ts>(args)...), false);
480}
481
482template <typename U>
483U*
485{
486 return p.m_ptr;
487}
488
489template <typename U>
490U*
492{
493 p.Acquire();
494 return p.m_ptr;
495}
496
497template <typename T>
498std::ostream&
499operator<<(std::ostream& os, const Ptr<T>& p)
500{
501 os << PeekPointer(p);
502 return os;
503}
504
505template <typename T1, typename T2>
506bool
507operator==(const Ptr<T1>& lhs, T2 const* rhs)
508{
509 return PeekPointer(lhs) == rhs;
510}
511
512template <typename T1, typename T2>
513bool
514operator==(T1 const* lhs, Ptr<T2>& rhs)
515{
516 return lhs == PeekPointer(rhs);
517}
518
519template <typename T1, typename T2>
520bool
521operator!=(const Ptr<T1>& lhs, T2 const* rhs)
522{
523 return PeekPointer(lhs) != rhs;
524}
525
526template <typename T1, typename T2>
527bool
528operator!=(T1 const* lhs, Ptr<T2>& rhs)
529{
530 return lhs != PeekPointer(rhs);
531}
532
533template <typename T1, typename T2>
534bool
535operator==(const Ptr<T1>& lhs, const Ptr<T2>& rhs)
536{
537 return PeekPointer(lhs) == PeekPointer(rhs);
538}
539
540template <typename T1, typename T2>
541bool
542operator!=(const Ptr<T1>& lhs, const Ptr<T2>& rhs)
543{
544 return PeekPointer(lhs) != PeekPointer(rhs);
545}
546
547template <typename T1, typename T2>
548std::enable_if_t<std::is_same_v<T2, std::nullptr_t>, bool>
549operator==(const Ptr<T1>& lhs, T2 rhs)
550{
551 return PeekPointer(lhs) == nullptr;
552}
553
554template <typename T1, typename T2>
555std::enable_if_t<std::is_same_v<T2, std::nullptr_t>, bool>
556operator!=(const Ptr<T1>& lhs, T2 rhs)
557{
558 return PeekPointer(lhs) != nullptr;
559}
560
561template <typename T>
562bool
563operator<(const Ptr<T>& lhs, const Ptr<T>& rhs)
564{
565 return PeekPointer<T>(lhs) < PeekPointer<T>(rhs);
566}
567
568template <typename T>
569bool
570operator<(const Ptr<T>& lhs, const Ptr<const T>& rhs)
571{
572 return PeekPointer<T>(lhs) < PeekPointer<const T>(rhs);
573}
574
575template <typename T>
576bool
577operator<(const Ptr<const T>& lhs, const Ptr<T>& rhs)
578{
579 return PeekPointer<const T>(lhs) < PeekPointer<T>(rhs);
580}
581
582template <typename T>
583bool
584operator<=(const Ptr<T>& lhs, const Ptr<T>& rhs)
585{
586 return PeekPointer<T>(lhs) <= PeekPointer<T>(rhs);
587}
588
589template <typename T>
590bool
591operator>(const Ptr<T>& lhs, const Ptr<T>& rhs)
592{
593 return PeekPointer<T>(lhs) > PeekPointer<T>(rhs);
594}
595
596template <typename T>
597bool
598operator>=(const Ptr<T>& lhs, const Ptr<T>& rhs)
599{
600 return PeekPointer<T>(lhs) >= PeekPointer<T>(rhs);
601}
602
612template <typename T1, typename T2>
613Ptr<T1>
615{
616 return Ptr<T1>(const_cast<T1*>(PeekPointer(p)));
617}
618
619template <typename T1, typename T2>
620Ptr<T1>
622{
623 return Ptr<T1>(dynamic_cast<T1*>(PeekPointer(p)));
624}
625
626template <typename T1, typename T2>
627Ptr<T1>
629{
630 return Ptr<T1>(static_cast<T1*>(PeekPointer(p)));
631}
632
643template <typename T>
644Ptr<T>
646{
647 Ptr<T> p = Ptr<T>(new T(*PeekPointer(object)), false);
648 return p;
649}
650
651template <typename T>
652Ptr<T>
653Copy(Ptr<const T> object)
654{
655 Ptr<T> p = Ptr<T>(new T(*PeekPointer(object)), false);
656 return p;
657}
658
661/****************************************************
662 * Member method implementations.
663 ***************************************************/
664
665template <typename T>
666void
668{
669 if (m_ptr != nullptr)
670 {
671 m_ptr->Ref();
672 }
673}
674
675template <typename T>
677 : m_ptr(nullptr)
678{
679}
680
681template <typename T>
683 : m_ptr(ptr)
684{
685 Acquire();
686}
687
688template <typename T>
689Ptr<T>::Ptr(T* ptr, bool ref)
690 : m_ptr(ptr)
691{
692 if (ref)
693 {
694 Acquire();
695 }
696}
697
698template <typename T>
700 : m_ptr(nullptr)
701{
702 T* ptr = PeekPointer(o);
703 if (ptr != nullptr)
704 {
705 m_ptr = ptr;
706 Acquire();
707 }
708}
709
710template <typename T>
711template <typename U>
713 : m_ptr(PeekPointer(o))
714{
715 Acquire();
716}
717
718template <typename T>
720{
721 if (m_ptr != nullptr)
722 {
723 m_ptr->Unref();
724 }
725}
726
727template <typename T>
728Ptr<T>&
730{
731 if (&o == this)
732 {
733 return *this;
734 }
735 if (m_ptr != nullptr)
736 {
737 m_ptr->Unref();
738 }
739 m_ptr = o.m_ptr;
740 Acquire();
741 return *this;
742}
743
744template <typename T>
745T*
747{
748 NS_ASSERT_MSG(m_ptr, "Attempted to dereference zero pointer");
749 return m_ptr;
750}
751
752template <typename T>
753T*
755{
756 NS_ASSERT_MSG(m_ptr, "Attempted to dereference zero pointer");
757 return m_ptr;
758}
759
760template <typename T>
761T&
763{
764 NS_ASSERT_MSG(m_ptr, "Attempted to dereference zero pointer");
765 return *m_ptr;
766}
767
768template <typename T>
769T&
771{
772 NS_ASSERT_MSG(m_ptr, "Attempted to dereference zero pointer");
773 return *m_ptr;
774}
775
776template <typename T>
777Ptr<T>::operator Tester*() const // NS_DEPRECATED_3_37
778{
779 if (m_ptr == 0)
780 {
781 return 0;
782 }
783 static Tester test;
784 return &test;
785}
786
787template <typename T>
789{
790 return m_ptr != nullptr;
791}
792
793} // namespace ns3
794
795/****************************************************
796 * Global Functions (outside namespace ns3)
797 ***************************************************/
798
812template <class T>
813struct std::hash<ns3::Ptr<T>>
814{
820 std::size_t operator()(ns3::Ptr<T> p) const
821 {
822 return std::hash<const T*>()(ns3::PeekPointer(p));
823 }
824};
825
826#endif /* PTR_H */
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
Helper to test for null pointer.
Definition: ptr.h:94
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
Ptr(T *ptr)
Create a smart pointer which points to the object pointed to by the input raw pointer ptr.
Definition: ptr.h:682
Ptr(const Ptr &o)
Copy by referencing the same underlying object.
Definition: ptr.h:699
T & operator*()
A dereference.
Definition: ptr.h:770
Ptr()
Create an empty smart pointer.
Definition: ptr.h:676
void Acquire() const
Mark this as a a reference by incrementing the reference count.
Definition: ptr.h:667
friend U * PeekPointer(const Ptr< U > &p)
Get a temporary pointer to the underlying object.
Definition: ptr.h:484
Ptr(const Ptr< U > &o)
Copy, removing const qualifier.
Definition: ptr.h:712
T * operator->()
An lvalue member access.
Definition: ptr.h:746
T & operator*() const
A const dereference.
Definition: ptr.h:762
friend U * GetPointer(const Ptr< U > &p)
Get a permanent pointer to the underlying object.
Definition: ptr.h:491
Ptr< T > & operator=(const Ptr &o)
Assignment operator by referencing the same underlying object.
Definition: ptr.h:729
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:689
~Ptr()
Destructor.
Definition: ptr.h:719
T * m_ptr
The pointer.
Definition: ptr.h:81
T * operator->() const
An rvalue member access.
Definition: ptr.h:754
NS_DEPRECATED macro definition.
#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:86
#define NS_DEPRECATED_3_37(msg)
Tag for things deprecated in version ns-3.37.
Definition: deprecated.h:103
bool operator>=(const int64x64_t &lhs, const int64x64_t &rhs)
Greater or equal operator.
Definition: int64x64.h:174
bool operator<=(const int64x64_t &lhs, const int64x64_t &rhs)
Less or equal operator.
Definition: int64x64.h:161
bool operator>(const Length &left, const Length &right)
Check if left has a value greater than right.
Definition: length.cc:421
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition: ptr.h:477
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:484
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition: callback.h:678
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
Ptr< T1 > const_pointer_cast(const Ptr< T2 > &p)
Return a copy of p with its stored pointer const casted from T2 to T1.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition: ptr.h:621
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:645
Ptr< T1 > StaticCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition: ptr.h:628
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:491
Ptr< T1 > ConstCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition: ptr.h:614
STL namespace.
-ns3 Test suite for the ns3 wrapper script
static T & GetReference(const Ptr< T > p)
Definition: ptr.h:433
static T & GetReference(Ptr< T > p)
Definition: ptr.h:460
Helper for the MakeEvent functions which take a class method.
Definition: make-event.h:370
std::size_t operator()(ns3::Ptr< T > p) const
The functor.
Definition: ptr.h:820