diff --git a/src/core/enum-value.cc b/src/core/enum-value.cc new file mode 100644 index 000000000..8cc1c110a --- /dev/null +++ b/src/core/enum-value.cc @@ -0,0 +1,145 @@ +#include "enum-value.h" +#include "fatal-error.h" +#include + +namespace ns3 { + + +EnumValue::EnumValue (int v) + : m_v (v) +{} +void +EnumValue::Set (int v) +{ + m_v = v; +} +int +EnumValue::Get (void) const +{ + return m_v; +} + +PValue +EnumValue::Copy (void) const +{ + return PValue::Create (*this); +} +std::string +EnumValue::SerializeToString (Ptr spec) const +{ + const EnumParamSpec *p = dynamic_cast (PeekPointer (spec)); + NS_ASSERT (p != 0); + for (EnumParamSpec::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++) + { + if (i->first == m_v) + { + return i->second; + } + } + + NS_FATAL_ERROR ("The user has set an invalid C++ value in this EnumValue"); + // quiet compiler. + return ""; +} +bool +EnumValue::DeserializeFromString (std::string value, Ptr spec) +{ + const EnumParamSpec *p = dynamic_cast (PeekPointer (spec)); + NS_ASSERT (p != 0); + for (EnumParamSpec::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++) + { + if (i->second == value) + { + m_v = i->first; + return true; + } + } + return false; +} + +EnumValue::EnumValue (PValue value) +{ + const EnumValue *v = value.DynCast (); + if (v == 0) + { + NS_FATAL_ERROR ("assigning non-Enum value to Enum value."); + } + m_v = v->m_v; +} +EnumValue::operator PValue () const +{ + return PValue::Create (*this); +} + + + +EnumParamSpec::EnumParamSpec () +{} + +void +EnumParamSpec::AddDefault (int v, std::string name) +{ + m_valueSet.push_front (std::make_pair (v, name)); +} +void +EnumParamSpec::Add (int v, std::string name) +{ + m_valueSet.push_back (std::make_pair (v, name)); +} + +bool +EnumParamSpec::Set (ObjectBase * object, PValue value) const +{ + const EnumValue *p = value.DynCast (); + if (p == 0) + { + return false; + } + DoSet (object, p); + return true; +} +bool +EnumParamSpec::Get (const ObjectBase * object, PValue value) const +{ + EnumValue *p = value.DynCast (); + if (p == 0) + { + return false; + } + DoGet (object, p); + return true; +} +bool +EnumParamSpec::Check (PValue value) const +{ + const EnumValue *p = value.DynCast (); + if (p == 0) + { + return false; + } + for (ValueSet::const_iterator i = m_valueSet.begin (); i != m_valueSet.end (); i++) + { + if (i->first == p->Get ()) + { + return true; + } + } + return false; +} + +PValue +EnumParamSpec::GetInitialValue (void) const +{ + NS_ASSERT (!m_valueSet.empty ()); + return PValue::Create (m_valueSet.front ().first); +} + +PValue +EnumParamSpec::CreateValue (void) const +{ + return PValue::Create (m_valueSet.front ().first); +} + + + +} // namespace ns3 diff --git a/src/core/enum-value.h b/src/core/enum-value.h new file mode 100644 index 000000000..65361f819 --- /dev/null +++ b/src/core/enum-value.h @@ -0,0 +1,172 @@ +#ifndef ENUM_VALUE_H +#define ENUM_VALUE_H + +#include "value.h" +#include "param-spec-helper.h" +#include + +namespace ns3 { + +class EnumValue : public Value +{ +public: + EnumValue (int v); + void Set (int v); + int Get (void) const; + + virtual PValue Copy (void) const; + virtual std::string SerializeToString (Ptr spec) const; + virtual bool DeserializeFromString (std::string value, Ptr spec); + + EnumValue (PValue value); + operator PValue () const; +private: + int m_v; +}; + +class EnumParamSpec : public ParamSpec +{ +public: + EnumParamSpec (); + + void AddDefault (int v, std::string name); + void Add (int v, std::string name); + + virtual bool Set (ObjectBase* object, PValue value) const; + virtual bool Get (const ObjectBase* object, PValue value) const; + virtual bool Check (PValue value) const; + + virtual PValue GetInitialValue (void) const; + virtual PValue CreateValue (void) const; +private: + virtual bool DoSet (ObjectBase *object, const EnumValue *value) const = 0; + virtual bool DoGet (const ObjectBase *object, EnumValue *value) const = 0; + friend class EnumValue; + typedef std::list > ValueSet; + ValueSet m_valueSet; +}; + +template +Ptr MakeEnumParamSpec (U T::*v, + int v1, std::string n1, + int v2 = 0, std::string n2 = "", + int v3 = 0, std::string n3 = "", + int v4 = 0, std::string n4 = "", + int v5 = 0, std::string n5 = "", + int v6 = 0, std::string n6 = "", + int v7 = 0, std::string n7 = "", + int v8 = 0, std::string n8 = "", + int v9 = 0, std::string n9 = "", + int v10 = 0, std::string n10 = "", + int v11 = 0, std::string n11 = "", + int v12 = 0, std::string n12 = ""); + + +} // namespace ns3 + +namespace ns3 { + +template +Ptr MakeEnumParamSpec (U T::*v, + int v1, std::string n1, + int v2 = 0, std::string n2 = "", + int v3 = 0, std::string n3 = "", + int v4 = 0, std::string n4 = "", + int v5 = 0, std::string n5 = "", + int v6 = 0, std::string n6 = "", + int v7 = 0, std::string n7 = "", + int v8 = 0, std::string n8 = "", + int v9 = 0, std::string n9 = "", + int v10 = 0, std::string n10 = "", + int v11 = 0, std::string n11 = "", + int v12 = 0, std::string n12 = "") +{ + class MemberVariable : public EnumParamSpec + { + private: + U T::*m_memberVariable; + public: + MemberVariable (U T::*memberVariable) + : m_memberVariable (memberVariable) {} + virtual bool DoSet (ObjectBase *object, const EnumValue *value) const { + T *o = dynamic_cast (object); + if (o == 0) + { + return false; + } + (o->*m_memberVariable) = U (value->Get ()); + return true; + } + virtual bool DoGet (const ObjectBase *object, EnumValue *value) const { + const T *o = dynamic_cast (object); + if (o == 0) + { + return false; + } + value->Set (o->*m_memberVariable); + return true; + } + }; + Ptr spec = Ptr (new MemberVariable (v), false); + spec->AddDefault (v1, n1); + if (n2 == "") + { + return spec; + } + spec->Add (v2, n2); + if (n3 == "") + { + return spec; + } + spec->Add (v3, n3); + if (n4 == "") + { + return spec; + } + spec->Add (v4, n4); + if (n5 == "") + { + return spec; + } + spec->Add (v5, n5); + if (n6 == "") + { + return spec; + } + spec->Add (v6, n6); + if (n7 == "") + { + return spec; + } + spec->Add (v7, n7); + if (n8 == "") + { + return spec; + } + spec->Add (v8, n8); + if (n9 == "") + { + return spec; + } + spec->Add (v9, n9); + if (n10 == "") + { + return spec; + } + spec->Add (v10, n10); + if (n11 == "") + { + return spec; + } + spec->Add (v11, n11); + if (n12 == "") + { + return spec; + } + spec->Add (v12, n12); + return spec; +} + +} // namespace ns3 + +#endif /* ENUM_VALUE_H */ diff --git a/src/core/value-test.cc b/src/core/value-test.cc index 8ae421e4e..c7d8fe24d 100644 --- a/src/core/value-test.cc +++ b/src/core/value-test.cc @@ -4,9 +4,9 @@ #include "boolean-value.h" #include "int-value.h" #include "uint-value.h" +#include "enum-value.h" #if 0 #include "fp-value.h" -#include "enum-value.h" #endif namespace ns3 { @@ -61,14 +61,14 @@ public: .AddParameter ("TestUint8", "help text", MakeUintParamSpec (&ParamSpecObjectTest::m_uint8, 1)) -#if 0 - .AddParameter ("TestFloat", "help text", - MakeFpParamSpec (-1.1, &ParamSpecObjectTest::m_float)) .AddParameter ("TestEnum", "help text", MakeEnumParamSpec (&ParamSpecObjectTest::m_enum, TEST_A, "TestA", TEST_B, "TestB", TEST_C, "TestC")) +#if 0 + .AddParameter ("TestFloat", "help text", + MakeFpParamSpec (-1.1, &ParamSpecObjectTest::m_float)) #endif ; @@ -249,7 +249,7 @@ ParamSpecTest::RunTests (void) CHECK_GET_STR (p, "TestFloat", "-1.1"); NS_TEST_ASSERT (p->Set ("TestFloat", FpValue ((float)+2.3))); CHECK_GET_PARAM (p, "TestFloat", FpValue, (float)+2.3); - +#endif CHECK_GET_STR (p, "TestEnum", "TestA"); CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_A); NS_TEST_ASSERT (p->Set ("TestEnum", EnumValue (ParamSpecObjectTest::TEST_C))); @@ -264,7 +264,6 @@ ParamSpecTest::RunTests (void) NS_TEST_ASSERT (!p->Set ("TestEnum", EnumValue (5))); CHECK_GET_STR (p, "TestEnum", "TestB"); CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_B); -#endif #if 0 p->Set ("TestBoolName", "true"); diff --git a/src/core/wscript b/src/core/wscript index 5f440ecbd..95cb7076a 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -58,6 +58,7 @@ def build(bld): 'value-test.cc', 'int-value.cc', 'uint-value.cc', + 'enum-value.cc', ] if sys.platform == 'win32':