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
30
using namespace
ns3
;
31
35
class
PtrExample
:
public
Object
36
{
37
public
:
39
PtrExample
();
41
~
PtrExample
();
43
void
Method (
void
);
44
};
45
PtrExample::PtrExample
()
46
{
47
std::cout <<
"PtrExample constructor"
<< std::endl;
48
}
49
PtrExample::~PtrExample
()
50
{
51
std::cout <<
"PtrExample destructor"
<< std::endl;
52
}
53
void
54
PtrExample::Method
(
void
)
55
{
56
std::cout <<
"PtrExample method"
<< std::endl;
57
}
58
59
63
static
Ptr<PtrExample>
g_ptr
= 0;
64
73
static
Ptr<PtrExample>
74
StorePtr
(
Ptr<PtrExample>
p)
75
{
76
Ptr<PtrExample>
prev =
g_ptr
;
77
g_ptr = p;
78
return
prev;
79
}
80
84
static
void
85
ClearPtr
(
void
)
86
{
87
g_ptr = 0;
88
}
89
90
91
92
int
main (
int
argc,
char
*argv[])
93
{
94
{
95
// Create a new object of type PtrExample, store it in global
96
// variable g_ptr
97
Ptr<PtrExample>
p = CreateObject<PtrExample> ();
98
p->
Method
();
99
Ptr<PtrExample>
prev =
StorePtr
(p);
100
NS_ASSERT
(prev == 0);
101
}
102
103
{
104
// Create a new object of type PtrExample, store it in global
105
// variable g_ptr, get a hold on the previous PtrExample object.
106
Ptr<PtrExample>
p = CreateObject<PtrExample> ();
107
Ptr<PtrExample>
prev =
StorePtr
(p);
108
// call method on object
109
prev->Method ();
110
// Clear the currently-stored object
111
ClearPtr
();
112
// get the raw pointer and release it.
113
PtrExample
*raw =
GetPointer
(prev);
114
prev = 0;
115
raw->
Method
();
116
raw->
Unref
();
117
}
118
119
120
return
0;
121
}
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:568
ns3::SimpleRefCount::Unref
void Unref(void) const
Decrement the reference count.
Definition:
simple-ref-count.h:111
PtrExample::Method
void Method(void)
Example class method.
Definition:
main-ptr.cc:54
PtrExample
Example class illustrating use of Ptr.
Definition:
main-ptr.cc:35
ClearPtr
static void ClearPtr(void)
Set g_ptr to NULL.
Definition:
main-ptr.cc:85
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
PtrExample::~PtrExample
~PtrExample()
Destructor.
Definition:
main-ptr.cc:49
StorePtr
static Ptr< PtrExample > StorePtr(Ptr< PtrExample > p)
Example Ptr manipulations.
Definition:
main-ptr.cc:74
ns3::Object
A base class which provides memory management and object aggregation.
Definition:
object.h:87
PtrExample::PtrExample
PtrExample()
Constructor.
Definition:
main-ptr.cc:45
g_ptr
static Ptr< PtrExample > g_ptr
Example Ptr global variable.
Definition:
main-ptr.cc:63
src
core
examples
main-ptr.cc
Generated on Wed Sep 30 2015 15:55:26 for ns-3 by
1.8.9.1