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  {
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  }
150  virtual std::string GetValueTypeName (void) const
151  {
152  return "ns3::PointerValue";
153  }
154  virtual bool HasUnderlyingTypeInformation (void) const
155  {
156  return true;
157  }
158  virtual std::string GetUnderlyingTypeInformation (void) const
159  {
160  TypeId tid = T::GetTypeId ();
161  return "ns3::Ptr< " + tid.GetName () + " >";
162  }
163  virtual Ptr<AttributeValue> Create (void) const
164  {
165  return ns3::Create<PointerValue> ();
166  }
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  }
178  virtual TypeId GetPointeeTypeId (void) const
179  {
180  return T::GetTypeId ();
181  }
182 };
183 
184 } // namespace internal
185 
186 template <typename T>
188 {
189  m_value = object;
190 }
191 
192 template <typename T>
193 void
194 PointerValue::Set (const Ptr<T> &object)
195 {
196  m_value = object;
197 }
198 
199 template <typename T>
200 Ptr<T>
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
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 
228 
229 template <typename T>
232 {
233  return Create<internal::PointerChecker<T> > ();
234 }
235 
236 
237 } // namespace ns3
238 
239 #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:977
Ptr< T > Get(void) const
Definition: pointer.h:201
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:411
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:167
PointerChecker implementation.
Definition: pointer.h:130
virtual bool Check(const AttributeValue &val) const
Definition: pointer.h:132
Ptr< AttributeChecker > MakePointerChecker(void)
Create a PointerChecker for a type.
Definition: pointer.h:231
virtual std::string GetValueTypeName(void) const
Definition: pointer.h:150
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
virtual Ptr< AttributeValue > Create(void) const
Definition: pointer.h:163
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:158
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:215
virtual TypeId GetPointeeTypeId(void) const
Get the TypeId of the base type.
Definition: pointer.h:178
virtual bool HasUnderlyingTypeInformation(void) const
Definition: pointer.h:154
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:194
virtual Ptr< AttributeValue > Copy(void) const
Definition: pointer.cc:62