A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
48
PtrExample::PtrExample
()
49
{
50
std::cout <<
"PtrExample constructor"
<< std::endl;
51
}
52
53
PtrExample::~PtrExample
()
54
{
55
std::cout <<
"PtrExample destructor"
<< std::endl;
56
}
57
58
void
59
PtrExample::Method
()
60
{
61
std::cout <<
"PtrExample method"
<< std::endl;
62
}
63
67
static
Ptr<PtrExample>
g_ptr
=
nullptr
;
68
77
static
Ptr<PtrExample>
78
StorePtr
(
Ptr<PtrExample>
p)
79
{
80
Ptr<PtrExample>
prev
=
g_ptr
;
81
g_ptr
= p;
82
return
prev
;
83
}
84
88
static
void
89
ClearPtr
()
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();
105
Ptr<PtrExample>
prev
=
StorePtr
(p);
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>();
113
Ptr<PtrExample>
prev
=
StorePtr
(p);
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
}
PtrExample
Example class illustrating use of Ptr.
Definition:
main-ptr.cc:38
PtrExample::~PtrExample
~PtrExample() override
Destructor.
Definition:
main-ptr.cc:53
PtrExample::PtrExample
PtrExample()
Constructor.
Definition:
main-ptr.cc:48
PtrExample::Method
void Method()
Example class method.
Definition:
main-ptr.cc:59
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:232
ns3::Object
A base class which provides memory management and object aggregation.
Definition:
object.h:89
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition:
ptr.h:77
ns3::SimpleRefCount::Unref
void Unref() const
Decrement the reference count.
Definition:
simple-ref-count.h:126
NS_ASSERT
#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
StorePtr
static Ptr< PtrExample > StorePtr(Ptr< PtrExample > p)
Example Ptr manipulations.
Definition:
main-ptr.cc:78
g_ptr
static Ptr< PtrExample > g_ptr
Example Ptr global variable.
Definition:
main-ptr.cc:67
ClearPtr
static void ClearPtr()
Set g_ptr to NULL.
Definition:
main-ptr.cc:89
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::GetPointer
U * GetPointer(const Ptr< U > &p)
Definition:
ptr.h:456
second.cmd
cmd
Definition:
second.py:40
prev
uint32_t prev
Definition:
tcp-bbr-example.cc:72
src
core
examples
main-ptr.cc
Generated on Sun Mar 3 2024 17:10:55 for ns-3 by
1.9.1