00001 #ifndef OBJECT_VECTOR_H
00002 #define OBJECT_VECTOR_H
00003
00004 #include <vector>
00005 #include "object.h"
00006 #include "ptr.h"
00007 #include "attribute.h"
00008
00009 namespace ns3 {
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 class ObjectVectorValue : public AttributeValue
00020 {
00021 public:
00022 typedef std::vector<Ptr<Object> >::const_iterator Iterator;
00023
00024 ObjectVectorValue ();
00025
00026
00027
00028
00029 Iterator Begin (void) const;
00030
00031
00032
00033 Iterator End (void) const;
00034
00035
00036
00037 uint32_t GetN (void) const;
00038
00039
00040
00041
00042 Ptr<Object> Get (uint32_t i) const;
00043
00044 virtual Ptr<AttributeValue> Copy (void) const;
00045 virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
00046 virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
00047
00048 private:
00049 friend class ObjectVectorAccessor;
00050 std::vector<Ptr<Object> > m_objects;
00051 };
00052
00053 template <typename T, typename U>
00054 Ptr<const AttributeAccessor>
00055 MakeObjectVectorAccessor (U T::*memberVector);
00056
00057 template <typename T, typename U, typename INDEX>
00058 Ptr<const AttributeAccessor>
00059 MakeObjectVectorAccessor (Ptr<U> (T::*get) (INDEX) const,
00060 INDEX (T::*getN) (void) const);
00061
00062 template <typename T, typename U, typename INDEX>
00063 Ptr<const AttributeAccessor>
00064 MakeObjectVectorAccessor (INDEX (T::*getN) (void) const,
00065 Ptr<U> (T::*get) (INDEX) const);
00066
00067 class ObjectVectorChecker : public AttributeChecker
00068 {
00069 public:
00070 virtual TypeId GetItemTypeId (void) const = 0;
00071 };
00072
00073 template <typename T>
00074 Ptr<const AttributeChecker> MakeObjectVectorChecker (void);
00075
00076 }
00077
00078 namespace ns3 {
00079
00080 namespace internal {
00081
00082 template <typename T>
00083 class AnObjectVectorChecker : public ObjectVectorChecker
00084 {
00085 public:
00086 virtual TypeId GetItemTypeId (void) const {
00087 return T::GetTypeId ();
00088 }
00089 virtual bool Check (const AttributeValue &value) const {
00090 return dynamic_cast<const ObjectVectorValue *> (&value) != 0;
00091 }
00092 virtual std::string GetValueTypeName (void) const {
00093 return "ns3::ObjectVectorValue";
00094 }
00095 virtual bool HasUnderlyingTypeInformation (void) const {
00096 return true;
00097 }
00098 virtual std::string GetUnderlyingTypeInformation (void) const {
00099 return "ns3::Ptr< " + T::GetTypeId ().GetName () + " >";
00100 }
00101 virtual Ptr<AttributeValue> Create (void) const {
00102 return ns3::Create<ObjectVectorValue> ();
00103 }
00104 virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
00105 const ObjectVectorValue *src = dynamic_cast<const ObjectVectorValue *> (&source);
00106 ObjectVectorValue *dst = dynamic_cast<ObjectVectorValue *> (&destination);
00107 if (src == 0 || dst == 0)
00108 {
00109 return false;
00110 }
00111 *dst = *src;
00112 return true;
00113 }
00114 };
00115
00116 }
00117
00118
00119 class ObjectVectorAccessor : public AttributeAccessor
00120 {
00121 public:
00122 virtual bool Set (ObjectBase * object, const AttributeValue &value) const;
00123 virtual bool Get (const ObjectBase * object, AttributeValue &value) const;
00124 virtual bool HasGetter (void) const;
00125 virtual bool HasSetter (void) const;
00126 private:
00127 virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const = 0;
00128 virtual Ptr<Object> DoGet (const ObjectBase *object, uint32_t i) const = 0;
00129 };
00130
00131 template <typename T, typename U>
00132 Ptr<const AttributeAccessor>
00133 MakeObjectVectorAccessor (U T::*memberVector)
00134 {
00135 struct MemberStdContainer : public ObjectVectorAccessor
00136 {
00137 virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const {
00138 const T *obj = dynamic_cast<const T *> (object);
00139 if (obj == 0)
00140 {
00141 return false;
00142 }
00143 *n = (obj->*m_memberVector).size ();
00144 return true;
00145 }
00146 virtual Ptr<Object> DoGet (const ObjectBase *object, uint32_t i) const {
00147 const T *obj = static_cast<const T *> (object);
00148 typename U::const_iterator begin = (obj->*m_memberVector).begin ();
00149 typename U::const_iterator end = (obj->*m_memberVector).end ();
00150 uint32_t k = 0;
00151 for (typename U::const_iterator j = begin; j != end; j++, k++)
00152 {
00153 if (k == i)
00154 {
00155 return *j;
00156 break;
00157 }
00158 }
00159 NS_ASSERT (false);
00160
00161 return 0;
00162 }
00163 U T::*m_memberVector;
00164 } *spec = new MemberStdContainer ();
00165 spec->m_memberVector = memberVector;
00166 return Ptr<const AttributeAccessor> (spec, false);
00167 }
00168
00169 template <typename T, typename U, typename INDEX>
00170 Ptr<const AttributeAccessor>
00171 MakeObjectVectorAccessor (Ptr<U> (T::*get) (INDEX) const,
00172 INDEX (T::*getN) (void) const)
00173 {
00174 struct MemberGetters : public ObjectVectorAccessor
00175 {
00176 virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const {
00177 const T *obj = dynamic_cast<const T *> (object);
00178 if (obj == 0)
00179 {
00180 return false;
00181 }
00182 *n = (obj->*m_getN) ();
00183 return true;
00184 }
00185 virtual Ptr<Object> DoGet (const ObjectBase *object, uint32_t i) const {
00186 const T *obj = static_cast<const T *> (object);
00187 return (obj->*m_get) (i);
00188 }
00189 Ptr<U> (T::*m_get) (INDEX) const;
00190 INDEX (T::*m_getN) (void) const;
00191 } *spec = new MemberGetters ();
00192 spec->m_get = get;
00193 spec->m_getN = getN;
00194 return Ptr<const AttributeAccessor> (spec, false);
00195 }
00196
00197 template <typename T, typename U, typename INDEX>
00198 Ptr<const AttributeAccessor>
00199 MakeObjectVectorAccessor (INDEX (T::*getN) (void) const,
00200 Ptr<U> (T::*get) (INDEX) const)
00201 {
00202 return MakeObjectVectorAccessor (get, getN);
00203 }
00204
00205 template <typename T>
00206 Ptr<const AttributeChecker> MakeObjectVectorChecker (void)
00207 {
00208 return Create<internal::AnObjectVectorChecker<T> > ();
00209 }
00210
00211
00212 }
00213
00214 #endif