add back EnumValue

This commit is contained in:
Mathieu Lacage
2008-02-04 23:13:54 +01:00
parent b1495e485e
commit c94bc0c5c8
4 changed files with 323 additions and 6 deletions

145
src/core/enum-value.cc Normal file
View File

@@ -0,0 +1,145 @@
#include "enum-value.h"
#include "fatal-error.h"
#include <sstream>
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<EnumValue> (*this);
}
std::string
EnumValue::SerializeToString (Ptr<const ParamSpec> spec) const
{
const EnumParamSpec *p = dynamic_cast<const EnumParamSpec *> (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<const ParamSpec> spec)
{
const EnumParamSpec *p = dynamic_cast<const EnumParamSpec *> (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<const EnumValue *> ();
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<EnumValue> (*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<const EnumValue *> ();
if (p == 0)
{
return false;
}
DoSet (object, p);
return true;
}
bool
EnumParamSpec::Get (const ObjectBase * object, PValue value) const
{
EnumValue *p = value.DynCast<EnumValue *> ();
if (p == 0)
{
return false;
}
DoGet (object, p);
return true;
}
bool
EnumParamSpec::Check (PValue value) const
{
const EnumValue *p = value.DynCast<const EnumValue *> ();
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<EnumValue> (m_valueSet.front ().first);
}
PValue
EnumParamSpec::CreateValue (void) const
{
return PValue::Create<EnumValue> (m_valueSet.front ().first);
}
} // namespace ns3

172
src/core/enum-value.h Normal file
View File

@@ -0,0 +1,172 @@
#ifndef ENUM_VALUE_H
#define ENUM_VALUE_H
#include "value.h"
#include "param-spec-helper.h"
#include <list>
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<const ParamSpec> spec) const;
virtual bool DeserializeFromString (std::string value, Ptr<const ParamSpec> 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<std::pair<int,std::string> > ValueSet;
ValueSet m_valueSet;
};
template <typename T, typename U>
Ptr<ParamSpec> 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 <typename T, typename U>
Ptr<ParamSpec> 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<T *> (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<const T *> (object);
if (o == 0)
{
return false;
}
value->Set (o->*m_memberVariable);
return true;
}
};
Ptr<EnumParamSpec> spec = Ptr<EnumParamSpec> (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 */

View File

@@ -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");

View File

@@ -58,6 +58,7 @@ def build(bld):
'value-test.cc',
'int-value.cc',
'uint-value.cc',
'enum-value.cc',
]
if sys.platform == 'win32':