src/simulator/event.tcc
changeset 25 9b3bb088c560
parent 24 706b1d903da9
child 26 011c8d27b674
equal deleted inserted replaced
24:706b1d903da9 25:9b3bb088c560
     1 /* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
       
     2 /*
       
     3  * Copyright (c) 2005 INRIA
       
     4  * All rights reserved.
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License version 2 as
       
     8  * published by the Free Software Foundation;
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  *
       
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    20  */
       
    21 
       
    22 #ifndef EVENT_TCC
       
    23 #define EVENT_TCC
       
    24 
       
    25 #include "event.h"
       
    26 #include "event-impl.h"
       
    27 
       
    28 /**
       
    29  * ns3 namespace
       
    30  */
       
    31 namespace ns3 {
       
    32 
       
    33 /**
       
    34  * \defgroup make_event make_event
       
    35  *
       
    36  * Every make_event template function returns a newly-created Event
       
    37  * which holds a pointer to a special subclass of the EventImpl 
       
    38  * base class. Each of these subclasses holds information about which
       
    39  * function or method to call and which parameters must be forwarded
       
    40  * to this function or method.
       
    41  *
       
    42  * Sample code is shown below:
       
    43  * \include samples/main-event.cc
       
    44  */
       
    45 
       
    46 template<typename T>
       
    47 class EventMemberImpl0 : public EventImpl {
       
    48 public:
       
    49 	typedef void (T::*F)(void);
       
    50 
       
    51 	EventMemberImpl0 (T *obj, F function) 
       
    52 		: m_obj (obj), 
       
    53 		  m_function (function)
       
    54 	{}
       
    55 	virtual ~EventMemberImpl0 () {}
       
    56 private:
       
    57 	virtual void notify (void) { 
       
    58 		(m_obj->*m_function) (); 
       
    59 	}
       
    60 	T* m_obj;
       
    61 	F m_function;
       
    62 };
       
    63 
       
    64 template<typename T, typename T1>
       
    65 class EventMemberImpl1 : public EventImpl {
       
    66 public:
       
    67 	typedef void (T::*F)(T1);
       
    68 
       
    69 	EventMemberImpl1 (T *obj, F function, T1 a1) 
       
    70 		: m_obj (obj), 
       
    71 		  m_function (function),
       
    72 		  m_a1 (a1)
       
    73 	{ }
       
    74 	virtual ~EventMemberImpl1 () {}
       
    75 private:
       
    76 	virtual void notify (void) { 
       
    77 		(m_obj->*m_function) (m_a1);
       
    78 	}
       
    79 	T* m_obj;
       
    80 	F m_function;
       
    81 	T1 m_a1;
       
    82 };
       
    83 
       
    84 template<typename T, typename T1, typename T2>
       
    85 class EventMemberImpl2 : public EventImpl {
       
    86 public:
       
    87 	typedef void (T::*F)(T1, T2);
       
    88 
       
    89 	EventMemberImpl2 (T *obj, F function, T1 a1, T2 a2) 
       
    90 		: m_obj (obj), 
       
    91 		  m_function (function),
       
    92 		  m_a1 (a1),
       
    93 		  m_a2 (a2)
       
    94 	{ }
       
    95 	virtual ~EventMemberImpl2 () {}
       
    96 private:
       
    97 	virtual void notify (void) { 
       
    98 		(m_obj->*m_function) (m_a1, m_a2);
       
    99 	}
       
   100 	T* m_obj;
       
   101 	F m_function;
       
   102 	T1 m_a1;
       
   103 	T2 m_a2;
       
   104 };
       
   105 
       
   106 template<typename T, typename T1, typename T2, typename T3>
       
   107 class EventMemberImpl3 : public EventImpl {
       
   108 public:
       
   109 	typedef void (T::*F)(T1, T2, T3);
       
   110 
       
   111 	EventMemberImpl3 (T *obj, F function, T1 a1, T2 a2, T3 a3) 
       
   112 		: m_obj (obj), 
       
   113 		  m_function (function),
       
   114 		  m_a1 (a1),
       
   115 		  m_a2 (a2),
       
   116 		  m_a3 (a3)
       
   117 	{ }
       
   118 	virtual ~EventMemberImpl3 () {}
       
   119 private:
       
   120 	virtual void notify (void) { 
       
   121 		(m_obj->*m_function) (m_a1, m_a2, m_a3);
       
   122 	}
       
   123 	T* m_obj;
       
   124 	F m_function;
       
   125 	T1 m_a1;
       
   126 	T2 m_a2;
       
   127 	T3 m_a3;
       
   128 };
       
   129 
       
   130 template<typename T, typename T1, typename T2, typename T3, typename T4>
       
   131 class EventMemberImpl4 : public EventImpl {
       
   132 public:
       
   133 	typedef void (T::*F)(T1, T2, T3, T4);
       
   134 
       
   135 	EventMemberImpl4 (T *obj, F function, T1 a1, T2 a2, T3 a3, T4 a4) 
       
   136 		: m_obj (obj), 
       
   137 		  m_function (function),
       
   138 		  m_a1 (a1),
       
   139 		  m_a2 (a2),
       
   140 		  m_a3 (a3),
       
   141 		  m_a4 (a4)
       
   142 	{ }
       
   143 	virtual ~EventMemberImpl4 () {}
       
   144 private:
       
   145 	virtual void notify (void) { 
       
   146 		(m_obj->*m_function) (m_a1, m_a2, m_a3, m_a4);
       
   147 	}
       
   148 	T* m_obj;
       
   149 	F m_function;
       
   150 	T1 m_a1;
       
   151 	T2 m_a2;
       
   152 	T3 m_a3;
       
   153 	T4 m_a4;
       
   154 };
       
   155 
       
   156 template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
       
   157 class EventMemberImpl5 : public EventImpl {
       
   158 public:
       
   159 	typedef void (T::*F)(T1, T2, T3, T4, T5);
       
   160 
       
   161 	EventMemberImpl5 (T *obj, F function, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) 
       
   162 		: m_obj (obj), 
       
   163 		  m_function (function),
       
   164 		  m_a1 (a1),
       
   165 		  m_a2 (a2),
       
   166 		  m_a3 (a3),
       
   167 		  m_a4 (a4),
       
   168 		  m_a5 (a5)
       
   169 	{ }
       
   170 	virtual ~EventMemberImpl5 () {}
       
   171 private:
       
   172 	virtual void notify (void) { 
       
   173 		(m_obj->*m_function) (m_a1, m_a2, m_a3, m_a4, m_a5);
       
   174 	}
       
   175 	T* m_obj;
       
   176 	F m_function;
       
   177 	T1 m_a1;
       
   178 	T2 m_a2;
       
   179 	T3 m_a3;
       
   180 	T4 m_a4;
       
   181 	T5 m_a5;
       
   182 };
       
   183 
       
   184 /**
       
   185  * \ingroup make_event
       
   186  * \param f class method member pointer
       
   187  * \param t class instance
       
   188  * \return a wrapper Event
       
   189  * Build Events for class method members which take no arguments.
       
   190  */
       
   191 template<typename T>
       
   192 Event make_event(void (T::*f) (void), T* t) {
       
   193 	return Event (new EventMemberImpl0<T>(t, f));
       
   194 }
       
   195 /**
       
   196  * \ingroup make_event
       
   197  * \param f class method member pointer
       
   198  * \param t class instance
       
   199  * \param a1 first argument to pass to the target method when the event expires
       
   200  * \return a wrapper Event
       
   201  * Build Events for class method members which take only one argument
       
   202  */
       
   203 template<typename T, typename T1>
       
   204 Event make_event(void (T::*f) (T1), T* t, T1 a1) {
       
   205 	return Event (new EventMemberImpl1<T, T1>(t, f, a1));
       
   206 }
       
   207 /**
       
   208  * \ingroup make_event
       
   209  * \param f class method member pointer
       
   210  * \param t class instance
       
   211  * \param a1 first argument to pass to the target method when the event expires
       
   212  * \param a2 second argument to pass to the target method when the event expires
       
   213  * \return a wrapper Event
       
   214  * Build Events for class method members which take two arguments
       
   215  */
       
   216 template<typename T, typename T1, typename T2>
       
   217 Event make_event(void (T::*f) (T1, T2), T* t, T1 a1, T2 a2) {
       
   218 	return Event (new EventMemberImpl2<T, T1, T2>(t, f, a1, a2));
       
   219 }
       
   220 /**
       
   221  * \ingroup make_event
       
   222  * \param f class method member pointer
       
   223  * \param t class instance
       
   224  * \param a1 first argument to pass to the target method when the event expires
       
   225  * \param a2 second argument to pass to the target method when the event expires
       
   226  * \param a3 third argument to pass to the target method when the event expires
       
   227  * \return a wrapper Event
       
   228  * Build Events for class method members which take three arguments
       
   229  */
       
   230 template<typename T, typename T1, typename T2, typename T3>
       
   231 Event make_event(void (T::*f) (T1, T2, T3), T* t, T1 a1, T2 a2, T3 a3) {
       
   232 	return Event (new EventMemberImpl3<T, T1, T2, T3>(t, f, a1, a2, a3));
       
   233 }
       
   234 /**
       
   235  * \ingroup make_event
       
   236  * \param f class method member pointer
       
   237  * \param t class instance
       
   238  * \param a1 first argument to pass to the target method when the event expires
       
   239  * \param a2 second argument to pass to the target method when the event expires
       
   240  * \param a3 third argument to pass to the target method when the event expires
       
   241  * \param a4 fourth argument to pass to the target method when the event expires
       
   242  * \return a wrapper Event
       
   243  * Build Events for class method members which take four arguments
       
   244  */
       
   245 template<typename T, typename T1, typename T2, typename T3, typename T4>
       
   246 Event make_event(void (T::*f) (T1, T2, T3, T4), T* t, T1 a1, T2 a2, T3 a3, T4 a4) {
       
   247 	return Event (new EventMemberImpl4<T, T1, T2, T3, T4>(t, f, a1, a2, a3, a4));
       
   248 }
       
   249 /**
       
   250  * \ingroup make_event
       
   251  * \param f class method member pointer
       
   252  * \param t class instance
       
   253  * \param a1 first argument to pass to the target method when the event expires
       
   254  * \param a2 second argument to pass to the target method when the event expires
       
   255  * \param a3 third argument to pass to the target method when the event expires
       
   256  * \param a4 fourth argument to pass to the target method when the event expires
       
   257  * \param a5 fifth argument to pass to the target method when the event expires
       
   258  * \return a wrapper Event
       
   259  * Build Events for class method members which take five arguments.
       
   260  */
       
   261 template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
       
   262 Event make_event(void (T::*f) (T1, T2, T3, T4, T5), T* t, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) {
       
   263 	return Event (new EventMemberImpl5<T, T1, T2, T3, T4, T5>(t, f, a1, a2, a3, a4, a5));
       
   264 }
       
   265 
       
   266 template<typename T1>
       
   267 class EventFunctionImpl1 : public EventImpl {
       
   268 public:
       
   269 	typedef void (*F)(T1);
       
   270 
       
   271 	EventFunctionImpl1 (F function, T1 a1) 
       
   272 		: m_function (function),
       
   273 		  m_a1 (a1)
       
   274 	{ }
       
   275 	virtual ~EventFunctionImpl1 () {}
       
   276 private:
       
   277 	virtual void notify (void) { 
       
   278 		(*m_function) (m_a1);
       
   279 	}
       
   280 	F m_function;
       
   281 	T1 m_a1;
       
   282 };
       
   283 
       
   284 template<typename T1, typename T2>
       
   285 class EventFunctionImpl2 : public EventImpl {
       
   286 public:
       
   287 	typedef void (*F)(T1, T2);
       
   288 
       
   289 	EventFunctionImpl2 (F function, T1 a1, T2 a2) 
       
   290 		: m_function (function),
       
   291 		  m_a1 (a1),
       
   292 		  m_a2 (a2)
       
   293 	{ }
       
   294 	virtual ~EventFunctionImpl2 () {}
       
   295 private:
       
   296 	virtual void notify (void) { 
       
   297 		(*m_function) (m_a1, m_a2);
       
   298 	}
       
   299 	F m_function;
       
   300 	T1 m_a1;
       
   301 	T2 m_a2;
       
   302 };
       
   303 
       
   304 template<typename T1, typename T2, typename T3>
       
   305 class EventFunctionImpl3 : public EventImpl {
       
   306 public:
       
   307 	typedef void (*F)(T1, T2, T3);
       
   308 
       
   309 	EventFunctionImpl3 (F function, T1 a1, T2 a2, T3 a3) 
       
   310 		: m_function (function),
       
   311 		  m_a1 (a1),
       
   312 		  m_a2 (a2),
       
   313 		  m_a3 (a3)
       
   314 	{ }
       
   315 	virtual ~EventFunctionImpl3 () {}
       
   316 private:
       
   317 	virtual void notify (void) { 
       
   318 		(*m_function) (m_a1, m_a2, m_a3);
       
   319 	}
       
   320 	F m_function;
       
   321 	T1 m_a1;
       
   322 	T2 m_a2;
       
   323 	T3 m_a3;
       
   324 };
       
   325 
       
   326 template<typename T1, typename T2, typename T3, typename T4>
       
   327 class EventFunctionImpl4 : public EventImpl {
       
   328 public:
       
   329 	typedef void (*F)(T1, T2, T3, T4);
       
   330 
       
   331 	EventFunctionImpl4 (F function, T1 a1, T2 a2, T3 a3, T4 a4) 
       
   332 		: m_function (function),
       
   333 		  m_a1 (a1),
       
   334 		  m_a2 (a2),
       
   335 		  m_a3 (a3),
       
   336 		  m_a4 (a4)
       
   337 	{ }
       
   338 	virtual ~EventFunctionImpl4 () {}
       
   339 private:
       
   340 	virtual void notify (void) { 
       
   341 		(*m_function) (m_a1, m_a2, m_a3, m_a4);
       
   342 	}
       
   343 	F m_function;
       
   344 	T1 m_a1;
       
   345 	T2 m_a2;
       
   346 	T3 m_a3;
       
   347 	T4 m_a4;
       
   348 };
       
   349 
       
   350 template<typename T1, typename T2, typename T3, typename T4, typename T5>
       
   351 class EventFunctionImpl5 : public EventImpl {
       
   352 public:
       
   353 	typedef void (*F)(T1, T2, T3, T4, T5);
       
   354 
       
   355 	EventFunctionImpl5 (F function, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) 
       
   356 		: m_function (function),
       
   357 		  m_a1 (a1),
       
   358 		  m_a2 (a2),
       
   359 		  m_a3 (a3),
       
   360 		  m_a4 (a4),
       
   361 		  m_a5 (a5)
       
   362 	{ }
       
   363 	virtual ~EventFunctionImpl5 () {}
       
   364 private:
       
   365 	virtual void notify (void) { 
       
   366 		(*m_function) (m_a1, m_a2, m_a3, m_a4, m_a5);
       
   367 	}
       
   368 	F m_function;
       
   369 	T1 m_a1;
       
   370 	T2 m_a2;
       
   371 	T3 m_a3;
       
   372 	T4 m_a4;
       
   373 	T5 m_a5;
       
   374 };
       
   375 
       
   376 
       
   377 /**
       
   378  * \ingroup make_event
       
   379  * \param f function pointer
       
   380  * \return a wrapper Event
       
   381  * Build Events for function pointers which take no arguments
       
   382  */
       
   383 Event make_event(void (*f) (void));
       
   384 
       
   385 /**
       
   386  * \ingroup make_event
       
   387  * \param f function pointer
       
   388  * \param a1 first argument to pass to the target function when the event expires
       
   389  * \return a wrapper Event
       
   390  * Build Events for function pointers which take one argument
       
   391  */
       
   392 template<typename T1>
       
   393 Event make_event(void (*f) (T1), T1 a1) {
       
   394 	return Event (new EventFunctionImpl1<T1>(f, a1));
       
   395 }
       
   396 /**
       
   397  * \ingroup make_event
       
   398  * \param f function pointer
       
   399  * \param a1 first argument to pass to the target function when the event expires
       
   400  * \param a2 second argument to pass to the target function when the event expires
       
   401  * \return a wrapper Event
       
   402  * Build Events for function pointers which take two argument
       
   403  */
       
   404 template<typename T1, typename T2>
       
   405 Event make_event(void (*f) (T1, T2), T1 a1, T2 a2) {
       
   406 	return Event (new EventFunctionImpl2<T1, T2>(f, a1, a2));
       
   407 }
       
   408 /**
       
   409  * \ingroup make_event
       
   410  * \param f function pointer
       
   411  * \param a1 first argument to pass to the target function when the event expires
       
   412  * \param a2 second argument to pass to the target function when the event expires
       
   413  * \param a3 third argument to pass to the target function when the event expires
       
   414  * \return a wrapper Event
       
   415  * Build Events for function pointers which take three argument
       
   416  */
       
   417 template<typename T1, typename T2, typename T3>
       
   418 Event make_event(void (*f) (T1, T2, T3), T1 a1, T2 a2, T3 a3) {
       
   419 	return Event (new EventFunctionImpl3<T1, T2, T3>(f, a1, a2, a3));
       
   420 }
       
   421 /**
       
   422  * \ingroup make_event
       
   423  * \param f function pointer
       
   424  * \param a1 first argument to pass to the target function when the event expires
       
   425  * \param a2 second argument to pass to the target function when the event expires
       
   426  * \param a3 third argument to pass to the target function when the event expires
       
   427  * \param a4 fourth argument to pass to the target function when the event expires
       
   428  * \return a wrapper Event
       
   429  * Build Events for function pointers which take four argument
       
   430  */
       
   431 template<typename T1, typename T2, typename T3, typename T4>
       
   432 Event make_event(void (*f) (T1, T2, T3, T4), T1 a1, T2 a2, T3 a3, T4 a4) {
       
   433 	return Event (new EventFunctionImpl4<T1, T2, T3, T4>(f, a1, a2, a3, a4));
       
   434 }
       
   435 /**
       
   436  * \ingroup make_event
       
   437  * \param f function pointer
       
   438  * \param a1 first argument to pass to the target function when the event expires
       
   439  * \param a2 second argument to pass to the target function when the event expires
       
   440  * \param a3 third argument to pass to the target function when the event expires
       
   441  * \param a4 fourth argument to pass to the target function when the event expires
       
   442  * \param a5 fifth argument to pass to the target function when the event expires
       
   443  * \return a wrapper Event
       
   444  * Build Events for function pointers which take five argument
       
   445  */
       
   446 template<typename T1, typename T2, typename T3, typename T4, typename T5>
       
   447 Event make_event(void (*f) (T1, T2, T3, T4, T5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) {
       
   448 	return Event (new EventFunctionImpl5<T1, T2, T3, T4, T5>(f, a1, a2, a3, a4, a5));
       
   449 }
       
   450 
       
   451 
       
   452 }; // namespace ns3
       
   453 
       
   454 #endif /* EVENT_TCC */