A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
main-ptr.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2006 INRIA
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
*/
7
8
#include "ns3/command-line.h"
9
#include "ns3/object.h"
10
#include "ns3/ptr.h"
11
12
#include <iostream>
13
14
/**
15
* @file
16
* @ingroup core-examples
17
* @ingroup ptr
18
* Example program illustrating use of the ns3::Ptr smart pointer.
19
*/
20
21
using namespace
ns3
;
22
23
/**
24
* Example class illustrating use of Ptr.
25
*/
26
class
PtrExample
:
public
Object
27
{
28
public
:
29
/**
30
* @brief Get the type ID.
31
* @return the object TypeId
32
*/
33
static
TypeId
GetTypeId
();
34
35
/** Constructor. */
36
PtrExample
();
37
/** Destructor. */
38
~PtrExample
()
override
;
39
/** Example class method. */
40
void
Method
();
41
};
42
43
NS_OBJECT_ENSURE_REGISTERED
(
PtrExample
);
44
45
TypeId
46
PtrExample::GetTypeId
()
47
{
48
static
TypeId
tid =
49
TypeId
(
"ns3::PtrExample"
).
SetParent
<
Object
>().SetGroupName(
"Core"
).HideFromDocumentation();
50
return
tid;
51
}
52
53
PtrExample::PtrExample
()
54
{
55
std::cout <<
"PtrExample constructor"
<< std::endl;
56
}
57
58
PtrExample::~PtrExample
()
59
{
60
std::cout <<
"PtrExample destructor"
<< std::endl;
61
}
62
63
void
64
PtrExample::Method
()
65
{
66
std::cout <<
"PtrExample method"
<< std::endl;
67
}
68
69
/**
70
* Example Ptr global variable.
71
*/
72
static
Ptr<PtrExample>
g_ptr
=
nullptr
;
73
74
/**
75
* Example Ptr manipulations.
76
*
77
* This function stores it's argument in the global variable \c g_ptr
78
* and returns the old value of \c g_ptr.
79
* @param [in] p A Ptr.
80
* @returns The prior value of \c g_ptr.
81
*/
82
static
Ptr<PtrExample>
83
StorePtr
(
Ptr<PtrExample>
p)
84
{
85
Ptr<PtrExample>
prev
=
g_ptr
;
86
g_ptr
= p;
87
return
prev
;
88
}
89
90
/**
91
* Set \c g_ptr to NULL.
92
*/
93
static
void
94
ClearPtr
()
95
{
96
g_ptr
=
nullptr
;
97
}
98
99
int
100
main(
int
argc,
char
* argv[])
101
{
102
CommandLine
cmd
(__FILE__);
103
cmd
.Parse(argc, argv);
104
105
{
106
// Create a new object of type PtrExample, store it in global
107
// variable g_ptr
108
Ptr<PtrExample>
p =
CreateObject<PtrExample>
();
109
p->Method();
110
Ptr<PtrExample>
prev
=
StorePtr
(p);
111
NS_ASSERT
(!
prev
);
112
}
113
114
{
115
// Create a new object of type PtrExample, store it in global
116
// variable g_ptr, get a hold on the previous PtrExample object.
117
Ptr<PtrExample>
p =
CreateObject<PtrExample>
();
118
Ptr<PtrExample>
prev
=
StorePtr
(p);
119
// call method on object
120
prev
->Method();
121
// Clear the currently-stored object
122
ClearPtr
();
123
// get the raw pointer and release it.
124
PtrExample
* raw =
GetPointer
(
prev
);
125
prev
=
nullptr
;
126
raw->
Method
();
127
raw->
Unref
();
128
}
129
130
return
0;
131
}
PtrExample
Example class illustrating use of Ptr.
Definition
main-ptr.cc:27
PtrExample::~PtrExample
~PtrExample() override
Destructor.
Definition
main-ptr.cc:58
PtrExample::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
main-ptr.cc:46
PtrExample::PtrExample
PtrExample()
Constructor.
Definition
main-ptr.cc:53
PtrExample::Method
void Method()
Example class method.
Definition
main-ptr.cc:64
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::Object::Object
Object()
Constructor.
Definition
object.cc:96
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:67
ns3::SimpleRefCount::Unref
void Unref() const
Decrement the reference count.
Definition
simple-ref-count.h:115
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:49
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1001
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:55
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition
object.h:619
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition
object-base.h:35
g_ptr
static Ptr< PtrExample > g_ptr
Example Ptr global variable.
Definition
main-ptr.cc:72
StorePtr
static Ptr< PtrExample > StorePtr(Ptr< PtrExample > p)
Example Ptr manipulations.
Definition
main-ptr.cc:83
ClearPtr
static void ClearPtr()
Set g_ptr to NULL.
Definition
main-ptr.cc:94
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:455
second.cmd
cmd
Definition
second.py:29
prev
uint32_t prev
Definition
tcp-bbr-example.cc:61
src
core
examples
main-ptr.cc
Generated on Sat Sep 6 2025 11:06:54 for ns-3 by
1.13.2