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 
71  template <typename T>
72  operator Ptr<T> () const;
73 
74  // Documentation generated by print-introspected-doxygen.cc
75  template <typename T>
76  void Set (const Ptr<T> & value);
77 
78  template <typename T>
79  Ptr<T> Get (void) const;
80 
81  template <typename T>
82  bool GetAccessor (Ptr<T> &value) const;
83 
84  virtual Ptr<AttributeValue> Copy (void) const;
85  virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
86  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
87 
88 private:
90 };
91 
92 
94 {
95 public:
96 
101  virtual TypeId GetPointeeTypeId (void) const = 0;
102 };
103 template <typename T>
105 
106 } // namespace ns3
107 
108 
109 
110 /***************************************************************
111  * Implementation of the templates declared above.
112  ***************************************************************/
113 
114 namespace ns3 {
115 
116 namespace internal {
117 
119 template <typename T>
121 {
122  virtual bool Check (const AttributeValue &val) const {
123  const PointerValue *value = dynamic_cast<const PointerValue *> (&val);
124  if (value == 0)
125  {
126  return false;
127  }
128  if (value->GetObject () == 0)
129  {
130  return true;
131  }
132  T *ptr = dynamic_cast<T*> (PeekPointer (value->GetObject ()));
133  if (ptr == 0)
134  {
135  return false;
136  }
137  return true;
138  }
139  virtual std::string GetValueTypeName (void) const {
140  return "ns3::PointerValue";
141  }
142  virtual bool HasUnderlyingTypeInformation (void) const {
143  return true;
144  }
145  virtual std::string GetUnderlyingTypeInformation (void) const {
146  TypeId tid = T::GetTypeId ();
147  return "ns3::Ptr< " + tid.GetName () + " >";
148  }
149  virtual Ptr<AttributeValue> Create (void) const {
150  return ns3::Create<PointerValue> ();
151  }
152  virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
153  const PointerValue *src = dynamic_cast<const PointerValue *> (&source);
154  PointerValue *dst = dynamic_cast<PointerValue *> (&destination);
155  if (src == 0 || dst == 0)
156  {
157  return false;
158  }
159  *dst = *src;
160  return true;
161  }
162  virtual TypeId GetPointeeTypeId (void) const {
163  return T::GetTypeId ();
164  }
165 };
166 
167 } // namespace internal
168 
169 template <typename T>
171 {
172  m_value = object;
173 }
174 
175 template <typename T>
176 void
177 PointerValue::Set (const Ptr<T> &object)
178 {
179  m_value = object;
180 }
181 
182 template <typename T>
183 Ptr<T>
184 PointerValue::Get (void) const
185 {
186  T *v = dynamic_cast<T *> (PeekPointer (m_value));
187  return v;
188 }
189 
190 template <typename T>
191 PointerValue::operator Ptr<T> () const
192 {
193  return Get<T> ();
194 }
195 
196 template <typename T>
197 bool
199 {
200  Ptr<T> ptr = dynamic_cast<T*> (PeekPointer (m_value));
201  if (ptr == 0)
202  {
203  return false;
204  }
205  v = ptr;
206  return true;
207 }
208 
209 
211 
212 template <typename T>
215 {
216  return Create<internal::PointerChecker<T> > ();
217 }
218 
219 
220 } // namespace ns3
221 
222 #endif /* NS_POINTER_H */
virtual TypeId GetPointeeTypeId(void) const
Get the TypeId of the base type.
Definition: pointer.h:162
Represent the type of an attribute.
Definition: attribute.h:166
Ptr< T > Get(void) const
Definition: pointer.h:184
Ptr< AttributeChecker > MakePointerChecker(void)
Definition: pointer.h:214
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:562
virtual TypeId GetPointeeTypeId(void) const =0
Get the TypeId of the base type.
virtual Ptr< AttributeValue > Create(void) const
Definition: pointer.h:149
virtual bool Copy(const AttributeValue &source, AttributeValue &destination) const
Copy the source to the destination.
Definition: pointer.h:152
virtual bool HasUnderlyingTypeInformation(void) const
Definition: pointer.h:142
Ptr< Object > m_value
The stored Pointer instance.
Definition: pointer.h:89
PointerChecker implementation.
Definition: pointer.h:120
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
virtual std::string GetUnderlyingTypeInformation(void) const
Definition: pointer.h:145
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
std::string GetName(void) const
Definition: type-id.cc:685
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.
bool GetAccessor(Ptr< T > &value) const
Access the Pointer value as type T.
Definition: pointer.h:198
virtual std::string GetValueTypeName(void) const
Definition: pointer.h:139
AttributeChecker implementation for PointerValue.
Definition: pointer.h:93
virtual Ptr< AttributeValue > Copy(void) const
Definition: pointer.cc:62
virtual bool Check(const AttributeValue &val) const
Definition: pointer.h:122
virtual std::string SerializeToString(Ptr< const AttributeChecker > checker) const
Definition: pointer.cc:68
a unique identifier for an interface.
Definition: type-id.h:51
void Set(const Ptr< T > &value)
Set the value.
Definition: pointer.h:177