From f44a019aed81b7aed2992bd0e5d73c80784b3245 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 7 Feb 2008 23:23:33 +0100 Subject: [PATCH] add back FpValue support --- src/core/fp-value.cc | 74 +++++++++++++++++++++++++++++++++++++++ src/core/fp-value.h | 78 ++++++++++++++++++++++++++++++++++++++++++ src/core/value-test.cc | 10 ++---- src/core/wscript | 1 + 4 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 src/core/fp-value.cc create mode 100644 src/core/fp-value.h diff --git a/src/core/fp-value.cc b/src/core/fp-value.cc new file mode 100644 index 000000000..71e2e3583 --- /dev/null +++ b/src/core/fp-value.cc @@ -0,0 +1,74 @@ +#include "fp-value.h" +#include "object.h" +#include + +namespace ns3 { + +FpValue::FpValue (double value) + : m_value (value) +{} +PValue +FpValue::Copy (void) const +{ + return PValue::Create (*this); +} + +void +FpValue::Set (double value) +{ + m_value = value; +} +double +FpValue::Get (void) const +{ + return m_value; +} +std::string +FpValue::SerializeToString (Ptr spec) const +{ + std::ostringstream oss; + oss << m_value; + return oss.str (); +} +bool +FpValue::DeserializeFromString (std::string value, Ptr 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 (); + 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 (*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 diff --git a/src/core/fp-value.h b/src/core/fp-value.h new file mode 100644 index 000000000..f4d7da3ce --- /dev/null +++ b/src/core/fp-value.h @@ -0,0 +1,78 @@ +#ifndef FP_VALUE_H +#define FP_VALUE_H + +#include "value.h" +#include "param-spec-helper.h" +#include + +namespace ns3 { + +class FpValue : public Value +{ +public: + FpValue (double value); + + virtual PValue Copy (void) const; + virtual std::string SerializeToString (Ptr spec) const; + virtual bool DeserializeFromString (std::string value, Ptr spec); + + void Set (double value); + double Get (void) const; + + FpValue (PValue value); + operator PValue () const; +private: + double m_value; +}; + + +template +Ptr MakeFpParamSpec (U T::*memberVariable, + double initialValue); +template +Ptr 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 +Ptr +MakeFpParamSpec (U T::*memberVariable, + double initialValue) +{ + double minValue = -std::numeric_limits::max (); + double maxValue = std::numeric_limits::max (); + return MakeMemberVariableParamSpecWithChecker (memberVariable, + FpValue (initialValue), + FpValueChecker (minValue, maxValue)); +} + +template +Ptr +MakeFpParamSpec (U T::*memberVariable, + double initialValue, + double minValue, + double maxValue) +{ + return MakeMemberVariableParamSpecWithChecker (memberVariable, + FpValue (initialValue), + FpValueChecker (minValue, maxValue)); +} + +} // namespace ns3 + +#endif /* FP_VALUE_H */ diff --git a/src/core/value-test.cc b/src/core/value-test.cc index dc31c45b1..83319abf8 100644 --- a/src/core/value-test.cc +++ b/src/core/value-test.cc @@ -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))); diff --git a/src/core/wscript b/src/core/wscript index 95cb7076a..ac96b2ba6 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -59,6 +59,7 @@ def build(bld): 'int-value.cc', 'uint-value.cc', 'enum-value.cc', + 'fp-value.cc', ] if sys.platform == 'win32':