A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
src
core
examples
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 "ns3/command-line.h"
23
#include <iostream>
24
32
using namespace
ns3
;
33
37
class
PtrExample
:
public
Object
38
{
39
public
:
41
PtrExample
();
43
~
PtrExample
();
45
void
Method (
void
);
46
};
47
PtrExample::PtrExample
()
48
{
49
std::cout <<
"PtrExample constructor"
<< std::endl;
50
}
51
PtrExample::~PtrExample
()
52
{
53
std::cout <<
"PtrExample destructor"
<< std::endl;
54
}
55
void
56
PtrExample::Method
(
void
)
57
{
58
std::cout <<
"PtrExample method"
<< std::endl;
59
}
60
61
65
static
Ptr<PtrExample>
g_ptr
= 0;
66
75
static
Ptr<PtrExample>
76
StorePtr
(
Ptr<PtrExample>
p)
77
{
78
Ptr<PtrExample>
prev =
g_ptr
;
79
g_ptr
= p;
80
return
prev;
81
}
82
86
static
void
87
ClearPtr
(
void
)
88
{
89
g_ptr
= 0;
90
}
91
92
93
94
int
main (
int
argc,
char
*argv[])
95
{
96
CommandLine
cmd
;
97
cmd
.Parse(argc, argv);
98
99
{
100
// Create a new object of type PtrExample, store it in global
101
// variable g_ptr
102
Ptr<PtrExample>
p = CreateObject<PtrExample> ();
103
p->
Method
();
104
Ptr<PtrExample>
prev =
StorePtr
(p);
105
NS_ASSERT
(prev == 0);
106
}
107
108
{
109
// Create a new object of type PtrExample, store it in global
110
// variable g_ptr, get a hold on the previous PtrExample object.
111
Ptr<PtrExample>
p = CreateObject<PtrExample> ();
112
Ptr<PtrExample>
prev =
StorePtr
(p);
113
// call method on object
114
prev->Method ();
115
// Clear the currently-stored object
116
ClearPtr
();
117
// get the raw pointer and release it.
118
PtrExample
*raw =
GetPointer
(prev);
119
prev = 0;
120
raw->
Method
();
121
raw->
Unref
();
122
}
123
124
125
return
0;
126
}
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition:
ptr.h:73
ns3::SimpleRefCount::Unref
void Unref(void) const
Decrement the reference count.
Definition:
simple-ref-count.h:116
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:67
second.cmd
cmd
Definition:
second.py:35
ns3::GetPointer
U * GetPointer(const Ptr< U > &p)
Definition:
ptr.h:570
PtrExample::Method
void Method(void)
Example class method.
Definition:
main-ptr.cc:56
PtrExample
Example class illustrating use of Ptr.
Definition:
main-ptr.cc:37
ClearPtr
static void ClearPtr(void)
Set g_ptr to NULL.
Definition:
main-ptr.cc:87
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:213
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
PtrExample::~PtrExample
~PtrExample()
Destructor.
Definition:
main-ptr.cc:51
StorePtr
static Ptr< PtrExample > StorePtr(Ptr< PtrExample > p)
Example Ptr manipulations.
Definition:
main-ptr.cc:76
ns3::Object
A base class which provides memory management and object aggregation.
Definition:
object.h:87
PtrExample::PtrExample
PtrExample()
Constructor.
Definition:
main-ptr.cc:47
g_ptr
static Ptr< PtrExample > g_ptr
Example Ptr global variable.
Definition:
main-ptr.cc:65
Generated on Wed Nov 7 2018 10:01:49 for ns-3 by
1.8.14