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 * @hidecaller
105 */
106template <typename T>
108
109/**
110 * @ingroup tracing
111 *
112 * Create an empty TraceSourceAccessor.
113 *
114 * @returns The empty TraceSourceAccessor (runtime exception if used)
115 */
121
122} // namespace ns3
123
124/********************************************************************
125 * Implementation of the templates declared above.
126 ********************************************************************/
127
128namespace ns3
129{
130
131/**
132 * @ingroup tracing
133 * MakeTraceSourceAccessor() implementation.
134 *
135 * @tparam T \deduced Class type of the TracedCallback
136 * @tparam SOURCE \deduced Type of the underlying value.
137 * @param [in] a The underlying data value.
138 * @returns The TraceSourceAccessor
139 */
140template <typename T, typename SOURCE>
143{
144 struct Accessor : public TraceSourceAccessor
145 {
146 bool ConnectWithoutContext(ObjectBase* obj, const CallbackBase& cb) const override
147 {
148 T* p = dynamic_cast<T*>(obj);
149 if (p == nullptr)
150 {
151 return false;
152 }
153 (p->*m_source).ConnectWithoutContext(cb);
154 return true;
155 }
156
157 bool Connect(ObjectBase* obj, std::string context, const CallbackBase& cb) const override
158 {
159 T* p = dynamic_cast<T*>(obj);
160 if (p == nullptr)
161 {
162 return false;
163 }
164 (p->*m_source).Connect(cb, context);
165 return true;
166 }
167
168 bool DisconnectWithoutContext(ObjectBase* obj, const CallbackBase& cb) const override
169 {
170 T* p = dynamic_cast<T*>(obj);
171 if (p == nullptr)
172 {
173 return false;
174 }
175 (p->*m_source).DisconnectWithoutContext(cb);
176 return true;
177 }
178
179 bool Disconnect(ObjectBase* obj, std::string context, const CallbackBase& cb) const override
180 {
181 T* p = dynamic_cast<T*>(obj);
182 if (p == nullptr)
183 {
184 return false;
185 }
186 (p->*m_source).Disconnect(cb, context);
187 return true;
188 }
189
190 SOURCE T::* m_source;
191 }* accessor = new Accessor();
192
193 accessor->m_source = a;
194 return Ptr<const TraceSourceAccessor>(accessor, false);
195}
196
197template <typename T>
198Ptr<const TraceSourceAccessor>
203
204} // namespace ns3
205
206#endif /* TRACE_SOURCE_ACCESSOR_H */
Declaration of the various callback functions.
Base class for Callback class.
Definition callback.h:347
Anchor the ns-3 type and attribute system.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
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.