add Value support to Rectangle

This commit is contained in:
Mathieu Lacage
2008-02-07 23:56:03 +01:00
parent 16086b3349
commit 5137b37f86
2 changed files with 146 additions and 0 deletions

View File

@@ -20,8 +20,10 @@
#include "rectangle.h"
#include "vector.h"
#include "ns3/assert.h"
#include "ns3/fatal-error.h"
#include <cmath>
#include <algorithm>
#include <sstream>
namespace ns3 {
@@ -117,5 +119,105 @@ Rectangle::CalculateIntersection (const Vector &current, const Vector &speed) co
}
Rectangle::Rectangle (PValue value)
{
const RectangleValue *v = value.DynCast<const RectangleValue *> ();
if (v == 0)
{
NS_FATAL_ERROR ("expecting value of type Rectangle.");
}
*this = v->Get ();
}
Rectangle::operator PValue () const
{
return PValue::Create<RectangleValue> (*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<RectangleValue> (*this);
}
std::string
RectangleValue::SerializeToString (Ptr<const ParamSpec> 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<const ParamSpec> 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

View File

@@ -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<const ParamSpec> spec) const;
virtual bool DeserializeFromString (std::string value, Ptr<const ParamSpec> spec);
RectangleValue (PValue value);
operator PValue () const;
private:
void ReadAsDouble (std::string value, double &v, bool &ok);
Rectangle m_rectangle;
};
template <typename T>
Ptr<ParamSpec> MakeRectangleParamSpec (Rectangle T::*memberVariable,
Rectangle initialValue);
} // namespace ns3
namespace ns3 {
template <typename T>
Ptr<ParamSpec>
MakeRectangleParamSpec (Rectangle T::*memberVariable,
Rectangle initialValue)
{
return MakeMemberVariableParamSpec (memberVariable, initialValue);
}
} // namespace ns3
#endif /* RECTANGLE_H */