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 /** Constructor. */
30 PtrExample();
31 /** Destructor. */
32 ~PtrExample() override;
33 /** Example class method. */
34 void Method();
35};
36
38{
39 std::cout << "PtrExample constructor" << std::endl;
40}
41
43{
44 std::cout << "PtrExample destructor" << std::endl;
45}
46
47void
49{
50 std::cout << "PtrExample method" << std::endl;
51}
52
53/**
54 * Example Ptr global variable.
55 */
56static Ptr<PtrExample> g_ptr = nullptr;
57
58/**
59 * Example Ptr manipulations.
60 *
61 * This function stores it's argument in the global variable \c g_ptr
62 * and returns the old value of \c g_ptr.
63 * \param [in] p A Ptr.
64 * \returns The prior value of \c g_ptr.
65 */
66static Ptr<PtrExample>
68{
70 g_ptr = p;
71 return prev;
72}
73
74/**
75 * Set \c g_ptr to NULL.
76 */
77static void
79{
80 g_ptr = nullptr;
81}
82
83int
84main(int argc, char* argv[])
85{
86 CommandLine cmd(__FILE__);
87 cmd.Parse(argc, argv);
88
89 {
90 // Create a new object of type PtrExample, store it in global
91 // variable g_ptr
93 p->Method();
96 }
97
98 {
99 // Create a new object of type PtrExample, store it in global
100 // variable g_ptr, get a hold on the previous PtrExample object.
103 // call method on object
104 prev->Method();
105 // Clear the currently-stored object
106 ClearPtr();
107 // get the raw pointer and release it.
108 PtrExample* raw = GetPointer(prev);
109 prev = nullptr;
110 raw->Method();
111 raw->Unref();
112 }
113
114 return 0;
115}
Example class illustrating use of Ptr.
Definition main-ptr.cc:27
~PtrExample() override
Destructor.
Definition main-ptr.cc:42
PtrExample()
Constructor.
Definition main-ptr.cc:37
void Method()
Example class method.
Definition main-ptr.cc:48
Parse command-line arguments.
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
void Unref() 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:55
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
static Ptr< PtrExample > g_ptr
Example Ptr global variable.
Definition main-ptr.cc:56
static Ptr< PtrExample > StorePtr(Ptr< PtrExample > p)
Example Ptr manipulations.
Definition main-ptr.cc:67
static void ClearPtr()
Set g_ptr to NULL.
Definition main-ptr.cc:78
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * GetPointer(const Ptr< U > &p)
Definition ptr.h:450
uint32_t prev