BooleanValue -> Boolean
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 21 Feb 2008 00:19:31 +0100
changeset 2441 d4a91f8bb374
parent 2440 baffd08c6898
child 2442 78446ab94ef5
BooleanValue -> Boolean
src/core/boolean-value.cc
src/core/boolean-value.h
src/core/value-test.cc
--- a/src/core/boolean-value.cc	Thu Feb 21 00:00:18 2008 +0100
+++ b/src/core/boolean-value.cc	Thu Feb 21 00:19:31 2008 +0100
@@ -3,77 +3,64 @@
 
 namespace ns3 {
 
-BooleanValue::BooleanValue (bool value)
+Boolean::Boolean ()
+  : m_value (false)
+{}
+Boolean::Boolean (bool value)
   : m_value (value)
 {}
 void 
-BooleanValue::Set (bool value)
+Boolean::Set (bool value)
 {
   m_value = value;
 }
 bool 
-BooleanValue::Get (void) const
+Boolean::Get (void) const
 {
   return m_value;
 }
-Attribute
-BooleanValue::Copy (void) const
+Boolean::operator bool () const
 {
-  return Attribute::Create<BooleanValue> (*this);
+  return m_value;
 }
-std::string 
-BooleanValue::SerializeToString (Ptr<const AttributeChecker> checker) const
+
+std::ostream & operator << (std::ostream &os, const Boolean &value)
 {
-  std::string value;
-  if (m_value)
+  if (value.Get ())
     {
-      value = "true";
+      os << "true";
     }
   else
     {
-      value = "false";
+      os << "false";
     }
-  return value;
+  return os;
 }
-bool 
-BooleanValue::DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker)
+std::istream & operator >> (std::istream &is, Boolean &value)
 {
-  if (value == "true" ||
-      value == "1" ||
-      value == "t")
+  std::string v;
+  is >> v;
+  if (v == "true" ||
+      v == "1" ||
+      v == "t")
     {
-      m_value = true;
-      return true;
+      value.Set (true);
     }
-  else if (value == "false" ||
-	   value == "0" ||
-	   value == "f")
+  else if (v == "false" ||
+	   v == "0" ||
+	   v == "f")
     {
-      m_value = false;
-      return true;
+      value.Set (false);
     }
   else
     {
-      return false;
+      is.setstate (std::ios_base::badbit);
     }  
-}
-BooleanValue::BooleanValue (Attribute value)
-{
-  const BooleanValue *v = value.DynCast<const BooleanValue *> ();
-  if (v == 0)
-    {
-      NS_FATAL_ERROR ("assigning non-Boolean value to Boolean value.");
-    }
-  m_value = v->m_value;
-}
-BooleanValue::operator Attribute () const
-{
-  return Attribute::Create<BooleanValue> (*this);
+  return is;
 }
 
-Ptr<const AttributeChecker> MakeBooleanChecker (void)
-{
-  return MakeSimpleAttributeChecker<BooleanValue> ();
-}
+ATTRIBUTE_CONVERTER_IMPLEMENT (Boolean);
+ATTRIBUTE_VALUE_IMPLEMENT (Boolean);
+ATTRIBUTE_CHECKER_IMPLEMENT (Boolean);
 
 } // namespace ns3
--- a/src/core/boolean-value.h	Thu Feb 21 00:00:18 2008 +0100
+++ b/src/core/boolean-value.h	Thu Feb 21 00:19:31 2008 +0100
@@ -2,56 +2,31 @@
 #define BOOLEAN_VALUE_H
 
 #include "attribute.h"
-#include "param-spec-helper.h"
-#include "ptr.h"
+#include "value-helper.h"
 
 namespace ns3 {
 
-class BooleanValue : public AttributeValue
+class Boolean
 {
 public:
-  BooleanValue (bool value);
+  Boolean ();
+  Boolean (bool value);
   void Set (bool value);
   bool Get (void) const;
 
-  virtual Attribute Copy (void) const;
-  virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
-  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
+  operator bool () const;
 
-  BooleanValue (Attribute value);
-  operator Attribute () const;
+  ATTRIBUTE_CONVERTER_DEFINE (Boolean);
 private:
   bool m_value;
 };
 
-class BooleanAccessor : public AttributeAccessor {};
-
-
-template <typename T1>
-Ptr<const AttributeAccessor> MakeBooleanAccessor (T1 a1);
-
-template <typename T1, typename T2>
-Ptr<const AttributeAccessor> MakeBooleanAccessor (T1 a1, T2 a2);
-
-Ptr<const AttributeChecker> MakeBooleanChecker (void);
-
-} // namespace ns3
-
+std::ostream & operator << (std::ostream &os, const Boolean &value);
+std::istream & operator >> (std::istream &is, Boolean &value);
 
-// Implementation of template functions below.
-namespace ns3 {
-
-template <typename T1>
-Ptr<const AttributeAccessor> MakeBooleanAccessor (T1 a1)
-{
-  return MakeAccessorHelper<BooleanAccessor,BooleanValue> (a1);
-}
-
-template <typename T1, typename T2>
-Ptr<const AttributeAccessor> MakeBooleanAccessor (T1 a1, T2 a2)
-{
-  return MakeAccessorHelper<BooleanAccessor,BooleanValue> (a1, a2);
-}
+ATTRIBUTE_VALUE_DEFINE (Boolean);
+ATTRIBUTE_CHECKER_DEFINE (Boolean);
+ATTRIBUTE_ACCESSOR_DEFINE (Boolean);
 
 } // namespace ns3
 
--- a/src/core/value-test.cc	Thu Feb 21 00:00:18 2008 +0100
+++ b/src/core/value-test.cc	Thu Feb 21 00:19:31 2008 +0100
@@ -42,11 +42,11 @@
     static TypeId tid = TypeId ("AccessorObjectTest")
       .SetParent<Object> ()
       .AddParameter ("TestBoolName", "help text",
-		     BooleanValue (false),
+		     Boolean (false),
 		     MakeBooleanAccessor (&AccessorObjectTest::m_boolTest),
 		     MakeBooleanChecker ())
       .AddParameter ("TestBoolA", "help text",
-		     BooleanValue (false),
+		     Boolean (false),
 		     MakeBooleanAccessor (&AccessorObjectTest::DoSetTestB,
 					   &AccessorObjectTest::DoGetTestB),
 		     MakeBooleanChecker ())
@@ -172,31 +172,31 @@
   NS_TEST_ASSERT (params.Set ("AccessorObjectTest::TestBoolName", "false"));
   p = CreateObject<AccessorObjectTest> (params);
   CHECK_GET_STR (p, "TestBoolName", "false");
-  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
+  CHECK_GET_PARAM (p, "TestBoolName", Boolean, false);
 
   NS_TEST_ASSERT (p->Set ("TestBoolName", "true"));
   CHECK_GET_STR (p, "TestBoolName", "true");
-  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
+  CHECK_GET_PARAM (p, "TestBoolName", Boolean, true);
 
-  NS_TEST_ASSERT (p->Set ("TestBoolName", BooleanValue (false)));
+  NS_TEST_ASSERT (p->Set ("TestBoolName", Boolean (false)));
   CHECK_GET_STR (p, "TestBoolName", "false");
-  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false);
+  CHECK_GET_PARAM (p, "TestBoolName", Boolean, false);
 
   p = CreateObjectWith<AccessorObjectTest> ("TestBoolName", "true");
   CHECK_GET_STR (p, "TestBoolName", "true");
-  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
+  CHECK_GET_PARAM (p, "TestBoolName", Boolean, true);
 
-  p = CreateObjectWith<AccessorObjectTest> ("TestBoolName", BooleanValue (true));
+  p = CreateObjectWith<AccessorObjectTest> ("TestBoolName", Boolean (true));
   CHECK_GET_STR (p, "TestBoolName", "true");
-  CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
+  CHECK_GET_PARAM (p, "TestBoolName", Boolean, true);
 
   NS_TEST_ASSERT (p->Set ("TestBoolA", "false"));
   CHECK_GET_STR (p, "TestBoolA", "false");
-  CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, false);
+  CHECK_GET_PARAM (p, "TestBoolA", Boolean, false);
 
   NS_TEST_ASSERT (p->Set ("TestBoolA", "true"));
   CHECK_GET_STR (p, "TestBoolA", "true");
-  CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, true);
+  CHECK_GET_PARAM (p, "TestBoolA", Boolean, true);
 
 
   Ptr<Derived> derived = p->Get ("TestPtr");