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