A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 auto 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  return ptr;
144  }
145 
146  std::string GetValueTypeName() const override
147  {
148  return "ns3::PointerValue";
149  }
150 
151  bool HasUnderlyingTypeInformation() const override
152  {
153  return true;
154  }
155 
156  std::string GetUnderlyingTypeInformation() const override
157  {
158  TypeId tid = T::GetTypeId();
159  return "ns3::Ptr< " + tid.GetName() + " >";
160  }
161 
162  Ptr<AttributeValue> Create() const override
163  {
164  return ns3::Create<PointerValue>();
165  }
166 
167  bool Copy(const AttributeValue& source, AttributeValue& destination) const override
168  {
169  const auto src = dynamic_cast<const PointerValue*>(&source);
170  auto dst = dynamic_cast<PointerValue*>(&destination);
171  if (src == nullptr || dst == nullptr)
172  {
173  return false;
174  }
175  *dst = *src;
176  return true;
177  }
178 
179  TypeId GetPointeeTypeId() const override
180  {
181  return T::GetTypeId();
182  }
183 };
184 
185 } // namespace internal
186 
187 template <typename T>
189 {
190  m_value = object;
191 }
192 
193 template <typename T>
194 void
195 PointerValue::Set(const Ptr<T>& object)
196 {
197  m_value = object;
198 }
199 
200 template <typename T>
201 Ptr<T>
203 {
204  T* v = dynamic_cast<T*>(PeekPointer(m_value));
205  return v;
206 }
207 
208 template <typename T>
209 PointerValue::operator Ptr<T>() const
210 {
211  return Get<T>();
212 }
213 
214 template <typename T>
215 bool
217 {
218  Ptr<T> ptr = dynamic_cast<T*>(PeekPointer(m_value));
219  if (!ptr)
220  {
221  return false;
222  }
223  v = ptr;
224  return true;
225 }
226 
228 
229 template <typename T>
232 {
233  return Create<internal::PointerChecker<T>>();
234 }
235 
236 } // namespace ns3
237 
238 #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
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< AttributeValue > Copy() const override
Definition: pointer.cc:64
Ptr< Object > GetObject() const
Get the Object referenced by the PointerValue.
Definition: pointer.cc:57
Ptr< T > Get() const
Definition: pointer.h:202
Ptr< Object > m_value
Definition: pointer.h:93
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Definition: pointer.cc:80
void Set(const Ptr< T > &value)
Definition: pointer.h:195
void SetObject(Ptr< Object > object)
Set the value from by reference an Object.
Definition: pointer.cc:50
bool GetAccessor(Ptr< T > &value) const
Definition: pointer.h:216
a unique identifier for an interface.
Definition: type-id.h:59
std::string GetName() const
Get the name.
Definition: type-id.cc:991
PointerChecker implementation.
Definition: pointer.h:129
std::string GetUnderlyingTypeInformation() const override
Definition: pointer.h:156
bool HasUnderlyingTypeInformation() const override
Definition: pointer.h:151
Ptr< AttributeValue > Create() const override
Definition: pointer.h:162
bool Check(const AttributeValue &val) const override
Definition: pointer.h:130
std::string GetValueTypeName() const override
Definition: pointer.h:146
bool Copy(const AttributeValue &source, AttributeValue &destination) const override
Copy the source to the destination.
Definition: pointer.h:167
TypeId GetPointeeTypeId() const override
Get the TypeId of the base type.
Definition: pointer.h:179
#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.
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition: pointer.h:231
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:449
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.