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