src/core/attribute-accessor-helper.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3527 1548d7e3604f
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
mathieu@2581
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@2581
     2
/*
mathieu@2581
     3
 * Copyright (c) 2008 INRIA
mathieu@2581
     4
 *
mathieu@2581
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@2581
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@2581
     7
 * published by the Free Software Foundation;
mathieu@2581
     8
 *
mathieu@2581
     9
 * This program is distributed in the hope that it will be useful,
mathieu@2581
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@2581
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@2581
    12
 * GNU General Public License for more details.
mathieu@2581
    13
 *
mathieu@2581
    14
 * You should have received a copy of the GNU General Public License
mathieu@2581
    15
 * along with this program; if not, write to the Free Software
mathieu@2581
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@2581
    17
 *
mathieu@2581
    18
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@2581
    19
 */
mathieu@2449
    20
#ifndef ATTRIBUTE_ACCESSOR_HELPER_H
mathieu@2449
    21
#define ATTRIBUTE_ACCESSOR_HELPER_H
mathieu@2371
    22
mathieu@2724
    23
#include "attribute.h"
mathieu@2724
    24
mathieu@2371
    25
namespace ns3 {
mathieu@2371
    26
tomh@3182
    27
/**
tomh@3182
    28
 * \ingroup AttributeHelper
tomh@3182
    29
 */
mathieu@2461
    30
template <typename V, typename T1>
mathieu@2436
    31
Ptr<const AttributeAccessor>
mathieu@2435
    32
MakeAccessorHelper (T1 a1);
mathieu@2375
    33
tomh@3182
    34
/**
tomh@3182
    35
 * \ingroup AttributeHelper
tomh@3182
    36
 */
mathieu@2461
    37
template <typename V, typename T1, typename T2>
mathieu@2436
    38
Ptr<const AttributeAccessor>
mathieu@2435
    39
MakeAccessorHelper (T1 a1, T2 a2);
mathieu@2375
    40
mathieu@2375
    41
} // namespace ns3
mathieu@2375
    42
mathieu@2408
    43
/***************************************************************
mathieu@2408
    44
 *        The implementation of the above functions.
mathieu@2408
    45
 ***************************************************************/
mathieu@2375
    46
mathieu@2408
    47
#include "type-traits.h"
mathieu@2375
    48
mathieu@2375
    49
namespace ns3 {
mathieu@2375
    50
mathieu@3763
    51
template <typename T>
mathieu@3763
    52
struct AccessorTrait
mathieu@3763
    53
{
mathieu@3763
    54
  typedef typename TypeTraits<typename TypeTraits<T>::ReferencedType>::NonConstType Result;
mathieu@3763
    55
};
mathieu@3763
    56
mathieu@2461
    57
template <typename T, typename U>
mathieu@2461
    58
class AccessorHelper : public AttributeAccessor
mathieu@2371
    59
{
mathieu@2371
    60
public:
mathieu@2435
    61
  AccessorHelper () {}
mathieu@2410
    62
mathieu@2965
    63
  virtual bool Set (ObjectBase * object, const AttributeValue & val) const {
mathieu@2965
    64
    const U *value = dynamic_cast<const U *> (&val);
mathieu@2410
    65
    if (value == 0)
mathieu@2410
    66
      {
mathieu@2410
    67
	return false;
mathieu@2410
    68
      }
mathieu@2410
    69
    T *obj = dynamic_cast<T *> (object);
mathieu@2410
    70
    if (obj == 0)
mathieu@2410
    71
      {
mathieu@2410
    72
	return false;
mathieu@2410
    73
      }
mathieu@2410
    74
    return DoSet (obj, value);
mathieu@2410
    75
  }
mathieu@2410
    76
mathieu@2965
    77
  virtual bool Get (const ObjectBase * object, AttributeValue &val) const {
mathieu@2965
    78
    U *value = dynamic_cast<U *> (&val);
mathieu@2410
    79
    if (value == 0)
mathieu@2410
    80
      {
mathieu@2410
    81
	return false;
mathieu@2410
    82
      }
mathieu@2410
    83
    const T *obj = dynamic_cast<const T *> (object);
mathieu@2410
    84
    if (obj == 0)
mathieu@2410
    85
      {
mathieu@2410
    86
	return false;
mathieu@2410
    87
      }
mathieu@2410
    88
    return DoGet (obj, value);
mathieu@2410
    89
  }
mathieu@2410
    90
mathieu@2371
    91
private:
mathieu@2403
    92
  virtual bool DoSet (T *object, const U *v) const = 0;
mathieu@2403
    93
  virtual bool DoGet (const T *object, U *v) const = 0;
mathieu@2371
    94
};
mathieu@2371
    95
mathieu@2461
    96
template <typename V, typename T, typename U>
mathieu@2436
    97
Ptr<const AttributeAccessor>
mathieu@2435
    98
DoMakeAccessorHelperOne (U T::*memberVariable)
mathieu@2371
    99
{
mathieu@2461
   100
  class MemberVariable : public AccessorHelper<T,V>
mathieu@2375
   101
    {
mathieu@2375
   102
    public:
mathieu@2427
   103
      MemberVariable (U T::*memberVariable)
mathieu@2461
   104
	: AccessorHelper<T,V> (),
mathieu@2375
   105
	m_memberVariable (memberVariable)
mathieu@2375
   106
	{}
mathieu@2375
   107
    private:
mathieu@2403
   108
      virtual bool DoSet (T *object, const V *v) const {
mathieu@3763
   109
        typename AccessorTrait<U>::Result tmp;
mathieu@3763
   110
        bool ok = v->GetAccessor (tmp);
mathieu@3763
   111
        if (!ok)
mathieu@3763
   112
          {
mathieu@3763
   113
            return false;
mathieu@3763
   114
          }
mathieu@3763
   115
	(object->*m_memberVariable) = tmp;
mathieu@2403
   116
	return true;
mathieu@2375
   117
      }
mathieu@2403
   118
      virtual bool DoGet (const T *object, V *v) const {
mathieu@2375
   119
	v->Set (object->*m_memberVariable);
mathieu@2403
   120
	return true;
mathieu@2375
   121
      }
mathieu@2950
   122
      virtual bool HasGetter (void) const {
mathieu@2950
   123
        return true;
mathieu@2950
   124
      }
mathieu@2950
   125
      virtual bool HasSetter (void) const {
mathieu@2950
   126
        return true;
mathieu@2950
   127
      }
mathieu@2375
   128
      
mathieu@2375
   129
      U T::*m_memberVariable;
mathieu@2375
   130
    };
mathieu@2436
   131
  return Ptr<const AttributeAccessor> (new MemberVariable (memberVariable), false);
mathieu@2375
   132
}
mathieu@2427
   133
mathieu@2461
   134
template <typename V, typename T, typename U>
mathieu@2436
   135
Ptr<const AttributeAccessor>
mathieu@2435
   136
DoMakeAccessorHelperOne (U (T::*getter) (void) const)
mathieu@2408
   137
{
mathieu@2461
   138
  class MemberMethod : public AccessorHelper<T,V>
mathieu@2408
   139
    {
mathieu@2408
   140
    public:
mathieu@2427
   141
      MemberMethod (U (T::*getter) (void) const)
mathieu@2461
   142
	: AccessorHelper<T,V> (),
mathieu@2408
   143
	m_getter (getter)
mathieu@2408
   144
	{}
mathieu@2408
   145
    private:
mathieu@2408
   146
      virtual bool DoSet (T *object, const V *v) const {
mathieu@2408
   147
	return false;
mathieu@2408
   148
      }
mathieu@2408
   149
      virtual bool DoGet (const T *object, V *v) const {
mathieu@2408
   150
	v->Set ((object->*m_getter) ());
mathieu@2408
   151
	return true;
mathieu@2408
   152
      }
mathieu@2950
   153
      virtual bool HasGetter (void) const {
mathieu@2950
   154
        return true;
mathieu@2950
   155
      }
mathieu@2950
   156
      virtual bool HasSetter (void) const {
mathieu@2950
   157
        return false;
mathieu@2950
   158
      }
mathieu@2408
   159
      U (T::*m_getter) (void) const;
mathieu@2408
   160
    };
mathieu@2436
   161
  return Ptr<const AttributeAccessor> (new MemberMethod (getter), false);
mathieu@2408
   162
}
mathieu@2371
   163
mathieu@2427
   164
mathieu@2461
   165
template <typename V, typename T, typename U>
mathieu@2436
   166
Ptr<const AttributeAccessor>
mathieu@2435
   167
DoMakeAccessorHelperOne (void (T::*setter) (U))
mathieu@2371
   168
{
mathieu@2461
   169
  class MemberMethod : public AccessorHelper<T,V>
mathieu@2408
   170
    {
mathieu@2408
   171
    public:
mathieu@2427
   172
      MemberMethod (void (T::*setter) (U))
mathieu@2461
   173
	: AccessorHelper<T,V> (),
mathieu@2408
   174
	m_setter (setter)
mathieu@2408
   175
	{}
mathieu@2408
   176
    private:
mathieu@2408
   177
      virtual bool DoSet (T *object, const V *v) const {
mathieu@3763
   178
        typename AccessorTrait<U>::Result tmp;
mathieu@3763
   179
        bool ok = v->GetAccessor (tmp);
mathieu@3763
   180
        if (!ok)
mathieu@3763
   181
          {
mathieu@3763
   182
            return false;
mathieu@3763
   183
          }
mathieu@3763
   184
	(object->*m_setter) (tmp);
mathieu@2408
   185
	return true;
mathieu@2408
   186
      }
mathieu@2513
   187
      virtual bool DoGet (const T *object, V *v) const {
mathieu@2408
   188
	return false;
mathieu@2408
   189
      }
mathieu@2950
   190
      virtual bool HasGetter (void) const {
mathieu@2950
   191
        return false;
mathieu@2950
   192
      }
mathieu@2950
   193
      virtual bool HasSetter (void) const {
mathieu@2950
   194
        return true;
mathieu@2950
   195
      }
mathieu@2408
   196
      void (T::*m_setter) (U);
mathieu@2408
   197
    };
mathieu@2436
   198
  return Ptr<const AttributeAccessor> (new MemberMethod (setter), false);
mathieu@2408
   199
}
mathieu@2408
   200
mathieu@2461
   201
template <typename W, typename T, typename U, typename V>
mathieu@2436
   202
Ptr<const AttributeAccessor>
mathieu@2435
   203
DoMakeAccessorHelperTwo (void (T::*setter) (U), 
mathieu@2427
   204
			  V (T::*getter) (void) const)
mathieu@2375
   205
{
mathieu@2461
   206
  class MemberMethod : public AccessorHelper<T,W>
mathieu@2375
   207
    {
mathieu@2375
   208
    public:
mathieu@2375
   209
      MemberMethod (void (T::*setter) (U), 
mathieu@2427
   210
		    V (T::*getter) (void) const)
mathieu@2461
   211
	: AccessorHelper<T,W> (),
mathieu@2375
   212
	m_setter (setter),
mathieu@2375
   213
	m_getter (getter)
mathieu@2375
   214
	{}
mathieu@2375
   215
    private:
mathieu@2403
   216
      virtual bool DoSet (T *object, const W *v) const {
mathieu@3763
   217
        typename AccessorTrait<U>::Result tmp;
mathieu@3763
   218
        bool ok = v->GetAccessor (tmp);
mathieu@3763
   219
        if (!ok)
mathieu@3763
   220
          {
mathieu@3763
   221
            return false;
mathieu@3763
   222
          }
mathieu@3763
   223
	(object->*m_setter) (tmp);
mathieu@2403
   224
	return true;
mathieu@2375
   225
      }
mathieu@2403
   226
      virtual bool DoGet (const T *object, W *v) const {
mathieu@2375
   227
	v->Set ((object->*m_getter) ());
mathieu@2403
   228
	return true;
mathieu@2375
   229
      }
mathieu@2950
   230
      virtual bool HasGetter (void) const {
mathieu@2950
   231
        return true;
mathieu@2950
   232
      }
mathieu@2950
   233
      virtual bool HasSetter (void) const {
mathieu@2950
   234
        return true;
mathieu@2950
   235
      }
mathieu@2375
   236
      void (T::*m_setter) (U);
mathieu@2393
   237
      V (T::*m_getter) (void) const;
mathieu@2375
   238
    };
mathieu@2436
   239
  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
mathieu@2375
   240
}
mathieu@2375
   241
mathieu@2461
   242
template <typename W, typename T, typename U, typename V>
mathieu@2436
   243
Ptr<const AttributeAccessor>
mathieu@2435
   244
DoMakeAccessorHelperTwo (V (T::*getter) (void) const, 
mathieu@2424
   245
			  void (T::*setter) (U))
mathieu@2375
   246
{
mathieu@2461
   247
  return DoMakeAccessorHelperTwo<W> (setter, getter);
mathieu@2375
   248
}
mathieu@2375
   249
mathieu@3527
   250
template <typename W, typename T, typename U, typename V>
mathieu@3527
   251
Ptr<const AttributeAccessor>
mathieu@3527
   252
DoMakeAccessorHelperTwo (bool (T::*setter) (U), 
mathieu@3527
   253
                         V (T::*getter) (void) const)
mathieu@3527
   254
{
mathieu@3527
   255
  class MemberMethod : public AccessorHelper<T,W>
mathieu@3527
   256
    {
mathieu@3527
   257
    public:
mathieu@3527
   258
      MemberMethod (bool (T::*setter) (U), 
mathieu@3527
   259
		    V (T::*getter) (void) const)
mathieu@3527
   260
	: AccessorHelper<T,W> (),
mathieu@3527
   261
	m_setter (setter),
mathieu@3527
   262
	m_getter (getter)
mathieu@3527
   263
	{}
mathieu@3527
   264
    private:
mathieu@3527
   265
      virtual bool DoSet (T *object, const W *v) const {
mathieu@3763
   266
        typename AccessorTrait<U>::Result tmp;
mathieu@3763
   267
        bool ok = v->GetAccessor (tmp);
mathieu@3763
   268
        if (!ok)
mathieu@3763
   269
          {
mathieu@3763
   270
            return false;
mathieu@3763
   271
          }
mathieu@3763
   272
	ok = (object->*m_setter) (tmp);
mathieu@3527
   273
        return ok;
mathieu@3527
   274
      }
mathieu@3527
   275
      virtual bool DoGet (const T *object, W *v) const {
mathieu@3527
   276
	v->Set ((object->*m_getter) ());
mathieu@3527
   277
	return true;
mathieu@3527
   278
      }
mathieu@3527
   279
      virtual bool HasGetter (void) const {
mathieu@3527
   280
        return true;
mathieu@3527
   281
      }
mathieu@3527
   282
      virtual bool HasSetter (void) const {
mathieu@3527
   283
        return true;
mathieu@3527
   284
      }
mathieu@3527
   285
      bool (T::*m_setter) (U);
mathieu@3527
   286
      V (T::*m_getter) (void) const;
mathieu@3527
   287
    };
mathieu@3527
   288
  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
mathieu@3527
   289
}
mathieu@3527
   290
mathieu@3527
   291
template <typename W, typename T, typename U, typename V>
mathieu@3527
   292
Ptr<const AttributeAccessor>
mathieu@3527
   293
DoMakeAccessorHelperTwo (bool (T::*getter) (void) const, 
mathieu@3527
   294
                         void (T::*setter) (U))
mathieu@3527
   295
{
mathieu@3527
   296
  return DoMakeAccessorHelperTwo<W> (setter, getter);
mathieu@3527
   297
}
mathieu@3527
   298
mathieu@2461
   299
template <typename V, typename T1>
mathieu@2436
   300
Ptr<const AttributeAccessor>
mathieu@2435
   301
MakeAccessorHelper (T1 a1)
mathieu@2408
   302
{
mathieu@2461
   303
  return DoMakeAccessorHelperOne<V> (a1);
mathieu@2408
   304
}
mathieu@2408
   305
mathieu@2461
   306
template <typename V, typename T1, typename T2>
mathieu@2436
   307
Ptr<const AttributeAccessor>
mathieu@2435
   308
MakeAccessorHelper (T1 a1, T2 a2)
mathieu@2408
   309
{
mathieu@2461
   310
  return DoMakeAccessorHelperTwo<V> (a1, a2);
mathieu@2408
   311
}
mathieu@2408
   312
mathieu@2371
   313
} // namespace ns3
mathieu@2371
   314
mathieu@2449
   315
#endif /* ATTRIBUTE_ACCESSOR_HELPER_H */