A Discrete-Event Network Simulator
API
main-ptr.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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  */
18 
19 #include "ns3/command-line.h"
20 #include "ns3/object.h"
21 #include "ns3/ptr.h"
22 
23 #include <iostream>
24 
32 using namespace ns3;
33 
37 class PtrExample : public Object
38 {
39  public:
41  PtrExample();
43  ~PtrExample() override;
45  void Method();
46 };
47 
49 {
50  std::cout << "PtrExample constructor" << std::endl;
51 }
52 
54 {
55  std::cout << "PtrExample destructor" << std::endl;
56 }
57 
58 void
60 {
61  std::cout << "PtrExample method" << std::endl;
62 }
63 
67 static Ptr<PtrExample> g_ptr = nullptr;
68 
77 static Ptr<PtrExample>
79 {
81  g_ptr = p;
82  return prev;
83 }
84 
88 static void
90 {
91  g_ptr = nullptr;
92 }
93 
94 int
95 main(int argc, char* argv[])
96 {
97  CommandLine cmd(__FILE__);
98  cmd.Parse(argc, argv);
99 
100  {
101  // Create a new object of type PtrExample, store it in global
102  // variable g_ptr
103  Ptr<PtrExample> p = CreateObject<PtrExample>();
104  p->Method();
106  NS_ASSERT(!prev);
107  }
108 
109  {
110  // Create a new object of type PtrExample, store it in global
111  // variable g_ptr, get a hold on the previous PtrExample object.
112  Ptr<PtrExample> p = CreateObject<PtrExample>();
114  // call method on object
115  prev->Method();
116  // Clear the currently-stored object
117  ClearPtr();
118  // get the raw pointer and release it.
119  PtrExample* raw = GetPointer(prev);
120  prev = nullptr;
121  raw->Method();
122  raw->Unref();
123  }
124 
125  return 0;
126 }
Example class illustrating use of Ptr.
Definition: main-ptr.cc:38
~PtrExample() override
Destructor.
Definition: main-ptr.cc:53
PtrExample()
Constructor.
Definition: main-ptr.cc:48
void Method()
Example class method.
Definition: main-ptr.cc:59
Parse command-line arguments.
Definition: command-line.h:232
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
void Unref() const
Decrement the reference count.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
static Ptr< PtrExample > StorePtr(Ptr< PtrExample > p)
Example Ptr manipulations.
Definition: main-ptr.cc:78
static Ptr< PtrExample > g_ptr
Example Ptr global variable.
Definition: main-ptr.cc:67
static void ClearPtr()
Set g_ptr to NULL.
Definition: main-ptr.cc:89
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:456
cmd
Definition: second.py:40
uint32_t prev