From 5137b37f86d862cf94cd8a207997c8455c7b85b5 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 7 Feb 2008 23:56:03 +0100 Subject: [PATCH] add Value support to Rectangle --- src/mobility/rectangle.cc | 102 ++++++++++++++++++++++++++++++++++++++ src/mobility/rectangle.h | 44 ++++++++++++++++ 2 files changed, 146 insertions(+) diff --git a/src/mobility/rectangle.cc b/src/mobility/rectangle.cc index e39f8ae74..01f2ea653 100644 --- a/src/mobility/rectangle.cc +++ b/src/mobility/rectangle.cc @@ -20,8 +20,10 @@ #include "rectangle.h" #include "vector.h" #include "ns3/assert.h" +#include "ns3/fatal-error.h" #include #include +#include namespace ns3 { @@ -117,5 +119,105 @@ Rectangle::CalculateIntersection (const Vector ¤t, const Vector &speed) co } +Rectangle::Rectangle (PValue value) +{ + const RectangleValue *v = value.DynCast (); + if (v == 0) + { + NS_FATAL_ERROR ("expecting value of type Rectangle."); + } + *this = v->Get (); +} +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; +} + +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) +{ + 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) + { + 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; + } + 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; +} + } // namespace ns3 diff --git a/src/mobility/rectangle.h b/src/mobility/rectangle.h index d61620960..94a721d23 100644 --- a/src/mobility/rectangle.h +++ b/src/mobility/rectangle.h @@ -20,6 +20,9 @@ #ifndef RECTANGLE_H #define RECTANGLE_H +#include "ns3/value.h" +#include "ns3/param-spec-helper.h" + namespace ns3 { class Vector; @@ -58,8 +61,49 @@ public: double xMax; double yMin; double yMax; + + Rectangle (PValue value); + operator PValue () const; }; +class RectangleValue : public Value +{ +public: + RectangleValue (const Rectangle &rectangle); + + void Set (const Rectangle &v); + Rectangle Get (void) const; + + // 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; +}; + +template +Ptr MakeRectangleParamSpec (Rectangle T::*memberVariable, + Rectangle initialValue); + +} // namespace ns3 + +namespace ns3 { + +template +Ptr +MakeRectangleParamSpec (Rectangle T::*memberVariable, + Rectangle initialValue) +{ + return MakeMemberVariableParamSpec (memberVariable, initialValue); +} + + } // namespace ns3 #endif /* RECTANGLE_H */