src/core/pointer.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3182 61fe7fe81ebd
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
     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 
    26 namespace ns3 {
    27 
    28 /**
    29  * \ingroup attribute
    30  *
    31  * \brief hold objects of type Ptr<T>
    32  */
    33 class PointerValue : public AttributeValue
    34 {
    35 public:
    36   PointerValue ();
    37 
    38   PointerValue (Ptr<Object> object);
    39 
    40   void SetObject (Ptr<Object> object);
    41 
    42   Ptr<Object> GetObject (void) const;
    43 
    44   template <typename T>
    45   PointerValue (const Ptr<T> &object);
    46 
    47   template <typename T>
    48   void Set (const Ptr<T> &object);
    49 
    50   template <typename T>
    51   Ptr<T> Get (void) const;
    52 
    53   template <typename T>
    54   bool GetAccessor (Ptr<T> &v) const;
    55 
    56   template <typename T>
    57   operator Ptr<T> () const;
    58 
    59   virtual Ptr<AttributeValue> Copy (void) const;
    60   virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
    61   virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
    62 
    63 private:
    64   Ptr<Object> m_value;
    65 };
    66 
    67 
    68 class PointerChecker : public AttributeChecker 
    69 {
    70 public:
    71   virtual TypeId GetPointeeTypeId (void) const = 0;
    72 };
    73 template <typename T>
    74 Ptr<AttributeChecker> MakePointerChecker (void);
    75 
    76 } // namespace ns3
    77 
    78 namespace ns3 {
    79 
    80 
    81 namespace internal {
    82 
    83 template <typename T>
    84 class APointerChecker : public PointerChecker
    85 {
    86   virtual bool Check (const AttributeValue &val) const {
    87     const PointerValue *value = dynamic_cast<const PointerValue *> (&val);
    88     if (value == 0)
    89       {
    90 	return false;
    91       }
    92     if (value->GetObject () == 0)
    93       {
    94 	return true;
    95       }
    96     T *ptr = dynamic_cast<T*> (PeekPointer (value->GetObject ()));
    97     if (ptr == 0)
    98       {
    99 	return false;
   100       }
   101     return true;
   102   }
   103   virtual std::string GetValueTypeName (void) const {
   104     return "ns3::PointerValue";
   105   }
   106   virtual bool HasUnderlyingTypeInformation (void) const {
   107     return true;
   108   }
   109   virtual std::string GetUnderlyingTypeInformation (void) const {
   110     TypeId tid = T::GetTypeId ();
   111     return "ns3::Ptr< " + tid.GetName () + " >";
   112   }
   113   virtual Ptr<AttributeValue> Create (void) const {
   114     return ns3::Create<PointerValue> ();
   115   }
   116   virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
   117     const PointerValue *src = dynamic_cast<const PointerValue *> (&source);
   118     PointerValue *dst = dynamic_cast<PointerValue *> (&destination);
   119     if (src == 0 || dst == 0)
   120       {
   121         return false;
   122       }
   123     *dst = *src;
   124     return true;
   125   }
   126   virtual TypeId GetPointeeTypeId (void) const {
   127     return T::GetTypeId ();
   128   }
   129 };
   130 
   131 } // namespace internal
   132 
   133 template <typename T>
   134 PointerValue::PointerValue (const Ptr<T> &object)
   135 {
   136   m_value = object;
   137 }
   138 
   139 template <typename T>
   140 void 
   141 PointerValue::Set (const Ptr<T> &object)
   142 {
   143   m_value = object;
   144 }
   145 
   146 template <typename T>
   147 Ptr<T> 
   148 PointerValue::Get (void) const
   149 {
   150   T *v = dynamic_cast<T *> (PeekPointer (m_value));
   151   return v;
   152 }
   153 
   154 template <typename T>
   155 PointerValue::operator Ptr<T> () const
   156 {
   157   return Get<T> ();
   158 }
   159 
   160 template <typename T>
   161 bool 
   162 PointerValue::GetAccessor (Ptr<T> &v) const
   163 {
   164   Ptr<T> ptr = dynamic_cast<T*> (PeekPointer (m_value));
   165   if (ptr == 0)
   166     {
   167       return false;
   168     }
   169   v = ptr;
   170   return true;
   171 }
   172 
   173 
   174 ATTRIBUTE_ACCESSOR_DEFINE (Pointer);
   175 
   176 template <typename T>
   177 Ptr<AttributeChecker>
   178 MakePointerChecker (void)
   179 {
   180   return Create<internal::APointerChecker<T> > ();
   181 }
   182 
   183 
   184 } // namespace ns3
   185 
   186 #endif /* NS_POINTER_H */