A Discrete-Event Network Simulator
API
traced-callback.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006,2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #ifndef TRACED_CALLBACK_H
22 #define TRACED_CALLBACK_H
23 
24 #include <list>
25 #include "callback.h"
26 
33 namespace ns3 {
34 
51 template<typename... Ts>
53 {
54 public:
62  void ConnectWithoutContext (const CallbackBase & callback);
72  void Connect (const CallbackBase & callback, std::string path);
78  void DisconnectWithoutContext (const CallbackBase & callback);
85  void Disconnect (const CallbackBase & callback, std::string path);
91  void operator() (Ts... args) const;
96  bool IsEmpty () const;
97 
104  // Uint32Callback appears to be the only one used at the moment.
105  // Feel free to add typedef's for any other POD you need.
106  typedef void (* Uint32Callback)(const uint32_t value);
109 private:
115  typedef std::list<Callback<void,Ts...> > CallbackList;
118 };
119 
120 } // namespace ns3
121 
122 
123 /********************************************************************
124  * Implementation of the templates declared above.
125  ********************************************************************/
126 
127 namespace ns3 {
128 
129 template<typename... Ts>
131  : m_callbackList ()
132 {}
133 template<typename... Ts>
134 void
136 {
137  Callback<void,Ts...> cb;
138  if (!cb.Assign (callback))
139  {
141  }
142  m_callbackList.push_back (cb);
143 }
144 template<typename... Ts>
145 void
146 TracedCallback<Ts...>::Connect (const CallbackBase & callback, std::string path)
147 {
148  Callback<void,std::string,Ts...> cb;
149  if (!cb.Assign (callback))
150  {
151  NS_FATAL_ERROR ("when connecting to " << path);
152  }
153  Callback<void,Ts...> realCb = cb.Bind (path);
154  m_callbackList.push_back (realCb);
155 }
156 template<typename... Ts>
157 void
159 {
160  for (typename CallbackList::iterator i = m_callbackList.begin ();
161  i != m_callbackList.end (); /* empty */)
162  {
163  if ((*i).IsEqual (callback))
164  {
165  i = m_callbackList.erase (i);
166  }
167  else
168  {
169  i++;
170  }
171  }
172 }
173 template<typename... Ts>
174 void
175 TracedCallback<Ts...>::Disconnect (const CallbackBase & callback, std::string path)
176 {
177  Callback<void,std::string,Ts...> cb;
178  if (!cb.Assign (callback))
179  {
180  NS_FATAL_ERROR ("when disconnecting from " << path);
181  }
182  Callback<void,Ts...> realCb = cb.Bind (path);
183  DisconnectWithoutContext (realCb);
184 }
185 template<typename... Ts>
186 void
188 {
189  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
190  i != m_callbackList.end (); i++)
191  {
192  (*i)(args...);
193  }
194 }
195 
196 template <typename... Ts>
197 bool
199 {
200  return m_callbackList.empty ();
201 }
202 
203 } // namespace ns3
204 
205 #endif /* TRACED_CALLBACK_H */
Declaration of the various callback functions.
Base class for Callback class.
Definition: callback.h:1196
Callback template class.
Definition: callback.h:1279
Callback< R, T2, T3, T4, T5, T6, T7, T8, T9 > Bind(T a)
Bind the first arguments.
Definition: callback.h:1329
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.
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.
std::list< Callback< void, Ts... > > CallbackList
Container type for holding the chain of Callbacks.
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:935
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
void DisconnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:914
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_FATAL_ERROR_NO_MSG()
Report a fatal error and terminate.
Definition: fatal-error.h:128
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define list