A Discrete-Event Network Simulator
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
23namespace ns3
24{
25
26class 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 */
36class TraceSourceAccessor : public SimpleRefCount<TraceSourceAccessor>
37{
38 public:
39 /** Constructor. */
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 */
105template <typename T>
107
108/**
109 * \ingroup tracing
110 *
111 * Create an empty TraceSourceAccessor.
112 *
113 * \returns The empty TraceSourceAccessor (runtime exception if used)
114 */
120
121} // namespace ns3
122
123/********************************************************************
124 * Implementation of the templates declared above.
125 ********************************************************************/
126
127namespace 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 */
139template <typename T, typename SOURCE>
140Ptr<const TraceSourceAccessor>
142{
143 struct Accessor : public TraceSourceAccessor
144 {
145 bool ConnectWithoutContext(ObjectBase* obj, const CallbackBase& cb) const override
146 {
147 T* p = dynamic_cast<T*>(obj);
148 if (p == nullptr)
149 {
150 return false;
151 }
152 (p->*m_source).ConnectWithoutContext(cb);
153 return true;
154 }
155
156 bool Connect(ObjectBase* obj, std::string context, const CallbackBase& cb) const override
157 {
158 T* p = dynamic_cast<T*>(obj);
159 if (p == nullptr)
160 {
161 return false;
162 }
163 (p->*m_source).Connect(cb, context);
164 return true;
165 }
166
167 bool DisconnectWithoutContext(ObjectBase* obj, const CallbackBase& cb) const override
168 {
169 T* p = dynamic_cast<T*>(obj);
170 if (p == nullptr)
171 {
172 return false;
173 }
174 (p->*m_source).DisconnectWithoutContext(cb);
175 return true;
176 }
177
178 bool Disconnect(ObjectBase* obj, std::string context, const CallbackBase& cb) const override
179 {
180 T* p = dynamic_cast<T*>(obj);
181 if (p == nullptr)
182 {
183 return false;
184 }
185 (p->*m_source).Disconnect(cb, context);
186 return true;
187 }
188
189 SOURCE T::*m_source;
190 }* accessor = new Accessor();
191
192 accessor->m_source = a;
193 return Ptr<const TraceSourceAccessor>(accessor, false);
194}
195
196template <typename T>
197Ptr<const TraceSourceAccessor>
202
203} // namespace ns3
204
205#endif /* TRACE_SOURCE_ACCESSOR_H */
Declaration of the various callback functions.
Base class for Callback class.
Definition callback.h:344
Anchor the ns-3 type and attribute system.
Smart pointer class similar to boost::intrusive_ptr.
A template-based reference counting class.
Control access to objects' trace sources.
virtual bool ConnectWithoutContext(ObjectBase *obj, const CallbackBase &cb) const =0
Connect a Callback to a TraceSource (without context.)
virtual bool Connect(ObjectBase *obj, std::string context, const CallbackBase &cb) const =0
Connect a Callback to a TraceSource with a context string.
virtual bool Disconnect(ObjectBase *obj, std::string context, const CallbackBase &cb) const =0
Disconnect a Callback from a TraceSource with a context string.
virtual bool DisconnectWithoutContext(ObjectBase *obj, const CallbackBase &cb) const =0
Disconnect a Callback from a TraceSource (without context).
virtual ~TraceSourceAccessor()
Destructor.
static Ptr< const TraceSourceAccessor > MakeEmptyTraceSourceAccessor()
Create an empty TraceSourceAccessor.
Ptr< const TraceSourceAccessor > DoMakeTraceSourceAccessor(SOURCE T::*a)
MakeTraceSourceAccessor() implementation.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ptr smart pointer declaration and implementation.
ns3::SimpleRefCount declaration and template implementation.