A Discrete-Event Network Simulator
API
pointer.h
Go to the documentation of this file.
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 
32 namespace ns3 {
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 (void) 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 (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:
94 };
95 
96 
98 {
99 public:
100 
105  virtual TypeId GetPointeeTypeId (void) const = 0;
106 };
107 
113 template <typename T>
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 
129 template <typename T>
131 {
132  virtual bool Check (const AttributeValue &val) const {
133  const PointerValue *value = dynamic_cast<const PointerValue *> (&val);
134  if (value == 0)
135  {
136  return false;
137  }
138  if (value->GetObject () == 0)
139  {
140  return true;
141  }
142  T *ptr = dynamic_cast<T*> (PeekPointer (value->GetObject ()));
143  if (ptr == 0)
144  {
145  return false;
146  }
147  return true;
148  }
149  virtual std::string GetValueTypeName (void) const {
150  return "ns3::PointerValue";
151  }
152  virtual bool HasUnderlyingTypeInformation (void) const {
153  return true;
154  }
155  virtual std::string GetUnderlyingTypeInformation (void) const {
156  TypeId tid = T::GetTypeId ();
157  return "ns3::Ptr< " + tid.GetName () + " >";
158  }
159  virtual Ptr<AttributeValue> Create (void) const {
160  return ns3::Create<PointerValue> ();
161  }
162  virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
163  const PointerValue *src = dynamic_cast<const PointerValue *> (&source);
164  PointerValue *dst = dynamic_cast<PointerValue *> (&destination);
165  if (src == 0 || dst == 0)
166  {
167  return false;
168  }
169  *dst = *src;
170  return true;
171  }
172  virtual TypeId GetPointeeTypeId (void) const {
173  return T::GetTypeId ();
174  }
175 };
176 
177 } // namespace internal
178 
179 template <typename T>
181 {
182  m_value = object;
183 }
184 
185 template <typename T>
186 void
187 PointerValue::Set (const Ptr<T> &object)
188 {
189  m_value = object;
190 }
191 
192 template <typename T>
193 Ptr<T>
194 PointerValue::Get (void) const
195 {
196  T *v = dynamic_cast<T *> (PeekPointer (m_value));
197  return v;
198 }
199 
200 template <typename T>
201 PointerValue::operator Ptr<T> () const
202 {
203  return Get<T> ();
204 }
205 
206 template <typename T>
207 bool
209 {
210  Ptr<T> ptr = dynamic_cast<T*> (PeekPointer (m_value));
211  if (ptr == 0)
212  {
213  return false;
214  }
215  v = ptr;
216  return true;
217 }
218 
219 
221 
222 template <typename T>
225 {
226  return Create<internal::PointerChecker<T> > ();
227 }
228 
229 
230 } // namespace ns3
231 
232 #endif /* NS_POINTER_H */
Represent the type of an attribute.
Definition: attribute.h:166
std::string GetName(void) const
Get the name.
Definition: type-id.cc:969
Ptr< AttributeChecker > MakePointerChecker(void)
Create a PointerChecker for a type.
Definition: pointer.h:224
Hold a value for an Attribute.
Definition: attribute.h:68
Ptr< Object > GetObject(void) const
Get the Object referenced by the PointerValue.
Definition: pointer.cc:55
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:564
virtual TypeId GetPointeeTypeId(void) const =0
Get the TypeId of the base type.
Ptr< Object > m_value
The stored Pointer instance.
Definition: pointer.h:93
virtual bool Copy(const AttributeValue &source, AttributeValue &destination) const
Copy the source to the destination.
Definition: pointer.h:162
PointerChecker implementation.
Definition: pointer.h:130
virtual bool Check(const AttributeValue &val) const
Definition: pointer.h:132
virtual std::string GetValueTypeName(void) const
Definition: pointer.h:149
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
virtual Ptr< AttributeValue > Create(void) const
Definition: pointer.h:159
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
virtual std::string GetUnderlyingTypeInformation(void) const
Definition: pointer.h:155
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
Definition: pointer.cc:77
void SetObject(Ptr< Object > object)
Set the value from by reference an Object.
Definition: pointer.cc:48
#define ATTRIBUTE_ACCESSOR_DEFINE(type)
Define the attribute accessor functions MakeTypeAccessor for class type.
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
AttributeChecker implementation for PointerValue.
Definition: pointer.h:97
bool GetAccessor(Ptr< T > &value) const
Access the Pointer value as type T.
Definition: pointer.h:208
Ptr< T > Get(void) const
Definition: pointer.h:194
virtual TypeId GetPointeeTypeId(void) const
Get the TypeId of the base type.
Definition: pointer.h:172
virtual bool HasUnderlyingTypeInformation(void) const
Definition: pointer.h:152
virtual std::string SerializeToString(Ptr< const AttributeChecker > checker) const
Definition: pointer.cc:68
a unique identifier for an interface.
Definition: type-id.h:58
void Set(const Ptr< T > &value)
Set the value.
Definition: pointer.h:187
virtual Ptr< AttributeValue > Copy(void) const
Definition: pointer.cc:62