diff --git a/src/core/uint-value.cc b/src/core/uint-value.cc new file mode 100644 index 000000000..0445e7899 --- /dev/null +++ b/src/core/uint-value.cc @@ -0,0 +1,75 @@ +#include "uint-value.h" +#include "fatal-error.h" +#include + +namespace ns3 { + +UintValue::UintValue (uint64_t value) + : m_value (value) +{} +PValue +UintValue::Copy (void) const +{ + return PValue::Create (*this); +} + +void +UintValue::Set (uint64_t value) +{ + m_value = value; +} +uint64_t +UintValue::Get (void) const +{ + return m_value; +} +std::string +UintValue::SerializeToString (Ptr spec) const +{ + std::ostringstream oss; + oss << m_value; + return oss.str (); +} +bool +UintValue::DeserializeFromString (std::string value, Ptr spec) +{ + uint64_t v; + std::istringstream iss; + iss.str (value); + iss >> v; + bool ok = !iss.bad () && !iss.fail (); + if (ok) + { + m_value = v; + } + return ok; +} + +UintValue::UintValue (PValue value) +{ + const UintValue *v = value.DynCast (); + if (v == 0) + { + NS_FATAL_ERROR ("assigning non-Uint value to Uint value."); + } + m_value = v->m_value; +} +UintValue::operator PValue () const +{ + return PValue::Create (*this); +} + + + +UintValueChecker::UintValueChecker (uint64_t minValue, uint64_t maxValue) + : m_minValue (minValue), + m_maxValue (maxValue) +{} +bool +UintValueChecker::Check (const uint64_t &value) const +{ + return value >= m_minValue && value <= m_maxValue; +} + + +} // namespace ns3 diff --git a/src/core/uint-value.h b/src/core/uint-value.h new file mode 100644 index 000000000..f53177c7f --- /dev/null +++ b/src/core/uint-value.h @@ -0,0 +1,108 @@ +#ifndef UINT_VALUE_H +#define UINT_VALUE_H + +#include "value.h" +#include "param-spec-helper.h" +#include + +namespace ns3 { + +class UintValue : public Value +{ +public: + UintValue (uint64_t value); + virtual PValue Copy (void) const; + virtual std::string SerializeToString (Ptr spec) const; + virtual bool DeserializeFromString (std::string value, Ptr spec); + + void Set (uint64_t value); + uint64_t Get (void) const; + + UintValue (PValue value); + operator PValue () const; +private: + uint64_t m_value; +}; + +template +Ptr MakeUintParamSpec (uint64_t initialValue, + U T::*memberVariable); +template +Ptr MakePtrUintParamSpec (uint64_t initialValue, + uint64_t minValue, + uint64_t maxValue, + U T::*memberVariable); +template +Ptr MakeUintParamSpec (uint64_t initialValue, + void (T::*setter) (U), + U (T::*getter) (void) const); +template +Ptr MakeUintParamSpec (uint64_t initialValue, + uint64_t minValue, + uint64_t maxValue, + void (T::*setter) (U), + U (T::*getter) (void) const); + +} // namespace ns3 + +namespace ns3 { + +class UintValueChecker +{ +public: + UintValueChecker (uint64_t minValue, uint64_t maxValue); + bool Check (const uint64_t &value) const; +private: + uint64_t m_minValue; + uint64_t m_maxValue; +}; + +template +Ptr +MakeUintParamSpec (U T::*memberVariable, + uint64_t initialValue) +{ + uint64_t minValue = std::numeric_limits::min (); + uint64_t maxValue = std::numeric_limits::max (); + return MakeMemberVariableParamSpecWithChecker (memberVariable, UintValue (initialValue), + UintValueChecker (minValue, maxValue)); +} + +template +Ptr +MakeUintParamSpec (U T::*memberVariable, + uint64_t initialValue, + uint64_t minValue, + uint64_t maxValue) +{ + return MakeMemberVariableParamSpecWithChecker (memberVariable, UintValue (initialValue), + UintValueChecker (minValue, maxValue)); +} + +template +Ptr +MakeUintParamSpec (void (T::*setter) (U), + U (T::*getter) (void) const, + uint64_t initialValue) +{ + uint64_t minValue = std::numeric_limits::min (); + uint64_t maxValue = std::numeric_limits::max (); + return MakeMemberMethodParamSpecWithChecker (setter, getter, UintValue (initialValue), + UintValueChecker (minValue, maxValue)); +} +template +Ptr +MakeUintParamSpec (void (T::*setter) (U), + U (T::*getter) (void) const, + uint64_t initialValue, + uint64_t minValue, + uint64_t maxValue) +{ + return MakeMemberMethodParamSpecWithChecker (setter, getter, UintValue (initialValue), + UintValueChecker (minValue, maxValue)); +} + + +} // namespace ns3 + +#endif /* UINT_PARAMETER_H */