A Discrete-Event Network Simulator
API
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
32using namespace ns3;
33
37class PtrExample : public Object
38{
39public:
41 PtrExample ();
43 ~PtrExample ();
45 void Method (void);
46};
48{
49 std::cout << "PtrExample constructor" << std::endl;
50}
52{
53 std::cout << "PtrExample destructor" << std::endl;
54}
55void
57{
58 std::cout << "PtrExample method" << std::endl;
59}
60
61
66
75static Ptr<PtrExample>
77{
79 g_ptr = p;
80 return prev;
81}
82
86static void
88{
89 g_ptr = 0;
90}
91
92
93
94int main (int argc, char *argv[])
95{
96 CommandLine cmd (__FILE__);
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 ();
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> ();
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}
Example class illustrating use of Ptr.
Definition: main-ptr.cc:38
PtrExample()
Constructor.
Definition: main-ptr.cc:47
~PtrExample()
Destructor.
Definition: main-ptr.cc:51
void Method(void)
Example class method.
Definition: main-ptr.cc:56
Parse command-line arguments.
Definition: command-line.h:229
A base class which provides memory management and object aggregation.
Definition: object.h:88
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
void Unref(void) const
Decrement the reference count.
#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
static void ClearPtr(void)
Set g_ptr to NULL.
Definition: main-ptr.cc:87
static Ptr< PtrExample > g_ptr
Example Ptr global variable.
Definition: main-ptr.cc:65
static Ptr< PtrExample > StorePtr(Ptr< PtrExample > p)
Example Ptr manipulations.
Definition: main-ptr.cc:76
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:421
cmd
Definition: second.py:35
uint32_t prev