A Discrete-Event Network Simulator
API
traced-callback.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005,2006,2007 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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 
20 #ifndef TRACED_CALLBACK_H
21 #define TRACED_CALLBACK_H
22 
23 #include "callback.h"
24 
25 #include <list>
26 
33 namespace ns3
34 {
35 
52 template <typename... Ts>
54 {
55  public:
63  void ConnectWithoutContext(const CallbackBase& callback);
73  void Connect(const CallbackBase& callback, std::string path);
79  void DisconnectWithoutContext(const CallbackBase& callback);
86  void Disconnect(const CallbackBase& callback, std::string path);
92  void operator()(Ts... args) const;
97  bool IsEmpty() const;
98 
105  // Uint32Callback appears to be the only one used at the moment.
106  // Feel free to add typedef's for any other POD you need.
107  typedef void (*Uint32Callback)(const uint32_t value);
110  private:
116  typedef std::list<Callback<void, Ts...>> CallbackList;
119 };
120 
121 } // namespace ns3
122 
123 /********************************************************************
124  * Implementation of the templates declared above.
125  ********************************************************************/
126 
127 namespace ns3
128 {
129 
130 template <typename... Ts>
132  : m_callbackList()
133 {
134 }
135 
136 template <typename... Ts>
137 void
139 {
140  Callback<void, Ts...> cb;
141  if (!cb.Assign(callback))
142  {
144  }
145  m_callbackList.push_back(cb);
146 }
147 
148 template <typename... Ts>
149 void
150 TracedCallback<Ts...>::Connect(const CallbackBase& callback, std::string path)
151 {
152  Callback<void, std::string, Ts...> cb;
153  if (!cb.Assign(callback))
154  {
155  NS_FATAL_ERROR("when connecting to " << path);
156  }
157  Callback<void, Ts...> realCb = cb.Bind(path);
158  m_callbackList.push_back(realCb);
159 }
160 
161 template <typename... Ts>
162 void
164 {
165  for (typename CallbackList::iterator i = m_callbackList.begin(); i != m_callbackList.end();
166  /* empty */)
167  {
168  if ((*i).IsEqual(callback))
169  {
170  i = m_callbackList.erase(i);
171  }
172  else
173  {
174  i++;
175  }
176  }
177 }
178 
179 template <typename... Ts>
180 void
182 {
183  Callback<void, std::string, Ts...> cb;
184  if (!cb.Assign(callback))
185  {
186  NS_FATAL_ERROR("when disconnecting from " << path);
187  }
188  Callback<void, Ts...> realCb = cb.Bind(path);
189  DisconnectWithoutContext(realCb);
190 }
191 
192 template <typename... Ts>
193 void
195 {
196  for (typename CallbackList::const_iterator i = m_callbackList.begin();
197  i != m_callbackList.end();
198  i++)
199  {
200  (*i)(args...);
201  }
202 }
203 
204 template <typename... Ts>
205 bool
207 {
208  return m_callbackList.empty();
209 }
210 
211 } // namespace ns3
212 
213 #endif /* TRACED_CALLBACK_H */
Declaration of the various callback functions.
Base class for Callback class.
Definition: callback.h:365
Callback template class.
Definition: callback.h:443
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
Definition: callback.h:560
Forward calls to a chain of Callback.
CallbackList m_callbackList
The chain of Callbacks.
void(* Uint32Callback)(const uint32_t value)
TracedCallback signature for POD.
void Disconnect(const CallbackBase &callback, std::string path)
Remove from the chain a Callback which was connected with a context.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
TracedCallback()
Constructor.
bool IsEmpty() const
Checks if the Callbacks list is empty.
std::list< Callback< void, Ts... > > CallbackList
Container type for holding the chain of Callbacks.
void operator()(Ts... args) const
Functor which invokes the chain of Callbacks.
void DisconnectWithoutContext(const CallbackBase &callback)
Remove from the chain a Callback which was connected without a context.
void Connect(const CallbackBase &callback, std::string path)
Append a Callback to the chain with a context.
void Disconnect(std::string path, const CallbackBase &cb)
Definition: config.cc:992
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:975
void DisconnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:968
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_FATAL_ERROR_NO_MSG()
Report a fatal error and terminate.
Definition: fatal-error.h:142
Every class exported by the ns3 library is enclosed in the ns3 namespace.
value
Definition: second.py:41
#define list