src/core/object-vector.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 2970 0384d6f6cca1
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
mathieu@2405
     1
#ifndef OBJECT_VECTOR_H
mathieu@2405
     2
#define OBJECT_VECTOR_H
mathieu@2405
     3
mathieu@2405
     4
#include <vector>
mathieu@2405
     5
#include "object.h"
mathieu@2405
     6
#include "ptr.h"
mathieu@2438
     7
#include "attribute.h"
mathieu@2405
     8
mathieu@2405
     9
namespace ns3 {
mathieu@2405
    10
mathieu@2930
    11
/**
tomh@3182
    12
 * \ingroup object
tomh@3182
    13
 * 
mathieu@2930
    14
 * \brief contain a vector of ns3::Object pointers.
mathieu@2930
    15
 *
mathieu@2930
    16
 * This class it used to get attribute access to an array of
mathieu@2930
    17
 * ns3::Object pointers.
mathieu@2930
    18
 */
mathieu@2965
    19
class ObjectVectorValue : public AttributeValue
mathieu@2405
    20
{
mathieu@2405
    21
public:
mathieu@2405
    22
  typedef std::vector<Ptr<Object> >::const_iterator Iterator;
mathieu@2405
    23
mathieu@2965
    24
  ObjectVectorValue ();
mathieu@2405
    25
mathieu@2967
    26
  /**
mathieu@2967
    27
   * \returns an iterator to the first object contained in this vector
mathieu@2967
    28
   */
mathieu@2405
    29
  Iterator Begin (void) const;
mathieu@2967
    30
  /**
mathieu@2967
    31
   * \returns an iterator to the last object contained in this vector
mathieu@2967
    32
   */
mathieu@2405
    33
  Iterator End (void) const;
mathieu@2967
    34
  /**
mathieu@2967
    35
   * \returns the number of objects contained in this vector.
mathieu@2967
    36
   */
mathieu@2405
    37
  uint32_t GetN (void) const;
mathieu@2967
    38
  /**
mathieu@2967
    39
   * \param i the index of the requested object.
mathieu@2967
    40
   * \returns the requested object
mathieu@2967
    41
   */
mathieu@2405
    42
  Ptr<Object> Get (uint32_t i) const;
mathieu@2405
    43
mathieu@2965
    44
  virtual Ptr<AttributeValue> Copy (void) const;
mathieu@2965
    45
  virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
mathieu@2965
    46
  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
mathieu@2965
    47
mathieu@2405
    48
private:
mathieu@2435
    49
  friend class ObjectVectorAccessor;
mathieu@2405
    50
  std::vector<Ptr<Object> > m_objects;
mathieu@2405
    51
};
mathieu@2405
    52
mathieu@2405
    53
template <typename T, typename U>
mathieu@2436
    54
Ptr<const AttributeAccessor>
mathieu@2435
    55
MakeObjectVectorAccessor (U T::*memberVector);
mathieu@2405
    56
mathieu@2405
    57
template <typename T, typename U, typename INDEX>
mathieu@2436
    58
Ptr<const AttributeAccessor>
mathieu@2435
    59
MakeObjectVectorAccessor (Ptr<U> (T::*get) (INDEX) const,
mathieu@2599
    60
			  INDEX (T::*getN) (void) const);
mathieu@2405
    61
mathieu@2405
    62
template <typename T, typename U, typename INDEX>
mathieu@2436
    63
Ptr<const AttributeAccessor>
mathieu@2435
    64
MakeObjectVectorAccessor (INDEX (T::*getN) (void) const,
mathieu@2599
    65
			  Ptr<U> (T::*get) (INDEX) const);
mathieu@2405
    66
mathieu@2933
    67
class ObjectVectorChecker : public AttributeChecker
mathieu@2933
    68
{
mathieu@2933
    69
public:
mathieu@2933
    70
  virtual TypeId GetItemTypeId (void) const = 0;
mathieu@2933
    71
};
mathieu@2405
    72
mathieu@2933
    73
template <typename T>
mathieu@2933
    74
Ptr<const AttributeChecker> MakeObjectVectorChecker (void);
mathieu@2427
    75
mathieu@2405
    76
} // namespace ns3
mathieu@2405
    77
mathieu@2405
    78
namespace ns3 {
mathieu@2405
    79
mathieu@2933
    80
namespace internal {
mathieu@2933
    81
mathieu@2933
    82
template <typename T>
mathieu@2933
    83
class AnObjectVectorChecker : public ObjectVectorChecker
mathieu@2933
    84
{
mathieu@2933
    85
public:
mathieu@2933
    86
  virtual TypeId GetItemTypeId (void) const {
mathieu@2933
    87
    return T::GetTypeId ();
mathieu@2933
    88
  }
mathieu@2965
    89
  virtual bool Check (const AttributeValue &value) const {
mathieu@2965
    90
    return dynamic_cast<const ObjectVectorValue *> (&value) != 0;
mathieu@2933
    91
  }
mathieu@2969
    92
  virtual std::string GetValueTypeName (void) const {
mathieu@2967
    93
    return "ns3::ObjectVectorValue";
mathieu@2933
    94
  }
mathieu@2969
    95
  virtual bool HasUnderlyingTypeInformation (void) const {
mathieu@2933
    96
    return true;
mathieu@2933
    97
  }
mathieu@2969
    98
  virtual std::string GetUnderlyingTypeInformation (void) const {
mathieu@2970
    99
    return "ns3::Ptr< " + T::GetTypeId ().GetName () + " >";
mathieu@2933
   100
  }
mathieu@2965
   101
  virtual Ptr<AttributeValue> Create (void) const {
mathieu@2965
   102
    return ns3::Create<ObjectVectorValue> ();
mathieu@2965
   103
  }
mathieu@2965
   104
  virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
mathieu@2965
   105
    const ObjectVectorValue *src = dynamic_cast<const ObjectVectorValue *> (&source);
mathieu@2965
   106
    ObjectVectorValue *dst = dynamic_cast<ObjectVectorValue *> (&destination);
mathieu@2965
   107
    if (src == 0 || dst == 0)
mathieu@2965
   108
      {
mathieu@2965
   109
	return false;
mathieu@2965
   110
      }
mathieu@2965
   111
    *dst = *src;
mathieu@2965
   112
    return true;    
mathieu@2933
   113
  }
mathieu@2933
   114
};
mathieu@2933
   115
mathieu@2933
   116
} // namespace internal
mathieu@2933
   117
mathieu@2933
   118
mathieu@2436
   119
class ObjectVectorAccessor : public AttributeAccessor
mathieu@2405
   120
{
mathieu@2405
   121
public:
mathieu@2965
   122
  virtual bool Set (ObjectBase * object, const AttributeValue &value) const;
mathieu@2965
   123
  virtual bool Get (const ObjectBase * object, AttributeValue &value) const;
mathieu@2950
   124
  virtual bool HasGetter (void) const;
mathieu@2950
   125
  virtual bool HasSetter (void) const;
mathieu@2405
   126
private:
mathieu@2405
   127
  virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const = 0;
mathieu@2405
   128
  virtual Ptr<Object> DoGet (const ObjectBase *object, uint32_t i) const = 0;
mathieu@2405
   129
};
mathieu@2405
   130
mathieu@2405
   131
template <typename T, typename U>
mathieu@2436
   132
Ptr<const AttributeAccessor>
mathieu@2435
   133
MakeObjectVectorAccessor (U T::*memberVector)
mathieu@2405
   134
{
mathieu@2435
   135
  struct MemberStdContainer : public ObjectVectorAccessor
mathieu@2405
   136
  {
mathieu@2405
   137
    virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const {
mathieu@2405
   138
      const T *obj = dynamic_cast<const T *> (object);
mathieu@2405
   139
      if (obj == 0)
mathieu@2405
   140
	{
mathieu@2405
   141
	  return false;
mathieu@2405
   142
	}
mathieu@2405
   143
      *n = (obj->*m_memberVector).size ();
mathieu@2405
   144
      return true;
mathieu@2405
   145
    }
mathieu@2405
   146
    virtual Ptr<Object> DoGet (const ObjectBase *object, uint32_t i) const {
mathieu@2405
   147
      const T *obj = static_cast<const T *> (object);
mathieu@2497
   148
      typename U::const_iterator begin = (obj->*m_memberVector).begin ();
mathieu@2497
   149
      typename U::const_iterator end = (obj->*m_memberVector).end ();
mathieu@2497
   150
      uint32_t k = 0;
mathieu@2497
   151
      for (typename U::const_iterator j = begin; j != end; j++, k++)
mathieu@2497
   152
	{
mathieu@2497
   153
	  if (k == i)
mathieu@2497
   154
	    {
mathieu@2497
   155
	      return *j;
mathieu@2497
   156
	      break;
mathieu@2497
   157
	    }
mathieu@2497
   158
	}
mathieu@2497
   159
      NS_ASSERT (false);
mathieu@2497
   160
      // quiet compiler.
mathieu@2497
   161
      return 0;
mathieu@2405
   162
    }
mathieu@2405
   163
    U T::*m_memberVector;
mathieu@2405
   164
  } *spec = new MemberStdContainer ();
mathieu@2405
   165
  spec->m_memberVector = memberVector;
mathieu@2436
   166
  return Ptr<const AttributeAccessor> (spec, false);
mathieu@2405
   167
}
mathieu@2405
   168
mathieu@2405
   169
template <typename T, typename U, typename INDEX>
mathieu@2436
   170
Ptr<const AttributeAccessor>
mathieu@2435
   171
MakeObjectVectorAccessor (Ptr<U> (T::*get) (INDEX) const,
mathieu@2405
   172
			   INDEX (T::*getN) (void) const)
mathieu@2405
   173
{
mathieu@2435
   174
  struct MemberGetters : public ObjectVectorAccessor
mathieu@2405
   175
  {
mathieu@2405
   176
    virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const {
mathieu@2405
   177
      const T *obj = dynamic_cast<const T *> (object);
mathieu@2405
   178
      if (obj == 0)
mathieu@2405
   179
	{
mathieu@2405
   180
	  return false;
mathieu@2405
   181
	}
mathieu@2405
   182
      *n = (obj->*m_getN) ();
mathieu@2405
   183
      return true;
mathieu@2405
   184
    }
mathieu@2405
   185
    virtual Ptr<Object> DoGet (const ObjectBase *object, uint32_t i) const {
mathieu@2405
   186
      const T *obj = static_cast<const T *> (object);
mathieu@2405
   187
      return (obj->*m_get) (i);
mathieu@2405
   188
    }
mathieu@2405
   189
    Ptr<U> (T::*m_get) (INDEX) const;
mathieu@2405
   190
    INDEX (T::*m_getN) (void) const;
mathieu@2405
   191
  } *spec = new MemberGetters ();
mathieu@2405
   192
  spec->m_get = get;
mathieu@2405
   193
  spec->m_getN = getN;
mathieu@2436
   194
  return Ptr<const AttributeAccessor> (spec, false);
mathieu@2405
   195
}
mathieu@2405
   196
mathieu@2405
   197
template <typename T, typename U, typename INDEX>
mathieu@2436
   198
Ptr<const AttributeAccessor>
mathieu@2435
   199
MakeObjectVectorAccessor (INDEX (T::*getN) (void) const,
mathieu@2405
   200
			   Ptr<U> (T::*get) (INDEX) const)
mathieu@2405
   201
{
mathieu@2435
   202
  return MakeObjectVectorAccessor (get, getN);
mathieu@2405
   203
}
mathieu@2405
   204
mathieu@2933
   205
template <typename T>
mathieu@2933
   206
Ptr<const AttributeChecker> MakeObjectVectorChecker (void)
mathieu@2933
   207
{
mathieu@2933
   208
  return Create<internal::AnObjectVectorChecker<T> > ();
mathieu@2933
   209
}
mathieu@2933
   210
mathieu@2933
   211
mathieu@2405
   212
} // namespace ns3
mathieu@2405
   213
mathieu@2405
   214
#endif /* OBJECT_VECTOR_H */