A Discrete-Event Network Simulator
API
main-ptr.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 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  */
19 
20 #include "ns3/ptr.h"
21 #include "ns3/object.h"
22 #include <iostream>
23 
31 using namespace ns3;
32 
36 class PtrExample : public Object
37 {
38 public:
40  PtrExample ();
42  ~PtrExample ();
44  void Method (void);
45 };
47 {
48  std::cout << "PtrExample constructor" << std::endl;
49 }
51 {
52  std::cout << "PtrExample destructor" << std::endl;
53 }
54 void
56 {
57  std::cout << "PtrExample method" << std::endl;
58 }
59 
60 
65 
74 static Ptr<PtrExample>
76 {
77  Ptr<PtrExample> prev = g_ptr;
78  g_ptr = p;
79  return prev;
80 }
81 
85 static void
86 ClearPtr (void)
87 {
88  g_ptr = 0;
89 }
90 
91 
92 
93 int main (int argc, char *argv[])
94 {
95  {
96  // Create a new object of type PtrExample, store it in global
97  // variable g_ptr
98  Ptr<PtrExample> p = CreateObject<PtrExample> ();
99  p->Method ();
100  Ptr<PtrExample> prev = StorePtr (p);
101  NS_ASSERT (prev == 0);
102  }
103 
104  {
105  // Create a new object of type PtrExample, store it in global
106  // variable g_ptr, get a hold on the previous PtrExample object.
107  Ptr<PtrExample> p = CreateObject<PtrExample> ();
108  Ptr<PtrExample> prev = StorePtr (p);
109  // call method on object
110  prev->Method ();
111  // Clear the currently-stored object
112  ClearPtr ();
113  // get the raw pointer and release it.
114  PtrExample *raw = GetPointer (prev);
115  prev = 0;
116  raw->Method ();
117  raw->Unref ();
118  }
119 
120 
121  return 0;
122 }
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:570
void Unref(void) const
Decrement the reference count.
void Method(void)
Example class method.
Definition: main-ptr.cc:55
Example class illustrating use of Ptr.
Definition: main-ptr.cc:36
static void ClearPtr(void)
Set g_ptr to NULL.
Definition: main-ptr.cc:86
Every class exported by the ns3 library is enclosed in the ns3 namespace.
~PtrExample()
Destructor.
Definition: main-ptr.cc:50
static Ptr< PtrExample > StorePtr(Ptr< PtrExample > p)
Example Ptr manipulations.
Definition: main-ptr.cc:75
A base class which provides memory management and object aggregation.
Definition: object.h:87
PtrExample()
Constructor.
Definition: main-ptr.cc:46
static Ptr< PtrExample > g_ptr
Example Ptr global variable.
Definition: main-ptr.cc:64