src/core/callback.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 4072 5d226c752e85
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
mathieu@150
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@9
     2
/*
mathieu@9
     3
 * Copyright (c) 2005,2006 INRIA
mathieu@9
     4
 *
mathieu@9
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@9
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@9
     7
 * published by the Free Software Foundation;
mathieu@9
     8
 *
mathieu@9
     9
 * This program is distributed in the hope that it will be useful,
mathieu@9
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@9
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@9
    12
 * GNU General Public License for more details.
mathieu@9
    13
 *
mathieu@9
    14
 * You should have received a copy of the GNU General Public License
mathieu@9
    15
 * along with this program; if not, write to the Free Software
mathieu@9
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@9
    17
 *
mathieu@9
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@9
    19
 */
mathieu@9
    20
mathieu@9
    21
#ifndef CALLBACK_H
mathieu@9
    22
#define CALLBACK_H
mathieu@9
    23
mathieu@687
    24
#include "ptr.h"
mathieu@528
    25
#include "fatal-error.h"
mathieu@714
    26
#include "empty.h"
mathieu@1418
    27
#include "type-traits.h"
mathieu@3763
    28
#include "attribute.h"
mathieu@3763
    29
#include "attribute-helper.h"
mathieu@3365
    30
#include <typeinfo>
mathieu@9
    31
mathieu@16
    32
namespace ns3 {
mathieu@9
    33
mathieu@9
    34
/***
mathieu@9
    35
 * \internal
mathieu@9
    36
 * This code was originally written based on the techniques 
mathieu@9
    37
 * described in http://www.codeproject.com/cpp/TTLFunction.asp
mathieu@9
    38
 * It was subsequently rewritten to follow the architecture
mathieu@9
    39
 * outlined in "Modern C++ Design" by Andrei Alexandrescu in 
mathieu@9
    40
 * chapter 5, "Generalized Functors".
mathieu@9
    41
 *
mathieu@9
    42
 * This code uses:
mathieu@9
    43
 *   - default template parameters to saves users from having to
mathieu@9
    44
 *     specify empty parameters when the number of parameters
mathieu@9
    45
 *     is smaller than the maximum supported number
mathieu@9
    46
 *   - the pimpl idiom: the Callback class is passed around by 
mathieu@9
    47
 *     value and delegates the crux of the work to its pimpl
mathieu@9
    48
 *     pointer.
mathieu@9
    49
 *   - two pimpl implementations which derive from CallbackImpl
mathieu@9
    50
 *     FunctorCallbackImpl can be used with any functor-type
mathieu@9
    51
 *     while MemPtrCallbackImpl can be used with pointers to
mathieu@9
    52
 *     member functions.
mathieu@9
    53
 *   - a reference list implementation to implement the Callback's
mathieu@9
    54
 *     value semantics.
mathieu@9
    55
 *
mathieu@9
    56
 * This code most notably departs from the alexandrescu 
mathieu@9
    57
 * implementation in that it does not use type lists to specify
mathieu@9
    58
 * and pass around the types of the callback arguments.
mathieu@9
    59
 * Of course, it also does not use copy-destruction semantics
mathieu@53
    60
 * and relies on a reference list rather than autoPtr to hold
mathieu@9
    61
 * the pointer.
mathieu@9
    62
 */
mathieu@685
    63
template <typename T>
mathieu@685
    64
struct CallbackTraits;
mathieu@685
    65
mathieu@685
    66
template <typename T>
mathieu@685
    67
struct CallbackTraits<T *>
mathieu@685
    68
{
mathieu@685
    69
  static T & GetReference (T * const p)
mathieu@685
    70
  {
mathieu@685
    71
    return *p;
mathieu@685
    72
  }
mathieu@685
    73
};
mathieu@685
    74
mathieu@2630
    75
class CallbackImplBase
mathieu@2370
    76
{
mathieu@219
    77
public:
mathieu@687
    78
  CallbackImplBase ()
mathieu@687
    79
    : m_count (1) {}
mathieu@219
    80
  virtual ~CallbackImplBase () {}
mathieu@2206
    81
  void Ref (void) const {
mathieu@687
    82
    m_count++;
mathieu@687
    83
  }
mathieu@2206
    84
  void Unref (void) const {
mathieu@687
    85
    m_count--;
mathieu@687
    86
    if (m_count == 0) {
mathieu@687
    87
      delete this;
mathieu@687
    88
    }
mathieu@687
    89
  }
mathieu@3391
    90
  uint32_t GetReferenceCount (void) const { return m_count; }
mathieu@2206
    91
  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const = 0;
mathieu@687
    92
private:
mathieu@2206
    93
  mutable uint32_t m_count;
mathieu@219
    94
};
mathieu@219
    95
mathieu@9
    96
// declare the CallbackImpl class
Providence@4072
    97
template <typename R, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
mathieu@9
    98
class CallbackImpl;
mathieu@9
    99
// define CallbackImpl for 0 params
mathieu@9
   100
template <typename R>
Providence@4072
   101
class CallbackImpl<R,empty,empty,empty,empty,empty,empty,empty,empty,empty> : public CallbackImplBase {
mathieu@9
   102
public:
mathieu@150
   103
  virtual ~CallbackImpl () {}
mathieu@150
   104
  virtual R operator() (void) = 0;
mathieu@9
   105
};
mathieu@9
   106
// define CallbackImpl for 1 params
mathieu@9
   107
template <typename R, typename T1>
Providence@4072
   108
class CallbackImpl<R,T1,empty,empty,empty,empty,empty,empty,empty,empty> : public CallbackImplBase {
mathieu@9
   109
public:
mathieu@150
   110
  virtual ~CallbackImpl () {}
mathieu@150
   111
  virtual R operator() (T1) = 0;
mathieu@9
   112
};
mathieu@9
   113
// define CallbackImpl for 2 params
mathieu@9
   114
template <typename R, typename T1, typename T2>
Providence@4072
   115
class CallbackImpl<R,T1,T2,empty,empty,empty,empty,empty,empty,empty> : public CallbackImplBase {
mathieu@9
   116
public:
mathieu@150
   117
  virtual ~CallbackImpl () {}
mathieu@150
   118
  virtual R operator() (T1, T2) = 0;
mathieu@9
   119
};
mathieu@9
   120
// define CallbackImpl for 3 params
mathieu@9
   121
template <typename R, typename T1, typename T2, typename T3>
Providence@4072
   122
class CallbackImpl<R,T1,T2,T3,empty,empty,empty,empty,empty,empty> : public CallbackImplBase {
mathieu@9
   123
public:
mathieu@150
   124
  virtual ~CallbackImpl () {}
mathieu@150
   125
  virtual R operator() (T1, T2, T3) = 0;
mathieu@9
   126
};
mathieu@9
   127
// define CallbackImpl for 4 params
mathieu@9
   128
template <typename R, typename T1, typename T2, typename T3, typename T4>
Providence@4072
   129
class CallbackImpl<R,T1,T2,T3,T4,empty,empty,empty,empty,empty> : public CallbackImplBase {
mathieu@9
   130
public:
mathieu@150
   131
  virtual ~CallbackImpl () {}
mathieu@150
   132
  virtual R operator() (T1, T2, T3, T4) = 0;
mathieu@9
   133
};
mathieu@9
   134
// define CallbackImpl for 5 params
mathieu@9
   135
template <typename R, typename T1, typename T2, typename T3, typename T4, typename T5>
Providence@4072
   136
class CallbackImpl<R,T1,T2,T3,T4,T5,empty,empty,empty,empty> : public CallbackImplBase {
mathieu@9
   137
public:
mathieu@150
   138
  virtual ~CallbackImpl () {}
mathieu@150
   139
  virtual R operator() (T1, T2, T3, T4, T5) = 0;
mathieu@9
   140
};
mathieu@1417
   141
// define CallbackImpl for 6 params
Providence@4072
   142
template <typename R, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
Providence@4072
   143
class CallbackImpl<R,T1,T2,T3,T4,T5,T6,empty,empty,empty> : public CallbackImplBase {
mathieu@1417
   144
public:
mathieu@1417
   145
  virtual ~CallbackImpl () {}
mathieu@1417
   146
  virtual R operator() (T1, T2, T3, T4, T5, T6) = 0;
mathieu@1417
   147
};
Providence@4072
   148
// define CallbackImpl for 7 params
Providence@4072
   149
template <typename R, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
Providence@4072
   150
class CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,empty,empty> : public CallbackImplBase {
Providence@4072
   151
public:
Providence@4072
   152
  virtual ~CallbackImpl () {}
Providence@4072
   153
  virtual R operator() (T1, T2, T3, T4, T5, T6, T7) = 0;
Providence@4072
   154
};
Providence@4072
   155
// define CallbackImpl for 8 params
Providence@4072
   156
template <typename R, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
Providence@4072
   157
class CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,T8,empty> : public CallbackImplBase {
Providence@4072
   158
public:
Providence@4072
   159
  virtual ~CallbackImpl () {}
Providence@4072
   160
  virtual R operator() (T1, T2, T3, T4, T5, T6, T7, T8) = 0;
Providence@4072
   161
};
Providence@4072
   162
// define CallbackImpl for 9 params
Providence@4072
   163
template <typename R, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
Providence@4072
   164
class CallbackImpl : public CallbackImplBase {
Providence@4072
   165
public:
Providence@4072
   166
  virtual ~CallbackImpl () {}
Providence@4072
   167
  virtual R operator() (T1, T2, T3, T4, T5, T6, T7, T8, T9) = 0;
Providence@4072
   168
};
mathieu@9
   169
mathieu@9
   170
mathieu@9
   171
// an impl for Functors:
Providence@4072
   172
template <typename T, typename R, typename T1, typename T2, typename T3, typename T4,typename T5, typename T6, typename T7, typename T8, typename T9>
Providence@4072
   173
class FunctorCallbackImpl : public CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> {
mathieu@9
   174
public:
mathieu@150
   175
  FunctorCallbackImpl (T const &functor)
mathieu@687
   176
    : m_functor (functor) {}
mathieu@150
   177
  virtual ~FunctorCallbackImpl () {}
mathieu@150
   178
  R operator() (void) {
Providence@4072
   179
    return m_functor ();
mathieu@150
   180
  }
mathieu@150
   181
  R operator() (T1 a1) {
Providence@4072
   182
    return m_functor (a1);
mathieu@150
   183
  }
mathieu@150
   184
  R operator() (T1 a1,T2 a2) {
Providence@4072
   185
    return m_functor (a1,a2);
mathieu@150
   186
  }
mathieu@150
   187
  R operator() (T1 a1,T2 a2,T3 a3) {
Providence@4072
   188
    return m_functor (a1,a2,a3);
mathieu@150
   189
  }
mathieu@150
   190
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4) {
Providence@4072
   191
    return m_functor (a1,a2,a3,a4);
mathieu@150
   192
  }
mathieu@150
   193
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5) {
Providence@4072
   194
    return m_functor (a1,a2,a3,a4,a5);
mathieu@150
   195
  }
mathieu@1417
   196
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6) {
mathieu@1417
   197
    return m_functor (a1,a2,a3,a4,a5,a6);
mathieu@1417
   198
  }
Providence@4072
   199
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7) {
Providence@4072
   200
    return m_functor (a1,a2,a3,a4,a5,a6,a7);
Providence@4072
   201
  }
Providence@4072
   202
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8) {
Providence@4072
   203
    return m_functor (a1,a2,a3,a4,a5,a6,a7,a8);
Providence@4072
   204
  }
Providence@4072
   205
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8,T9 a9) {
Providence@4072
   206
    return m_functor (a1,a2,a3,a4,a5,a6,a7,a8,a9);
Providence@4072
   207
  }
mathieu@2206
   208
  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const {
Providence@4072
   209
    FunctorCallbackImpl<T,R,T1,T2,T3,T4,T5,T6,T7,T8,T9> const *otherDerived = 
Providence@4072
   210
      dynamic_cast<FunctorCallbackImpl<T,R,T1,T2,T3,T4,T5,T6,T7,T8,T9> const *> (PeekPointer(other));
tomh@345
   211
    if (otherDerived == 0)
tomh@345
   212
      {
tomh@345
   213
        return false;
tomh@345
   214
      }
tomh@345
   215
    else if (otherDerived->m_functor != m_functor)
tomh@345
   216
      {
tomh@345
   217
        return false;
tomh@345
   218
      }
tomh@345
   219
    return true;
tomh@345
   220
  }
mathieu@9
   221
private:
mathieu@150
   222
  T m_functor;
mathieu@9
   223
};
mathieu@9
   224
mathieu@9
   225
// an impl for pointer to member functions
Providence@4072
   226
template <typename OBJ_PTR, typename MEM_PTR, typename R, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
Providence@4072
   227
class MemPtrCallbackImpl : public CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> {
mathieu@9
   228
public:
mathieu@150
   229
  MemPtrCallbackImpl (OBJ_PTR const&objPtr, MEM_PTR mem_ptr)
mathieu@687
   230
    : m_objPtr (objPtr), m_memPtr (mem_ptr) {}
mathieu@150
   231
  virtual ~MemPtrCallbackImpl () {}
mathieu@150
   232
  R operator() (void) {
mathieu@685
   233
    return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) ();
mathieu@150
   234
  }
mathieu@150
   235
  R operator() (T1 a1) {
mathieu@685
   236
    return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) (a1);
mathieu@150
   237
  }
mathieu@150
   238
  R operator() (T1 a1,T2 a2) {
mathieu@685
   239
    return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) (a1, a2);
mathieu@150
   240
  }
mathieu@150
   241
  R operator() (T1 a1,T2 a2,T3 a3) {
mathieu@685
   242
    return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) (a1, a2, a3);
mathieu@150
   243
  }
mathieu@150
   244
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4) {
mathieu@685
   245
    return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) (a1, a2, a3, a4);
mathieu@150
   246
  }
mathieu@150
   247
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5) {
mathieu@685
   248
    return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) (a1, a2, a3, a4, a5);
mathieu@150
   249
  }
mathieu@1417
   250
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6) {
mathieu@1417
   251
    return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) (a1, a2, a3, a4, a5, a6);
mathieu@1417
   252
  }
Providence@4072
   253
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7) {
Providence@4072
   254
    return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) (a1, a2, a3, a4, a5, a6, a7);
Providence@4072
   255
  }
Providence@4072
   256
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8) {
Providence@4072
   257
    return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) (a1, a2, a3, a4, a5, a6, a7, a8);
Providence@4072
   258
  }
Providence@4072
   259
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8, T9 a9) {
Providence@4072
   260
    return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) (a1, a2, a3, a4, a5, a6, a7, a8, a9);
Providence@4072
   261
  }
mathieu@2206
   262
  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const {
Providence@4072
   263
    MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6,T7,T8,T9> const *otherDerived = 
Providence@4072
   264
      dynamic_cast<MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6,T7,T8,T9> const *> (PeekPointer (other));
tomh@345
   265
    if (otherDerived == 0)
tomh@345
   266
      {
tomh@345
   267
        return false;
tomh@345
   268
      }
tomh@345
   269
    else if (otherDerived->m_objPtr != m_objPtr ||
tomh@345
   270
             otherDerived->m_memPtr != m_memPtr)
tomh@345
   271
      {
tomh@345
   272
        return false;
tomh@345
   273
      }
tomh@345
   274
    return true;
tomh@345
   275
  }
mathieu@9
   276
private:
mathieu@150
   277
  OBJ_PTR const m_objPtr;
mathieu@150
   278
  MEM_PTR m_memPtr;
mathieu@9
   279
};
mathieu@9
   280
mathieu@2531
   281
// an impl for Bound Functors:
Providence@4072
   282
template <typename T, typename R, typename TX, typename T1, typename T2, typename T3, typename T4,typename T5, typename T6, typename T7, typename T8>
Providence@4072
   283
class BoundFunctorCallbackImpl : public CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,T8,empty> {
mathieu@2531
   284
public:
mathieu@2531
   285
  template <typename FUNCTOR, typename ARG>
mathieu@2531
   286
  BoundFunctorCallbackImpl (FUNCTOR functor, ARG a)
Providence@4072
   287
    : m_functor (functor), m_a (a) {}
mathieu@2531
   288
  virtual ~BoundFunctorCallbackImpl () {}
mathieu@2531
   289
  R operator() (void) {
Providence@4072
   290
    return m_functor (m_a);
mathieu@2531
   291
  }
mathieu@2531
   292
  R operator() (T1 a1) {
Providence@4072
   293
    return m_functor (m_a,a1);
mathieu@2531
   294
  }
mathieu@2531
   295
  R operator() (T1 a1,T2 a2) {
Providence@4072
   296
    return m_functor (m_a,a1,a2);
mathieu@2531
   297
  }
mathieu@2531
   298
  R operator() (T1 a1,T2 a2,T3 a3) {
Providence@4072
   299
    return m_functor (m_a,a1,a2,a3);
mathieu@2531
   300
  }
mathieu@2531
   301
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4) {
Providence@4072
   302
    return m_functor (m_a,a1,a2,a3,a4);
mathieu@2531
   303
  }
mathieu@2531
   304
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5) {
Providence@4072
   305
    return m_functor (m_a,a1,a2,a3,a4,a5);
Providence@4072
   306
  }
Providence@4072
   307
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6) {
Providence@4072
   308
    return m_functor (m_a,a1,a2,a3,a4,a5,a6);
Providence@4072
   309
  }
Providence@4072
   310
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7) {
Providence@4072
   311
    return m_functor (m_a,a1,a2,a3,a4,a5,a6,a7);
Providence@4072
   312
  }
Providence@4072
   313
  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8) {
Providence@4072
   314
    return m_functor (m_a,a1,a2,a3,a4,a5,a6,a7,a8);
mathieu@2531
   315
  }
mathieu@2531
   316
  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const {
Providence@4072
   317
    BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5,T6,T7,T8> const *otherDerived = 
Providence@4072
   318
      dynamic_cast<BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5,T6,T7,T8> const *> (PeekPointer (other));
mathieu@2531
   319
    if (otherDerived == 0)
mathieu@2531
   320
      {
mathieu@2531
   321
        return false;
mathieu@2531
   322
      }
mathieu@2531
   323
    else if (otherDerived->m_functor != m_functor ||
mathieu@2531
   324
             otherDerived->m_a != m_a)
mathieu@2531
   325
      {
mathieu@2531
   326
        return false;
mathieu@2531
   327
      }
mathieu@2531
   328
    return true;
mathieu@2531
   329
  }
mathieu@2531
   330
private:
mathieu@2531
   331
  T m_functor;
mathieu@2531
   332
  typename TypeTraits<TX>::ReferencedType m_a;
mathieu@2531
   333
};
mathieu@2531
   334
mathieu@2531
   335
mathieu@219
   336
class CallbackBase {
mathieu@219
   337
public:
mathieu@2206
   338
  CallbackBase () : m_impl () {}
mathieu@2206
   339
  Ptr<CallbackImplBase> GetImpl (void) const {return m_impl;}
mathieu@2206
   340
protected:
mathieu@2206
   341
  CallbackBase (Ptr<CallbackImplBase> impl) : m_impl (impl) {}
mathieu@2206
   342
  Ptr<CallbackImplBase> m_impl;
timo@4205
   343
timo@4205
   344
  static std::string Demangle(const std::string& mangled);
mathieu@219
   345
};
mathieu@219
   346
mathieu@9
   347
/**
mathieu@9
   348
 * \brief Callback template class
mathieu@9
   349
 *
mathieu@9
   350
 * This class template implements the Functor Design Pattern.
mathieu@9
   351
 * It is used to declare the type of a Callback:
mathieu@9
   352
 *  - the first non-optional template argument represents
mathieu@9
   353
 *    the return type of the callback.
mathieu@9
   354
 *  - the second optional template argument represents
mathieu@9
   355
 *    the type of the first argument to the callback.
mathieu@9
   356
 *  - the third optional template argument represents
mathieu@9
   357
 *    the type of the second argument to the callback.
mathieu@9
   358
 *  - the fourth optional template argument represents
mathieu@9
   359
 *    the type of the third argument to the callback.
mathieu@9
   360
 *  - the fifth optional template argument represents
mathieu@9
   361
 *    the type of the fourth argument to the callback.
mathieu@9
   362
 *  - the sixth optional template argument represents
mathieu@9
   363
 *    the type of the fifth argument to the callback.
mathieu@9
   364
 *
mathieu@122
   365
 * Callback instances are built with the \ref MakeCallback
mathieu@9
   366
 * template functions. Callback instances have POD semantics:
mathieu@9
   367
 * the memory they allocate is managed automatically, without
mathieu@9
   368
 * user intervention which allows you to pass around Callback
mathieu@9
   369
 * instances by value.
mathieu@9
   370
 *
mathieu@9
   371
 * Sample code which shows how to use this class template 
mathieu@122
   372
 * as well as the function templates \ref MakeCallback :
mathieu@9
   373
 * \include samples/main-callback.cc
mathieu@9
   374
 */
tomh@345
   375
mathieu@9
   376
template<typename R, 
Providence@4072
   377
         typename T1 = empty, typename T2 = empty, 
Providence@4072
   378
         typename T3 = empty, typename T4 = empty,
Providence@4072
   379
         typename T5 = empty, typename T6 = empty,
Providence@4072
   380
         typename T7 = empty, typename T8 = empty,
Providence@4072
   381
         typename T9 = empty>
mathieu@219
   382
class Callback : public CallbackBase {
mathieu@9
   383
public:
mathieu@2206
   384
  Callback () {}
mathieu@2206
   385
tomh@345
   386
  // There are two dummy args below to ensure that this constructor is
tomh@345
   387
  // always properly disambiguited by the c++ compiler
mathieu@150
   388
  template <typename FUNCTOR>
tomh@345
   389
  Callback (FUNCTOR const &functor, bool, bool) 
Providence@4072
   390
    : CallbackBase (Create<FunctorCallbackImpl<FUNCTOR,R,T1,T2,T3,T4,T5,T6,T7,T8,T9> > (functor))
mathieu@150
   391
  {}
mathieu@9
   392
mathieu@150
   393
  template <typename OBJ_PTR, typename MEM_PTR>
mathieu@150
   394
  Callback (OBJ_PTR const &objPtr, MEM_PTR mem_ptr)
Providence@4072
   395
    : CallbackBase (Create<MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6,T7,T8,T9> > (objPtr, mem_ptr))
mathieu@150
   396
  {}
mathieu@9
   397
Providence@4072
   398
  Callback (Ptr<CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> > const &impl)
mathieu@2206
   399
    : CallbackBase (impl)
mathieu@150
   400
  {}
mathieu@9
   401
mathieu@2531
   402
  template <typename T>
Providence@4072
   403
  Callback<R,T2,T3,T4,T5,T6,T7,T8,T9> Bind (T a) {
Providence@4072
   404
    Ptr<CallbackImpl<R,T2,T3,T4,T5,T6,T7,T8,T9,empty> > impl =
Providence@4072
   405
      Ptr<CallbackImpl<R,T2,T3,T4,T5,T6,T7,T8,T9,empty> > (
Providence@4072
   406
                                                  new BoundFunctorCallbackImpl<
Providence@4072
   407
                                                  Callback<R,T1,T2,T3,T4,T5,T6,T7,T8,T9>,
Providence@4072
   408
                                                  R,T1,T2,T3,T4,T5,T6,T7,T8,T9> (*this, a), false);
Providence@4072
   409
    return Callback<R,T2,T3,T4,T5,T6,T7,T8,T9> (impl);
mathieu@2531
   410
  }
mathieu@2531
   411
mathieu@879
   412
  bool IsNull (void) const {
mathieu@2206
   413
    return (DoPeekImpl () == 0)?true:false;
mathieu@150
   414
  }
mathieu@686
   415
  void Nullify (void) {
mathieu@686
   416
    m_impl = 0;
mathieu@686
   417
  }
mathieu@9
   418
tomh@345
   419
  R operator() (void) const {
mathieu@2206
   420
    return (*(DoPeekImpl ())) ();
mathieu@150
   421
  }
tomh@345
   422
  R operator() (T1 a1) const {
mathieu@2206
   423
    return (*(DoPeekImpl ())) (a1);
mathieu@150
   424
  }
tomh@345
   425
  R operator() (T1 a1, T2 a2) const {
mathieu@2206
   426
    return (*(DoPeekImpl ())) (a1,a2);
mathieu@150
   427
  }
tomh@345
   428
  R operator() (T1 a1, T2 a2, T3 a3) const {
mathieu@2206
   429
    return (*(DoPeekImpl ())) (a1,a2,a3);
mathieu@150
   430
  }
tomh@345
   431
  R operator() (T1 a1, T2 a2, T3 a3, T4 a4) const {
mathieu@2206
   432
    return (*(DoPeekImpl ())) (a1,a2,a3,a4);
mathieu@150
   433
  }
tomh@345
   434
  R operator() (T1 a1, T2 a2, T3 a3, T4 a4,T5 a5) const {
mathieu@2206
   435
    return (*(DoPeekImpl ())) (a1,a2,a3,a4,a5);
mathieu@150
   436
  }
mathieu@1417
   437
  R operator() (T1 a1, T2 a2, T3 a3, T4 a4,T5 a5,T6 a6) const {
mathieu@2206
   438
    return (*(DoPeekImpl ())) (a1,a2,a3,a4,a5,a6);
mathieu@1417
   439
  }
Providence@4072
   440
  R operator() (T1 a1, T2 a2, T3 a3, T4 a4,T5 a5,T6 a6,T7 a7) const {
Providence@4072
   441
    return (*(DoPeekImpl ())) (a1,a2,a3,a4,a5,a6,a7);
Providence@4072
   442
  }
Providence@4072
   443
  R operator() (T1 a1, T2 a2, T3 a3, T4 a4,T5 a5,T6 a6,T7 a7,T8 a8) const {
Providence@4072
   444
    return (*(DoPeekImpl ())) (a1,a2,a3,a4,a5,a6,a7,a8);
Providence@4072
   445
  }
Providence@4072
   446
  R operator() (T1 a1, T2 a2, T3 a3, T4 a4,T5 a5,T6 a6,T7 a7,T8 a8, T9 a9) const {
Providence@4072
   447
    return (*(DoPeekImpl ())) (a1,a2,a3,a4,a5,a6,a7,a8,a9);
Providence@4072
   448
  }
mathieu@219
   449
mathieu@2206
   450
  bool IsEqual (const CallbackBase &other) const {
mathieu@2206
   451
    return m_impl->IsEqual (other.GetImpl ());
tomh@345
   452
  }
tomh@345
   453
mathieu@2206
   454
  bool CheckType (const CallbackBase & other) const {
mathieu@2206
   455
    return DoCheckType (other.GetImpl ());
mathieu@2206
   456
  }
mathieu@2206
   457
  void Assign (const CallbackBase &other) {
mathieu@2206
   458
    DoAssign (other.GetImpl ());
mathieu@2206
   459
  }
mathieu@2206
   460
private:
Providence@4072
   461
  CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> *DoPeekImpl (void) const {
Providence@4072
   462
    return static_cast<CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> *> (PeekPointer (m_impl));
mathieu@2206
   463
  }
mathieu@2206
   464
  bool DoCheckType (Ptr<const CallbackImplBase> other) const {
Providence@4072
   465
    if (other != 0 && dynamic_cast<const CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> *> (PeekPointer (other)) != 0)
mathieu@3763
   466
      {
mathieu@3763
   467
        return true;
mathieu@3763
   468
      }
mathieu@3763
   469
    else if (other == 0)
mathieu@219
   470
      {
mathieu@219
   471
        return true;
mathieu@219
   472
      }
mathieu@219
   473
    else
mathieu@219
   474
      {
mathieu@219
   475
        return false;
mathieu@219
   476
      }
mathieu@219
   477
  }
mathieu@2206
   478
  void DoAssign (Ptr<const CallbackImplBase> other) {
mathieu@2206
   479
    if (!DoCheckType (other))
mathieu@528
   480
      {
timo@4205
   481
        NS_FATAL_ERROR ("Incompatible types. (feed to \"c++filt -t\" if needed)" << std::endl <<
timo@4205
   482
                        "got=" << Demangle ( typeid (*other).name () ) << std::endl <<
timo@4205
   483
                        "expected=" << Demangle ( typeid (CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> *).name () ));
mathieu@528
   484
      }
mathieu@2206
   485
    m_impl = const_cast<CallbackImplBase *> (PeekPointer (other));
mathieu@528
   486
  }
mathieu@9
   487
};
mathieu@9
   488
mathieu@2531
   489
mathieu@2531
   490
template <typename R, typename T1, typename T2,
mathieu@2531
   491
          typename T3, typename T4,
Providence@4072
   492
          typename T5, typename T6,
Providence@4072
   493
          typename T7, typename T8,
Providence@4072
   494
          typename T9>
Providence@4072
   495
bool operator != (Callback<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> a, Callback<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> b)
mathieu@2531
   496
{
mathieu@2531
   497
  return !a.IsEqual (b);
mathieu@2531
   498
}
mathieu@2531
   499
mathieu@9
   500
/**
tomh@2217
   501
 * \ingroup core
mathieu@122
   502
 * \defgroup MakeCallback MakeCallback
mathieu@9
   503
 *
mathieu@9
   504
 */
mathieu@9
   505
mathieu@9
   506
/**
mathieu@122
   507
 * \ingroup MakeCallback
mathieu@1250
   508
 * \param memPtr class method member pointer
mathieu@53
   509
 * \param objPtr class instance
mathieu@9
   510
 * \return a wrapper Callback
mathieu@9
   511
 * Build Callbacks for class method members which takes no arguments
mathieu@9
   512
 * and potentially return a value.
mathieu@9
   513
 */
mathieu@685
   514
template <typename T, typename OBJ, typename R>
mathieu@685
   515
Callback<R> MakeCallback (R (T::*memPtr) (void), OBJ objPtr) {
mathieu@685
   516
  return Callback<R> (objPtr, memPtr);
mathieu@9
   517
}
mathieu@685
   518
template <typename T, typename OBJ, typename R>
mathieu@1417
   519
Callback<R> MakeCallback (R (T::*mem_ptr) () const, OBJ objPtr) {
mathieu@443
   520
  return Callback<R> (objPtr, mem_ptr);
mathieu@443
   521
}
mathieu@9
   522
/**
mathieu@122
   523
 * \ingroup MakeCallback
mathieu@9
   524
 * \param mem_ptr class method member pointer
mathieu@53
   525
 * \param objPtr class instance
mathieu@9
   526
 * \return a wrapper Callback
mathieu@9
   527
 * Build Callbacks for class method members which takes one argument
mathieu@9
   528
 * and potentially return a value.
mathieu@9
   529
 */
mathieu@685
   530
template <typename T, typename OBJ, typename R, typename T1>
mathieu@1417
   531
Callback<R,T1> MakeCallback (R (T::*mem_ptr) (T1), OBJ objPtr) {
mathieu@150
   532
  return Callback<R,T1> (objPtr, mem_ptr);
mathieu@9
   533
}
mathieu@685
   534
template <typename T, typename OBJ, typename R, typename T1>
mathieu@1417
   535
Callback<R,T1> MakeCallback (R (T::*mem_ptr) (T1) const, OBJ objPtr) {
mathieu@443
   536
  return Callback<R,T1> (objPtr, mem_ptr);
mathieu@443
   537
}
mathieu@9
   538
/**
mathieu@122
   539
 * \ingroup MakeCallback
mathieu@9
   540
 * \param mem_ptr class method member pointer
mathieu@53
   541
 * \param objPtr class instance
mathieu@9
   542
 * \return a wrapper Callback
mathieu@9
   543
 * Build Callbacks for class method members which takes two arguments
mathieu@9
   544
 * and potentially return a value.
mathieu@9
   545
 */
mathieu@685
   546
template <typename T, typename OBJ, typename R, typename T1, typename T2>
mathieu@1417
   547
Callback<R,T1,T2> MakeCallback (R (T::*mem_ptr) (T1,T2), OBJ objPtr) {
mathieu@150
   548
  return Callback<R,T1,T2> (objPtr, mem_ptr);
mathieu@9
   549
}
mathieu@685
   550
template <typename T, typename OBJ, typename R, typename T1, typename T2>
mathieu@1417
   551
Callback<R,T1,T2> MakeCallback (R (T::*mem_ptr) (T1,T2) const, OBJ objPtr) {
mathieu@443
   552
  return Callback<R,T1,T2> (objPtr, mem_ptr);
mathieu@443
   553
}
mathieu@9
   554
/**
mathieu@122
   555
 * \ingroup MakeCallback
mathieu@9
   556
 * \param mem_ptr class method member pointer
mathieu@53
   557
 * \param objPtr class instance
mathieu@9
   558
 * \return a wrapper Callback
mathieu@9
   559
 * Build Callbacks for class method members which takes three arguments
mathieu@9
   560
 * and potentially return a value.
mathieu@9
   561
 */
mathieu@685
   562
template <typename T, typename OBJ, typename R, typename T1,typename T2, typename T3>
mathieu@1417
   563
Callback<R,T1,T2,T3> MakeCallback (R (T::*mem_ptr) (T1,T2,T3), OBJ objPtr) {
mathieu@150
   564
  return Callback<R,T1,T2,T3> (objPtr, mem_ptr);
mathieu@9
   565
}
mathieu@685
   566
template <typename T, typename OBJ, typename R, typename T1,typename T2, typename T3>
mathieu@1417
   567
Callback<R,T1,T2,T3> MakeCallback (R (T::*mem_ptr) (T1,T2,T3) const, OBJ objPtr) {
mathieu@443
   568
  return Callback<R,T1,T2,T3> (objPtr, mem_ptr);
mathieu@443
   569
}
mathieu@9
   570
/**
mathieu@122
   571
 * \ingroup MakeCallback
mathieu@9
   572
 * \param mem_ptr class method member pointer
mathieu@53
   573
 * \param objPtr class instance
mathieu@9
   574
 * \return a wrapper Callback
mathieu@9
   575
 * Build Callbacks for class method members which takes four arguments
mathieu@9
   576
 * and potentially return a value.
mathieu@9
   577
 */
mathieu@685
   578
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4>
mathieu@1417
   579
Callback<R,T1,T2,T3,T4> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4), OBJ objPtr) {
mathieu@150
   580
  return Callback<R,T1,T2,T3,T4> (objPtr, mem_ptr);
mathieu@9
   581
}
mathieu@685
   582
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4>
mathieu@1417
   583
Callback<R,T1,T2,T3,T4> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4) const, OBJ objPtr) {
mathieu@443
   584
  return Callback<R,T1,T2,T3,T4> (objPtr, mem_ptr);
mathieu@443
   585
}
mathieu@9
   586
/**
mathieu@122
   587
 * \ingroup MakeCallback
mathieu@9
   588
 * \param mem_ptr class method member pointer
mathieu@53
   589
 * \param objPtr class instance
mathieu@9
   590
 * \return a wrapper Callback
mathieu@9
   591
 * Build Callbacks for class method members which takes five arguments
mathieu@9
   592
 * and potentially return a value.
mathieu@9
   593
 */
mathieu@685
   594
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4,typename T5>
mathieu@1417
   595
Callback<R,T1,T2,T3,T4,T5> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5), OBJ objPtr) {
mathieu@150
   596
  return Callback<R,T1,T2,T3,T4,T5> (objPtr, mem_ptr);
mathieu@9
   597
}
mathieu@685
   598
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4,typename T5>
mathieu@1417
   599
Callback<R,T1,T2,T3,T4,T5> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5) const, OBJ objPtr) {
mathieu@443
   600
  return Callback<R,T1,T2,T3,T4,T5> (objPtr, mem_ptr);
mathieu@443
   601
}
mathieu@1417
   602
/**
mathieu@1417
   603
 * \ingroup MakeCallback
mathieu@1417
   604
 * \param mem_ptr class method member pointer
mathieu@1417
   605
 * \param objPtr class instance
mathieu@1417
   606
 * \return a wrapper Callback
Providence@4072
   607
 * Build Callbacks for class method members which takes six arguments
mathieu@1417
   608
 * and potentially return a value.
mathieu@1417
   609
 */
mathieu@1417
   610
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4,typename T5,typename T6>
mathieu@1417
   611
Callback<R,T1,T2,T3,T4,T5,T6> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5,T6), OBJ objPtr) {
mathieu@1417
   612
  return Callback<R,T1,T2,T3,T4,T5,T6> (objPtr, mem_ptr);
mathieu@1417
   613
}
mathieu@1417
   614
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4,typename T5, typename T6>
mathieu@1417
   615
Callback<R,T1,T2,T3,T4,T5,T6> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5,T6) const, OBJ objPtr) {
mathieu@1417
   616
  return Callback<R,T1,T2,T3,T4,T5,T6> (objPtr, mem_ptr);
mathieu@1417
   617
}
mathieu@9
   618
mathieu@9
   619
/**
mathieu@122
   620
 * \ingroup MakeCallback
Providence@4072
   621
 * \param mem_ptr class method member pointer
Providence@4072
   622
 * \param objPtr class instance
Providence@4072
   623
 * \return a wrapper Callback
Providence@4072
   624
 * Build Callbacks for class method members which takes seven arguments
Providence@4072
   625
 * and potentially return a value.
Providence@4072
   626
 */
Providence@4072
   627
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4,typename T5,typename T6, typename T7>
Providence@4072
   628
Callback<R,T1,T2,T3,T4,T5,T6,T7> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5,T6,T7), OBJ objPtr) {
Providence@4072
   629
  return Callback<R,T1,T2,T3,T4,T5,T6,T7> (objPtr, mem_ptr);
Providence@4072
   630
}
Providence@4072
   631
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4,typename T5, typename T6, typename T7>
Providence@4072
   632
Callback<R,T1,T2,T3,T4,T5,T6,T7> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5,T6,T7) const, OBJ objPtr) {
Providence@4072
   633
  return Callback<R,T1,T2,T3,T4,T5,T6,T7> (objPtr, mem_ptr);
Providence@4072
   634
}
Providence@4072
   635
Providence@4072
   636
Providence@4072
   637
/**
Providence@4072
   638
 * \ingroup MakeCallback
Providence@4072
   639
 * \param mem_ptr class method member pointer
Providence@4072
   640
 * \param objPtr class instance
Providence@4072
   641
 * \return a wrapper Callback
Providence@4072
   642
 * Build Callbacks for class method members which takes eight arguments
Providence@4072
   643
 * and potentially return a value.
Providence@4072
   644
 */
Providence@4072
   645
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4,typename T5,typename T6, typename T7, typename T8>
Providence@4072
   646
Callback<R,T1,T2,T3,T4,T5,T6,T7,T8> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5,T6,T7,T8), OBJ objPtr) {
Providence@4072
   647
  return Callback<R,T1,T2,T3,T4,T5,T6,T7,T8> (objPtr, mem_ptr);
Providence@4072
   648
}
Providence@4072
   649
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4,typename T5, typename T6, typename T7, typename T8>
Providence@4072
   650
Callback<R,T1,T2,T3,T4,T5,T6,T7,T8> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5,T6,T7,T8) const, OBJ objPtr) {
Providence@4072
   651
  return Callback<R,T1,T2,T3,T4,T5,T6,T7,T8> (objPtr, mem_ptr);
Providence@4072
   652
}
Providence@4072
   653
Providence@4072
   654
/**
Providence@4072
   655
 * \ingroup MakeCallback
Providence@4072
   656
 * \param mem_ptr class method member pointer
Providence@4072
   657
 * \param objPtr class instance
Providence@4072
   658
 * \return a wrapper Callback
Providence@4072
   659
 * Build Callbacks for class method members which takes nine arguments
Providence@4072
   660
 * and potentially return a value.
Providence@4072
   661
 */
Providence@4072
   662
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4,typename T5,typename T6, typename T7, typename T8, typename T9>
Providence@4072
   663
Callback<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5,T6,T7,T8,T9), OBJ objPtr) {
Providence@4072
   664
  return Callback<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> (objPtr, mem_ptr);
Providence@4072
   665
}
Providence@4072
   666
template <typename T, typename OBJ, typename R, typename T1, typename T2, typename T3, typename T4,typename T5, typename T6, typename T7, typename T8, typename T9>
Providence@4072
   667
Callback<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5,T6,T7,T8,T9) const, OBJ objPtr) {
Providence@4072
   668
  return Callback<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> (objPtr, mem_ptr);
Providence@4072
   669
}
Providence@4072
   670
Providence@4072
   671
/**
Providence@4072
   672
 * \ingroup MakeCallback
mathieu@53
   673
 * \param fnPtr function pointer
mathieu@9
   674
 * \return a wrapper Callback
mathieu@9
   675
 * Build Callbacks for functions which takes no arguments
mathieu@9
   676
 * and potentially return a value.
mathieu@9
   677
 */
mathieu@9
   678
template <typename R>
mathieu@122
   679
Callback<R> MakeCallback (R (*fnPtr) ()) {
tomh@345
   680
  return Callback<R> (fnPtr, true, true);
mathieu@9
   681
}
mathieu@9
   682
/**
mathieu@122
   683
 * \ingroup MakeCallback
mathieu@53
   684
 * \param fnPtr function pointer
mathieu@9
   685
 * \return a wrapper Callback
mathieu@9
   686
 * Build Callbacks for functions which takes one argument
mathieu@9
   687
 * and potentially return a value.
mathieu@9
   688
 */
mathieu@9
   689
template <typename R, typename T1>
mathieu@122
   690
Callback<R,T1> MakeCallback (R (*fnPtr) (T1)) {
tomh@345
   691
  return Callback<R,T1> (fnPtr, true, true);
mathieu@9
   692
}
mathieu@9
   693
/**
mathieu@122
   694
 * \ingroup MakeCallback
mathieu@53
   695
 * \param fnPtr function pointer
mathieu@9
   696
 * \return a wrapper Callback
mathieu@9
   697
 * Build Callbacks for functions which takes two arguments
mathieu@9
   698
 * and potentially return a value.
mathieu@9
   699
 */
mathieu@9
   700
template <typename R, typename T1, typename T2>
mathieu@122
   701
Callback<R,T1,T2> MakeCallback (R (*fnPtr) (T1,T2)) {
tomh@345
   702
  return Callback<R,T1,T2> (fnPtr, true, true);
mathieu@9
   703
}
mathieu@9
   704
/**
mathieu@122
   705
 * \ingroup MakeCallback
mathieu@53
   706
 * \param fnPtr function pointer
mathieu@9
   707
 * \return a wrapper Callback
mathieu@9
   708
 * Build Callbacks for functions which takes three arguments
mathieu@9
   709
 * and potentially return a value.
mathieu@9
   710
 */
mathieu@9
   711
template <typename R, typename T1, typename T2,typename T3>
mathieu@122
   712
Callback<R,T1,T2,T3> MakeCallback (R (*fnPtr) (T1,T2,T3)) {
tomh@345
   713
  return Callback<R,T1,T2,T3> (fnPtr, true, true);
mathieu@9
   714
}
mathieu@9
   715
/**
mathieu@122
   716
 * \ingroup MakeCallback
mathieu@53
   717
 * \param fnPtr function pointer
mathieu@9
   718
 * \return a wrapper Callback
mathieu@9
   719
 * Build Callbacks for functions which takes four arguments
mathieu@9
   720
 * and potentially return a value.
mathieu@9
   721
 */
mathieu@9
   722
template <typename R, typename T1, typename T2,typename T3,typename T4>
mathieu@122
   723
Callback<R,T1,T2,T3,T4> MakeCallback (R (*fnPtr) (T1,T2,T3,T4)) {
tomh@345
   724
  return Callback<R,T1,T2,T3,T4> (fnPtr, true, true);
mathieu@9
   725
}
mathieu@9
   726
/**
mathieu@122
   727
 * \ingroup MakeCallback
mathieu@53
   728
 * \param fnPtr function pointer
mathieu@9
   729
 * \return a wrapper Callback
mathieu@9
   730
 * Build Callbacks for functions which takes five arguments
mathieu@9
   731
 * and potentially return a value.
mathieu@9
   732
 */
mathieu@9
   733
template <typename R, typename T1, typename T2,typename T3,typename T4,typename T5>
mathieu@122
   734
Callback<R,T1,T2,T3,T4,T5> MakeCallback (R (*fnPtr) (T1,T2,T3,T4,T5)) {
tomh@345
   735
  return Callback<R,T1,T2,T3,T4,T5> (fnPtr, true, true);
mathieu@9
   736
}
mathieu@1417
   737
/**
mathieu@1417
   738
 * \ingroup MakeCallback
mathieu@1417
   739
 * \param fnPtr function pointer
mathieu@1417
   740
 * \return a wrapper Callback
Providence@4072
   741
 * Build Callbacks for functions which takes six arguments
mathieu@1417
   742
 * and potentially return a value.
mathieu@1417
   743
 */
mathieu@1417
   744
template <typename R, typename T1, typename T2,typename T3,typename T4,typename T5,typename T6>
mathieu@1417
   745
Callback<R,T1,T2,T3,T4,T5,T6> MakeCallback (R (*fnPtr) (T1,T2,T3,T4,T5,T6)) {
mathieu@1417
   746
  return Callback<R,T1,T2,T3,T4,T5,T6> (fnPtr, true, true);
mathieu@1417
   747
}
mathieu@9
   748
Providence@4072
   749
/**
Providence@4072
   750
 * \ingroup MakeCallback
Providence@4072
   751
 * \param fnPtr function pointer
Providence@4072
   752
 * \return a wrapper Callback
Providence@4072
   753
 * Build Callbacks for functions which takes seven arguments
Providence@4072
   754
 * and potentially return a value.
Providence@4072
   755
 */
Providence@4072
   756
template <typename R, typename T1, typename T2,typename T3,typename T4,typename T5,typename T6, typename T7>
Providence@4072
   757
Callback<R,T1,T2,T3,T4,T5,T6,T7> MakeCallback (R (*fnPtr) (T1,T2,T3,T4,T5,T6,T7)) {
Providence@4072
   758
  return Callback<R,T1,T2,T3,T4,T5,T6,T7> (fnPtr, true, true);
Providence@4072
   759
}
Providence@4072
   760
Providence@4072
   761
/**
Providence@4072
   762
 * \ingroup MakeCallback
Providence@4072
   763
 * \param fnPtr function pointer
Providence@4072
   764
 * \return a wrapper Callback
Providence@4072
   765
 * Build Callbacks for functions which takes eight arguments
Providence@4072
   766
 * and potentially return a value.
Providence@4072
   767
 */
Providence@4072
   768
template <typename R, typename T1, typename T2,typename T3,typename T4,typename T5,typename T6, typename T7, typename T8>
Providence@4072
   769
Callback<R,T1,T2,T3,T4,T5,T6,T7,T8> MakeCallback (R (*fnPtr) (T1,T2,T3,T4,T5,T6,T7,T8)) {
Providence@4072
   770
  return Callback<R,T1,T2,T3,T4,T5,T6,T7,T8> (fnPtr, true, true);
Providence@4072
   771
}
Providence@4072
   772
Providence@4072
   773
/**
Providence@4072
   774
 * \ingroup MakeCallback
Providence@4072
   775
 * \param fnPtr function pointer
Providence@4072
   776
 * \return a wrapper Callback
Providence@4072
   777
 * Build Callbacks for functions which takes nine arguments
Providence@4072
   778
 * and potentially return a value.
Providence@4072
   779
 */
Providence@4072
   780
template <typename R, typename T1, typename T2,typename T3,typename T4,typename T5,typename T6, typename T7, typename T8, typename T9>
Providence@4072
   781
Callback<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> MakeCallback (R (*fnPtr) (T1,T2,T3,T4,T5,T6,T7,T8,T9)) {
Providence@4072
   782
  return Callback<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> (fnPtr, true, true);
Providence@4072
   783
}
Providence@4072
   784
mathieu@9
   785
mathieu@9
   786
mathieu@9
   787
/**
mathieu@122
   788
 * \ingroup MakeCallback
mathieu@9
   789
 * \return a wrapper Callback
mathieu@9
   790
 * Build a null callback which takes no arguments
mathieu@9
   791
 * and potentially return a value.
mathieu@9
   792
 */
mathieu@9
   793
template <typename R>
mathieu@122
   794
Callback<R> MakeNullCallback (void) {
mathieu@150
   795
  return Callback<R> ();
mathieu@9
   796
}
mathieu@9
   797
/**
mathieu@122
   798
 * \ingroup MakeCallback
mathieu@416
   799
 * \overload Callback<R> MakeNullCallback (void)
mathieu@9
   800
 * \return a wrapper Callback
mathieu@9
   801
 * Build a null callback which takes one argument
mathieu@9
   802
 * and potentially return a value.
mathieu@9
   803
 */
mathieu@9
   804
template <typename R, typename T1>
mathieu@122
   805
Callback<R,T1> MakeNullCallback (void) {
mathieu@150
   806
  return Callback<R,T1> ();
mathieu@9
   807
}
mathieu@9
   808
/**
mathieu@122
   809
 * \ingroup MakeCallback
mathieu@416
   810
 * \overload Callback<R> MakeNullCallback (void)
mathieu@9
   811
 * \return a wrapper Callback
mathieu@9
   812
 * Build a null callback which takes two arguments
mathieu@9
   813
 * and potentially return a value.
mathieu@9
   814
 */
mathieu@9
   815
template <typename R, typename T1, typename T2>
mathieu@122
   816
Callback<R,T1,T2> MakeNullCallback (void) {
mathieu@150
   817
  return Callback<R,T1,T2> ();
mathieu@9
   818
}
mathieu@9
   819
/**
mathieu@122
   820
 * \ingroup MakeCallback
mathieu@416
   821
 * \overload Callback<R> MakeNullCallback (void)
mathieu@9
   822
 * \return a wrapper Callback
mathieu@9
   823
 * Build a null callback which takes three arguments
mathieu@9
   824
 * and potentially return a value.
mathieu@9
   825
 */
mathieu@9
   826
template <typename R, typename T1, typename T2,typename T3>
mathieu@122
   827
Callback<R,T1,T2,T3> MakeNullCallback (void) {
mathieu@150
   828
  return Callback<R,T1,T2,T3> ();
mathieu@9
   829
}
mathieu@9
   830
/**
mathieu@122
   831
 * \ingroup MakeCallback
mathieu@416
   832
 * \overload Callback<R> MakeNullCallback (void)
mathieu@9
   833
 * \return a wrapper Callback
mathieu@9
   834
 * Build a null callback which takes four arguments
mathieu@9
   835
 * and potentially return a value.
mathieu@9
   836
 */
mathieu@9
   837
template <typename R, typename T1, typename T2,typename T3,typename T4>
mathieu@122
   838
Callback<R,T1,T2,T3,T4> MakeNullCallback (void) {
mathieu@150
   839
  return Callback<R,T1,T2,T3,T4> ();
mathieu@9
   840
}
mathieu@9
   841
/**
mathieu@122
   842
 * \ingroup MakeCallback
mathieu@416
   843
 * \overload Callback<R> MakeNullCallback (void)
mathieu@9
   844
 * \return a wrapper Callback
mathieu@9
   845
 * Build a null callback which takes five arguments
mathieu@9
   846
 * and potentially return a value.
mathieu@9
   847
 */
mathieu@9
   848
template <typename R, typename T1, typename T2,typename T3,typename T4,typename T5>
mathieu@122
   849
Callback<R,T1,T2,T3,T4,T5> MakeNullCallback (void) {
mathieu@150
   850
  return Callback<R,T1,T2,T3,T4,T5> ();
mathieu@9
   851
}
mathieu@1417
   852
/**
mathieu@1417
   853
 * \ingroup MakeCallback
mathieu@1417
   854
 * \overload Callback<R> MakeNullCallback (void)
mathieu@1417
   855
 * \return a wrapper Callback
Providence@4072
   856
 * Build a null callback which takes six arguments
mathieu@1417
   857
 * and potentially return a value.
mathieu@1417
   858
 */
mathieu@1417
   859
template <typename R, typename T1, typename T2,typename T3,typename T4,typename T5,typename T6>
mathieu@1417
   860
Callback<R,T1,T2,T3,T4,T5,T6> MakeNullCallback (void) {
mathieu@1417
   861
  return Callback<R,T1,T2,T3,T4,T5,T6> ();
mathieu@1417
   862
}
mathieu@9
   863
Providence@4072
   864
/**
Providence@4072
   865
 * \ingroup MakeCallback
Providence@4072
   866
 * \overload Callback<R> MakeNullCallback (void)
Providence@4072
   867
 * \return a wrapper Callback
Providence@4072
   868
 * Build a null callback which takes seven arguments
Providence@4072
   869
 * and potentially return a value.
Providence@4072
   870
 */
Providence@4072
   871
template <typename R, typename T1, typename T2,typename T3,typename T4,typename T5,typename T6, typename T7>
Providence@4072
   872
Callback<R,T1,T2,T3,T4,T5,T6,T7> MakeNullCallback (void) {
Providence@4072
   873
  return Callback<R,T1,T2,T3,T4,T5,T6,T7> ();
Providence@4072
   874
}
Providence@4072
   875
Providence@4072
   876
/**
Providence@4072
   877
 * \ingroup MakeCallback
Providence@4072
   878
 * \overload Callback<R> MakeNullCallback (void)
Providence@4072
   879
 * \return a wrapper Callback
Providence@4072
   880
 * Build a null callback which takes eight arguments
Providence@4072
   881
 * and potentially return a value.
Providence@4072
   882
 */
Providence@4072
   883
template <typename R, typename T1, typename T2,typename T3,typename T4,typename T5,typename T6, typename T7, typename T8>
Providence@4072
   884
Callback<R,T1,T2,T3,T4,T5,T6,T7,T8> MakeNullCallback (void) {
Providence@4072
   885
  return Callback<R,T1,T2,T3,T4,T5,T6,T7,T8> ();
Providence@4072
   886
}
Providence@4072
   887
Providence@4072
   888
/**
Providence@4072
   889
 * \ingroup MakeCallback
Providence@4072
   890
 * \overload Callback<R> MakeNullCallback (void)
Providence@4072
   891
 * \return a wrapper Callback
Providence@4072
   892
 * Build a null callback which takes nine arguments
Providence@4072
   893
 * and potentially return a value.
Providence@4072
   894
 */
Providence@4072
   895
template <typename R, typename T1, typename T2,typename T3,typename T4,typename T5,typename T6, typename T7, typename T8, typename T9>
Providence@4072
   896
Callback<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> MakeNullCallback (void) {
Providence@4072
   897
  return Callback<R,T1,T2,T3,T4,T5,T6,T7,T8,T9> ();
Providence@4072
   898
}
Providence@4072
   899
mathieu@73
   900
raj@632
   901
/*
mathieu@73
   902
 * The following is experimental code. It works but we have
mathieu@73
   903
 * not yet determined whether or not it is really useful and whether
mathieu@73
   904
 * or not we really want to use it.
mathieu@73
   905
 */
mathieu@73
   906
mathieu@1418
   907
template <typename R, typename TX, typename ARG>
mathieu@1418
   908
Callback<R> MakeBoundCallback (R (*fnPtr) (TX), ARG a) {
Providence@4072
   909
  Ptr<CallbackImpl<R,empty,empty,empty,empty,empty,empty,empty,empty,empty> > impl =
Providence@4072
   910
    Create<BoundFunctorCallbackImpl<R (*) (TX),R,TX,empty,empty,empty,empty,empty,empty,empty,empty> >(fnPtr, a);
tomh@345
   911
  return Callback<R> (impl);
tomh@345
   912
}
tomh@345
   913
mathieu@1418
   914
template <typename R, typename TX, typename ARG, 
mathieu@1418
   915
          typename T1>
mathieu@1418
   916
Callback<R,T1> MakeBoundCallback (R (*fnPtr) (TX,T1), ARG a) {
Providence@4072
   917
  Ptr<CallbackImpl<R,T1,empty,empty,empty,empty,empty,empty,empty,empty> > impl =
Providence@4072
   918
    Create<BoundFunctorCallbackImpl<R (*) (TX,T1),R,TX,T1,empty,empty,empty,empty,empty,empty,empty> > (fnPtr, a);
mathieu@150
   919
  return Callback<R,T1> (impl);
mathieu@9
   920
}
mathieu@1418
   921
template <typename R, typename TX, typename ARG, 
mathieu@1418
   922
          typename T1, typename T2>
mathieu@1418
   923
Callback<R,T1,T2> MakeBoundCallback (R (*fnPtr) (TX,T1,T2), ARG a) {
Providence@4072
   924
  Ptr<CallbackImpl<R,T1,T2,empty,empty,empty,empty,empty,empty,empty> > impl =
Providence@4072
   925
    Create<BoundFunctorCallbackImpl<R (*) (TX,T1,T2),R,TX,T1,T2,empty,empty,empty,empty,empty,empty> > (fnPtr, a);
mathieu@150
   926
  return Callback<R,T1,T2> (impl);
mathieu@9
   927
}
mathieu@1418
   928
template <typename R, typename TX, typename ARG,
mathieu@1418
   929
          typename T1, typename T2,typename T3>
mathieu@1418
   930
Callback<R,T1,T2,T3> MakeBoundCallback (R (*fnPtr) (TX,T1,T2,T3), ARG a) {
Providence@4072
   931
  Ptr<CallbackImpl<R,T1,T2,T3,empty,empty,empty,empty,empty,empty> > impl =
Providence@4072
   932
    Create<BoundFunctorCallbackImpl<R (*) (TX,T1,T2,T3),R,TX,T1,T2,T3,empty,empty,empty,empty,empty> > (fnPtr, a);
mathieu@1418
   933
  return Callback<R,T1,T2,T3> (impl);
mathieu@1418
   934
}
mathieu@1418
   935
template <typename R, typename TX, typename ARG,
mathieu@1418
   936
          typename T1, typename T2,typename T3,typename T4>
mathieu@1418
   937
Callback<R,T1,T2,T3,T4> MakeBoundCallback (R (*fnPtr) (TX,T1,T2,T3,T4), ARG a) {
Providence@4072
   938
  Ptr<CallbackImpl<R,T1,T2,T3,T4,empty,empty,empty,empty,empty> > impl =
Providence@4072
   939
    Create<BoundFunctorCallbackImpl<R (*) (TX,T1,T2,T3,T4),R,TX,T1,T2,T3,T4,empty,empty,empty,empty> > (fnPtr, a);
mathieu@150
   940
  return Callback<R,T1,T2,T3,T4> (impl);
mathieu@9
   941
}
mathieu@1418
   942
template <typename R, typename TX, typename ARG,
mathieu@1418
   943
          typename T1, typename T2,typename T3,typename T4,typename T5>
mathieu@1418
   944
Callback<R,T1,T2,T3,T4,T5> MakeBoundCallback (R (*fnPtr) (TX,T1,T2,T3,T4,T5), ARG a) {
Providence@4072
   945
  Ptr<CallbackImpl<R,T1,T2,T3,T4,T5,empty,empty,empty,empty> > impl =
Providence@4072
   946
    Create<BoundFunctorCallbackImpl<R (*) (TX,T1,T2,T3,T4,T5),R,TX,T1,T2,T3,T4,T5,empty,empty,empty> > (fnPtr, a);
mathieu@150
   947
  return Callback<R,T1,T2,T3,T4,T5> (impl);
mathieu@9
   948
}
Providence@4072
   949
template <typename R, typename TX, typename ARG,
Providence@4072
   950
          typename T1, typename T2,typename T3,typename T4,typename T5, typename T6>
Providence@4072
   951
Callback<R,T1,T2,T3,T4,T5,T6> MakeBoundCallback (R (*fnPtr) (TX,T1,T2,T3,T4,T5,T6), ARG a) {
Providence@4072
   952
  Ptr<CallbackImpl<R,T1,T2,T3,T4,T5,T6,empty,empty,empty> > impl =
Providence@4072
   953
    Create<BoundFunctorCallbackImpl<R (*) (TX,T1,T2,T3,T4,T5,T6),R,TX,T1,T2,T3,T4,T5,T6,empty,empty> > (fnPtr, a);
Providence@4072
   954
  return Callback<R,T1,T2,T3,T4,T5,T6> (impl);
Providence@4072
   955
}
Providence@4072
   956
template <typename R, typename TX, typename ARG,
Providence@4072
   957
          typename T1, typename T2,typename T3,typename T4,typename T5, typename T6, typename T7>
Providence@4072
   958
Callback<R,T1,T2,T3,T4,T5,T6,T7> MakeBoundCallback (R (*fnPtr) (TX,T1,T2,T3,T4,T5,T6,T7), ARG a) {
Providence@4072
   959
  Ptr<CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,empty,empty> > impl =
Providence@4072
   960
    Create<BoundFunctorCallbackImpl<R (*) (TX,T1,T2,T3,T4,T5,T6,T7),R,TX,T1,T2,T3,T4,T5,T6,T7,empty> > (fnPtr, a);
Providence@4072
   961
  return Callback<R,T1,T2,T3,T4,T5,T6,T7> (impl);
Providence@4072
   962
}
Providence@4072
   963
template <typename R, typename TX, typename ARG,
Providence@4072
   964
          typename T1, typename T2,typename T3,typename T4,typename T5, typename T6, typename T7, typename T8>
Providence@4072
   965
Callback<R,T1,T2,T3,T4,T5,T6,T7,T8> MakeBoundCallback (R (*fnPtr) (TX,T1,T2,T3,T4,T5,T6,T7,T8), ARG a) {
Providence@4072
   966
  Ptr<CallbackImpl<R,T1,T2,T3,T4,T5,T6,T7,T8,empty> > impl =
Providence@4072
   967
    Create<BoundFunctorCallbackImpl<R (*) (TX,T1,T2,T3,T4,T5,T6,T7,T8),R,TX,T1,T2,T3,T4,T5,T6,T7,T8> > (fnPtr, a);
Providence@4072
   968
  return Callback<R,T1,T2,T3,T4,T5,T6,T7,T8> (impl);
Providence@4072
   969
}
mathieu@3763
   970
} // namespace ns3
mathieu@9
   971
mathieu@3763
   972
namespace ns3 {
mathieu@9
   973
mathieu@3763
   974
class CallbackValue : public AttributeValue
mathieu@3763
   975
{
mathieu@3763
   976
public:
mathieu@3763
   977
  CallbackValue ();
mathieu@3763
   978
  CallbackValue (const CallbackBase &base);
mathieu@3763
   979
  virtual ~CallbackValue ();
mathieu@3763
   980
  void Set (CallbackBase base);
mathieu@3763
   981
  template <typename T>
mathieu@3763
   982
  bool GetAccessor (T &value) const;
mathieu@3763
   983
  virtual Ptr<AttributeValue> Copy (void) const;
mathieu@3763
   984
  virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
mathieu@3763
   985
  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
mathieu@3763
   986
private:
mathieu@3763
   987
  CallbackBase m_value;
mathieu@3763
   988
};
mathieu@3763
   989
mathieu@3763
   990
ATTRIBUTE_ACCESSOR_DEFINE(Callback);
mathieu@3763
   991
ATTRIBUTE_CHECKER_DEFINE (Callback);
mathieu@3763
   992
mathieu@3763
   993
} // namespace ns3
mathieu@3763
   994
mathieu@3763
   995
namespace ns3 {
mathieu@3763
   996
mathieu@3763
   997
template <typename T>
mathieu@3763
   998
bool CallbackValue::GetAccessor (T &value) const
mathieu@3763
   999
{
mathieu@3763
  1000
  if (value.CheckType (m_value))
mathieu@3763
  1001
    {
mathieu@3763
  1002
      value.Assign (m_value);
mathieu@3763
  1003
      return true;
mathieu@3763
  1004
    }
mathieu@3763
  1005
  return false;
mathieu@3763
  1006
}
mathieu@3763
  1007
mathieu@3763
  1008
} // namespace ns3
mathieu@9
  1009
mathieu@9
  1010
mathieu@9
  1011
#endif /* CALLBACK_H */