A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
34 namespace ns3
35 {
36 
37 class ObjectBase;
38 
47 class TraceSourceAccessor : public SimpleRefCount<TraceSourceAccessor>
48 {
49  public:
53  virtual ~TraceSourceAccessor();
54 
63  virtual bool ConnectWithoutContext(ObjectBase* obj, const CallbackBase& cb) const = 0;
76  virtual bool Connect(ObjectBase* obj, std::string context, const CallbackBase& cb) const = 0;
85  virtual bool DisconnectWithoutContext(ObjectBase* obj, const CallbackBase& cb) const = 0;
98  virtual bool Disconnect(ObjectBase* obj, std::string context, const CallbackBase& cb) const = 0;
99 };
100 
116 template <typename T>
118 
126 static inline Ptr<const TraceSourceAccessor>
128 {
129  return Ptr<const TraceSourceAccessor>(nullptr);
130 }
131 
132 } // namespace ns3
133 
134 /********************************************************************
135  * Implementation of the templates declared above.
136  ********************************************************************/
137 
138 namespace ns3
139 {
140 
150 template <typename T, typename SOURCE>
151 Ptr<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 
207 template <typename T>
208 Ptr<const TraceSourceAccessor>
210 {
211  return DoMakeTraceSourceAccessor(a);
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.
void Disconnect(std::string path, const CallbackBase &cb)
Definition: config.cc:991
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:974
void DisconnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:967
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:950
Ptr< const TraceSourceAccessor > DoMakeTraceSourceAccessor(SOURCE T::*a)
MakeTraceSourceAccessor() implementation.
static Ptr< const TraceSourceAccessor > MakeEmptyTraceSourceAccessor()
Create an empty TraceSourceAccessor.
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.