get rid of scary implementation details of Attribute.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 10 Apr 2008 12:59:31 -0700
changeset 2947 b0fec3381418
parent 2946 14194a052b7d
child 2948 858fc65447a5
get rid of scary implementation details of Attribute.
src/core/attribute-helper.h
src/core/attribute.cc
src/core/attribute.h
src/core/double.cc
src/core/enum.cc
src/core/integer.cc
src/core/object-vector.cc
src/core/object-vector.h
src/core/pointer.h
src/core/random-variable.cc
src/core/uinteger.cc
src/simulator/time.cc
--- a/src/core/attribute-helper.h	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/core/attribute-helper.h	Thu Apr 10 12:59:31 2008 -0700
@@ -46,7 +46,7 @@
       return "";
     }
     virtual Attribute Create (void) const {
-      return Attribute::Create<T> ();
+      return Attribute (ns3::Create<T> ());
     }
     std::string m_type;
   } *checker = new SimpleAttributeChecker ();
@@ -168,7 +168,7 @@
   }									\
   Attribute								\
   type##Value::Copy (void) const {					\
-    return Attribute::Create<type##Value> (*this);			\
+    return Attribute (ns3::Create<type##Value> (*this));                \
   }									\
   type##Value::type##Value (Attribute value)				\
   {									\
@@ -181,7 +181,7 @@
   }									\
   type##Value::operator Attribute () const				\
   {									\
-    return Attribute::Create<type##Value> (*this);			\
+    return Attribute (ns3::Create<type##Value> (*this));                \
   }
 
 /**
@@ -241,7 +241,7 @@
   }									\
   type::operator Attribute () const					\
   {									\
-    return Attribute::Create<type##Value> (*this);			\
+    return Attribute (ns3::Create<type##Value> (*this));                \
   }
 
 
--- a/src/core/attribute.cc	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/core/attribute.cc	Thu Apr 10 12:59:31 2008 -0700
@@ -27,94 +27,16 @@
 namespace ns3 {
 
 AttributeValue::AttributeValue ()
-  : m_count (1)
 {}
-AttributeValue::AttributeValue (const AttributeValue &o)
-  : m_count (1)
-{}
-AttributeValue &
-AttributeValue::operator = (const AttributeValue &o)
-{
-  return *this;
-}
 AttributeValue::~AttributeValue ()
 {}
 
-/***************************************************************
- *   Big interesting warning.
- *   ------------------------
- *
- * One might wonder why we re-implement a smart pointer below 
- * in the Attribute class. This is a very good question and the answer
- * is unfortunately pretty complicated.
- *
- * 1) We could have requested the user to use Ptr<AttributeValue> and save us
- *    a lot of pain. This, however, does not work because our smart 
- *    pointer needs a special constructor which can be used to convert
- *    objects of type Ptr<T> into a PtrValue<T> to hold the pointer.
- *
- * 2) We could have made the m_value member variable below a Ptr<AttributeValue>
- *    rather than store a raw pointer. This, however, does not work
- *    because this would mean that the constructor Attribute (AttributeValue *)
- *    should be morphed into Attribute (Ptr<AttributeValue>) which, unfortunately,
- *    would conflict with the template constructor Attribute (Ptr<T>)...
- *
- * This is definitely not fun.   
- */
 Attribute::Attribute ()
-  : m_value (0)
+  : m_value ()
 {}
-Attribute::Attribute (const Attribute &o)
-  : m_value (o.m_value)
-{
-  if (m_value != 0)
-    {
-      m_value->m_count++;
-      NS_LOG_DEBUG ("this="<<m_value<<" ++count="<<m_value->m_count);
-    }
-}
-Attribute &
-Attribute::operator = (const Attribute &o)
-{
-  if (&o != this)
-    {
-      if (m_value != 0)
-	{
-	  m_value->m_count--;
-	  NS_LOG_DEBUG ("this="<<m_value<<" --count="<<m_value->m_count);
-	  if (m_value->m_count == 0)
-	    {
-	      delete m_value;
-	      m_value = 0;
-	    }
-	}
-      m_value = o.m_value;
-      if (m_value != 0)
-	{
-	  m_value->m_count++;
-	  NS_LOG_DEBUG ("this="<<m_value<<" ++count="<<m_value->m_count);
-	}
-    }
-  return *this;
-}
-Attribute::~Attribute ()
-{
-  if (m_value != 0) 
-    {
-      m_value->m_count--;
-      NS_LOG_DEBUG ("this="<<m_value<<" --count="<<m_value->m_count);
-      if (m_value->m_count == 0)
-	{
-	  delete m_value;
-	  m_value = 0;
-	}
-    }
-}
-Attribute::Attribute (AttributeValue *value)
+Attribute::Attribute (Ptr<AttributeValue> value)
   : m_value (value)
-{
-  NS_LOG_DEBUG ("this="<<m_value<<" count="<<((m_value!=0)?m_value->m_count:666));
-}
+{}
 
 Attribute 
 Attribute::Copy (void) const
--- a/src/core/attribute.h	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/core/attribute.h	Thu Apr 10 12:59:31 2008 -0700
@@ -23,6 +23,7 @@
 #include <string>
 #include <stdint.h>
 #include "ptr.h"
+#include "ref-count-base.h"
 
 namespace ns3 {
 
@@ -34,17 +35,14 @@
 /**
  * \brief Hold a value for an Attribute.
  *
- * Instances of this class are usually created by Attribute::Create<> and
- * should always be wrapped into an Attribute object.
+ * Instances of this class should always be wrapped into an Attribute object.
  * Most subclasses of this base class are implemented by the 
  * ATTRIBUTE_HELPER_* macros.
  */
-class AttributeValue
+class AttributeValue : public RefCountBase
 {
 public:
   AttributeValue ();
-  AttributeValue (const AttributeValue &o);
-  AttributeValue &operator = (const AttributeValue &o);
   virtual ~AttributeValue ();
 
   /**
@@ -94,9 +92,6 @@
 {
 public:
   Attribute ();
-  Attribute (const Attribute &o);
-  Attribute &operator = (const Attribute &o);
-  ~Attribute ();
 
   /**
    * Forward to AttributeValue::Copy
@@ -111,19 +106,6 @@
    */
   bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
 
-  /**
-   * \returns a new Attribute object which wraps an instance of the requested
-   * subclass of AttributeValue.
-   */
-  template <typename T>
-  static Attribute Create (void);
-  /**
-   * \param a1 a value to pass through to the constructor of the class T.
-   * \returns a new Attribute object which wraps an instance of the requested
-   * subclass of AttributeValue.
-   */
-  template <typename T, typename T1>
-  static Attribute Create (T1 a1);
 
   /**
    * This method performs a dynamic_cast on the underlying AttributeValue.
@@ -135,9 +117,9 @@
   template <typename T>
   T DynCast (void) const;
 
+  Attribute (Ptr<AttributeValue> value);
 private:
-  Attribute (AttributeValue *value);
-  AttributeValue *m_value;
+  Ptr<AttributeValue> m_value;
 };
 
 /**
@@ -230,23 +212,10 @@
  ********************************************************/
 
 template <typename T>
-Attribute 
-Attribute::Create (void)
-{
-  return Attribute (new T ());
-}
-template <typename T, typename T1>
-Attribute 
-Attribute::Create (T1 a1)
-{
-  return Attribute (new T (a1));
-}
-
-template <typename T>
 T
 Attribute::DynCast (void) const
 {
-  return dynamic_cast<T> (m_value);
+  return dynamic_cast<T> (PeekPointer (m_value));
 }
 
 } // namespace ns3
--- a/src/core/double.cc	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/core/double.cc	Thu Apr 10 12:59:31 2008 -0700
@@ -88,7 +88,7 @@
       return oss.str ();
     }
     virtual Attribute Create (void) const {
-      return Attribute::Create<DoubleValue> ();
+      return Attribute (ns3::Create<DoubleValue> ());
     }
     double m_minValue;
     double m_maxValue;
--- a/src/core/enum.cc	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/core/enum.cc	Thu Apr 10 12:59:31 2008 -0700
@@ -43,7 +43,7 @@
 Attribute
 Enum::Copy (void) const
 {
-  return Attribute::Create<Enum> (*this);
+  return Attribute (ns3::Create<Enum> (*this));
 }
 std::string 
 Enum::SerializeToString (Ptr<const AttributeChecker> checker) const
@@ -89,7 +89,7 @@
 }
 Enum::operator Attribute () const
 {
-  return Attribute::Create<Enum> (*this);
+  return Attribute (ns3::Create<Enum> (*this));
 }
 
 
@@ -152,7 +152,7 @@
 Attribute 
 EnumChecker::Create (void) const
 {
-  return Attribute::Create<Enum> ();
+  return Attribute (ns3::Create<Enum> ());
 }
 
 
--- a/src/core/integer.cc	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/core/integer.cc	Thu Apr 10 12:59:31 2008 -0700
@@ -93,7 +93,7 @@
       return oss.str ();
     }
     virtual Attribute Create (void) const {
-      return Attribute::Create<IntegerValue> ();
+      return Attribute (ns3::Create<IntegerValue> ());
     }
     int64_t m_minValue;
     int64_t m_maxValue;
--- a/src/core/object-vector.cc	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/core/object-vector.cc	Thu Apr 10 12:59:31 2008 -0700
@@ -38,7 +38,7 @@
 
 ObjectVector::operator Attribute () const
 {
-  return Attribute::Create<ObjectVectorValue> ();
+  return Attribute (ns3::Create<ObjectVectorValue> ());
 }
 
 ObjectVectorValue::ObjectVectorValue ()
@@ -58,7 +58,7 @@
 Attribute 
 ObjectVectorValue::Copy (void) const
 {
-  return Attribute::Create<ObjectVectorValue> (*this);
+  return Attribute (ns3::Create<ObjectVectorValue> (*this));
 }
 std::string 
 ObjectVectorValue::SerializeToString (Ptr<const AttributeChecker> checker) const
--- a/src/core/object-vector.h	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/core/object-vector.h	Thu Apr 10 12:59:31 2008 -0700
@@ -100,7 +100,7 @@
     return T::GetTypeId ().GetName ();
   }
   virtual Attribute Create (void) const {
-    return Attribute::Create<ObjectVectorValue> ();
+    return Attribute (ns3::Create<ObjectVectorValue> ());
   }
 };
 
--- a/src/core/pointer.h	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/core/pointer.h	Thu Apr 10 12:59:31 2008 -0700
@@ -123,7 +123,7 @@
     return "";
   }
   virtual Attribute Create (void) const {
-    return Attribute::Create<PointerValue> ();
+    return Attribute (ns3::Create<PointerValue> ());
   }
   virtual TypeId GetPointeeTypeId (void) const {
     return T::GetTypeId ();
--- a/src/core/random-variable.cc	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/core/random-variable.cc	Thu Apr 10 12:59:31 2008 -0700
@@ -311,7 +311,7 @@
 }
 RandomVariable::operator Attribute () const
 {
-  return Attribute::Create<RandomVariableValue> (*this);
+  return Attribute (ns3::Create<RandomVariableValue> (*this));
 }
 
 ATTRIBUTE_VALUE_IMPLEMENT (RandomVariable);
--- a/src/core/uinteger.cc	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/core/uinteger.cc	Thu Apr 10 12:59:31 2008 -0700
@@ -88,7 +88,7 @@
       return oss.str ();
     }
     virtual Attribute Create (void) const {
-      return Attribute::Create<UintegerValue> ();
+      return Attribute (ns3::Create<UintegerValue> ());
     }
     uint64_t m_minValue;
     uint64_t m_maxValue;
--- a/src/simulator/time.cc	Thu Apr 10 12:59:15 2008 -0700
+++ b/src/simulator/time.cc	Thu Apr 10 12:59:31 2008 -0700
@@ -312,7 +312,7 @@
 }
 TimeUnit<1>::operator Attribute () const
 {
-  return Attribute::Create<TimeValue> (*this);
+  return Attribute (ns3::Create<TimeValue> (*this));
 }
 
 ATTRIBUTE_VALUE_IMPLEMENT (Time);