A Discrete-Event Network Simulator
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
21using namespace ns3;
22
23/**
24 * Example class illustrating use of Ptr.
25 */
26class PtrExample : public Object
27{
28 public:
29 /**
30 * @brief Get the type ID.
31 * @return the object TypeId
32 */
33 static TypeId GetTypeId();
34
35 /** Constructor. */
36 PtrExample();
37 /** Destructor. */
38 ~PtrExample() override;
39 /** Example class method. */
40 void Method();
41};
42
44
47{
48 static TypeId tid =
49 TypeId("ns3::PtrExample").SetParent<Object>().SetGroupName("Core").HideFromDocumentation();
50 return tid;
51}
52
54{
55 std::cout << "PtrExample constructor" << std::endl;
56}
57
59{
60 std::cout << "PtrExample destructor" << std::endl;
61}
62
63void
65{
66 std::cout << "PtrExample method" << std::endl;
67}
68
69/**
70 * Example Ptr global variable.
71 */
72static Ptr<PtrExample> g_ptr = nullptr;
73
74/**
75 * Example Ptr manipulations.
76 *
77 * This function stores it's argument in the global variable \c g_ptr
78 * and returns the old value of \c g_ptr.
79 * @param [in] p A Ptr.
80 * @returns The prior value of \c g_ptr.
81 */
82static Ptr<PtrExample>
84{
86 g_ptr = p;
87 return prev;
88}
89
90/**
91 * Set \c g_ptr to NULL.
92 */
93static void
95{
96 g_ptr = nullptr;
97}
98
99int
100main(int argc, char* argv[])
101{
102 CommandLine cmd(__FILE__);
103 cmd.Parse(argc, argv);
104
105 {
106 // Create a new object of type PtrExample, store it in global
107 // variable g_ptr
109 p->Method();
111 NS_ASSERT(!prev);
112 }
113
114 {
115 // Create a new object of type PtrExample, store it in global
116 // variable g_ptr, get a hold on the previous PtrExample object.
119 // call method on object
120 prev->Method();
121 // Clear the currently-stored object
122 ClearPtr();
123 // get the raw pointer and release it.
124 PtrExample* raw = GetPointer(prev);
125 prev = nullptr;
126 raw->Method();
127 raw->Unref();
128 }
129
130 return 0;
131}
Example class illustrating use of Ptr.
Definition main-ptr.cc:27
~PtrExample() override
Destructor.
Definition main-ptr.cc:58
static TypeId GetTypeId()
Get the type ID.
Definition main-ptr.cc:46
PtrExample()
Constructor.
Definition main-ptr.cc:53
void Method()
Example class method.
Definition main-ptr.cc:64
Parse command-line arguments.
Object()
Constructor.
Definition object.cc:96
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
void Unref() const
Decrement the reference count.
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
static Ptr< PtrExample > g_ptr
Example Ptr global variable.
Definition main-ptr.cc:72
static Ptr< PtrExample > StorePtr(Ptr< PtrExample > p)
Example Ptr manipulations.
Definition main-ptr.cc:83
static void ClearPtr()
Set g_ptr to NULL.
Definition main-ptr.cc:94
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * GetPointer(const Ptr< U > &p)
Definition ptr.h:455
uint32_t prev