add back FpValue support

This commit is contained in:
Mathieu Lacage
2008-02-07 23:23:33 +01:00
parent f826fbc2d9
commit f44a019aed
4 changed files with 156 additions and 7 deletions

74
src/core/fp-value.cc Normal file
View File

@@ -0,0 +1,74 @@
#include "fp-value.h"
#include "object.h"
#include <sstream>
namespace ns3 {
FpValue::FpValue (double value)
: m_value (value)
{}
PValue
FpValue::Copy (void) const
{
return PValue::Create<FpValue> (*this);
}
void
FpValue::Set (double value)
{
m_value = value;
}
double
FpValue::Get (void) const
{
return m_value;
}
std::string
FpValue::SerializeToString (Ptr<const ParamSpec> spec) const
{
std::ostringstream oss;
oss << m_value;
return oss.str ();
}
bool
FpValue::DeserializeFromString (std::string value, Ptr<const ParamSpec> spec)
{
double v;
std::istringstream iss;
iss.str (value);
iss >> v;
bool ok = !iss.bad () && !iss.fail ();
if (ok)
{
m_value = v;
}
return ok;
}
FpValue::FpValue (PValue value)
{
const FpValue *v = value.DynCast<const FpValue *> ();
if (v == 0)
{
NS_FATAL_ERROR ("assigning non-Fp value to Fp value.");
}
m_value = v->m_value;
}
FpValue::operator PValue () const
{
return PValue::Create<FpValue> (*this);
}
FpValueChecker::FpValueChecker (double min, double max)
: m_min (min),
m_max (max)
{}
bool
FpValueChecker::Check (double v) const
{
return v >= m_min && v <= m_max;
}
} // namespace ns3

78
src/core/fp-value.h Normal file
View File

@@ -0,0 +1,78 @@
#ifndef FP_VALUE_H
#define FP_VALUE_H
#include "value.h"
#include "param-spec-helper.h"
#include <stdint.h>
namespace ns3 {
class FpValue : public Value
{
public:
FpValue (double value);
virtual PValue Copy (void) const;
virtual std::string SerializeToString (Ptr<const ParamSpec> spec) const;
virtual bool DeserializeFromString (std::string value, Ptr<const ParamSpec> spec);
void Set (double value);
double Get (void) const;
FpValue (PValue value);
operator PValue () const;
private:
double m_value;
};
template <typename U, typename T>
Ptr<ParamSpec> MakeFpParamSpec (U T::*memberVariable,
double initialValue);
template <typename U, typename T>
Ptr<ParamSpec> MakeFpParamSpec (U T::*memberVariable,
double initialValue,
double minValue,
double maxValue);
} // namespace ns3
namespace ns3 {
class FpValueChecker
{
public:
FpValueChecker (double min, double max);
bool Check (double v) const;
private:
double m_min;
double m_max;
};
template <typename U, typename T>
Ptr<ParamSpec>
MakeFpParamSpec (U T::*memberVariable,
double initialValue)
{
double minValue = -std::numeric_limits<U>::max ();
double maxValue = std::numeric_limits<U>::max ();
return MakeMemberVariableParamSpecWithChecker (memberVariable,
FpValue (initialValue),
FpValueChecker (minValue, maxValue));
}
template <typename U, typename T>
Ptr<ParamSpec>
MakeFpParamSpec (U T::*memberVariable,
double initialValue,
double minValue,
double maxValue)
{
return MakeMemberVariableParamSpecWithChecker (memberVariable,
FpValue (initialValue),
FpValueChecker (minValue, maxValue));
}
} // namespace ns3
#endif /* FP_VALUE_H */

View File

@@ -6,9 +6,7 @@
#include "uint-value.h"
#include "enum-value.h"
#include "random-variable.h"
#if 0
#include "fp-value.h"
#endif
namespace ns3 {
class ParamSpecTest : public Test
@@ -70,10 +68,8 @@ public:
.AddParameter ("TestRandom", "help text",
MakeRandomVariableParamSpec (&ParamSpecObjectTest::m_random,
ConstantVariable (1.0)))
#if 0
.AddParameter ("TestFloat", "help text",
MakeFpParamSpec (-1.1, &ParamSpecObjectTest::m_float))
#endif
MakeFpParamSpec (&ParamSpecObjectTest::m_float, -1.1))
;
return tid;
@@ -250,11 +246,11 @@ ParamSpecTest::RunTests (void)
NS_TEST_ASSERT (!p->Set ("TestUint8", UintValue (-1)));
CHECK_GET_STR (p, "TestUint8", "255");
CHECK_GET_PARAM (p, "TestUint8", UintValue, 255);
#if 0
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)));

View File

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