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
/** Constructor. */
30
PtrExample
();
31
/** Destructor. */
32
~PtrExample
()
override
;
33
/** Example class method. */
34
void
Method
();
35
};
36
37
PtrExample::PtrExample
()
38
{
39
std::cout <<
"PtrExample constructor"
<< std::endl;
40
}
41
42
PtrExample::~PtrExample
()
43
{
44
std::cout <<
"PtrExample destructor"
<< std::endl;
45
}
46
47
void
48
PtrExample::Method
()
49
{
50
std::cout <<
"PtrExample method"
<< std::endl;
51
}
52
53
/**
54
* Example Ptr global variable.
55
*/
56
static
Ptr<PtrExample>
g_ptr
=
nullptr
;
57
58
/**
59
* Example Ptr manipulations.
60
*
61
* This function stores it's argument in the global variable \c g_ptr
62
* and returns the old value of \c g_ptr.
63
* \param [in] p A Ptr.
64
* \returns The prior value of \c g_ptr.
65
*/
66
static
Ptr<PtrExample>
67
StorePtr
(
Ptr<PtrExample>
p)
68
{
69
Ptr<PtrExample>
prev
=
g_ptr
;
70
g_ptr
= p;
71
return
prev
;
72
}
73
74
/**
75
* Set \c g_ptr to NULL.
76
*/
77
static
void
78
ClearPtr
()
79
{
80
g_ptr
=
nullptr
;
81
}
82
83
int
84
main(
int
argc,
char
* argv[])
85
{
86
CommandLine
cmd
(__FILE__);
87
cmd
.Parse(argc, argv);
88
89
{
90
// Create a new object of type PtrExample, store it in global
91
// variable g_ptr
92
Ptr<PtrExample>
p =
CreateObject<PtrExample>
();
93
p->Method();
94
Ptr<PtrExample>
prev
=
StorePtr
(p);
95
NS_ASSERT
(!
prev
);
96
}
97
98
{
99
// Create a new object of type PtrExample, store it in global
100
// variable g_ptr, get a hold on the previous PtrExample object.
101
Ptr<PtrExample>
p =
CreateObject<PtrExample>
();
102
Ptr<PtrExample>
prev
=
StorePtr
(p);
103
// call method on object
104
prev
->Method();
105
// Clear the currently-stored object
106
ClearPtr
();
107
// get the raw pointer and release it.
108
PtrExample
* raw =
GetPointer
(
prev
);
109
prev
=
nullptr
;
110
raw->
Method
();
111
raw->
Unref
();
112
}
113
114
return
0;
115
}
PtrExample
Example class illustrating use of Ptr.
Definition
main-ptr.cc:27
PtrExample::~PtrExample
~PtrExample() override
Destructor.
Definition
main-ptr.cc:42
PtrExample::PtrExample
PtrExample()
Constructor.
Definition
main-ptr.cc:37
PtrExample::Method
void Method()
Example class method.
Definition
main-ptr.cc:48
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::Object
A base class which provides memory management and object aggregation.
Definition
object.h:78
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
mpi-test-fixtures.h:37
ns3::SimpleRefCount::Unref
void Unref() const
Decrement the reference count.
Definition
simple-ref-count.h:115
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
g_ptr
static Ptr< PtrExample > g_ptr
Example Ptr global variable.
Definition
main-ptr.cc:56
StorePtr
static Ptr< PtrExample > StorePtr(Ptr< PtrExample > p)
Example Ptr manipulations.
Definition
main-ptr.cc:67
ClearPtr
static void ClearPtr()
Set g_ptr to NULL.
Definition
main-ptr.cc:78
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:450
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 Fri Nov 8 2024 13:58:59 for ns-3 by
1.11.0