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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef TRACE_SOURCE_ACCESSOR_H
20#define TRACE_SOURCE_ACCESSOR_H
21
22#include "callback.h"
23#include "ptr.h"
24#include "simple-ref-count.h"
25
26#include <stdint.h>
27
28/**
29 * \file
30 * \ingroup tracing
31 * ns3::TraceSourceAccessor and ns3::MakeTraceSourceAccessor declarations.
32 */
33
34namespace ns3
35{
36
37class ObjectBase;
38
39/**
40 * \ingroup tracing
41 *
42 * \brief Control access to objects' trace sources.
43 *
44 * This class abstracts the kind of trace source to which we want to connect
45 * and provides services to Connect and Disconnect a sink to a trace source.
46 */
47class TraceSourceAccessor : public SimpleRefCount<TraceSourceAccessor>
48{
49 public:
50 /** Constructor. */
52 /** Destructor. */
53 virtual ~TraceSourceAccessor();
54
55 /**
56 * Connect a Callback to a TraceSource (without context.)
57 *
58 * \param [in] obj The object instance which contains the target trace source.
59 * \param [in] cb The callback to connect to the target trace source.
60 * \return \c true unless the connection could not be made, typically because
61 * the \c obj couldn't be cast to the correct type.
62 */
63 virtual bool ConnectWithoutContext(ObjectBase* obj, const CallbackBase& cb) const = 0;
64 /**
65 * Connect a Callback to a TraceSource with a context string.
66 *
67 * The context string will be provided as the first argument to the
68 * Callback function.
69 *
70 * \param [in] obj The object instance which contains the target trace source.
71 * \param [in] context The context to bind to the user callback.
72 * \param [in] cb The callback to connect to the target trace source.
73 * \return \c true unless the connection could not be made, typically because
74 * the \c obj couldn't be cast to the correct type.
75 */
76 virtual bool Connect(ObjectBase* obj, std::string context, const CallbackBase& cb) const = 0;
77 /**
78 * Disconnect a Callback from a TraceSource (without context).
79 *
80 * \param [in] obj The object instance which contains the target trace source.
81 * \param [in] cb The callback to disconnect from the target trace source.
82 * \return \c true unless the connection could not be made, typically because
83 * the \c obj couldn't be cast to the correct type.
84 */
85 virtual bool DisconnectWithoutContext(ObjectBase* obj, const CallbackBase& cb) const = 0;
86 /**
87 * Disconnect a Callback from a TraceSource with a context string.
88 *
89 * The context string will be provided as the first argument to the
90 * Callback function.
91 *
92 * \param [in] obj the object instance which contains the target trace source.
93 * \param [in] context the context which was bound to the user callback.
94 * \param [in] cb the callback to disconnect from the target trace source.
95 * \return \c true unless the connection could not be made, typically because
96 * the \c obj couldn't be cast to the correct type.
97 */
98 virtual bool Disconnect(ObjectBase* obj, std::string context, const CallbackBase& cb) const = 0;
99};
100
101/**
102 * \ingroup tracing
103 *
104 * Create a TraceSourceAccessor which will control access to the underlying
105 * trace source.
106 *
107 * This helper template method assumes that the underlying
108 * type implements a statically-polymorphic set of Connect and Disconnect
109 * methods and creates a dynamic-polymorphic class to wrap the underlying
110 * static-polymorphic class. This functionality is typically provided
111 * by wrapping an object data member in a TracedCallback or TracedValue.
112 *
113 * \param [in] a The trace source
114 * \returns The TraceSourceAccessor
115 */
116template <typename T>
118
119/**
120 * \ingroup tracing
121 *
122 * Create an empty TraceSourceAccessor.
123 *
124 * \returns The empty TraceSourceAccessor (runtime exception if used)
125 */
128{
129 return Ptr<const TraceSourceAccessor>(nullptr);
130}
131
132} // namespace ns3
133
134/********************************************************************
135 * Implementation of the templates declared above.
136 ********************************************************************/
137
138namespace ns3
139{
140
141/**
142 * \ingroup tracing
143 * MakeTraceSourceAccessor() implementation.
144 *
145 * \tparam T \deduced Class type of the TracedCallback
146 * \tparam SOURCE \deduced Type of the underlying value.
147 * \param [in] a The underlying data value.
148 * \returns The TraceSourceAccessor
149 */
150template <typename T, typename SOURCE>
151Ptr<const TraceSourceAccessor>
153{
154 struct Accessor : public TraceSourceAccessor
155 {
156 bool ConnectWithoutContext(ObjectBase* obj, 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).ConnectWithoutContext(cb);
164 return true;
165 }
166
167 bool Connect(ObjectBase* obj, std::string context, 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).Connect(cb, context);
175 return true;
176 }
177
178 bool DisconnectWithoutContext(ObjectBase* obj, 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).DisconnectWithoutContext(cb);
186 return true;
187 }
188
189 bool Disconnect(ObjectBase* obj, std::string context, const CallbackBase& cb) const override
190 {
191 T* p = dynamic_cast<T*>(obj);
192 if (p == nullptr)
193 {
194 return false;
195 }
196 (p->*m_source).Disconnect(cb, context);
197 return true;
198 }
199
200 SOURCE T::*m_source;
201 }* accessor = new Accessor();
202
203 accessor->m_source = a;
204 return Ptr<const TraceSourceAccessor>(accessor, false);
205}
206
207template <typename T>
208Ptr<const TraceSourceAccessor>
210{
212}
213
214} // namespace ns3
215
216#endif /* TRACE_SOURCE_ACCESSOR_H */
Declaration of the various callback functions.
Base class for Callback class.
Definition: callback.h:360
Anchor the ns-3 type and attribute system.
Definition: object-base.h:173
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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.