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
34namespace ns3 {
35
72template <typename T>
73class Ptr
74{
75private:
76
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
121public:
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);
171 T *operator -> () const;
181 T &operator * () const;
208 operator Tester * () const;
209};
210
227template <typename T,
228 typename... Ts>
229Ptr<T> Create (Ts&&... args);
230
241template <typename T>
242std::ostream &operator << (std::ostream &os, const Ptr<T> &p);
243
265template <typename T1, typename T2>
266bool operator == (Ptr<T1> const &lhs, T2 const *rhs);
267
268template <typename T1, typename T2>
269bool operator == (T1 const *lhs, Ptr<T2> &rhs);
270
271template <typename T1, typename T2>
272bool operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
296template <typename T1, typename T2>
297bool operator != (Ptr<T1> const &lhs, T2 const *rhs);
298
299template <typename T1, typename T2>
300bool operator != (T1 const *lhs, Ptr<T2> &rhs);
301
302template <typename T1, typename T2>
303bool operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
317template <typename T>
318bool operator < (const Ptr<T> &lhs, const Ptr<T> &rhs);
319template <typename T>
320bool operator< (const Ptr<T> &lhs, const Ptr<const T> &rhs);
321template <typename T>
322bool operator< (const Ptr<const T> &lhs, const Ptr<T> &rhs);
323template <typename T>
324bool operator<= (const Ptr<T> &lhs, const Ptr<T> &rhs);
325template <typename T>
326bool operator > (const Ptr<T> &lhs, const Ptr<T> &rhs);
327template <typename T>
328bool operator >= (const Ptr<T> &lhs, const Ptr<T> &rhs);
340template <typename T1, typename T2>
342
343// Duplicate of struct CallbackTraits<T> as defined in callback.h
344template <typename T>
345struct CallbackTraits;
346
357template <typename 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
373template <typename T>
374struct EventMemberImplObjTraits;
375
384template <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
402namespace ns3 {
403
404/*************************************************
405 * friend non-member function implementations
406 ************************************************/
407
408template <typename T, typename... Ts>
409Ptr<T> Create (Ts&&... args)
410{
411 return Ptr<T> (new T (std::forward<Ts> (args)...), false);
412}
413
414template <typename U>
415U * PeekPointer (const Ptr<U> &p)
416{
417 return p.m_ptr;
418}
419
420template <typename U>
421U * GetPointer (const Ptr<U> &p)
422{
423 p.Acquire ();
424 return p.m_ptr;
425}
426
427template <typename T>
428std::ostream &operator << (std::ostream &os, const Ptr<T> &p)
429{
430 os << PeekPointer (p);
431 return os;
432}
433
434template <typename T1, typename T2>
435bool
436operator == (Ptr<T1> const &lhs, T2 const *rhs)
437{
438 return PeekPointer (lhs) == rhs;
439}
440
441template <typename T1, typename T2>
442bool
443operator == (T1 const *lhs, Ptr<T2> &rhs)
444{
445 return lhs == PeekPointer (rhs);
446}
447
448template <typename T1, typename T2>
449bool
450operator != (Ptr<T1> const &lhs, T2 const *rhs)
451{
452 return PeekPointer (lhs) != rhs;
453}
454
455template <typename T1, typename T2>
456bool
457operator != (T1 const *lhs, Ptr<T2> &rhs)
458{
459 return lhs != PeekPointer (rhs);
460}
461
462template <typename T1, typename T2>
463bool
464operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
465{
466 return PeekPointer (lhs) == PeekPointer (rhs);
467}
468
469template <typename T1, typename T2>
470bool
471operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
472{
473 return PeekPointer (lhs) != PeekPointer (rhs);
474}
475
476template <typename T>
477bool operator < (const Ptr<T> &lhs, const Ptr<T> &rhs)
478{
479 return PeekPointer<T> (lhs) < PeekPointer<T> (rhs);
480}
481
482template <typename T>
483bool
484operator< (const Ptr<T> &lhs, const Ptr<const T> &rhs)
485{
486 return PeekPointer<T> (lhs) < PeekPointer<const T> (rhs);
487}
488
489template <typename T>
490bool
491operator< (const Ptr<const T> &lhs, const Ptr<T> &rhs)
492{
493 return PeekPointer<const T> (lhs) < PeekPointer<T> (rhs);
494}
495
496template <typename T>
497bool
498operator<= (const Ptr<T> &lhs, const Ptr<T> &rhs)
499{
500 return PeekPointer<T> (lhs) <= PeekPointer<T> (rhs);
501}
502
503template <typename T>
504bool operator > (const Ptr<T> &lhs, const Ptr<T> &rhs)
505{
506 return PeekPointer<T> (lhs) > PeekPointer<T> (rhs);
507}
508
509template <typename T>
510bool operator >= (const Ptr<T> &lhs, const Ptr<T> &rhs)
511{
512 return PeekPointer<T> (lhs) >= PeekPointer<T> (rhs);
513}
514
524template <typename T1, typename T2>
525Ptr<T1>
527{
528 return Ptr<T1> (const_cast<T1 *> (PeekPointer (p)));
529}
530
531template <typename T1, typename T2>
532Ptr<T1>
534{
535 return Ptr<T1> (dynamic_cast<T1 *> (PeekPointer (p)));
536}
537
538template <typename T1, typename T2>
539Ptr<T1>
541{
542 return Ptr<T1> (static_cast<T1 *> (PeekPointer (p)));
543}
554template <typename T>
556{
557 Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
558 return p;
559}
560
561template <typename T>
562Ptr<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
573template <typename T>
574void
575Ptr<T>::Acquire (void) const
576{
577 if (m_ptr != 0)
578 {
579 m_ptr->Ref ();
580 }
581}
582
583template <typename T>
585 : m_ptr (0)
586{}
587
588template <typename T>
590 : m_ptr (ptr)
591{
592 Acquire ();
593}
594
595template <typename T>
596Ptr<T>::Ptr (T *ptr, bool ref)
597 : m_ptr (ptr)
598{
599 if (ref)
600 {
601 Acquire ();
602 }
603}
604
605template <typename T>
607 : m_ptr (PeekPointer (o))
608{
609 Acquire ();
610}
611template <typename T>
612template <typename U>
614 : m_ptr (PeekPointer (o))
615{
616 Acquire ();
617}
618
619template <typename T>
621{
622 if (m_ptr != 0)
623 {
624 m_ptr->Unref ();
625 }
626}
627
628template <typename T>
629Ptr<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
645template <typename T>
646T *
648{
649 NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
650 return m_ptr;
651}
652
653template <typename T>
654T *
656{
657 NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
658 return m_ptr;
659}
660
661template <typename T>
662T &
664{
665 NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
666 return *m_ptr;
667}
668
669template <typename T>
670T &
672{
673 NS_ASSERT_MSG (m_ptr, "Attempted to dereference zero pointer");
674 return *m_ptr;
675}
676
677template <typename T>
678bool
680{
681 return m_ptr == 0;
682}
683
684template <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
716template<class T>
717struct
718std::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
Ptr()
Create an empty smart pointer.
Definition: ptr.h:584
friend U * PeekPointer(const Ptr< U > &p)
Get a temporary pointer to the underlying object.
Definition: ptr.h:415
bool operator!()
Test for NULL pointer.
Definition: ptr.h:679
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
friend U * GetPointer(const Ptr< U > &p)
Get a permanent pointer to the underlying object.
Definition: ptr.h:421
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:422
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.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:415
Ptr< T1 > StaticCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:540
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:158
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.
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:176
Ptr< T1 > ConstCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:526
Ptr< T1 > DynamicCast(Ptr< T2 > const &p)
Cast a Ptr.
Definition: ptr.h:533
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:555
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
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