1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #ifndef NS_POINTER_H
21 #define NS_POINTER_H
22 
23 #include "attribute.h"
24 #include "object.h"
25 
26 /**
27  * \file
28  * \ingroup attribute_Pointer
29  * ns3::PointerValue attribute value declarations and template implementations.
30  */
31 
32 namespace ns3 {
33 
34 //  Additional docs for class PointerValue:
35 /** Hold objects of type Ptr<T>. */
36 class PointerValue : public AttributeValue
37 {
38 public:
39   PointerValue ();
40 
41   /**
42    * Construct this PointerValue by referencing an explicit Object.
43    *
44    * \param [in] object The object to begin with.
45    */
46   PointerValue (Ptr<Object> object);
47 
48   /**
49    * Set the value from by reference an Object.
50    *
51    * \param [in] object The object to reference.
52    */
53   void SetObject (Ptr<Object> object);
54 
55   /**
56    * Get the Object referenced by the PointerValue.
57    * \returns The Object.
58    */
59   Ptr<Object> GetObject (void) const;
60 
61   /**
62    * Construct this PointerValue by referencing an explicit Object.
63    *
64    * \tparam T \deduced The type of the object.
65    * \param [in] object The object to begin with.
66    */
67   template <typename T>
68   PointerValue (const Ptr<T> & object);
69 
70   /**
71    * Cast to an Object of type \c T.
72    * \tparam T \explicit The type to cast to.
73    */
74   template <typename T>
75   operator Ptr<T> () const;
76 
77   // Documentation generated by print-introspected-doxygen.cc
78   template <typename T>
79   void Set (const Ptr<T> & value);
80 
81   /** \tparam T \explicit The type to cast to. */
82   template <typename T>
83   Ptr<T> Get (void) const;
84 
85   template <typename T>
86   bool GetAccessor (Ptr<T> &value) const;
87 
88   virtual Ptr<AttributeValue> Copy (void) const;
89   virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
90   virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
91 
92 private:
93   Ptr<Object> m_value;
94 };
95 
96 
97 class PointerChecker : public AttributeChecker
98 {
99 public:
100 
101   /**
102    * Get the TypeId of the base type.
103    * \returns The base TypeId.
104    */
105   virtual TypeId GetPointeeTypeId (void) const = 0;
106 };
107 
108 /**
109  * Create a PointerChecker for a type.
110  * \tparam T \explicit The underlying type.
111  * \returns The PointerChecker.
112  */
113 template <typename T>
114 Ptr<AttributeChecker> MakePointerChecker (void);
115 
116 } // namespace ns3
117 
118 
119 
120 /***************************************************************
121  *  Implementation of the templates declared above.
122  ***************************************************************/
123 
124 namespace ns3 {
125 
126 namespace internal {
127 
128 /** PointerChecker implementation. */
129 template <typename T>
130 class PointerChecker : public ns3::PointerChecker
131 {
Check(const AttributeValue & val)132   virtual bool Check (const AttributeValue &val) const
133   {
134     const PointerValue *value = dynamic_cast<const PointerValue *> (&val);
135     if (value == 0)
136       {
137         return false;
138       }
139     if (value->GetObject () == 0)
140       {
141         return true;
142       }
143     T *ptr = dynamic_cast<T*> (PeekPointer (value->GetObject ()));
144     if (ptr == 0)
145       {
146         return false;
147       }
148     return true;
149   }
GetValueTypeName(void)150   virtual std::string GetValueTypeName (void) const
151   {
152     return "ns3::PointerValue";
153   }
HasUnderlyingTypeInformation(void)154   virtual bool HasUnderlyingTypeInformation (void) const
155   {
156     return true;
157   }
GetUnderlyingTypeInformation(void)158   virtual std::string GetUnderlyingTypeInformation (void) const
159   {
160     TypeId tid = T::GetTypeId ();
161     return "ns3::Ptr< " + tid.GetName () + " >";
162   }
Create(void)163   virtual Ptr<AttributeValue> Create (void) const
164   {
165     return ns3::Create<PointerValue> ();
166   }
Copy(const AttributeValue & source,AttributeValue & destination)167   virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const
168   {
169     const PointerValue *src = dynamic_cast<const PointerValue *> (&source);
170     PointerValue *dst = dynamic_cast<PointerValue *> (&destination);
171     if (src == 0 || dst == 0)
172       {
173         return false;
174       }
175     *dst = *src;
176     return true;
177   }
GetPointeeTypeId(void)178   virtual TypeId GetPointeeTypeId (void) const
179   {
180     return T::GetTypeId ();
181   }
182 };
183 
184 } // namespace internal
185 
186 template <typename T>
PointerValue(const Ptr<T> & object)187 PointerValue::PointerValue (const Ptr<T> &object)
188 {
189   m_value = object;
190 }
191 
192 template <typename T>
193 void
Set(const Ptr<T> & object)194 PointerValue::Set (const Ptr<T> &object)
195 {
196   m_value = object;
197 }
198 
199 template <typename T>
200 Ptr<T>
Get(void)201 PointerValue::Get (void) const
202 {
203   T *v = dynamic_cast<T *> (PeekPointer (m_value));
204   return v;
205 }
206 
207 template <typename T>
208 PointerValue::operator Ptr<T> () const
209 {
210   return Get<T> ();
211 }
212 
213 template <typename T>
214 bool
GetAccessor(Ptr<T> & v)215 PointerValue::GetAccessor (Ptr<T> &v) const
216 {
217   Ptr<T> ptr = dynamic_cast<T*> (PeekPointer (m_value));
218   if (ptr == 0)
219     {
220       return false;
221     }
222   v = ptr;
223   return true;
224 }
225 
226 
227 ATTRIBUTE_ACCESSOR_DEFINE (Pointer);
228 
229 template <typename T>
230 Ptr<AttributeChecker>
MakePointerChecker(void)231 MakePointerChecker (void)
232 {
233   return Create<internal::PointerChecker<T> > ();
234 }
235 
236 
237 } // namespace ns3
238 
239 #endif /* NS_POINTER_H */
240