add back IntValue support

This commit is contained in:
Mathieu Lacage
2008-02-04 22:18:07 +01:00
parent ad6f51646d
commit f21962defc
7 changed files with 382 additions and 170 deletions

View File

@@ -49,7 +49,7 @@ namespace ns3 {
template <typename T>
Ptr<ParamSpec> MakeBooleanParamSpec (bool T::*v, bool initialValue)
{
return Create<MemberVariableParamSpecHelper<T,BooleanValue,bool> > (v, initialValue);
return MakeMemberVariableParamSpec (v, BooleanValue (initialValue));
}
template <typename T>
@@ -57,21 +57,21 @@ Ptr<ParamSpec> MakeBooleanParamSpec (void (T::*setter) (bool),
bool (T::*getter) (void) const,
bool initialValue)
{
return Create<MemberMethodParamSpecHelper<T,BooleanValue,bool> > (setter, getter, initialValue);
return MakeMemberMethodParamSpec (setter, getter, BooleanValue (initialValue));
}
template <typename T>
Ptr<ParamSpec> MakeBooleanParamSpec (bool (T::*getter) (void) const,
bool initialValue)
{
return Create<MemberMethodGetterParamSpecHelper<T,BooleanValue,bool> > (getter, initialValue);
return MakeMemberMethodGetterParamSpec (getter, BooleanValue (initialValue));
}
template <typename T>
Ptr<ParamSpec> MakeBooleanParamSpec (void (T::*setter) (bool),
bool initialValue)
{
return Create<MemberMethodSetterParamSpecHelper<T,BooleanValue,bool> > (setter, initialValue);
return MakeMemberMethodSetterParamSpec (setter, BooleanValue (initialValue));
}
} // namespace ns3

75
src/core/int-value.cc Normal file
View File

@@ -0,0 +1,75 @@
#include "int-value.h"
#include "object.h"
#include <sstream>
namespace ns3 {
IntValue::IntValue (int64_t value)
: m_value (value)
{}
PValue
IntValue::Copy (void) const
{
return PValue::Create<IntValue> (*this);
}
void
IntValue::Set (int64_t value)
{
m_value = value;
}
int64_t
IntValue::Get (void) const
{
return m_value;
}
std::string
IntValue::SerializeToString (Ptr<const ParamSpec> spec) const
{
std::ostringstream oss;
oss << m_value;
return oss.str ();
}
bool
IntValue::DeserializeFromString (std::string value, Ptr<const ParamSpec> spec)
{
int64_t v;
std::istringstream iss;
iss.str (value);
iss >> v;
bool ok = !iss.bad () && !iss.fail ();
if (ok)
{
m_value = v;
}
return ok;
}
IntValue::IntValue (PValue value)
{
const IntValue *v = value.DynCast<const IntValue *> ();
if (v == 0)
{
NS_FATAL_ERROR ("assigning non-Int value to Int value.");
}
m_value = v->m_value;
}
IntValue::operator PValue () const
{
return PValue::Create<IntValue> (*this);
}
IntValueChecker::IntValueChecker (int64_t minValue, int64_t maxValue)
: m_minValue (minValue),
m_maxValue (maxValue)
{}
bool
IntValueChecker::Check (const int64_t &value) const
{
return value >= m_minValue && value <= m_maxValue;
}
} // namespace ns3

105
src/core/int-value.h Normal file
View File

@@ -0,0 +1,105 @@
#ifndef INT_VALUE_H
#define INT_VALUE_H
#include "value.h"
#include "param-spec-helper.h"
#include <stdint.h>
namespace ns3 {
class IntValue : public Value
{
public:
IntValue (int64_t value);
void Set (int64_t value);
int64_t 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);
IntValue (PValue value);
operator PValue () const;
private:
int64_t m_value;
};
template <typename U, typename T>
Ptr<ParamSpec> MakeIntParamSpec (U T::*memberVariable,
int64_t initialValue);
template <typename U, typename T>
Ptr<ParamSpec> MakeIntParamSpec (U T::*memberVariable,
int64_t initialValue,
int64_t minValue,
int64_t maxValue);
template <typename U, typename T>
Ptr<ParamSpec> MakeIntParamSpec (void (T::*setter) (U),
U (T::*getter) (void) const,
int64_t initialValue);
template <typename U, typename T>
Ptr<ParamSpec> MakeIntParamSpec (void (T::*setter) (U),
U (T::*getter) (void) const,
int64_t initialValue,
int64_t minValue,
int64_t maxValue);
} // namespace ns3
namespace ns3 {
class IntValueChecker
{
public:
IntValueChecker (int64_t minValue, int64_t maxValue);
bool Check (const int64_t &value) const;
private:
int64_t m_minValue;
int64_t m_maxValue;
};
template <typename U, typename T>
Ptr<ParamSpec>
MakeIntParamSpec (U T::*memberVariable,
int64_t initialValue)
{
int64_t minValue = std::numeric_limits<U>::min ();
int64_t maxValue = std::numeric_limits<U>::max ();
return MakeMemberVariableParamSpecWithChecker (memberVariable, IntValue (initialValue),
IntValueChecker (minValue, maxValue));
}
template <typename U, typename T>
Ptr<ParamSpec> MakeIntParamSpec (U T::*memberVariable,
int64_t initialValue,
int64_t minValue,
int64_t maxValue)
{
return MakeMemberVariableParamSpecWithChecker (memberVariable, IntValue (initialValue),
IntValueChecker (minValue, maxValue));
}
template <typename U, typename T>
Ptr<ParamSpec> MakeIntParamSpec (void (T::*setter) (U),
U (T::*getter) (void) const,
int64_t initialValue)
{
int64_t minValue = std::numeric_limits<U>::min ();
int64_t maxValue = std::numeric_limits<U>::max ();
return MakeMemberMethodParamSpecWithChecker (setter, getter, IntValue (initialValue),
IntValueChecker (minValue, maxValue));
}
template <typename U, typename T>
Ptr<ParamSpec> MakeIntParamSpec (void (T::*setter) (U),
U (T::*getter) (void) const,
int64_t initialValue,
int64_t minValue,
int64_t maxValue)
{
return MakeMemberMethodParamSpecWithChecker (setter, getter, IntValue (initialValue),
IntValueChecker (minValue, maxValue));
}
} // namespace ns3
#endif /* INT_VALUE_H */

View File

@@ -908,12 +908,13 @@ Object::DoSet (std::string name, PValue value)
{
return false;
}
bool ok = spec->Set (this, value);
bool ok = spec->Check (value);
if (!ok)
{
return false;
}
return true;
ok = spec->Set (this, value);
return ok;
}
bool
Object::Set (std::string name, PValue value)
@@ -934,12 +935,13 @@ Object::Set (std::string name, std::string value)
{
return false;
}
spec->Set (this, v);
ok = spec->Check (v);
if (!ok)
{
return false;
}
return true;
ok = spec->Set (this, v);
return ok;
}
bool
Object::Get (std::string name, std::string &value) const

View File

@@ -4,90 +4,202 @@
namespace ns3 {
template <typename T, typename U, typename V>
Ptr<ParamSpec>
MakeMemberVariableParamSpec (U T::*memberVariable, V initialValue);
template <typename T, typename U, typename V>
Ptr<ParamSpec>
MakeMemberMethodParamSpec (void (T::*setter) (U),
U (T::*getter) (void) const,
V initialValue);
} // namespace ns3
/***************************************************************
* The implementation of the above class functions
***************************************************************/
namespace ns3 {
template <typename T, typename U, typename CHECKER>
class ParamSpecHelper : public ParamSpec
{
public:
ParamSpecHelper (V initialValue);
ParamSpecHelper (PValue initialValue, CHECKER checker);
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 CreateInitialValue (void) const;
private:
virtual void DoSet (T *object, const V &v) const = 0;
virtual V DoGet (const T *object) const = 0;
virtual bool DoCheck (const V &v) const;
V m_initialValue;
virtual void DoSet (T *object, const U *v) const = 0;
virtual void DoGet (const T *object, U *v) const = 0;
PValue m_initialValue;
CHECKER m_checker;
};
template <typename T, typename U, typename V>
class MemberVariableParamSpecHelper : public ParamSpecHelper<T,U,V>
template <typename T>
class ParamSpecHelperSimpleChecker
{
public:
MemberVariableParamSpecHelper (V T::*memberVariable, V initialValue);
private:
virtual void DoSet (T *object, const V &v) const;
virtual V DoGet (const T *object) const;
V T::*m_memberVariable;
bool Check (const T &value) const {return true;}
};
template <typename T, typename U, typename V>
class MemberMethodParamSpecHelper : public ParamSpecHelper<T,U,V>
template <typename T, typename U, typename V, typename CHECKER>
Ptr<ParamSpec>
MakeMemberVariableParamSpecWithChecker (U T::*memberVariable, V initialValue, CHECKER checker)
{
public:
MemberMethodParamSpecHelper (void (T::*setter) (V),
V (T::*getter) (void) const,
V initialValue);
private:
virtual void DoSet (T *object, const V &v) const;
virtual V DoGet (const T *object) const;
void (T::*m_setter) (V);
V (T::*m_getter) (void) const;
};
class MemberVariable : public ParamSpecHelper<T,V,CHECKER>
{
public:
MemberVariable (U T::*memberVariable, V initialValue, CHECKER checker)
: ParamSpecHelper<T,V,CHECKER> (initialValue, checker),
m_memberVariable (memberVariable)
{}
private:
virtual void DoSet (T *object, const V *v) const {
(object->*m_memberVariable) = v->Get ();
}
virtual void DoGet (const T *object, V *v) const {
v->Set (object->*m_memberVariable);
}
U T::*m_memberVariable;
};
return Ptr<ParamSpec> (new MemberVariable (memberVariable, initialValue, checker), false);
}
template <typename T, typename U, typename V>
class MemberMethodSetterParamSpecHelper : public ParamSpecHelper<T,U,V>
Ptr<ParamSpec>
MakeMemberVariableParamSpec (U T::*memberVariable, V initialValue)
{
public:
MemberMethodSetterParamSpecHelper (void (T::*setter) (V),
V initialValue);
private:
virtual void DoSet (T *object, const V &v) const;
virtual V DoGet (const T *object) const;
return MakeMemberVariableParamSpecWithChecker (memberVariable, initialValue,
ParamSpecHelperSimpleChecker<U> ());
}
void (T::*m_setter) (V);
};
template <typename T, typename U, typename V>
class MemberMethodGetterParamSpecHelper : public ParamSpecHelper<T,U,V>
template <typename T, typename U, typename V, typename CHECKER>
Ptr<ParamSpec>
MakeMemberMethodParamSpecWithChecker (void (T::*setter) (U),
U (T::*getter) (void) const,
V initialValue, CHECKER checker)
{
public:
MemberMethodGetterParamSpecHelper (V (T::*getter) (void) const);
private:
virtual void DoSet (T *object, const V &v) const;
virtual V DoGet (const T *object) const;
class MemberMethod : public ParamSpecHelper<T,V,CHECKER>
{
public:
MemberMethod (void (T::*setter) (U),
U (T::*getter) (void) const,
V initialValue, CHECKER checker)
: ParamSpecHelper<T,V,CHECKER> (initialValue, checker),
m_setter (setter),
m_getter (getter)
{}
private:
virtual void DoSet (T *object, const V *v) const {
(object->*m_setter) (v->Get ());
}
virtual void DoGet (const T *object, V *v) const {
v->Set ((object->*m_getter) ());
}
void (T::*m_setter) (U);
U (T::*m_getter) (void) const;
};
return Ptr<ParamSpec> (new MemberMethod (setter, getter, initialValue, checker), false);
}
V (T::*m_getter) (void) const;
};
} // namespace ns3
/***************************************************************
* The implementation of the above class templates.
***************************************************************/
namespace ns3 {
template <typename T, typename U, typename V>
ParamSpecHelper<T,U,V>::ParamSpecHelper (V initialValue)
: m_initialValue (initialValue)
Ptr<ParamSpec>
MakeMemberMethodParamSpec (void (T::*setter) (U),
U (T::*getter) (void) const,
V initialValue)
{
return MakeMemberMethodParamSpecWithChecker (setter, getter, initialValue,
ParamSpecHelperSimpleChecker<U> ());
}
template <typename T, typename U, typename V, typename CHECKER>
Ptr<ParamSpec>
MakeMemberMethodGetterParamSpecWithChecker (U (T::*getter) (void) const,
V initialValue, CHECKER checker)
{
class MemberMethod : public ParamSpecHelper<T,V,CHECKER>
{
public:
MemberMethod (U (T::*getter) (void) const,
V initialValue, CHECKER checker)
: ParamSpecHelper<T,V,CHECKER> (initialValue, checker),
m_getter (getter)
{}
private:
virtual void DoSet (T *object, const V *v) const {}
virtual void DoGet (const T *object, V *v) const {
v->Set ((object->*m_getter) ());
}
U (T::*m_getter) (void) const;
};
return Ptr<ParamSpec> (new MemberMethod (getter, initialValue, checker), false);
}
template <typename T, typename U, typename V>
Ptr<ParamSpec>
MakeMemberMethodGetterParamSpec (U (T::*getter) (void) const,
V initialValue)
{
return MakeMemberMethodParamSpecWithChecker (getter, initialValue,
ParamSpecHelperSimpleChecker<U> ());
}
template <typename T, typename U, typename V, typename CHECKER>
Ptr<ParamSpec>
MakeMemberMethodSetterParamSpecWithChecker (void (T::*setter) (U),
V initialValue, CHECKER checker)
{
class MemberMethod : public ParamSpecHelper<T,V,CHECKER>
{
public:
MemberMethod (void (T::*setter) (U),
V initialValue, CHECKER checker)
: ParamSpecHelper<T,V,CHECKER> (initialValue, checker),
m_setter (setter)
{}
private:
virtual void DoSet (T *object, const V *v) const {
(object->*m_setter) (v->Get ());
}
virtual void DoGet (const T *object, V *v) const {
v->Set ((object->*m_setter) ());
}
void (T::*m_setter) (U);
};
return Ptr<ParamSpec> (new MemberMethod (setter, initialValue, checker), false);
}
template <typename T, typename U, typename V>
Ptr<ParamSpec>
MakeMemberMethodParamSpec (void (T::*setter) (U),
V initialValue)
{
return MakeMemberMethodSetterParamSpecWithChecker (setter, initialValue,
ParamSpecHelperSimpleChecker<U> ());
}
template <typename T, typename U, typename CHECKER>
ParamSpecHelper<T,U,CHECKER>::ParamSpecHelper (PValue initialValue, CHECKER checker)
: m_initialValue (initialValue),
m_checker (checker)
{}
template <typename T, typename U, typename V>
template <typename T, typename U, typename CHECKER>
bool
ParamSpecHelper<T,U,V>::Set (ObjectBase * object, PValue val) const
ParamSpecHelper<T,U,CHECKER>::Set (ObjectBase * object, PValue val) const
{
const U *value = val.DynCast<const U*> ();
if (value == 0)
@@ -99,13 +211,12 @@ ParamSpecHelper<T,U,V>::Set (ObjectBase * object, PValue val) const
{
return false;
}
V v = value->Get ();
DoSet (obj, v);
DoSet (obj, value);
return true;
}
template <typename T, typename U, typename V>
template <typename T, typename U, typename CHECKER>
bool
ParamSpecHelper<T,U,V>::Get (const ObjectBase * object, PValue val) const
ParamSpecHelper<T,U,CHECKER>::Get (const ObjectBase * object, PValue val) const
{
U *value = val.DynCast<U*> ();
if (value == 0)
@@ -117,111 +228,27 @@ ParamSpecHelper<T,U,V>::Get (const ObjectBase * object, PValue val) const
{
return false;
}
V v = DoGet (obj);
value->Set (v);
DoGet (obj, value);
return true;
}
template <typename T, typename U, typename V>
template <typename T, typename U, typename CHECKER>
bool
ParamSpecHelper<T,U,V>::Check (PValue value) const
ParamSpecHelper<T,U,CHECKER>::Check (PValue value) const
{
const U *val = value.DynCast<const U*> ();
if (val == 0)
{
return false;
}
V v = val->Get ();
DoCheck (v);
return true;
return m_checker.Check (val->Get ());
}
template <typename T, typename U, typename V>
bool
ParamSpecHelper<T,U,V>::DoCheck (const V &v) const
{
return true;
}
template <typename T, typename U, typename V>
template <typename T, typename U, typename CHECKER>
PValue
ParamSpecHelper<T,U,V>::CreateInitialValue (void) const
ParamSpecHelper<T,U,CHECKER>::CreateInitialValue (void) const
{
return PValue::Create<U> (m_initialValue);
return m_initialValue;
}
template <typename T, typename U, typename V>
MemberVariableParamSpecHelper<T,U,V>::MemberVariableParamSpecHelper (V T::*memberVariable, V initialValue)
: ParamSpecHelper<T,U,V> (initialValue),
m_memberVariable (memberVariable)
{}
template <typename T, typename U, typename V>
void
MemberVariableParamSpecHelper<T,U,V>::DoSet (T *object, const V &v) const
{
(object->*m_memberVariable) = v;
}
template <typename T, typename U, typename V>
V
MemberVariableParamSpecHelper<T,U,V>::DoGet (const T *object) const
{
return (object->*m_memberVariable);
}
template <typename T, typename U, typename V>
MemberMethodParamSpecHelper<T,U,V>::MemberMethodParamSpecHelper (void (T::*setter) (V),
V (T::*getter) (void) const,
V initialValue)
: ParamSpecHelper<T,U,V> (initialValue),
m_setter (setter),
m_getter (getter)
{}
template <typename T, typename U, typename V>
void
MemberMethodParamSpecHelper<T,U,V>::DoSet (T *object, const V &v) const
{
(object->*m_setter) (v);
}
template <typename T, typename U, typename V>
V
MemberMethodParamSpecHelper<T,U,V>::DoGet (const T *object) const
{
return (object->*m_getter) ();
}
template <typename T, typename U, typename V>
MemberMethodSetterParamSpecHelper<T,U,V>::MemberMethodSetterParamSpecHelper (void (T::*setter) (V),
V initialValue)
: ParamSpecHelper<T,U,V> (initialValue),
m_setter (setter)
{}
template <typename T, typename U, typename V>
void
MemberMethodSetterParamSpecHelper<T,U,V>::DoSet (T *object, const V &v) const
{
(object->m_setter) (v);
}
template <typename T, typename U, typename V>
V
MemberMethodSetterParamSpecHelper<T,U,V>::DoGet (const T *object) const
{
return V ();
}
template <typename T, typename U, typename V>
MemberMethodGetterParamSpecHelper<T,U,V>::MemberMethodGetterParamSpecHelper (V (T::*getter) (void) const)
: m_getter (getter)
{}
template <typename T, typename U, typename V>
void
MemberMethodGetterParamSpecHelper<T,U,V>::DoSet (T *object, const V &v) const
{}
template <typename T, typename U, typename V>
V
MemberMethodGetterParamSpecHelper<T,U,V>::DoGet (const T *object) const
{
return (object->*m_getter) ();
}
} // namespace ns3
#endif /* PARAM_SPEC_HELPER_H */

View File

@@ -2,8 +2,8 @@
#include "test.h"
#include "object.h"
#include "boolean-value.h"
#if 0
#include "int-value.h"
#if 0
#include "uint-value.h"
#include "fp-value.h"
#include "enum-value.h"
@@ -48,15 +48,17 @@ public:
false))
.AddParameter ("TestPtr", "help text",
MakePtrParamSpec (&ParamSpecObjectTest::m_derived))
#if 0
.AddParameter ("TestInt16", "help text",
MakeIntParamSpec (-2, &ParamSpecObjectTest::m_int16))
MakeIntParamSpec (&ParamSpecObjectTest::m_int16,
-2))
.AddParameter ("TestInt16WithBounds", "help text",
MakeIntParamSpec (-2, -5, 10,
&ParamSpecObjectTest::m_int16WithBounds))
MakeIntParamSpec (&ParamSpecObjectTest::m_int16WithBounds,
-2, -5, 10))
.AddParameter ("TestInt16SetGet", "help text",
MakeIntParamSpec (6, &ParamSpecObjectTest::DoSetInt16,
&ParamSpecObjectTest::DoGetInt16))
MakeIntParamSpec (&ParamSpecObjectTest::DoSetInt16,
&ParamSpecObjectTest::DoGetInt16,
6))
#if 0
.AddParameter ("TestUint8", "help text",
MakeUintParamSpec (1, &ParamSpecObjectTest::m_uint8))
.AddParameter ("TestFloat", "help text",
@@ -175,7 +177,6 @@ ParamSpecTest::RunTests (void)
derived = p->Get ("TestPtr");
NS_TEST_ASSERT (derived != 0);
#if 0
CHECK_GET_STR (p, "TestInt16", "-2");
CHECK_GET_PARAM (p, "TestInt16", IntValue, -2);
@@ -222,7 +223,8 @@ ParamSpecTest::RunTests (void)
NS_TEST_ASSERT (p->Set ("TestInt16SetGet", IntValue (0)));
CHECK_GET_STR (p, "TestInt16SetGet", "0");
CHECK_GET_PARAM (p, "TestInt16SetGet", IntValue, 0);
#if 0
CHECK_GET_STR (p, "TestUint8", "1");
CHECK_GET_PARAM (p, "TestUint8", UintValue, 1);
NS_TEST_ASSERT (p->Set ("TestUint8", UintValue (0)));

View File

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