BooleanValue -> Boolean

This commit is contained in:
Mathieu Lacage
2008-02-21 00:19:31 +01:00
parent c5dc486e2c
commit fda20b8236
3 changed files with 66 additions and 104 deletions

View File

@@ -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);
}
std::string
BooleanValue::SerializeToString (Ptr<const AttributeChecker> checker) const
{
std::string value;
if (m_value)
{
value = "true";
}
else
{
value = "false";
}
return value;
}
bool
BooleanValue::DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker)
{
if (value == "true" ||
value == "1" ||
value == "t")
{
m_value = true;
return true;
}
else if (value == "false" ||
value == "0" ||
value == "f")
{
m_value = false;
return true;
}
else
{
return false;
}
}
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 m_value;
}
Ptr<const AttributeChecker> MakeBooleanChecker (void)
std::ostream & operator << (std::ostream &os, const Boolean &value)
{
return MakeSimpleAttributeChecker<BooleanValue> ();
if (value.Get ())
{
os << "true";
}
else
{
os << "false";
}
return os;
}
std::istream & operator >> (std::istream &is, Boolean &value)
{
std::string v;
is >> v;
if (v == "true" ||
v == "1" ||
v == "t")
{
value.Set (true);
}
else if (v == "false" ||
v == "0" ||
v == "f")
{
value.Set (false);
}
else
{
is.setstate (std::ios_base::badbit);
}
return is;
}
ATTRIBUTE_CONVERTER_IMPLEMENT (Boolean);
ATTRIBUTE_VALUE_IMPLEMENT (Boolean);
ATTRIBUTE_CHECKER_IMPLEMENT (Boolean);
} // namespace ns3

View File

@@ -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 {};
std::ostream & operator << (std::ostream &os, const Boolean &value);
std::istream & operator >> (std::istream &is, Boolean &value);
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
// 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

View File

@@ -42,11 +42,11 @@ public:
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 @@ AccessorTest::RunTests (void)
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");