A Discrete-Event Network Simulator
API
pointer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #ifndef NS_POINTER_H
20 #define NS_POINTER_H
21 
22 #include "attribute.h"
23 #include "object.h"
24 
31 namespace ns3
32 {
33 
34 // Additional docs for class PointerValue:
37 {
38  public:
39  PointerValue();
40 
46  PointerValue(Ptr<Object> object);
47 
53  void SetObject(Ptr<Object> object);
54 
59  Ptr<Object> GetObject() const;
60 
67  template <typename T>
68  PointerValue(const Ptr<T>& object);
69 
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 
82  template <typename T>
83  Ptr<T> Get() const;
84 
85  template <typename T>
86  bool GetAccessor(Ptr<T>& value) const;
87 
88  Ptr<AttributeValue> Copy() const override;
89  std::string SerializeToString(Ptr<const AttributeChecker> checker) const override;
90  bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) override;
91 
92  private:
94 };
95 
97 {
98  public:
103  virtual TypeId GetPointeeTypeId() const = 0;
104 };
105 
111 template <typename T>
113 
114 } // namespace ns3
115 
116 /***************************************************************
117  * Implementation of the templates declared above.
118  ***************************************************************/
119 
120 namespace ns3
121 {
122 
123 namespace internal
124 {
125 
127 template <typename T>
129 {
130  bool Check(const AttributeValue& val) const override
131  {
132  const PointerValue* value = dynamic_cast<const PointerValue*>(&val);
133  if (value == nullptr)
134  {
135  return false;
136  }
137  if (!value->GetObject())
138  {
139  // a null pointer is a valid value
140  return true;
141  }
142  T* ptr = dynamic_cast<T*>(PeekPointer(value->GetObject()));
143  if (ptr == nullptr)
144  {
145  return false;
146  }
147  return true;
148  }
149 
150  std::string GetValueTypeName() const override
151  {
152  return "ns3::PointerValue";
153  }
154 
155  bool HasUnderlyingTypeInformation() const override
156  {
157  return true;
158  }
159 
160  std::string GetUnderlyingTypeInformation() const override
161  {
162  TypeId tid = T::GetTypeId();
163  return "ns3::Ptr< " + tid.GetName() + " >";
164  }
165 
166  Ptr<AttributeValue> Create() const override
167  {
168  return ns3::Create<PointerValue>();
169  }
170 
171  bool Copy(const AttributeValue& source, AttributeValue& destination) const override
172  {
173  const PointerValue* src = dynamic_cast<const PointerValue*>(&source);
174  PointerValue* dst = dynamic_cast<PointerValue*>(&destination);
175  if (src == nullptr || dst == nullptr)
176  {
177  return false;
178  }
179  *dst = *src;
180  return true;
181  }
182 
183  TypeId GetPointeeTypeId() const override
184  {
185  return T::GetTypeId();
186  }
187 };
188 
189 } // namespace internal
190 
191 template <typename T>
193 {
194  m_value = object;
195 }
196 
197 template <typename T>
198 void
199 PointerValue::Set(const Ptr<T>& object)
200 {
201  m_value = object;
202 }
203 
204 template <typename T>
205 Ptr<T>
207 {
208  T* v = dynamic_cast<T*>(PeekPointer(m_value));
209  return v;
210 }
211 
212 template <typename T>
213 PointerValue::operator Ptr<T>() const
214 {
215  return Get<T>();
216 }
217 
218 template <typename T>
219 bool
221 {
222  Ptr<T> ptr = dynamic_cast<T*>(PeekPointer(m_value));
223  if (!ptr)
224  {
225  return false;
226  }
227  v = ptr;
228  return true;
229 }
230 
232 
233 template <typename T>
236 {
237  return Create<internal::PointerChecker<T>>();
238 }
239 
240 } // namespace ns3
241 
242 #endif /* NS_POINTER_H */
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Represent the type of an attribute.
Definition: attribute.h:168
Hold a value for an Attribute.
Definition: attribute.h:70
AttributeChecker implementation for PointerValue.
Definition: pointer.h:97
virtual TypeId GetPointeeTypeId() const =0
Get the TypeId of the base type.
Hold objects of type Ptr<T>.
Definition: pointer.h:37
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Definition: pointer.cc:71
Ptr< Object > m_value
The stored Pointer instance.
Definition: pointer.h:93
Ptr< AttributeValue > Copy() const override
Definition: pointer.cc:64
Ptr< Object > GetObject() const
Get the Object referenced by the PointerValue.
Definition: pointer.cc:57
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Definition: pointer.cc:80
void Set(const Ptr< T > &value)
Set the value.
Definition: pointer.h:199
Ptr< T > Get() const
Definition: pointer.h:206
void SetObject(Ptr< Object > object)
Set the value from by reference an Object.
Definition: pointer.cc:50
bool GetAccessor(Ptr< T > &value) const
Access the Pointer value as type T.
Definition: pointer.h:220
a unique identifier for an interface.
Definition: type-id.h:60
std::string GetName() const
Get the name.
Definition: type-id.cc:995
PointerChecker implementation.
Definition: pointer.h:129
std::string GetUnderlyingTypeInformation() const override
Definition: pointer.h:160
bool HasUnderlyingTypeInformation() const override
Definition: pointer.h:155
Ptr< AttributeValue > Create() const override
Definition: pointer.h:166
bool Check(const AttributeValue &val) const override
Definition: pointer.h:130
std::string GetValueTypeName() const override
Definition: pointer.h:150
bool Copy(const AttributeValue &source, AttributeValue &destination) const override
Copy the source to the destination.
Definition: pointer.h:171
TypeId GetPointeeTypeId() const override
Get the TypeId of the base type.
Definition: pointer.h:183
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition: pointer.h:235
#define ATTRIBUTE_ACCESSOR_DEFINE(type)
Define the attribute accessor functions MakeTypeAccessor for class type.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:488
value
Definition: second.py:41
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.