A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
trace-source-accessor.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2008 INRIA
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7
*/
8
#ifndef TRACE_SOURCE_ACCESSOR_H
9
#define TRACE_SOURCE_ACCESSOR_H
10
11
#include "
callback.h
"
12
#include "
ptr.h
"
13
#include "
simple-ref-count.h
"
14
15
#include <stdint.h>
16
17
/**
18
* @file
19
* @ingroup tracing
20
* ns3::TraceSourceAccessor and ns3::MakeTraceSourceAccessor declarations.
21
*/
22
23
namespace
ns3
24
{
25
26
class
ObjectBase
;
27
28
/**
29
* @ingroup tracing
30
*
31
* @brief Control access to objects' trace sources.
32
*
33
* This class abstracts the kind of trace source to which we want to connect
34
* and provides services to Connect and Disconnect a sink to a trace source.
35
*/
36
class
TraceSourceAccessor
:
public
SimpleRefCount
<TraceSourceAccessor>
37
{
38
public
:
39
/** Constructor. */
40
TraceSourceAccessor
();
41
/** Destructor. */
42
virtual
~TraceSourceAccessor
();
43
44
/**
45
* Connect a Callback to a TraceSource (without context.)
46
*
47
* @param [in] obj The object instance which contains the target trace source.
48
* @param [in] cb The callback to connect to the target trace source.
49
* @return \c true unless the connection could not be made, typically because
50
* the \c obj couldn't be cast to the correct type.
51
*/
52
virtual
bool
ConnectWithoutContext
(
ObjectBase
* obj,
const
CallbackBase
& cb)
const
= 0;
53
/**
54
* Connect a Callback to a TraceSource with a context string.
55
*
56
* The context string will be provided as the first argument to the
57
* Callback function.
58
*
59
* @param [in] obj The object instance which contains the target trace source.
60
* @param [in] context The context to bind to the user callback.
61
* @param [in] cb The callback to connect to the target trace source.
62
* @return \c true unless the connection could not be made, typically because
63
* the \c obj couldn't be cast to the correct type.
64
*/
65
virtual
bool
Connect
(
ObjectBase
* obj, std::string context,
const
CallbackBase
& cb)
const
= 0;
66
/**
67
* Disconnect a Callback from a TraceSource (without context).
68
*
69
* @param [in] obj The object instance which contains the target trace source.
70
* @param [in] cb The callback to disconnect from the target trace source.
71
* @return \c true unless the connection could not be made, typically because
72
* the \c obj couldn't be cast to the correct type.
73
*/
74
virtual
bool
DisconnectWithoutContext
(
ObjectBase
* obj,
const
CallbackBase
& cb)
const
= 0;
75
/**
76
* Disconnect a Callback from a TraceSource with a context string.
77
*
78
* The context string will be provided as the first argument to the
79
* Callback function.
80
*
81
* @param [in] obj the object instance which contains the target trace source.
82
* @param [in] context the context which was bound to the user callback.
83
* @param [in] cb the callback to disconnect from the target trace source.
84
* @return \c true unless the connection could not be made, typically because
85
* the \c obj couldn't be cast to the correct type.
86
*/
87
virtual
bool
Disconnect
(
ObjectBase
* obj, std::string context,
const
CallbackBase
& cb)
const
= 0;
88
};
89
90
/**
91
* @ingroup tracing
92
*
93
* Create a TraceSourceAccessor which will control access to the underlying
94
* trace source.
95
*
96
* This helper template method assumes that the underlying
97
* type implements a statically-polymorphic set of Connect and Disconnect
98
* methods and creates a dynamic-polymorphic class to wrap the underlying
99
* static-polymorphic class. This functionality is typically provided
100
* by wrapping an object data member in a TracedCallback or TracedValue.
101
*
102
* @param [in] a The trace source
103
* @returns The TraceSourceAccessor
104
*/
105
template
<
typename
T>
106
Ptr<const TraceSourceAccessor>
MakeTraceSourceAccessor
(T a);
107
108
/**
109
* @ingroup tracing
110
*
111
* Create an empty TraceSourceAccessor.
112
*
113
* @returns The empty TraceSourceAccessor (runtime exception if used)
114
*/
115
static
inline
Ptr<const TraceSourceAccessor>
116
MakeEmptyTraceSourceAccessor
()
117
{
118
return
Ptr<const TraceSourceAccessor>
(
nullptr
);
119
}
120
121
}
// namespace ns3
122
123
/********************************************************************
124
* Implementation of the templates declared above.
125
********************************************************************/
126
127
namespace
ns3
128
{
129
130
/**
131
* @ingroup tracing
132
* MakeTraceSourceAccessor() implementation.
133
*
134
* @tparam T \deduced Class type of the TracedCallback
135
* @tparam SOURCE \deduced Type of the underlying value.
136
* @param [in] a The underlying data value.
137
* @returns The TraceSourceAccessor
138
*/
139
// clang-format off
140
// Clang-format guard needed for versions <= 18
141
template
<
typename
T,
typename
SOURCE>
142
Ptr<const TraceSourceAccessor>
143
DoMakeTraceSourceAccessor
(SOURCE T::* a)
144
// clang-format on
145
{
146
struct
Accessor :
public
TraceSourceAccessor
147
{
148
bool
ConnectWithoutContext(
ObjectBase
* obj,
const
CallbackBase
& cb)
const override
149
{
150
T* p =
dynamic_cast<
T*
>
(obj);
151
if
(p ==
nullptr
)
152
{
153
return
false
;
154
}
155
(p->*m_source).ConnectWithoutContext(cb);
156
return
true
;
157
}
158
159
bool
Connect(
ObjectBase
* obj, std::string context,
const
CallbackBase
& cb)
const override
160
{
161
T* p =
dynamic_cast<
T*
>
(obj);
162
if
(p ==
nullptr
)
163
{
164
return
false
;
165
}
166
(p->*m_source).Connect(cb, context);
167
return
true
;
168
}
169
170
bool
DisconnectWithoutContext(
ObjectBase
* obj,
const
CallbackBase
& cb)
const override
171
{
172
T* p =
dynamic_cast<
T*
>
(obj);
173
if
(p ==
nullptr
)
174
{
175
return
false
;
176
}
177
(p->*m_source).DisconnectWithoutContext(cb);
178
return
true
;
179
}
180
181
bool
Disconnect(
ObjectBase
* obj, std::string context,
const
CallbackBase
& cb)
const override
182
{
183
T* p =
dynamic_cast<
T*
>
(obj);
184
if
(p ==
nullptr
)
185
{
186
return
false
;
187
}
188
(p->*m_source).Disconnect(cb, context);
189
return
true
;
190
}
191
192
// clang-format off
193
// Clang-format guard needed for versions <= 18
194
SOURCE T::* m_source;
195
// clang-format on
196
}* accessor =
new
Accessor();
197
198
accessor->m_source = a;
199
return
Ptr<const TraceSourceAccessor>
(accessor,
false
);
200
}
201
202
template
<
typename
T>
203
Ptr<const TraceSourceAccessor>
204
MakeTraceSourceAccessor
(T a)
205
{
206
return
DoMakeTraceSourceAccessor
(a);
207
}
208
209
}
// namespace ns3
210
211
#endif
/* TRACE_SOURCE_ACCESSOR_H */
callback.h
Declaration of the various callback functions.
ns3::CallbackBase
Base class for Callback class.
Definition
callback.h:344
ns3::ObjectBase
Anchor the ns-3 type and attribute system.
Definition
object-base.h:162
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:66
ns3::SimpleRefCount< TraceSourceAccessor >::SimpleRefCount
SimpleRefCount()
Definition
simple-ref-count.h:73
ns3::TraceSourceAccessor
Control access to objects' trace sources.
Definition
trace-source-accessor.h:37
ns3::TraceSourceAccessor::ConnectWithoutContext
virtual bool ConnectWithoutContext(ObjectBase *obj, const CallbackBase &cb) const =0
Connect a Callback to a TraceSource (without context.)
ns3::TraceSourceAccessor::Connect
virtual bool Connect(ObjectBase *obj, std::string context, const CallbackBase &cb) const =0
Connect a Callback to a TraceSource with a context string.
ns3::TraceSourceAccessor::Disconnect
virtual bool Disconnect(ObjectBase *obj, std::string context, const CallbackBase &cb) const =0
Disconnect a Callback from a TraceSource with a context string.
ns3::TraceSourceAccessor::TraceSourceAccessor
TraceSourceAccessor()
Constructor.
Definition
trace-source-accessor.cc:23
ns3::TraceSourceAccessor::DisconnectWithoutContext
virtual bool DisconnectWithoutContext(ObjectBase *obj, const CallbackBase &cb) const =0
Disconnect a Callback from a TraceSource (without context).
ns3::TraceSourceAccessor::~TraceSourceAccessor
virtual ~TraceSourceAccessor()
Destructor.
Definition
trace-source-accessor.cc:27
ns3::MakeEmptyTraceSourceAccessor
static Ptr< const TraceSourceAccessor > MakeEmptyTraceSourceAccessor()
Create an empty TraceSourceAccessor.
Definition
trace-source-accessor.h:116
ns3::DoMakeTraceSourceAccessor
Ptr< const TraceSourceAccessor > DoMakeTraceSourceAccessor(SOURCE T::*a)
MakeTraceSourceAccessor() implementation.
Definition
trace-source-accessor.h:143
ns3::MakeTraceSourceAccessor
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Definition
trace-source-accessor.h:204
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ptr.h
ns3::Ptr smart pointer declaration and implementation.
simple-ref-count.h
ns3::SimpleRefCount declaration and template implementation.
src
core
model
trace-source-accessor.h
Generated on Wed Jun 11 2025 13:15:28 for ns-3 by
1.13.2