convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
This commit is contained in:
1 parent
0537be3a47
commit
540bb4c68d
10 files changed
+238
-361
No files matched your search
+16
-87
@@ -121,102 +121,31 @@ Rectangle::CalculateIntersection (const Vector ¤t, 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 ();
|
||||
*this = ClassValueHelperExtractFrom<Rectangle,RectangleValue> (value);
|
||||
}
|
||||
Rectangle::operator PValue () const
|
||||
{
|
||||
return PValue::Create<RectangleValue> (*this);
|
||||
return ClassValueHelperConvertTo<Rectangle,RectangleValue> (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
RectangleValue::RectangleValue (const Rectangle &rectangle)
|
||||
: m_rectangle (rectangle)
|
||||
{}
|
||||
|
||||
void
|
||||
RectangleValue::Set (const Rectangle &v)
|
||||
std::ostream &
|
||||
operator << (std::ostream &os, const Rectangle &rectangle)
|
||||
{
|
||||
m_rectangle = v;
|
||||
os << rectangle.xMin << "|" << rectangle.xMax << "|" << rectangle.yMin << "|" << rectangle.yMax;
|
||||
return os;
|
||||
}
|
||||
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)
|
||||
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 != '|')
|
||||
{
|
||||
v = local;
|
||||
is.setstate (std::ios_base::failbit);
|
||||
}
|
||||
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;
|
||||
return is;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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 @@ public:
|
||||
operator PValue () const;
|
||||
};
|
||||
|
||||
class RectangleValue : public Value
|
||||
{
|
||||
public:
|
||||
RectangleValue (const Rectangle &rectangle);
|
||||
std::ostream &operator << (std::ostream &os, const Rectangle &rectangle);
|
||||
std::istream &operator >> (std::istream &is, 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;
|
||||
};
|
||||
class RectangleValue : public Value {};
|
||||
|
||||
template <typename T1>
|
||||
Ptr<ParamSpec> MakeRectangleParamSpec (T1 a1,
|
||||
@@ -102,13 +87,13 @@ template <typename T1>
|
||||
Ptr<ParamSpec> MakeRectangleParamSpec (T1 a1,
|
||||
Rectangle initialValue)
|
||||
{
|
||||
return MakeParamSpecHelper (a1, RectangleValue (initialValue));
|
||||
return MakeClassValueHelperParamSpec<Rectangle,RectangleValue> (a1, initialValue);
|
||||
}
|
||||
template <typename T1, typename T2>
|
||||
Ptr<ParamSpec> MakeRectangleParamSpec (T1 a1, T2 a2,
|
||||
Rectangle initialValue)
|
||||
{
|
||||
return MakeParamSpecHelper (a1, a2, RectangleValue (initialValue));
|
||||
return MakeClassValueHelperParamSpec<Rectangle,RectangleValue> (a1, a2, initialValue);
|
||||
}
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
+14
-47
@@ -39,16 +39,11 @@ Vector::Vector ()
|
||||
|
||||
Vector::Vector (PValue value)
|
||||
{
|
||||
const VectorValue *v = value.DynCast<const VectorValue *> ();
|
||||
if (v == 0)
|
||||
{
|
||||
NS_FATAL_ERROR ("Expected value of type Vector");
|
||||
}
|
||||
*this = v->Get ();
|
||||
*this = ClassValueHelperExtractFrom<Vector,VectorValue> (value);
|
||||
}
|
||||
Vector::operator PValue () const
|
||||
{
|
||||
return PValue::Create<VectorValue> (*this);
|
||||
return ClassValueHelperConvertTo<Vector,VectorValue> (this);
|
||||
}
|
||||
|
||||
|
||||
@@ -62,49 +57,21 @@ CalculateDistance (const Vector &a, const Vector &b)
|
||||
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)
|
||||
{
|
||||
m_vector = vector;
|
||||
os << vector.x << ":" << vector.y << ":" << vector.z;
|
||||
return os;
|
||||
}
|
||||
Vector
|
||||
VectorValue::Get (void) const
|
||||
std::istream &operator >> (std::istream &is, Vector &vector)
|
||||
{
|
||||
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<VectorValue> (*this);
|
||||
}
|
||||
std::string
|
||||
VectorValue::SerializeToString (Ptr<const ParamSpec> 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<const ParamSpec> spec)
|
||||
{
|
||||
// XXX implement parsing
|
||||
return false;
|
||||
}
|
||||
|
||||
VectorValue::VectorValue (PValue value)
|
||||
: m_vector (value)
|
||||
{}
|
||||
VectorValue::operator PValue () const
|
||||
{
|
||||
return m_vector;
|
||||
}
|
||||
|
||||
|
||||
} // namespace ns3
|
||||
+6
-19
@@ -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 @@ public:
|
||||
|
||||
double CalculateDistance (const Vector &a, const Vector &b);
|
||||
|
||||
class VectorValue : public Value
|
||||
{
|
||||
public:
|
||||
VectorValue (const Vector &vector);
|
||||
class VectorValue : public Value {};
|
||||
|
||||
void Set (const Vector &vector);
|
||||
Vector Get (void) const;
|
||||
|
||||
virtual PValue Copy (void) const;
|
||||
virtual std::string SerializeToString (Ptr<const ParamSpec> spec) const;
|
||||
virtual bool DeserializeFromString (std::string value, Ptr<const ParamSpec> 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 <typename T1>
|
||||
Ptr<ParamSpec>
|
||||
@@ -99,7 +86,7 @@ template <typename T1>
|
||||
Ptr<ParamSpec>
|
||||
MakeVectorParamSpec (T1 a1, const Vector &initialValue)
|
||||
{
|
||||
return MakeParamSpecHelper (a1, VectorValue (initialValue));
|
||||
return MakeClassValueHelperParamSpec<Vector,VectorValue> (a1, initialValue);
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
@@ -107,7 +94,7 @@ Ptr<ParamSpec>
|
||||
MakeVectorParamSpec (T1 a1, T2 a2,
|
||||
const Vector &initialValue)
|
||||
{
|
||||
return MakeParamSpecHelper (a1, a2, VectorValue (initialValue));
|
||||
return MakeClassValueHelperParamSpec<Vector,VectorValue> (a1, a2, initialValue);
|
||||
}
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
Reference in new issue
Block a user