# HG changeset patch # User Mathieu Lacage # Date 1202938757 -3600 # Node ID 8744ec60e8e63dbf3fbe12898488d961d1ba7c52 # Parent 04fe819b3ad13cb65c7aa7358fc84e23876e50c0 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper diff -r 04fe819b3ad1 -r 8744ec60e8e6 src/core/class-value-helper.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/core/class-value-helper.h Wed Feb 13 22:39:17 2008 +0100 @@ -0,0 +1,96 @@ +#ifndef CLASS_VALUE_HELPER_H +#define CLASS_VALUE_HELPER_H + +#include "value.h" +#include "param-spec-helper.h" +#include "fatal-error.h" +#include + +namespace ns3 { + +template +class ClassValue : public U +{ +public: + ClassValue (const T &value) + : m_value (value) {} + + void Set (const T &v) { + m_value = v; + } + T Get (void) const { + return m_value; + } + + // inherited from Value base class + virtual PValue Copy (void) const { + return PValue::Create > (*this); + } + virtual std::string SerializeToString (Ptr spec) const { + std::ostringstream oss; + oss << m_value; + return oss.str (); + } + virtual bool DeserializeFromString (std::string value, Ptr spec) { + std::istringstream iss; + iss.str (value); + iss >> m_value; + return !iss.bad () && !iss.fail (); + } + + ClassValue (PValue value) + { + ClassValue *v = value.DynCast *> (); + if (v == 0) + { + NS_FATAL_ERROR ("Unexpected type of value. Expected \""<< typeid (U).name () <<"\""); + } + m_value = v->Get (); + } + operator PValue () const + { + return PValue::Create > (*this); + } + +private: + T m_value; +}; + +template +T +ClassValueHelperExtractFrom (PValue value) +{ + ClassValue *v = value.DynCast *> (); + if (v == 0) + { + NS_FATAL_ERROR ("Unexpected type of value. Expected \"" << typeid (U).name () << "\""); + } + return v->Get (); +} + +template +PValue +ClassValueHelperConvertTo (const T *self) +{ + return PValue::Create > (*self); +} + + +template +Ptr +MakeClassValueHelperParamSpec (T1 a1, const T &initialValue) +{ + return MakeParamSpecHelper (a1, ClassValue (initialValue)); +} + + +template +Ptr +MakeClassValueHelperParamSpec (T1 a1, T2 a2, const T &initialValue) +{ + return MakeParamSpecHelper (a1, a2, ClassValue (initialValue)); +} + +} // namespace ns3 + +#endif /* CLASS_VALUE_HELPER_H */ diff -r 04fe819b3ad1 -r 8744ec60e8e6 src/core/random-variable.cc --- a/src/core/random-variable.cc Wed Feb 13 21:00:33 2008 +0100 +++ b/src/core/random-variable.cc Wed Feb 13 22:39:17 2008 +0100 @@ -300,18 +300,13 @@ return m_variable; } RandomVariable::RandomVariable (PValue value) + : m_variable (0) { - const RandomVariableValue *v = value.DynCast (); - if (v == 0) - { - NS_FATAL_ERROR ("assigning non-RandomVariable value to RandomVariable value."); - } - m_variable = v->Get ().m_variable->Copy (); - + *this = ClassValueHelperExtractFrom (value); } RandomVariable::operator PValue () const { - return PValue::Create (*this); + return ClassValueHelperConvertTo (this); } @@ -1597,76 +1592,53 @@ } -RandomVariableValue::RandomVariableValue (RandomVariable variable) - : m_variable (variable) -{} -void -RandomVariableValue::Set (RandomVariable variable) -{ - m_variable = variable; -} -RandomVariable -RandomVariableValue::Get (void) const +std::ostream &operator << (std::ostream &os, const RandomVariable &var) { - return m_variable; -} - -PValue -RandomVariableValue::Copy (void) const -{ - return PValue::Create (m_variable); -} -std::string -RandomVariableValue::SerializeToString (Ptr spec) const -{ - std::ostringstream oss; - RandomVariableBase *base = m_variable.Peek (); + RandomVariableBase *base = var.Peek (); ConstantVariableImpl *constant = dynamic_cast (base); if (constant != 0) { - oss << "Constant:" << constant->GetValue (); - return oss.str (); + os << "Constant:" << constant->GetValue (); + return os; } UniformVariableImpl *uniform = dynamic_cast (base); if (uniform != 0) { - oss << "Uniform:" << uniform->GetMin () << ":" << uniform->GetMax (); - return oss.str (); + os << "Uniform:" << uniform->GetMin () << ":" << uniform->GetMax (); + return os; } // XXX: support other distributions - return ""; + os.setstate (std::ios_base::badbit); + return os; } -bool -RandomVariableValue::DeserializeFromString (std::string value, Ptr spec) +std::istream &operator >> (std::istream &is, RandomVariable &var) { + std::string value; + is >> value; std::string::size_type tmp; tmp = value.find (":"); if (tmp == std::string::npos) { - return false; + is.setstate (std::ios_base::badbit); + return is; } std::string type = value.substr (0, tmp); if (value == "Constant") { - // XXX - return true; + // XXX parse + var = ConstantVariable (); } else if (value == "Uniform") { - // XXX - return true; + // XXX parse + var = UniformVariable (); } - // XXX: support other distributions. - return false; + else + { + // XXX: support other distributions. + } + return is; } -RandomVariableValue::RandomVariableValue (PValue value) - : m_variable (value) -{} -RandomVariableValue::operator PValue () const -{ - return m_variable; -} - diff -r 04fe819b3ad1 -r 8744ec60e8e6 src/core/random-variable.h --- a/src/core/random-variable.h Wed Feb 13 21:00:33 2008 +0100 +++ b/src/core/random-variable.h Wed Feb 13 22:39:17 2008 +0100 @@ -24,7 +24,10 @@ #include #include #include +#include +#include #include "value.h" +#include "class-value-helper.h" /** * \ingroup core @@ -166,6 +169,9 @@ private: friend class RandomVariableValue; + friend std::ostream &operator << (std::ostream &os, const RandomVariable &var); + friend std::istream &operator >> (std::istream &os, RandomVariable &var); + RandomVariableBase *m_variable; protected: RandomVariable (const RandomVariableBase &variable); @@ -655,23 +661,11 @@ static double GetSingleValue(double s, double l, double mean); }; - -class RandomVariableValue : public Value -{ -public: - RandomVariableValue (RandomVariable variable); - void Set (RandomVariable variable); - RandomVariable Get (void) const; +std::ostream &operator << (std::ostream &os, const RandomVariable &var); +std::istream &operator >> (std::istream &os, RandomVariable &var); - virtual PValue Copy (void) const; - virtual std::string SerializeToString (Ptr spec) const; - virtual bool DeserializeFromString (std::string value, Ptr spec); - RandomVariableValue (PValue value); - operator PValue () const; -private: - RandomVariable m_variable; -}; +class RandomVariableValue : public Value {}; template Ptr MakeRandomVariableParamSpec (T1 a1, @@ -690,14 +684,14 @@ Ptr MakeRandomVariableParamSpec (T1 a1, RandomVariable initialValue) { - return MakeParamSpecHelper (a1, RandomVariableValue (initialValue)); + return MakeClassValueHelperParamSpec (a1, initialValue); } template Ptr MakeRandomVariableParamSpec (T1 a1, T2 a2, RandomVariable initialValue) { - return MakeParamSpecHelper (a1, a2, RandomVariableValue (initialValue)); + return MakeClassValueHelperParamSpec (a1, a2, initialValue); } diff -r 04fe819b3ad1 -r 8744ec60e8e6 src/core/wscript --- a/src/core/wscript Wed Feb 13 21:00:33 2008 +0100 +++ b/src/core/wscript Wed Feb 13 22:39:17 2008 +0100 @@ -113,5 +113,6 @@ 'fp-value.h', 'enum-value.h', 'object-factory.h', + 'class-value-helper.h', ] diff -r 04fe819b3ad1 -r 8744ec60e8e6 src/mobility/rectangle.cc --- a/src/mobility/rectangle.cc Wed Feb 13 21:00:33 2008 +0100 +++ b/src/mobility/rectangle.cc Wed Feb 13 22:39:17 2008 +0100 @@ -121,102 +121,31 @@ Rectangle::Rectangle (PValue value) { - const RectangleValue *v = value.DynCast (); - if (v == 0) - { - NS_FATAL_ERROR ("expecting value of type Rectangle."); - } - *this = v->Get (); + *this = ClassValueHelperExtractFrom (value); } Rectangle::operator PValue () const { - return PValue::Create (*this); -} - - - -RectangleValue::RectangleValue (const Rectangle &rectangle) - : m_rectangle (rectangle) -{} - -void -RectangleValue::Set (const Rectangle &v) -{ - m_rectangle = v; -} -Rectangle -RectangleValue::Get (void) const -{ - return m_rectangle; + return ClassValueHelperConvertTo (this); } -PValue -RectangleValue::Copy (void) const -{ - return PValue::Create (*this); -} -std::string -RectangleValue::SerializeToString (Ptr spec) const -{ - std::ostringstream oss; - oss << m_rectangle.xMin << "|" << m_rectangle.xMax << "|" << m_rectangle.yMin << "|" << m_rectangle.yMax; - return oss.str (); -} -void -RectangleValue::ReadAsDouble (std::string value, double &v, bool &ok) -{ - std::istringstream iss; - iss.str (value); - double local; - iss >> local; - bool good = !iss.bad () && !iss.fail (); - if (good) - { - v = local; - } - else - { - ok = false; - } -} -bool -RectangleValue::DeserializeFromString (std::string value, Ptr spec) +std::ostream & +operator << (std::ostream &os, const Rectangle &rectangle) { - bool ok = true; - std::istringstream iss; - iss.str (value); - std::string::size_type cur = 0, next = 0; - next = value.find ("|", cur); - if (next == std::string::npos) + os << rectangle.xMin << "|" << rectangle.xMax << "|" << rectangle.yMin << "|" << rectangle.yMax; + return os; +} +std::istream & +operator >> (std::istream &is, Rectangle &rectangle) + { + char c1, c2, c3; + is >> rectangle.xMin >> c1 >> rectangle.xMax >> c2 >> rectangle.yMin >> c3 >> rectangle.yMax; + if (c1 != '|' || + c2 != '|' || + c3 != '|') { - return false; - } - ReadAsDouble (value.substr (cur, next-cur), m_rectangle.xMin, ok); - cur = next + 1; - next = value.find ("|", cur); - if (next == std::string::npos) - { - return false; + is.setstate (std::ios_base::failbit); } - ReadAsDouble (value.substr (cur, next-cur), m_rectangle.xMax, ok); - cur = next + 1; - next = value.find ("|", cur); - if (next == std::string::npos) - { - return false; - } - ReadAsDouble (value.substr (cur, next-cur), m_rectangle.yMin, ok); - cur = next + 1; - ReadAsDouble (value.substr (cur, value.size ()-cur), m_rectangle.yMax, ok); - return ok; -} - -RectangleValue::RectangleValue (PValue value) - : m_rectangle (value) -{} -RectangleValue::operator PValue () const -{ - return m_rectangle; + return is; } diff -r 04fe819b3ad1 -r 8744ec60e8e6 src/mobility/rectangle.h --- a/src/mobility/rectangle.h Wed Feb 13 21:00:33 2008 +0100 +++ b/src/mobility/rectangle.h Wed Feb 13 22:39:17 2008 +0100 @@ -21,7 +21,7 @@ #define RECTANGLE_H #include "ns3/value.h" -#include "ns3/param-spec-helper.h" +#include "ns3/class-value-helper.h" namespace ns3 { @@ -66,26 +66,11 @@ operator PValue () const; }; -class RectangleValue : public Value -{ -public: - RectangleValue (const Rectangle &rectangle); - - void Set (const Rectangle &v); - Rectangle Get (void) const; +std::ostream &operator << (std::ostream &os, const Rectangle &rectangle); +std::istream &operator >> (std::istream &is, Rectangle &rectangle); - // inherited from Parameter base class - virtual PValue Copy (void) const; - virtual std::string SerializeToString (Ptr spec) const; - virtual bool DeserializeFromString (std::string value, Ptr spec); - RectangleValue (PValue value); - operator PValue () const; - -private: - void ReadAsDouble (std::string value, double &v, bool &ok); - Rectangle m_rectangle; -}; +class RectangleValue : public Value {}; template Ptr MakeRectangleParamSpec (T1 a1, @@ -102,13 +87,13 @@ Ptr MakeRectangleParamSpec (T1 a1, Rectangle initialValue) { - return MakeParamSpecHelper (a1, RectangleValue (initialValue)); + return MakeClassValueHelperParamSpec (a1, initialValue); } template Ptr MakeRectangleParamSpec (T1 a1, T2 a2, Rectangle initialValue) { - return MakeParamSpecHelper (a1, a2, RectangleValue (initialValue)); + return MakeClassValueHelperParamSpec (a1, a2, initialValue); } } // namespace ns3 diff -r 04fe819b3ad1 -r 8744ec60e8e6 src/mobility/vector.cc --- a/src/mobility/vector.cc Wed Feb 13 21:00:33 2008 +0100 +++ b/src/mobility/vector.cc Wed Feb 13 22:39:17 2008 +0100 @@ -39,16 +39,11 @@ Vector::Vector (PValue value) { - const VectorValue *v = value.DynCast (); - if (v == 0) - { - NS_FATAL_ERROR ("Expected value of type Vector"); - } - *this = v->Get (); + *this = ClassValueHelperExtractFrom (value); } Vector::operator PValue () const { - return PValue::Create (*this); + return ClassValueHelperConvertTo (this); } @@ -62,49 +57,21 @@ return distance; } - - -VectorValue::VectorValue (const Vector &vector) - : m_vector (vector) -{} - -void -VectorValue::Set (const Vector &vector) +std::ostream &operator << (std::ostream &os, const Vector &vector) +{ + os << vector.x << ":" << vector.y << ":" << vector.z; + return os; +} +std::istream &operator >> (std::istream &is, Vector &vector) { - m_vector = vector; -} -Vector -VectorValue::Get (void) const -{ - return m_vector; + char c1, c2; + is >> vector.x >> c1 >> vector.y >> c2 >> vector.z; + if (c1 != ':' || + c2 != ':') + { + is.setstate (std::ios_base::failbit); + } + return is; } -PValue -VectorValue::Copy (void) const -{ - return PValue::Create (*this); -} -std::string -VectorValue::SerializeToString (Ptr spec) const -{ - std::ostringstream oss; - oss << m_vector.x << ":" << m_vector.y << ":" << m_vector.z; - return oss.str (); -} -bool -VectorValue::DeserializeFromString (std::string value, Ptr spec) -{ - // XXX implement parsing - return false; -} - -VectorValue::VectorValue (PValue value) - : m_vector (value) -{} -VectorValue::operator PValue () const -{ - return m_vector; -} - - } // namespace ns3 diff -r 04fe819b3ad1 -r 8744ec60e8e6 src/mobility/vector.h --- a/src/mobility/vector.h Wed Feb 13 21:00:33 2008 +0100 +++ b/src/mobility/vector.h Wed Feb 13 22:39:17 2008 +0100 @@ -21,7 +21,7 @@ #define VECTOR_H #include "ns3/value.h" -#include "ns3/param-spec-helper.h" +#include "ns3/class-value-helper.h" namespace ns3 { @@ -64,23 +64,10 @@ double CalculateDistance (const Vector &a, const Vector &b); -class VectorValue : public Value -{ -public: - VectorValue (const Vector &vector); - - void Set (const Vector &vector); - Vector Get (void) const; +class VectorValue : public Value {}; - virtual PValue Copy (void) const; - virtual std::string SerializeToString (Ptr spec) const; - virtual bool DeserializeFromString (std::string value, Ptr spec); - - VectorValue (PValue value); - operator PValue () const; -private: - Vector m_vector; -}; +std::ostream &operator << (std::ostream &os, const Vector &vector); +std::istream &operator >> (std::istream &is, Vector &vector); template Ptr @@ -99,7 +86,7 @@ Ptr MakeVectorParamSpec (T1 a1, const Vector &initialValue) { - return MakeParamSpecHelper (a1, VectorValue (initialValue)); + return MakeClassValueHelperParamSpec (a1, initialValue); } template @@ -107,7 +94,7 @@ MakeVectorParamSpec (T1 a1, T2 a2, const Vector &initialValue) { - return MakeParamSpecHelper (a1, a2, VectorValue (initialValue)); + return MakeClassValueHelperParamSpec (a1, a2, initialValue); } } // namespace ns3 diff -r 04fe819b3ad1 -r 8744ec60e8e6 src/simulator/nstime.h --- a/src/simulator/nstime.h Wed Feb 13 21:00:33 2008 +0100 +++ b/src/simulator/nstime.h Wed Feb 13 22:39:17 2008 +0100 @@ -22,7 +22,7 @@ #include "ns3/assert.h" #include "ns3/value.h" -#include "ns3/param-spec-helper.h" +#include "ns3/class-value-helper.h" #include #include #include @@ -520,7 +520,8 @@ typedef TimeUnit<1> Time; -std::ostream& operator<< (std::ostream& os, Time const& time); +std::ostream& operator<< (std::ostream& os, const Time & time); +std::istream& operator>> (std::istream& is, Time & time); /** * \brief create ns3::Time instances in units of seconds. @@ -669,24 +670,7 @@ typedef TimeUnit<2> TimeSquare; -class TimeValue : public Value -{ -public: - TimeValue (Time time); - - void Set (Time time); - Time Get (void) const; - - // inherited from Value base class. - virtual PValue Copy (void) const; - virtual std::string SerializeToString (Ptr spec) const; - virtual bool DeserializeFromString (std::string value, Ptr spec); - - TimeValue (PValue value); - operator PValue () const; -private: - Time m_time; -}; +class TimeValue : public Value {}; template Ptr MakeTimeParamSpec (T1 a1, @@ -704,14 +688,14 @@ Ptr MakeTimeParamSpec (T1 a1, Time initialValue) { - return MakeParamSpecHelper (a1, TimeValue (initialValue)); + return MakeClassValueHelperParamSpec (a1, initialValue); } template Ptr MakeTimeParamSpec (T1 a1, T2 a2, Time initialValue) { - return MakeParamSpecHelper (a1, a2, TimeValue (initialValue)); + return MakeClassValueHelperParamSpec (a1, a2, initialValue); } diff -r 04fe819b3ad1 -r 8744ec60e8e6 src/simulator/time.cc --- a/src/simulator/time.cc Wed Feb 13 21:00:33 2008 +0100 +++ b/src/simulator/time.cc Wed Feb 13 22:39:17 2008 +0100 @@ -189,7 +189,7 @@ std::ostream& -operator<< (std::ostream& os, Time const& time) +operator<< (std::ostream& os, const Time & time) { std::string unit; switch (TimeStepPrecision::Get ()) { @@ -215,6 +215,60 @@ os << time.GetTimeStep () << unit; return os; } +std::istream& operator>> (std::istream& is, Time & time) +{ + std::string value; + is >> value; + std::string::size_type n = value.find_first_not_of("0123456789."); + if (n == std::string::npos) + { + is.setstate (std::ios_base::failbit); + return is; + } + std::string trailer = value.substr(n, value.size ()-1-n); + std::istringstream iss; + iss.str (value.substr(0, n)); + + if (trailer == std::string("s")) + { + double v; + iss >> v; + time = Seconds (v); + return is; + } + uint64_t integer; + iss >> integer; + if (is.bad () || is.fail ()) + { + is.setstate (std::ios_base::failbit); + } + else if (trailer == std::string("ms")) + { + time = MilliSeconds (integer); + } + else if (trailer == std::string("us")) + { + time = MicroSeconds (integer); + } + else if (trailer == std::string("ns")) + { + time = NanoSeconds (integer); + } + else if (trailer == std::string("ps")) + { + time = PicoSeconds (integer); + } + else if (trailer == std::string("fs")) + { + time = FemtoSeconds (integer); + } + else + { + is.setstate (std::ios_base::failbit); + // XXX: problem ? + } + return is; +} Time Seconds (double seconds) { @@ -244,16 +298,11 @@ TimeUnit<1>::TimeUnit (PValue value) { - const TimeValue *v = value.DynCast (); - if (v == 0) - { - NS_FATAL_ERROR ("Expected value of type TimeValue."); - } - *this = v->Get (); + *this = ClassValueHelperExtractFrom (value); } TimeUnit<1>::operator PValue () const { - return PValue::Create (*this); + return ClassValueHelperConvertTo (this); } Time MilliSeconds (uint64_t ms) @@ -284,93 +333,6 @@ return TimeStep(ts); } -TimeValue::TimeValue (const Time time) - : m_time (time) -{} -void -TimeValue::Set (Time time) -{ - m_time = time; -} -Time -TimeValue::Get (void) const -{ - return m_time; -} -PValue -TimeValue::Copy (void) const -{ - return PValue::Create (*this); -} -std::string -TimeValue::SerializeToString (Ptr spec) const -{ - std::ostringstream oss; - oss << m_time.GetSeconds () << "s"; - return oss.str (); -} -bool -TimeValue::DeserializeFromString (std::string value, Ptr spec) -{ - std::string::size_type n = value.find_first_not_of("0123456789."); - if (n == std::string::npos) - { - return false; - } - std::string trailer = value.substr(n, value.size ()-1-n); - std::istringstream iss; - iss.str (value.substr(0, n)); - - if (trailer == std::string("s")) - { - double v; - iss >> v; - m_time = Seconds (v); - return !iss.bad () && !iss.fail (); - } - uint64_t integer; - iss >> integer; - if (iss.bad () || iss.fail ()) - { - return false; - } - if (trailer == std::string("ms")) - { - m_time = MilliSeconds (integer); - return true; - } - if (trailer == std::string("us")) - { - m_time = MicroSeconds (integer); - return true; - } - if (trailer == std::string("ns")) - { - m_time = NanoSeconds (integer); - return true; - } - if (trailer == std::string("ps")) - { - m_time = PicoSeconds (integer); - return true; - } - if (trailer == std::string("fs")) - { - m_time = FemtoSeconds (integer); - return true; - } - return false; -} - -TimeValue::TimeValue (PValue value) - : m_time (value) -{} - -TimeValue::operator PValue () const -{ - return m_time; -} - /* * The timestep value passed to this function must be of the precision