00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2008 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 #ifndef NS_POINTER_H 00021 #define NS_POINTER_H 00022 00023 #include "attribute.h" 00024 #include "object.h" 00025 00026 namespace ns3 { 00027 00028 /** 00029 * \ingroup attribute 00030 * 00031 * \brief hold objects of type Ptr<T> 00032 */ 00033 class PointerValue : public AttributeValue 00034 { 00035 public: 00036 PointerValue (); 00037 00038 PointerValue (Ptr<Object> object); 00039 00040 void SetObject (Ptr<Object> object); 00041 00042 Ptr<Object> GetObject (void) const; 00043 00044 template <typename T> 00045 PointerValue (const Ptr<T> &object); 00046 00047 template <typename T> 00048 void Set (const Ptr<T> &object); 00049 00050 template <typename T> 00051 Ptr<T> Get (void) const; 00052 00053 template <typename T> 00054 bool GetAccessor (Ptr<T> &v) const; 00055 00056 template <typename T> 00057 operator Ptr<T> () const; 00058 00059 virtual Ptr<AttributeValue> Copy (void) const; 00060 virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const; 00061 virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker); 00062 00063 private: 00064 Ptr<Object> m_value; 00065 }; 00066 00067 00068 class PointerChecker : public AttributeChecker 00069 { 00070 public: 00071 virtual TypeId GetPointeeTypeId (void) const = 0; 00072 }; 00073 template <typename T> 00074 Ptr<AttributeChecker> MakePointerChecker (void); 00075 00076 } // namespace ns3 00077 00078 namespace ns3 { 00079 00080 00081 namespace internal { 00082 00083 template <typename T> 00084 class APointerChecker : public PointerChecker 00085 { 00086 virtual bool Check (const AttributeValue &val) const { 00087 const PointerValue *value = dynamic_cast<const PointerValue *> (&val); 00088 if (value == 0) 00089 { 00090 return false; 00091 } 00092 if (value->GetObject () == 0) 00093 { 00094 return true; 00095 } 00096 T *ptr = dynamic_cast<T*> (PeekPointer (value->GetObject ())); 00097 if (ptr == 0) 00098 { 00099 return false; 00100 } 00101 return true; 00102 } 00103 virtual std::string GetValueTypeName (void) const { 00104 return "ns3::PointerValue"; 00105 } 00106 virtual bool HasUnderlyingTypeInformation (void) const { 00107 return true; 00108 } 00109 virtual std::string GetUnderlyingTypeInformation (void) const { 00110 TypeId tid = T::GetTypeId (); 00111 return "ns3::Ptr< " + tid.GetName () + " >"; 00112 } 00113 virtual Ptr<AttributeValue> Create (void) const { 00114 return ns3::Create<PointerValue> (); 00115 } 00116 virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const { 00117 const PointerValue *src = dynamic_cast<const PointerValue *> (&source); 00118 PointerValue *dst = dynamic_cast<PointerValue *> (&destination); 00119 if (src == 0 || dst == 0) 00120 { 00121 return false; 00122 } 00123 *dst = *src; 00124 return true; 00125 } 00126 virtual TypeId GetPointeeTypeId (void) const { 00127 return T::GetTypeId (); 00128 } 00129 }; 00130 00131 } // namespace internal 00132 00133 template <typename T> 00134 PointerValue::PointerValue (const Ptr<T> &object) 00135 { 00136 m_value = object; 00137 } 00138 00139 template <typename T> 00140 void 00141 PointerValue::Set (const Ptr<T> &object) 00142 { 00143 m_value = object; 00144 } 00145 00146 template <typename T> 00147 Ptr<T> 00148 PointerValue::Get (void) const 00149 { 00150 T *v = dynamic_cast<T *> (PeekPointer (m_value)); 00151 return v; 00152 } 00153 00154 template <typename T> 00155 PointerValue::operator Ptr<T> () const 00156 { 00157 return Get<T> (); 00158 } 00159 00160 template <typename T> 00161 bool 00162 PointerValue::GetAccessor (Ptr<T> &v) const 00163 { 00164 Ptr<T> ptr = dynamic_cast<T*> (PeekPointer (m_value)); 00165 if (ptr == 0) 00166 { 00167 return false; 00168 } 00169 v = ptr; 00170 return true; 00171 } 00172 00173 00174 ATTRIBUTE_ACCESSOR_DEFINE (Pointer); 00175 00176 template <typename T> 00177 Ptr<AttributeChecker> 00178 MakePointerChecker (void) 00179 { 00180 return Create<internal::APointerChecker<T> > (); 00181 } 00182 00183 00184 } // namespace ns3 00185 00186 #endif /* NS_POINTER_H */