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:
56  TracedCallback ();
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;
92 
99  // Uint32Callback appears to be the only one used at the moment.
100  // Feel free to add typedef's for any other POD you need.
101  typedef void (* Uint32Callback)(const uint32_t value);
104 private:
110  typedef std::list<Callback<void,Ts...> > CallbackList;
113 };
114 
115 } // namespace ns3
116 
117 
118 /********************************************************************
119  * Implementation of the templates declared above.
120  ********************************************************************/
121 
122 namespace ns3 {
123 
124 template<typename... Ts>
126  : m_callbackList ()
127 {}
128 template<typename... Ts>
129 void
131 {
132  Callback<void,Ts...> cb;
133  if (!cb.Assign (callback))
134  {
136  }
137  m_callbackList.push_back (cb);
138 }
139 template<typename... Ts>
140 void
141 TracedCallback<Ts...>::Connect (const CallbackBase & callback, std::string path)
142 {
143  Callback<void,std::string,Ts...> cb;
144  if (!cb.Assign (callback))
145  {
146  NS_FATAL_ERROR ("when connecting to " << path);
147  }
148  Callback<void,Ts...> realCb = cb.Bind (path);
149  m_callbackList.push_back (realCb);
150 }
151 template<typename... Ts>
152 void
154 {
155  for (typename CallbackList::iterator i = m_callbackList.begin ();
156  i != m_callbackList.end (); /* empty */)
157  {
158  if ((*i).IsEqual (callback))
159  {
160  i = m_callbackList.erase (i);
161  }
162  else
163  {
164  i++;
165  }
166  }
167 }
168 template<typename... Ts>
169 void
170 TracedCallback<Ts...>::Disconnect (const CallbackBase & callback, std::string path)
171 {
172  Callback<void,std::string,Ts...> cb;
173  if (!cb.Assign (callback))
174  {
175  NS_FATAL_ERROR ("when disconnecting from " << path);
176  }
177  Callback<void,Ts...> realCb = cb.Bind (path);
178  DisconnectWithoutContext (realCb);
179 }
180 template<typename... Ts>
181 void
183 {
184  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
185  i != m_callbackList.end (); i++)
186  {
187  (*i)(args...);
188  }
189 }
190 
191 } // namespace ns3
192 
193 #endif /* TRACED_CALLBACK_H */
std::list< Callback< void, Ts... > > CallbackList
Container type for holding the chain of Callbacks.
Callback template class.
Definition: callback.h:1278
void operator()(Ts... args) const
Functor which invokes the chain of Callbacks.
Forward calls to a chain of Callback.
Callback< R, T2, T3, T4, T5, T6, T7, T8, T9 > Bind(T a)
Bind the first arguments.
Definition: callback.h:1329
Base class for Callback class.
Definition: callback.h:1195
void DisconnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:912
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
TracedCallback()
Constructor.
void Connect(const CallbackBase &callback, std::string path)
Append a Callback to the chain with a context.
CallbackList m_callbackList
The chain of Callbacks.
Declaration of the various callback functions.
void DisconnectWithoutContext(const CallbackBase &callback)
Remove from the chain a Callback which was connected without a context.
void Disconnect(const CallbackBase &callback, std::string path)
Remove from the chain a Callback which was connected with a context.
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:899
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:918
#define list
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void(* Uint32Callback)(const uint32_t value)
TracedCallback signature for POD.
#define NS_FATAL_ERROR_NO_MSG()
Report a fatal error and terminate.
Definition: fatal-error.h:128
void Disconnect(std::string path, const CallbackBase &cb)
Definition: config.cc:933
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).