From 6dad8a4d79da5e278265116a8535455133ada4e7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 30 Jan 2008 17:25:06 +0100 Subject: [PATCH 001/130] introduce ObjectBase --- src/common/packet.h | 3 +- src/core/callback.h | 4 +- src/core/object-base.cc | 3 + src/core/object-base.h | 20 ++++++ src/core/object.h | 2 +- src/core/ptr.cc | 130 +++++++++++++++++++++-------------- src/core/trace-resolver.h | 3 +- src/core/wscript | 2 + src/devices/wifi/wifi-phy.cc | 4 +- src/simulator/event-impl.h | 4 +- src/simulator/simulator.cc | 2 +- 11 files changed, 120 insertions(+), 57 deletions(-) create mode 100644 src/core/object-base.cc create mode 100644 src/core/object-base.h diff --git a/src/common/packet.h b/src/common/packet.h index 7f4ca70bd..afd16d5c5 100644 --- a/src/common/packet.h +++ b/src/common/packet.h @@ -31,6 +31,7 @@ #include "ns3/callback.h" #include "ns3/assert.h" #include "ns3/ptr.h" +#include "ns3/object-base.h" namespace ns3 { @@ -73,7 +74,7 @@ class PacketPrinter; * The performance aspects of the Packet API are discussed in * \ref packetperf */ -class Packet { +class Packet : public ObjectBase { public: void Ref (void) const; void Unref (void) const; diff --git a/src/core/callback.h b/src/core/callback.h index 1fa9d9c52..a8a1fd41c 100644 --- a/src/core/callback.h +++ b/src/core/callback.h @@ -26,6 +26,7 @@ #include "fatal-error.h" #include "empty.h" #include "type-traits.h" +#include "object-base.h" namespace ns3 { @@ -70,7 +71,8 @@ struct CallbackTraits } }; -class CallbackImplBase { +class CallbackImplBase : public ObjectBase +{ public: CallbackImplBase () : m_count (1) {} diff --git a/src/core/object-base.cc b/src/core/object-base.cc new file mode 100644 index 000000000..98a080220 --- /dev/null +++ b/src/core/object-base.cc @@ -0,0 +1,3 @@ +#include "object-base.h" + +ns3::ObjectBase::~ObjectBase () {} diff --git a/src/core/object-base.h b/src/core/object-base.h new file mode 100644 index 000000000..36f09ffe5 --- /dev/null +++ b/src/core/object-base.h @@ -0,0 +1,20 @@ +#ifndef OBJECT_BASE_H +#define OBJECT_BASE_H + +namespace ns3 { + +/** + * This base class is really used only to make sure that + * every subclass has RTTI information and that they all + * share a single base class to allow us to make type + * checks across all these types. + */ +class ObjectBase +{ +public: + virtual ~ObjectBase (); +}; + +} // namespace ns3 + +#endif /* OBJECT_BASE_H */ diff --git a/src/core/object.h b/src/core/object.h index 952a1f7ab..1bca906b9 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -132,7 +132,7 @@ private: * BonoboObject in Bonobo: it provides three main methods: Ref, Unref and * QueryInterface. */ -class Object +class Object : public ObjectBase { public: static TypeId GetTypeId (void); diff --git a/src/core/ptr.cc b/src/core/ptr.cc index feb09ad61..39b7f9554 100644 --- a/src/core/ptr.cc +++ b/src/core/ptr.cc @@ -23,49 +23,81 @@ #ifdef RUN_SELF_TESTS #include "test.h" -#include "callback.h" -#include "object.h" +#include "object-base.h" namespace ns3 { +class NoCount; + template void Foo (void) {} - -class NoCount : public Object -{ -public: - NoCount (void (*fn) (void)); - NoCount (Callback cb); - ~NoCount (); - void Nothing (void) const; -private: - Callback m_cb; -}; -NoCount::NoCount (Callback cb) - : m_cb (cb) -{} -NoCount::~NoCount () -{ - m_cb (); -} -void -NoCount::Nothing () const -{} - class PtrTest : Test { public: PtrTest (); virtual ~PtrTest (); virtual bool RunTests (void); -private: void DestroyNotify (void); +private: Ptr CallTest (Ptr p); Ptr const CallTestConst (Ptr const p); uint32_t m_nDestroyed; }; + +class Base : public ObjectBase +{ +public: + Base (); + virtual ~Base (); + void Ref (void) const; + void Unref (void) const; +private: + mutable uint32_t m_count; +}; + +class NoCount : public Base +{ +public: + NoCount (PtrTest *test); + ~NoCount (); + void Nothing (void) const; +private: + PtrTest *m_test; +}; + +Base::Base () + : m_count (1) +{} +Base::~Base () +{} +void +Base::Ref (void) const +{ + m_count++; +} +void +Base::Unref (void) const +{ + m_count--; + if (m_count == 0) + { + delete this; + } +} + +NoCount::NoCount (PtrTest *test) + : m_test (test) +{} +NoCount::~NoCount () +{ + m_test->DestroyNotify (); +} +void +NoCount::Nothing () const +{} + PtrTest::PtrTest () : Test ("Ptr") {} @@ -95,10 +127,9 @@ PtrTest::RunTests (void) { bool ok = true; - Callback cb = MakeCallback (&PtrTest::DestroyNotify, this); m_nDestroyed = false; { - Ptr p = CreateObject (cb); + Ptr p = Create (this); } if (m_nDestroyed != 1) { @@ -108,7 +139,7 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p; - p = CreateObject (cb); + p = Create (this); p = p; } if (m_nDestroyed != 1) @@ -119,7 +150,7 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p1; - p1 = CreateObject (cb); + p1 = Create (this); Ptr p2 = p1; } if (m_nDestroyed != 1) @@ -130,7 +161,7 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p1; - p1 = CreateObject (cb); + p1 = Create (this); Ptr p2; p2 = p1; } @@ -142,8 +173,8 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p1; - p1 = CreateObject (cb); - Ptr p2 = CreateObject (cb); + p1 = Create (this); + Ptr p2 = Create (this); p2 = p1; } if (m_nDestroyed != 2) @@ -154,9 +185,9 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p1; - p1 = CreateObject (cb); + p1 = Create (this); Ptr p2; - p2 = CreateObject (cb); + p2 = Create (this); p2 = p1; } if (m_nDestroyed != 2) @@ -167,8 +198,8 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p1; - p1 = CreateObject (cb); - p1 = CreateObject (cb); + p1 = Create (this); + p1 = Create (this); } if (m_nDestroyed != 2) { @@ -180,8 +211,8 @@ PtrTest::RunTests (void) Ptr p1; { Ptr p2; - p1 = CreateObject (cb); - p2 = CreateObject (cb); + p1 = Create (this); + p2 = Create (this); p2 = p1; } if (m_nDestroyed != 1) @@ -199,8 +230,8 @@ PtrTest::RunTests (void) Ptr p1; { Ptr p2; - p1 = CreateObject (cb); - p2 = CreateObject (cb); + p1 = Create (this); + p2 = Create (this); p2 = CallTest (p1); } if (m_nDestroyed != 1) @@ -242,7 +273,7 @@ PtrTest::RunTests (void) { NoCount *raw; { - Ptr p = CreateObject (cb); + Ptr p = Create (this); { Ptr p1 = p; } @@ -256,10 +287,9 @@ PtrTest::RunTests (void) delete raw; } - m_nDestroyed = 0; { - Ptr p = CreateObject (cb); + Ptr p = Create (this); const NoCount *v1 = PeekPointer (p); NoCount *v2 = PeekPointer (p); v1->Nothing (); @@ -271,8 +301,8 @@ PtrTest::RunTests (void) } { - Ptr p0 = CreateObject (cb); - Ptr p1 = CreateObject (cb); + Ptr p0 = Create (this); + Ptr p1 = Create (this); if (p0 == p1) { ok = false; @@ -285,23 +315,23 @@ PtrTest::RunTests (void) ok = false; } } - +#if 0 { - Ptr p = CreateObject (cb); + Ptr p = Create (cb); Callback callback = MakeCallback (&NoCount::Nothing, p); callback (); } { - Ptr p = CreateObject (cb); + Ptr p = Create (cb); Callback callback = MakeCallback (&NoCount::Nothing, p); callback (); } - +#endif #if 0 // as expected, fails compilation. { - Ptr p = CreateObject (cb); + Ptr p = Create (cb); Callback callback = MakeCallback (&NoCount::Nothing, p); } // local types are not allowed as arguments to a template. diff --git a/src/core/trace-resolver.h b/src/core/trace-resolver.h index 027beb5c1..8a0bb9e1f 100644 --- a/src/core/trace-resolver.h +++ b/src/core/trace-resolver.h @@ -25,6 +25,7 @@ #include #include "trace-context.h" #include "trace-doc.h" +#include "object-base.h" namespace ns3 { @@ -39,7 +40,7 @@ class CallbackBase; * subclasses, doing so is complicated so, it is recommended to use * the default implementation ns3::CompositeTraceResolver instead. */ -class TraceResolver +class TraceResolver : public ObjectBase { public: diff --git a/src/core/wscript b/src/core/wscript index 1c69ee1db..088bce41b 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -31,6 +31,7 @@ def build(bld): 'callback-test.cc', 'log.cc', 'breakpoint.cc', + 'object-base.cc', 'ptr.cc', 'object.cc', 'test.cc', @@ -68,6 +69,7 @@ def build(bld): 'system-wall-clock-ms.h', 'empty.h', 'callback.h', + 'object-base.h', 'ptr.h', 'object.h', 'log.h', diff --git a/src/devices/wifi/wifi-phy.cc b/src/devices/wifi/wifi-phy.cc index 285e13212..ca741d6af 100644 --- a/src/devices/wifi/wifi-phy.cc +++ b/src/devices/wifi/wifi-phy.cc @@ -30,6 +30,7 @@ #include "ns3/assert.h" #include "ns3/log.h" #include "ns3/composite-trace-resolver.h" +#include "ns3/object-base.h" #include NS_LOG_COMPONENT_DEFINE ("WifiPhy"); @@ -76,7 +77,8 @@ WifiPhyListener::~WifiPhyListener () * Phy event class ****************************************************************/ -class RxEvent { +class RxEvent : public ObjectBase +{ public: RxEvent (uint32_t size, WifiMode payloadMode, enum WifiPreamble preamble, diff --git a/src/simulator/event-impl.h b/src/simulator/event-impl.h index 9fb967577..dcb63e0ff 100644 --- a/src/simulator/event-impl.h +++ b/src/simulator/event-impl.h @@ -21,10 +21,12 @@ #define EVENT_IMPL_H #include +#include "ns3/object-base.h" namespace ns3 { -class EventImpl { +class EventImpl : public ObjectBase +{ public: EventImpl (); inline void Ref (void) const; diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index 35b774e5e..aec7007cc 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -568,7 +568,7 @@ static void cber5 (const int &, const int &, const int &, const int &, const int {} -class SimulatorTests : public Test { +class SimulatorTests : public Test, public ObjectBase { public: SimulatorTests (); // only here for testing of Ptr<> From 98fc9e405391291e5e18f31d79391781283fc083 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 30 Jan 2008 17:28:18 +0100 Subject: [PATCH 002/130] initial work on Value code --- src/core/boolean-value.cc | 60 +++++++++ src/core/boolean-value.h | 77 ++++++++++++ src/core/param-spec-helper.h | 227 +++++++++++++++++++++++++++++++++++ src/core/param-spec.cc | 25 ++++ src/core/param-spec.h | 37 ++++++ src/core/value.cc | 25 ++++ src/core/value.h | 30 +++++ src/core/wscript | 3 + 8 files changed, 484 insertions(+) create mode 100644 src/core/boolean-value.cc create mode 100644 src/core/boolean-value.h create mode 100644 src/core/param-spec-helper.h create mode 100644 src/core/param-spec.cc create mode 100644 src/core/param-spec.h create mode 100644 src/core/value.cc create mode 100644 src/core/value.h diff --git a/src/core/boolean-value.cc b/src/core/boolean-value.cc new file mode 100644 index 000000000..80c7d7bca --- /dev/null +++ b/src/core/boolean-value.cc @@ -0,0 +1,60 @@ +#include "boolean-value.h" + +namespace ns3 { + +BooleanValue::BooleanValue (bool value) + : m_value (value) +{} +void +BooleanValue::Set (bool value) +{ + m_value = value; +} +bool +BooleanValue::Get (void) const +{ + return m_value; +} +Ptr +BooleanValue::Copy (void) const +{ + return Create (*this); +} +std::string +BooleanValue::SerializeToString (Ptr spec) const +{ + std::string value; + if (m_value) + { + value = "true"; + } + else + { + value = "false"; + } + return value; +} +bool +BooleanValue::DeserializeFromString (std::string value, Ptr spec) +{ + if (value == "true" || + value == "1" || + value == "t") + { + m_value = true; + return true; + } + else if (value == "false" || + value == "0" || + value == "f") + { + m_value = false; + return true; + } + else + { + return false; + } +} + +} // namespace ns3 diff --git a/src/core/boolean-value.h b/src/core/boolean-value.h new file mode 100644 index 000000000..c7e004503 --- /dev/null +++ b/src/core/boolean-value.h @@ -0,0 +1,77 @@ +#ifndef BOOLEAN_VALUE_H +#define BOOLEAN_VALUE_H + +#include "value.h" +#include "param-spec.h" +#include "param-spec-helper.h" +#include "ptr.h" + +namespace ns3 { + +class BooleanValue : public Value +{ +public: + BooleanValue (bool value); + void Set (bool value); + bool Get (void) const; + + virtual Ptr Copy (void) const; + virtual std::string SerializeToString (Ptr spec) const; + virtual bool DeserializeFromString (std::string value, Ptr spec); +private: + bool m_value; +}; + + +template +Ptr MakeBooleanParamSpec (bool T::*v, bool initialValue); + +template +Ptr MakeBooleanParamSpec (bool (T::*getter) (void) const, + bool initialValue); + +template +Ptr MakeBooleanParamSpec (bool (T::*getter) (void) const, + void (T::*setter) (bool), + bool initialValue); +template +Ptr MakeBooleanParamSpec (void (T::*setter) (bool), + bool initialValue); + +} // namespace ns3 + + +// Implementation of template functions below. +namespace ns3 { + +template +Ptr MakeBooleanParamSpec (bool T::*v, bool initialValue) +{ + return Create > (v, initialValue); +} + +template +Ptr MakeBooleanParamSpec (void (T::*setter) (bool), + bool (T::*getter) (void) const, + bool initialValue) +{ + return Create > (setter, getter, initialValue); +} + +template +Ptr MakeBooleanParamSpec (bool (T::*getter) (void) const, + bool initialValue) +{ + return Create > (getter, initialValue); +} + +template +Ptr MakeBooleanParamSpec (void (T::*setter) (bool), + bool initialValue) +{ + return Create > (setter, initialValue); +} + +} // namespace ns3 + +#endif /* BOOLEAN_PARAMETER_H */ diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h new file mode 100644 index 000000000..429cdadba --- /dev/null +++ b/src/core/param-spec-helper.h @@ -0,0 +1,227 @@ +#ifndef PARAM_SPEC_HELPER_H +#define PARAM_SPEC_HELPER_H + +namespace ns3 { + +template +class ParamSpecHelper : public ParamSpec +{ +public: + ParamSpecHelper (V initialValue); + virtual bool Set (ObjectBase * object, Ptr parameter) const; + virtual bool Get (const ObjectBase * object, Ptr parameter) const; + virtual bool Check (Ptr parameter) const; + virtual Ptr CreateInitialValue (void) const; + +private: + virtual void DoSet (T *object, const V &v) const = 0; + virtual V DoGet (const T *object) const = 0; + virtual bool DoCheck (const V &v) const; + V m_initialValue; +}; + +template +class MemberVariableParamSpecHelper : public ParamSpecHelper +{ +public: + MemberVariableParamSpecHelper (V T::*memberVariable, V initialValue); +private: + virtual void DoSet (T *object, const V &v) const; + virtual V DoGet (const T *object) const; + + V T::*m_memberVariable; +}; + +template +class MemberMethodParamSpecHelper : public ParamSpecHelper +{ +public: + MemberMethodParamSpecHelper (void (T::*setter) (V), + V (T::*getter) (void) const, + V initialValue); +private: + virtual void DoSet (T *object, const V &v) const; + virtual V DoGet (const T *object) const; + + void (T::*m_setter) (V); + V (T::*m_getter) (void) const; +}; + +template +class MemberMethodSetterParamSpecHelper : public ParamSpecHelper +{ +public: + MemberMethodSetterParamSpecHelper (void (T::*setter) (V), + V initialValue); +private: + virtual void DoSet (T *object, const V &v) const; + virtual V DoGet (const T *object) const; + + void (T::*m_setter) (V); +}; + +template +class MemberMethodGetterParamSpecHelper : public ParamSpecHelper +{ +public: + MemberMethodGetterParamSpecHelper (V (T::*getter) (void) const); +private: + virtual void DoSet (T *object, const V &v) const; + virtual V DoGet (const T *object) const; + + V (T::*m_getter) (void) const; +}; + +} // namespace ns3 + + +/*************************************************************** + * The implementation of the above class templates. + ***************************************************************/ + +namespace ns3 { + +template +ParamSpecHelper::ParamSpecHelper (V initialValue) + : m_initialValue (initialValue) +{} +template +bool +ParamSpecHelper::Set (ObjectBase * object, Ptr parameter) const +{ + const U *value = dynamic_cast (PeekPointer (parameter)); + if (value == 0) + { + return false; + } + T *obj = dynamic_cast (object); + if (obj == 0) + { + return false; + } + V v = *value; + DoSet (obj, v); + return true; +} +template +bool +ParamSpecHelper::Get (const ObjectBase * object, Ptr parameter) const +{ + U *value = dynamic_cast (PeekPointer (parameter)); + if (value == 0) + { + return false; + } + const T *obj = dynamic_cast (object); + if (obj == 0) + { + return false; + } + V v = DoGet (obj); + *value = v; + return true; +} +template +bool +ParamSpecHelper::Check (Ptr parameter) const +{ + const U *value = dynamic_cast (PeekPointer (parameter)); + if (value == 0) + { + return false; + } + V v = *value; + DoCheck (v); + return true; +} +template +bool +ParamSpecHelper::DoCheck (const V &v) const +{ + return true; +} +template +Ptr +ParamSpecHelper::CreateInitialValue (void) const +{ + return Create (m_initialValue); +} + + +template +MemberVariableParamSpecHelper::MemberVariableParamSpecHelper (V T::*memberVariable, V initialValue) + : ParamSpecHelper (initialValue), + m_memberVariable (memberVariable) +{} +template +void +MemberVariableParamSpecHelper::DoSet (T *object, const V &v) const +{ + (object->*m_memberVariable) = v; +} +template +V +MemberVariableParamSpecHelper::DoGet (const T *object) const +{ + return (object->*m_memberVariable); +} + +template +MemberMethodParamSpecHelper::MemberMethodParamSpecHelper (void (T::*setter) (V), + V (T::*getter) (void) const, + V initialValue) + : ParamSpecHelper (initialValue), + m_setter (setter), + m_getter (getter) +{} +template +void +MemberMethodParamSpecHelper::DoSet (T *object, const V &v) const +{ + (object->m_setter) (v); +} +template +V +MemberMethodParamSpecHelper::DoGet (const T *object) const +{ + return (object->*m_getter) (); +} + +template +MemberMethodSetterParamSpecHelper::MemberMethodSetterParamSpecHelper (void (T::*setter) (V), + V initialValue) + : ParamSpecHelper (initialValue), + m_setter (setter) +{} +template +void +MemberMethodSetterParamSpecHelper::DoSet (T *object, const V &v) const +{ + (object->m_setter) (v); +} +template +V +MemberMethodSetterParamSpecHelper::DoGet (const T *object) const +{ + return V (); +} + +template +MemberMethodGetterParamSpecHelper::MemberMethodGetterParamSpecHelper (V (T::*getter) (void) const) + : m_getter (getter) +{} +template +void +MemberMethodGetterParamSpecHelper::DoSet (T *object, const V &v) const +{} +template +V +MemberMethodGetterParamSpecHelper::DoGet (const T *object) const +{ + return (object->*m_getter) (); +} + + +} // namespace ns3 + +#endif /* PARAM_SPEC_HELPER_H */ diff --git a/src/core/param-spec.cc b/src/core/param-spec.cc new file mode 100644 index 000000000..c99543c0c --- /dev/null +++ b/src/core/param-spec.cc @@ -0,0 +1,25 @@ +#include "param-spec.h" + +namespace ns3 { + +ParamSpec::ParamSpec () + : m_count (1) +{} +void +ParamSpec::Ref (void) const +{ + m_count++; +} +void +ParamSpec::Unref (void) const +{ + m_count--; + if (m_count == 0) + { + delete this; + } +} +ParamSpec::~ParamSpec () +{} + +} // namespace ns3 diff --git a/src/core/param-spec.h b/src/core/param-spec.h new file mode 100644 index 000000000..32cb42bd5 --- /dev/null +++ b/src/core/param-spec.h @@ -0,0 +1,37 @@ +#ifndef PARAM_SPEC_H +#define PARAM_SPEC_H + +#include +#include +#include "object-base.h" +#include "ptr.h" + +namespace ns3 { + +class Value; + +class ParamSpec : public ObjectBase +{ +public: + ParamSpec (); + void Ref (void) const; + void Unref (void) const; + virtual ~ParamSpec (); + + /** + * \param object the object instance to set the value in + * \param value the value to set + * \returns true if the value is valid and it could be set + * successfully, false otherwise. + */ + virtual bool Set (ObjectBase * object, Ptr parameter) const = 0; + virtual bool Get (const ObjectBase * object, Ptr parameter) const = 0; + virtual bool Check (Ptr parameter) const = 0; + virtual Ptr CreateInitialValue (void) const = 0; +private: + mutable uint32_t m_count; +}; + +} // namespace ns3 + +#endif /* PARAM_SPEC_H */ diff --git a/src/core/value.cc b/src/core/value.cc new file mode 100644 index 000000000..83931ad36 --- /dev/null +++ b/src/core/value.cc @@ -0,0 +1,25 @@ +#include "value.h" + +namespace ns3 { + +Value::Value () + : m_count (1) +{} +void +Value::Ref (void) const +{ + m_count++; +} +void +Value::Unref (void) const +{ + m_count--; + if (m_count == 0) + { + delete this; + } +} +Value::~Value () +{} + +} // namespace ns3 diff --git a/src/core/value.h b/src/core/value.h new file mode 100644 index 000000000..c7ddc2859 --- /dev/null +++ b/src/core/value.h @@ -0,0 +1,30 @@ +#ifndef VALUE_H +#define VALUE_H + +#include +#include +#include "object-base.h" +#include "ptr.h" + +namespace ns3 { + +class ParamSpec; + +class Value : public ObjectBase +{ +public: + Value (); + void Ref (void) const; + void Unref (void) const; + virtual ~Value (); + + virtual Ptr Copy (void) const = 0; + virtual std::string SerializeToString (Ptr spec) const = 0; + virtual bool DeserializeFromString (std::string value, Ptr spec) = 0; +private: + mutable uint32_t m_count; +}; + +} // namespace ns3 + +#endif /* VALUE_H */ diff --git a/src/core/wscript b/src/core/wscript index 088bce41b..a6f131e00 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -53,6 +53,9 @@ def build(bld): 'type-traits-test.cc', 'array-trace-resolver.cc', 'type-id-default-value.cc', + 'value.cc', + 'param-spec.cc', + 'boolean-value.cc', ] if sys.platform == 'win32': From 1166a9780e8aef5fcf27731f99ecdc67e6738c41 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 30 Jan 2008 19:13:06 +0100 Subject: [PATCH 003/130] attempt to perform correctly automatic conversions. --- src/core/boolean-value.cc | 18 + src/core/boolean-value.h | 4 + src/core/object.cc | 649 ++++++++++++++++++++++++++++++++++- src/core/object.h | 488 +++++++++++++++++++------- src/core/param-spec-helper.h | 8 +- src/core/wscript | 3 + 6 files changed, 1017 insertions(+), 153 deletions(-) diff --git a/src/core/boolean-value.cc b/src/core/boolean-value.cc index 80c7d7bca..05fe3a85f 100644 --- a/src/core/boolean-value.cc +++ b/src/core/boolean-value.cc @@ -1,4 +1,5 @@ #include "boolean-value.h" +#include "fatal-error.h" namespace ns3 { @@ -56,5 +57,22 @@ BooleanValue::DeserializeFromString (std::string value, Ptr spe return false; } } +BooleanValue::BooleanValue (Ptr value) +{ + const BooleanValue *v = dynamic_cast (PeekPointer (value)); + if (v == 0) + { + NS_FATAL_ERROR ("assigning non-Boolean value to Boolean value."); + } + m_value = v->m_value; +} +BooleanValue::operator Ptr () const +{ + return Create (*this); +} +BooleanValue::operator Ptr () const +{ + return Create (*this); +} } // namespace ns3 diff --git a/src/core/boolean-value.h b/src/core/boolean-value.h index c7e004503..0f9ee9d23 100644 --- a/src/core/boolean-value.h +++ b/src/core/boolean-value.h @@ -18,6 +18,10 @@ public: virtual Ptr Copy (void) const; virtual std::string SerializeToString (Ptr spec) const; virtual bool DeserializeFromString (std::string value, Ptr spec); + + BooleanValue (Ptr value); + operator Ptr () const; + operator Ptr () const; private: bool m_value; }; diff --git a/src/core/object.cc b/src/core/object.cc index ee2581ce9..658f9b160 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -22,8 +22,11 @@ #include "assert.h" #include "singleton.h" #include "trace-resolver.h" +#include "value.h" +#include "param-spec.h" #include "log.h" #include +#include NS_LOG_COMPONENT_DEFINE ("Object"); @@ -36,25 +39,51 @@ namespace { class IidManager { public: + IidManager (); uint16_t AllocateUid (std::string name); void SetParent (uint16_t uid, uint16_t parent); + void SetTypeName (uint16_t uid, std::string typeName); + void SetGroupName (uint16_t uid, std::string groupName); void AddConstructor (uint16_t uid, ns3::CallbackBase callback, uint32_t nArguments); uint16_t GetUid (std::string name) const; std::string GetName (uint16_t uid) const; uint16_t GetParent (uint16_t uid) const; + std::string GetTypeName (uint16_t uid) const; + std::string GetGroupName (uint16_t uid) const; ns3::CallbackBase GetConstructor (uint16_t uid, uint32_t nArguments); bool HasConstructor (uint16_t uid); uint32_t GetRegisteredN (void); uint16_t GetRegistered (uint32_t i); + void AddParameter (uint16_t uid, + std::string name, + std::string help, + uint32_t flags, + ns3::Ptr spec); + uint32_t GetParametersN (uint16_t uid) const; + std::string GetParameterName (uint16_t uid, uint32_t i) const; + uint32_t GetParameterFlags (uint16_t uid, uint32_t i) const; + const ns3::Value *GetParameterInitialValue (uint16_t uid, uint32_t i) const; + uint32_t GetParameterUid (uint16_t uid, uint32_t i) const; + ns3::Ptr GetParameterParamSpec (uint16_t uid, uint32_t i) const; private: struct ConstructorInformation { ns3::CallbackBase cb; uint32_t nArguments; }; + struct ParameterInformation { + std::string name; + std::string help; + uint32_t flags; + uint32_t uid; + ns3::Ptr param; + }; struct IidInformation { std::string name; uint16_t parent; + std::string typeName; + std::string groupName; std::vector constructors; + std::vector parameters; }; typedef std::vector::const_iterator Iterator; @@ -63,6 +92,9 @@ private: std::vector m_information; }; +IidManager::IidManager () +{} + uint16_t IidManager::AllocateUid (std::string name) { @@ -79,6 +111,8 @@ IidManager::AllocateUid (std::string name) struct IidInformation information; information.name = name; information.parent = 0; + information.typeName = ""; + information.groupName = ""; m_information.push_back (information); uint32_t uid = m_information.size (); NS_ASSERT (uid <= 0xffff); @@ -99,6 +133,19 @@ IidManager::SetParent (uint16_t uid, uint16_t parent) struct IidInformation *information = LookupInformation (uid); information->parent = parent; } +void +IidManager::SetTypeName (uint16_t uid, std::string typeName) +{ + struct IidInformation *information = LookupInformation (uid); + information->typeName = typeName; +} +void +IidManager::SetGroupName (uint16_t uid, std::string groupName) +{ + struct IidInformation *information = LookupInformation (uid); + information->groupName = groupName; +} + void IidManager::AddConstructor (uint16_t uid, ns3::CallbackBase callback, uint32_t nArguments) { @@ -145,6 +192,19 @@ IidManager::GetParent (uint16_t uid) const struct IidInformation *information = LookupInformation (uid); return information->parent; } +std::string +IidManager::GetTypeName (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + return information->typeName; +} +std::string +IidManager::GetGroupName (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + return information->groupName; +} + ns3::CallbackBase IidManager::GetConstructor (uint16_t uid, uint32_t nArguments) { @@ -179,6 +239,60 @@ IidManager::GetRegistered (uint32_t i) return i + 1; } +void +IidManager::AddParameter (uint16_t uid, + std::string name, + std::string help, + uint32_t flags, + ns3::Ptr spec) +{ + struct IidInformation *information = LookupInformation (uid); + for (std::vector::const_iterator j = information->parameters.begin (); + j != information->parameters.end (); j++) + { + if (j->name == name) + { + NS_FATAL_ERROR ("Registered the same parameter twice name=\""<name<<"\""); + return; + } + } + struct ParameterInformation param; + param.name = name; + param.help = help; + param.flags = flags; + param.param = spec; + information->parameters.push_back (param); +} + + +uint32_t +IidManager::GetParametersN (uint16_t uid) const +{ + struct IidInformation *information = LookupInformation (uid); + return information->parameters.size (); +} +std::string +IidManager::GetParameterName (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->parameters.size ()); + return information->parameters[i].name; +} +uint32_t +IidManager::GetParameterFlags (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->parameters.size ()); + return information->parameters[i].flags; +} +ns3::Ptr +IidManager::GetParameterParamSpec (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->parameters.size ()); + return information->parameters[i].param; +} + } // anonymous namespace @@ -273,6 +387,19 @@ TypeId::LookupByName (std::string name) NS_ASSERT (uid != 0); return TypeId (uid); } +Ptr +TypeId::LookupParamSpecByFullName (std::string fullName) +{ + std::string::size_type pos = fullName.find ("::"); + if (pos == std::string::npos) + { + return 0; + } + std::string tidName = fullName.substr (0, pos); + std::string paramName = fullName.substr (pos+2, fullName.size () - (pos+2)); + TypeId tid = LookupByName (tidName); + return tid.LookupParamSpecByName (paramName); +} uint32_t TypeId::GetRegisteredN (void) { @@ -284,6 +411,41 @@ TypeId::GetRegistered (uint32_t i) return TypeId (Singleton::Get ()->GetRegistered (i)); } +Ptr +TypeId::LookupParamSpecByName (std::string name) const +{ + for (uint32_t i = 0; i < GetParametersN (); i++) + { + std::string paramName = GetParameterName (i); + if (paramName == name) + { + return GetParameterParamSpec (i); + } + } + return 0; +} +Ptr +TypeId::LookupParamSpecByPosition (uint32_t i) const +{ + uint32_t cur = 0; + TypeId tid = TypeId (0); + TypeId nextTid = *this; + do { + tid = nextTid; + for (uint32_t j = 0; j < tid.GetParametersN (); j++) + { + if (cur == i) + { + return tid.GetParameterParamSpec (j); + } + cur++; + } + nextTid = tid.GetParent (); + } while (nextTid != tid); + return 0; +} + + TypeId TypeId::SetParent (TypeId tid) { @@ -291,11 +453,36 @@ TypeId::SetParent (TypeId tid) return *this; } TypeId +TypeId::SetGroupName (std::string groupName) +{ + Singleton::Get ()->SetGroupName (m_tid, groupName); + return *this; +} +TypeId +TypeId::SetTypeName (std::string typeName) +{ + Singleton::Get ()->SetTypeName (m_tid, typeName); + return *this; +} +TypeId TypeId::GetParent (void) const { uint16_t parent = Singleton::Get ()->GetParent (m_tid); return TypeId (parent); } +std::string +TypeId::GetGroupName (void) const +{ + std::string groupName = Singleton::Get ()->GetGroupName (m_tid); + return groupName; +} +std::string +TypeId::GetTypeName (void) const +{ + std::string typeName = Singleton::Get ()->GetTypeName (m_tid); + return typeName; +} + std::string TypeId::GetName (void) const { @@ -316,23 +503,74 @@ TypeId::DoAddConstructor (CallbackBase cb, uint32_t nArguments) Singleton::Get ()->AddConstructor (m_tid, cb, nArguments); } +TypeId +TypeId::AddParameter (std::string name, + std::string help, + Ptr param) +{ + Singleton::Get ()->AddParameter (m_tid, name, help, 0, param); + return *this; +} + +TypeId +TypeId::AddParameter (std::string name, + std::string help, + uint32_t flags, + Ptr param) +{ + Singleton::Get ()->AddParameter (m_tid, name, help, flags, param); + return *this; +} + + CallbackBase -TypeId::LookupConstructor (uint32_t nArguments) +TypeId::LookupConstructor (uint32_t nArguments) const { CallbackBase constructor = Singleton::Get ()->GetConstructor (m_tid, nArguments); return constructor; } Ptr -TypeId::CreateObject (void) +TypeId::CreateObject (void) const +{ + return CreateObject (Parameters ()); +} +Ptr +TypeId::CreateObject (const Parameters ¶meters) const { CallbackBase cb = LookupConstructor (0); - Callback > realCb; + Callback,const Parameters &> realCb; realCb.Assign (cb); - Ptr object = realCb (); - return object; + Ptr object = realCb (parameters); + return object; } +uint32_t +TypeId::GetParametersN (void) const +{ + uint32_t n = Singleton::Get ()->GetParametersN (m_tid); + return n; +} +std::string +TypeId::GetParameterName (uint32_t i) const +{ + std::string name = Singleton::Get ()->GetParameterName (m_tid, i); + return name; +} +std::string +TypeId::GetParameterFullName (uint32_t i) const +{ + return GetName () + "::" + GetParameterName (i); +} +Ptr +TypeId::GetParameterParamSpec (uint32_t i) const +{ + // Used exclusively by the Object class. + Ptr param = Singleton::Get ()->GetParameterParamSpec (m_tid, i); + return param; +} + + bool operator == (TypeId a, TypeId b) { return a.m_tid == b.m_tid; @@ -343,6 +581,236 @@ bool operator != (TypeId a, TypeId b) return a.m_tid != b.m_tid; } +/********************************************************************* + * The Parameters container implementation + *********************************************************************/ + +Parameters::Parameters () +{} + +Parameters::Parameters (const Parameters &o) +{ + for (Params::const_iterator i = o.m_parameters.begin (); i != o.m_parameters.end (); i++) + { + struct Param param; + param.spec = i->spec; + param.value = i->value->Copy (); + m_parameters.push_back (param); + } +} +Parameters & +Parameters::operator = (const Parameters &o) +{ + Reset (); + for (Params::const_iterator i = o.m_parameters.begin (); i != o.m_parameters.end (); i++) + { + struct Param param; + param.spec = i->spec; + param.value = i->value->Copy (); + m_parameters.push_back (param); + } + return *this; +} +Parameters::~Parameters () +{ + Reset (); +} + +bool +Parameters::Set (std::string name, Ptr value) +{ + Ptr spec = TypeId::LookupParamSpecByFullName (name); + bool ok = DoSet (spec, value); + return ok; +} +bool +Parameters::Set (std::string name, std::string value) +{ + Ptr spec = TypeId::LookupParamSpecByFullName (name); + bool ok = DoSet (spec,value); + return ok; +} +void +Parameters::SetWithTid (TypeId tid, std::string name, std::string value) +{ + Ptr spec = tid.LookupParamSpecByName (name); + DoSet (spec, value); +} +void +Parameters::SetWithTid (TypeId tid, std::string name, Ptr value) +{ + Ptr spec = tid.LookupParamSpecByName (name); + DoSet (spec, value); +} +void +Parameters::SetWithTid (TypeId tid, uint32_t position, std::string value) +{ + Ptr spec = tid.LookupParamSpecByPosition (position); + DoSet (spec, value); +} +void +Parameters::SetWithTid (TypeId tid, uint32_t position, Ptr value) +{ + Ptr spec = tid.LookupParamSpecByPosition (position); + DoSet (spec, value); +} + +void +Parameters::DoSetOne (Ptr spec, Ptr value) +{ + // get rid of any previous value stored in this + // vector of values. + for (Params::iterator k = m_parameters.begin (); k != m_parameters.end (); k++) + { + if (k->spec == spec) + { + m_parameters.erase (k); + break; + } + } + // store the new value. + struct Param p; + p.spec = spec; + p.value = value->Copy (); + m_parameters.push_back (p); +} +bool +Parameters::DoSet (Ptr spec, Ptr value) +{ + if (spec == 0) + { + return false; + } + bool ok = spec->Check (value); + if (!ok) + { + return false; + } + DoSetOne (spec, value); + return true; +} +bool +Parameters::DoSet (Ptr spec, std::string value) +{ + if (spec == 0) + { + return false; + } + Ptr v = spec->CreateInitialValue (); + bool ok = v->DeserializeFromString (value, spec); + if (!ok) + { + return false; + } + ok = spec->Check (v); + if (!ok) + { + return false; + } + DoSetOne (spec, v); + return true; +} +void +Parameters::Reset (void) +{ + m_parameters.clear (); +} +Parameters * +Parameters::GetGlobal (void) +{ + return Singleton::Get (); +} + +std::string +Parameters::LookupParameterFullNameByParamSpec (Ptr spec) const +{ + for (uint32_t i = 0; i < TypeId::GetRegisteredN (); i++) + { + TypeId tid = TypeId::GetRegistered (i); + for (uint32_t j = 0; j < tid.GetParametersN (); j++) + { + if (spec == tid.GetParameterParamSpec (j)) + { + return tid.GetParameterFullName (j); + } + } + } + NS_FATAL_ERROR ("Could not find requested ParamSpec."); + // quiet compiler. + return ""; +} + +std::string +Parameters::SerializeToString (void) const +{ + std::ostringstream oss; + for (Params::const_iterator i = m_parameters.begin (); i != m_parameters.end (); i++) + { + std::string name = LookupParameterFullNameByParamSpec (i->spec); + oss << name << "=" << i->value->SerializeToString (PeekPointer (i->spec)); + if (i != m_parameters.end ()) + { + oss << "|"; + } + } + return oss.str (); +} +bool +Parameters::DeserializeFromString (std::string str) +{ + Reset (); + + std::string::size_type cur; + cur = 0; + do { + std::string::size_type equal = str.find ("=", cur); + if (equal == std::string::npos) + { + // XXX: invalid parameter. + break; + } + else + { + std::string name = str.substr (cur, equal-cur); + Ptr spec = TypeId::LookupParamSpecByFullName (name); + if (spec == 0) + { + // XXX invalid name. + break; + } + else + { + std::string::size_type next = str.find ("|", cur); + std::string value; + if (next == std::string::npos) + { + value = str.substr (equal+1, str.size () - (equal+1)); + cur = str.size (); + } + else + { + value = str.substr (equal+1, next - (equal+1)); + cur++; + } + Ptr val = spec->CreateInitialValue (); + bool ok = val->DeserializeFromString (value, spec); + if (!ok) + { + // XXX invalid value + break; + } + else + { + DoSetOne (spec, val); + } + } + } + } while (cur != str.size ()); + + return true; +} + + /********************************************************************* * The Object implementation *********************************************************************/ @@ -376,6 +844,152 @@ Object::~Object () { m_next = 0; } +void +Object::Construct (const Parameters ¶meters) +{ + // loop over the inheritance tree back to the Object base class. + TypeId tid = m_tid; + do { + // loop over all parameters in object type + NS_LOG_DEBUG ("construct tid="<spec == paramSpec) + { + // We have a matching parameter value. + paramSpec->Set (this, j->value); + NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< + tid.GetParameterName (i)<<"\""); + found = true; + break; + } + } + if (!found) + { + // is this parameter stored in the global instance instance ? + for (Parameters::Params::const_iterator j = Parameters::GetGlobal ()->m_parameters.begin (); + j != Parameters::GetGlobal ()->m_parameters.end (); j++) + { + if (j->spec == paramSpec) + { + // We have a matching parameter value. + paramSpec->Set (this, j->value); + NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< + tid.GetParameterName (i)<<"\" from global"); + found = true; + break; + } + } + } + if (!found) + { + // No matching parameter value so we set the default value. + Ptr initial = paramSpec->CreateInitialValue (); + paramSpec->Set (this, initial); + NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< + tid.GetParameterName (i)<<"\" from local"); + } + } + tid = tid.GetParent (); + } while (tid != Object::GetTypeId ()); + NotifyConstructionCompleted (); +} +bool +Object::DoSet (std::string name, Ptr value) +{ + Ptr spec = m_tid.LookupParamSpecByName (name); + if (spec == 0) + { + return false; + } + bool ok = spec->Set (this, value); + if (!ok) + { + return false; + } + return true; +} +bool +Object::Set (std::string name, Ptr value) +{ + return DoSet (name, value); +} +bool +Object::Set (std::string name, std::string value) +{ + Ptr spec = m_tid.LookupParamSpecByName (name); + if (spec == 0) + { + return false; + } + Ptr parameter = spec->CreateInitialValue (); + bool ok = parameter->DeserializeFromString (value, spec); + if (!ok) + { + return false; + } + spec->Set (this, parameter); + if (!ok) + { + return false; + } + return true; +} +bool +Object::Get (std::string name, std::string &value) const +{ + Ptr paramSpec = m_tid.LookupParamSpecByName (name); + if (paramSpec == 0) + { + return false; + } + Ptr parameter = paramSpec->CreateInitialValue (); + bool ok = paramSpec->Get (this, parameter); + if (ok) + { + value = parameter->SerializeToString (paramSpec); + } + return ok; +} + +Ptr +Object::Get (std::string name) const +{ + Ptr paramSpec = m_tid.LookupParamSpecByName (name); + if (paramSpec == 0) + { + return 0; + } + Ptr value = paramSpec->CreateInitialValue (); + bool ok = paramSpec->Get (this, value); + if (!ok) + { + return 0; + } + return value; +} + +bool +Object::DoGet (std::string name, Ptr parameter) const +{ + Ptr paramSpec = m_tid.LookupParamSpecByName (name); + if (paramSpec == 0) + { + return false; + } + bool ok = paramSpec->Get (this, parameter); + return ok; +} + + Ptr Object::DoQueryInterface (TypeId tid) const { @@ -408,7 +1022,9 @@ Object::Dispose (void) current = current->m_next; } while (current != this); } - +void +Object::NotifyConstructionCompleted (void) +{} void Object::AddInterface (Ptr o) { @@ -619,10 +1235,10 @@ public: static ns3::TypeId GetTypeId (void) { static ns3::TypeId tid = ns3::TypeId ("DerivedA") .SetParent (BaseA::GetTypeId ()) - .AddConstructor (); + .AddConstructor (); return tid; } - DerivedA (int v) + DerivedA () {} void DerivedGenerateTrace (int16_t v) { m_sourceDerived = v; } @@ -671,13 +1287,10 @@ public: static ns3::TypeId GetTypeId (void) { static ns3::TypeId tid = ns3::TypeId ("DerivedB") .SetParent (BaseB::GetTypeId ()) - .AddConstructor () - .AddConstructor (); + .AddConstructor (); return tid; } - DerivedB (int v) - {} - DerivedB (int v1, int &v2) + DerivedB () {} void DerivedGenerateTrace (int16_t v) { m_sourceDerived = v; } @@ -754,7 +1367,7 @@ ObjectTest::RunTests (void) NS_TEST_ASSERT_EQUAL (baseA->QueryInterface (), baseA); NS_TEST_ASSERT_EQUAL (baseA->QueryInterface (DerivedA::GetTypeId ()), 0); NS_TEST_ASSERT_EQUAL (baseA->QueryInterface (), 0); - baseA = CreateObject (10); + baseA = CreateObject (); NS_TEST_ASSERT_EQUAL (baseA->QueryInterface (), baseA); NS_TEST_ASSERT_EQUAL (baseA->QueryInterface (DerivedA::GetTypeId ()), baseA); NS_TEST_ASSERT_UNEQUAL (baseA->QueryInterface (), 0); @@ -773,8 +1386,8 @@ ObjectTest::RunTests (void) NS_TEST_ASSERT_EQUAL (baseB->QueryInterface (), 0); NS_TEST_ASSERT_UNEQUAL (baseBCopy->QueryInterface (), 0); - baseA = CreateObject (1); - baseB = CreateObject (1); + baseA = CreateObject (); + baseB = CreateObject (); baseBCopy = baseB; baseA->AddInterface (baseB); NS_TEST_ASSERT_UNEQUAL (baseA->QueryInterface (), 0); @@ -836,7 +1449,7 @@ ObjectTest::RunTests (void) baseA->TraceDisconnect ("/$BaseA/basea-x", MakeCallback (&ObjectTest::BaseATrace, this)); Ptr derivedA; - derivedA = CreateObject (1); + derivedA = CreateObject (); baseB = CreateObject (); derivedA->AddInterface (baseB); baseB->TraceConnect ("/$DerivedA/deriveda-x", MakeCallback (&ObjectTest::DerivedATrace, this)); @@ -866,7 +1479,7 @@ ObjectTest::RunTests (void) NS_TEST_ASSERT_EQUAL (a->QueryInterface (), a); NS_TEST_ASSERT_EQUAL (a->QueryInterface (DerivedA::GetTypeId ()), 0); NS_TEST_ASSERT_EQUAL (a->QueryInterface (), 0); - a = DerivedA::GetTypeId ().CreateObject (10); + a = DerivedA::GetTypeId ().CreateObject (); NS_TEST_ASSERT_EQUAL (a->QueryInterface (), a); NS_TEST_ASSERT_EQUAL (a->QueryInterface (DerivedA::GetTypeId ()), a); NS_TEST_ASSERT_UNEQUAL (a->QueryInterface (), 0); diff --git a/src/core/object.h b/src/core/object.h index 1bca906b9..03436ce6a 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -26,13 +26,14 @@ #include "ptr.h" #include "trace-resolver.h" #include "callback.h" -#include "empty.h" +#include "value.h" +#include "object-base.h" #define NS_OBJECT_ENSURE_REGISTERED(type) \ static struct X##type##RegistrationClass \ { \ X##type##RegistrationClass () { \ - ns3::TypeId tid = type::GetTypeId (); \ + ns3::TypeId tid = type::GetTypeId (); \ tid.GetParent (); \ } \ } x_##type##RegistrationVariable @@ -43,14 +44,32 @@ namespace ns3 { class TraceContext; class CallbackBase; class Object; +class ParamSpec; +class Value; +class Parameters; /** * \brief a unique identifier for an interface. * + * This class records a lot of meta-information about a + * subclass of the Object base class: + * - the base class of the subclass + * - the set of accessible constructors in the subclass + * - the set of 'parameters' accessible in the subclass */ class TypeId { public: + enum { + PARAM_READ = 1<<0, + PARAM_WRITE = 1<<1, + PARAM_CONSTRUCT = 1<<2, + PARAM_RWC = PARAM_READ | PARAM_WRITE | PARAM_CONSTRUCT, + PARAM_NO_READ = 0, + PARAM_NO_WRITE = 0, + PARAM_NO_CONSTRUCT = 0, + }; + /** * \param name the name of the requested interface * \returns the unique id associated with the requested @@ -60,17 +79,41 @@ public: * name is not a valid interface name. */ static TypeId LookupByName (std::string name); - static uint32_t GetRegisteredN (void); - static TypeId GetRegistered (uint32_t i); + /** - * \param tid a unique id - * \returns the parent of the requested id + * \returns the number of TypeId instances constructed + */ + static uint32_t GetRegisteredN (void); + /** + * \param i index + * \returns the TypeId instance whose index is \i. + */ + static TypeId GetRegistered (uint32_t i); + + /** + * \param name the name of the interface to construct. + * + * No two instances can share the same name. + */ + TypeId (std::string name); + + /** + * \returns the parent of this TypeId * * This method cannot fail: it will crash if the input * id is not a valid interface id. */ TypeId GetParent (void) const; + /** + * \returns the name of the group associated to this TypeId. + */ + std::string GetGroupName (void) const; + /** + * \returns the fully-qualified C++ typename of this TypeId. + */ + std::string GetTypeName (void) const; + /** * \returns the name of this interface. */ @@ -81,56 +124,219 @@ public: */ bool HasConstructor (void) const; - TypeId (std::string); + /** + * \returns the number of parameters associated to this TypeId + */ + uint32_t GetParametersN (void) const; + /** + * \param i index into parameter array + * \returns the name associated to the parameter whose + * index is \i i. + */ + std::string GetParameterName (uint32_t i) const; + /** + * \param i index into parameter array + * \returns the full name associated to the parameter whose + * index is \i i. + */ + std::string GetParameterFullName (uint32_t i) const; + /** + * \param i index into parameter array + * \param value the value to validate + * \returns true if the input value is a valid value + * associated to the parameter whose index is \i i. + */ + bool CheckParameterValue (uint32_t i, std::string value) const; + Ptr CreateObject (const Parameters ¶meters) const; + + + Ptr CreateObject (void) const; + template + Ptr CreateObject (T1 a1) const; + template + Ptr CreateObject (T1 a1, T2 a2) const; + + + /** + * \param tid the TypeId of the base class. + * \return this TypeId instance. + * + * Record in this TypeId which TypeId is the TypeId + * of the base class of the subclass. + */ TypeId SetParent (TypeId tid); + /** + * \return this TypeId instance. + * + * Record in this TypeId which TypeId is the TypeId + * of the base class of the subclass. + */ template TypeId SetParent (void); + /** + * \param groupName the name of the group this TypeId belongs to. + * \returns this TypeId instance. + * + * The group name is purely an advisory information used to + * group together types according to a user-specific grouping + * scheme. + */ + TypeId SetGroupName (std::string groupName); + + /** + * \param typeName the fully-qualified C++ typename of this TypeId. + * \returns this TypeId instance. + */ + TypeId SetTypeName (std::string typeName); + + /** + * \returns this TypeId instance + * + * Record in this TypeId the fact that the default constructor + * is accessible. + */ template TypeId AddConstructor (void); + template TypeId AddConstructor (void); + template TypeId AddConstructor (void); - template - TypeId AddConstructor (void); - template - TypeId AddConstructor (void); - template - TypeId AddConstructor (void); + /** + * \param name the name of the new parameter + * \param help some help text which describes the purpose of this + * parameter + * \param param an instance of the associated ParamSpec subclass + * \returns this TypeId instance + * + * Record in this TypeId the fact that a new parameter exists. + */ + TypeId AddParameter (std::string name, + std::string help, Ptr spec); - Ptr CreateObject (void); - template - Ptr CreateObject (T1 a1); - template - Ptr CreateObject (T1 a1, T2 a2); - template - Ptr CreateObject (T1 a1, T2 a2, T3 a3); - template - Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4); - template - Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); + /** + * \param name the name of the new parameter + * \param help some help text which describes the purpose of this + * parameter + * \param flags flags which describe how this parameter can be read and/or written. + * \param param an instance of the associated ParamSpec subclass + * \returns this TypeId instance + * + * Record in this TypeId the fact that a new parameter exists. + */ + TypeId AddParameter (std::string name, + std::string help, + uint32_t flags, + Ptr spec); ~TypeId (); private: + friend class Object; + friend class Parameters; friend bool operator == (TypeId a, TypeId b); friend bool operator != (TypeId a, TypeId b); + /** + * \param name the name of the requested parameter + * \returns the ParamSpec associated to the requested parameter + */ + Ptr LookupParamSpecByName (std::string name) const; + /** + * \param i the position of the requested parameter + * \returns the ParamSpec associated to the requested parameter + */ + Ptr LookupParamSpecByPosition (uint32_t i) const; + /** + * \param fullName the full name of the requested parameter + * \returns the ParamSpec associated to the requested parameter + */ + static Ptr LookupParamSpecByFullName (std::string fullName); + explicit TypeId (uint16_t tid); void DoAddConstructor (CallbackBase callback, uint32_t nArguments); - CallbackBase LookupConstructor (uint32_t nArguments); + CallbackBase LookupConstructor (uint32_t nArguments) const; + Ptr GetParameterParamSpec (uint32_t i) const; uint16_t m_tid; }; + +/** + * \brief a container of parameters to be used during object's construction + * and in ns3::Object::Set. + * + */ +class Parameters +{ +public: + Parameters (); + Parameters (const Parameters &o); + Parameters &operator = (const Parameters &o); + ~Parameters (); + /** + * \param name the name of the parameter to set + * \param value the value to set + * + * This method checks that a parameter with the requested + * name exists and that the value specified is an acceptable + * value of that parameter. If any of these checks fails, + * the program terminates with a message. + */ + bool Set (std::string name, std::string value); + bool Set (std::string name, Ptr value); + + void SetWithTid (TypeId tid, std::string name, std::string value); + void SetWithTid (TypeId tid, std::string name, Ptr value); + void SetWithTid (TypeId tid, uint32_t position, std::string value); + void SetWithTid (TypeId tid, uint32_t position, Ptr value); + + /** + * Clear the content of this instance. + */ + void Reset (void); + + /** + * \returns the global parameter container + * + * The global parameter container can be used to specify + * a set of parameter values without having to re-specify + * them for each object when it is created. This container + * is checked only during object construction and + * it is always checked last, after any per-object + * container is checked. + */ + static Parameters *GetGlobal (void); + + std::string SerializeToString (void) const; + bool DeserializeFromString (std::string value); +private: + friend class Object; + struct Param { + Ptr spec; + Ptr value; + }; + typedef std::vector Params; + typedef Params::iterator Iterator; + typedef Params::const_iterator CIterator; + + + + bool DoSet (Ptr spec, Ptr param); + bool DoSet (Ptr spec, std::string value); + void DoSetOne (Ptr spec, Ptr param); + std::string LookupParameterFullNameByParamSpec (Ptr spec) const; + + Params m_parameters; +}; + + /** * \brief a base class which provides memory management and object aggregation * - * Note: This base class is quite similar in spirit to IUnknown in COM or - * BonoboObject in Bonobo: it provides three main methods: Ref, Unref and - * QueryInterface. */ class Object : public ObjectBase { @@ -139,6 +345,30 @@ public: Object (); virtual ~Object (); + + /** + * \param name the name of the parameter to set + * \param value the name of the parameter to set + * + * Set a single parameter. + */ + bool Set (std::string name, std::string value); + bool Set (std::string name, Ptr value); + /** + * \param name the name of the parameter to read + * \param value a reference to the string where the value of the + * parameter should be stored. + * \returns true if the requested parameter was found, false otherwise. + */ + bool Get (std::string name, std::string &value) const; + /** + * \param name the name of the parameter to read + * \param value a reference to the object where the value of the + * parameter should be stored. + * \returns true if the requested parameter was found, false otherwise. + */ + Ptr Get (std::string name) const; + /** * Increment the reference count. This method should not be called * by user code. Object instances are expected to be used in conjunction @@ -212,8 +442,13 @@ protected: * up to their parent's implementation once they are done. */ virtual void DoDispose (void); + virtual void NotifyConstructionCompleted (void); private: friend class TypeIdTraceResolver; + + template + friend Ptr CreateObject (const Parameters ¶meters); + template friend Ptr CreateObject (void); template @@ -231,57 +466,76 @@ private: template friend Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7); + Ptr DoQueryInterface (TypeId tid) const; void DoCollectSources (std::string path, const TraceContext &context, TraceResolver::SourceCollection *collection) const; void DoTraceAll (std::ostream &os, const TraceContext &context) const; bool Check (void) const; bool CheckLoose (void) const; + bool DoSet (std::string name, Ptr value); + bool DoGet (std::string name, Ptr value) const; + /** + * Attempt to delete this object. This method iterates + * over all aggregated objects to check if they all + * have a zero refcount. If yes, the object and all + * its aggregates are deleted. If not, nothing is done. + */ void MaybeDelete (void) const; /** * \param tid an TypeId * - * Every subclass which defines a new TypeId for itself - * should register this TypeId by calling this method - * from its constructor. + * Invoked from ns3::CreateObject only. + * Initialize the m_tid member variable to + * keep track of the type of this object instance. */ void SetTypeId (TypeId tid); + /** + * \param parameters the parameter values used to initialize + * the member variables of this object's instance. + * + * Invoked from ns3::CreateObject only. + * Initialize all the member variables which were + * registered with the associated TypeId. + */ + void Construct (const Parameters ¶meters); + /** + * The reference count for this object. Each aggregate + * has an individual reference count. When the global + * reference count (the sum of all reference counts) + * reaches zero, the object and all its aggregates is + * deleted. + */ mutable uint32_t m_count; + /** + * Identifies the type of this object instance. + */ TypeId m_tid; + /** + * Set to true when the DoDispose method of the object + * has run, false otherwise. + */ bool m_disposed; mutable bool m_collecting; + /** + * A pointer to the next aggregate object. This is a circular + * linked list of aggregated objects: the last one points + * back to the first one. If an object is not aggregated to + * any other object, the value of this field is equal to the + * value of the 'this' pointer. + */ Object *m_next; }; -template -Ptr CreateObject (void); - -template -Ptr CreateObject (T1 a1); - -template -Ptr CreateObject (T1 a1, T2 a2); - -template -Ptr CreateObject (T1 a1, T2 a2, T3 a3); - -template -Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4); - -template -Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); - -template -Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6); - -template -Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7); - } // namespace ns3 namespace ns3 { +/************************************************************************* + * The TypeId implementation which depends on templates + *************************************************************************/ + template TypeId TypeId::SetParent (void) @@ -294,8 +548,8 @@ TypeId TypeId::AddConstructor (void) { struct Maker { - static Ptr Create (void) { - return ns3::CreateObject (); + static Ptr Create (const Parameters ¶meters) { + return ns3::CreateObject (parameters); } }; CallbackBase cb = MakeCallback (&Maker::Create); @@ -328,49 +582,9 @@ TypeId::AddConstructor (void) DoAddConstructor (cb, 2); return *this; } -template -TypeId -TypeId::AddConstructor (void) -{ - struct Maker { - static Ptr Create (T1 a1, T2 a2, T3 a3) { - return ns3::CreateObject (a1, a2, a3); - } - }; - CallbackBase cb = MakeCallback (&Maker::Create); - DoAddConstructor (cb, 3); - return *this; -} -template -TypeId -TypeId::AddConstructor (void) -{ - struct Maker { - static Ptr Create (T1 a1, T2 a2, T3 a3, T4 a4) { - return ns3::CreateObject (a1, a2, a3, a4); - } - }; - CallbackBase cb = MakeCallback (&Maker::Create); - DoAddConstructor (cb, 4); - return *this; -} -template -TypeId -TypeId::AddConstructor (void) -{ - struct Maker { - static Ptr Create (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) { - return ns3::CreateObject (a1, a2, a3, a4, a5); - } - }; - CallbackBase cb = MakeCallback (&Maker::Create); - DoAddConstructor (cb, 5); - return *this; -} - template Ptr -TypeId::CreateObject (T1 a1) +TypeId::CreateObject (T1 a1) const { CallbackBase cb = LookupConstructor (1); Callback,T1> realCb; @@ -380,7 +594,7 @@ TypeId::CreateObject (T1 a1) } template Ptr -TypeId::CreateObject (T1 a1, T2 a2) +TypeId::CreateObject (T1 a1, T2 a2) const { CallbackBase cb = LookupConstructor (2); Callback,T1,T2> realCb; @@ -388,38 +602,10 @@ TypeId::CreateObject (T1 a1, T2 a2) Ptr object = realCb (a1,a2); return object; } -template -Ptr -TypeId::CreateObject (T1 a1, T2 a2, T3 a3) -{ - CallbackBase cb = LookupConstructor (3); - Callback,T1,T2,T3> realCb; - realCb.Assign (cb); - Ptr object = realCb (a1,a2,a3); - return object; -} -template -Ptr -TypeId::CreateObject (T1 a1, T2 a2, T3 a3, T4 a4) -{ - CallbackBase cb = LookupConstructor (4); - Callback,T1,T2,T3,T4> realCb; - realCb.Assign (cb); - Ptr object = realCb (a1,a2,a3,a4); - return object; -} - template -Ptr -TypeId::CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) -{ - CallbackBase cb = LookupConstructor (5); - Callback,T1,T2,T3,T4,T5> realCb; - realCb.Assign (cb); - Ptr object = realCb (a1,a2,a3,a4,a5); - return object; -} - +/************************************************************************* + * The Object implementation which depends on templates + *************************************************************************/ void Object::Ref (void) const @@ -461,11 +647,26 @@ Object::QueryInterface (TypeId tid) const return 0; } +/************************************************************************* + * The helper functions which need templates. + *************************************************************************/ + + +template +Ptr CreateObject (const Parameters ¶meters) +{ + Ptr p = Ptr (new T (), false); + p->SetTypeId (T::GetTypeId ()); + p->Object::Construct (parameters); + return p; +} + template Ptr CreateObject (void) { Ptr p = Ptr (new T (), false); p->SetTypeId (T::GetTypeId ()); + p->Object::Construct (Parameters ()); return p; } @@ -474,6 +675,7 @@ Ptr CreateObject (T1 a1) { Ptr p = Ptr (new T (a1), false); p->SetTypeId (T::GetTypeId ()); + p->Object::Construct (Parameters ()); return p; } @@ -482,9 +684,9 @@ Ptr CreateObject (T1 a1, T2 a2) { Ptr p = Ptr (new T (a1, a2), false); p->SetTypeId (T::GetTypeId ()); + p->Object::Construct (Parameters ()); return p; } - template Ptr CreateObject (T1 a1, T2 a2, T3 a3) { @@ -526,6 +728,30 @@ Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) } +template +Ptr +CreateObjectWith (std::string n1, Ptr v1, + std::string n2 = "", Ptr v2 = 0) + +{ + Parameters parameters; + parameters.SetWithTid (T::GetTypeId (), n1, v1); + parameters.SetWithTid (T::GetTypeId (), n2, v2); + return CreateObject (parameters); +} + +template +Ptr +CreateObjectWith (std::string n1, std::string v1, + std::string n2 = "", std::string v2 = "") + +{ + Parameters parameters; + parameters.SetWithTid (T::GetTypeId (), n1, v1); + parameters.SetWithTid (T::GetTypeId (), n2, v2); + return CreateObject (parameters); +} + } // namespace ns3 #endif /* OBJECT_H */ diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index 429cdadba..5a6162c46 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -99,7 +99,7 @@ ParamSpecHelper::Set (ObjectBase * object, Ptr parameter) co { return false; } - V v = *value; + V v = value->Get (); DoSet (obj, v); return true; } @@ -118,7 +118,7 @@ ParamSpecHelper::Get (const ObjectBase * object, Ptr parameter) co return false; } V v = DoGet (obj); - *value = v; + value->Set (v); return true; } template @@ -130,7 +130,7 @@ ParamSpecHelper::Check (Ptr parameter) const { return false; } - V v = *value; + V v = value->Get (); DoCheck (v); return true; } @@ -178,7 +178,7 @@ template void MemberMethodParamSpecHelper::DoSet (T *object, const V &v) const { - (object->m_setter) (v); + (object->*m_setter) (v); } template V diff --git a/src/core/wscript b/src/core/wscript index a6f131e00..daa6cd27c 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -56,6 +56,7 @@ def build(bld): 'value.cc', 'param-spec.cc', 'boolean-value.cc', + 'value-test.cc', ] if sys.platform == 'win32': @@ -100,5 +101,7 @@ def build(bld): 'trace-doc.h', 'int-to-type.h', 'type-id-default-value.h', + 'value.h', + 'param-spec.h', ] From 0123b4509cd77e6f511cfb50e993a9aad1c2ff40 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 30 Jan 2008 19:13:21 +0100 Subject: [PATCH 004/130] get rid of very long constructor --- src/mobility/random-position.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mobility/random-position.cc b/src/mobility/random-position.cc index 22b235d3b..8823b2262 100644 --- a/src/mobility/random-position.cc +++ b/src/mobility/random-position.cc @@ -118,8 +118,7 @@ RandomDiscPosition::GetTypeId (void) { static TypeId tid = TypeId ("RandomDiscPosition") .SetParent () - .AddConstructor () - .AddConstructor (); + .AddConstructor (); return tid; } From 656c4862251fde6c8fb4cae2d1e3e9d4afc8cec8 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 31 Jan 2008 15:10:21 +0100 Subject: [PATCH 005/130] make Ptr convertible to a Value. --- src/core/boolean-value.cc | 16 +- src/core/boolean-value.h | 8 +- src/core/object.cc | 56 ++++--- src/core/object.h | 24 +-- src/core/param-spec-helper.h | 28 ++-- src/core/param-spec.cc | 25 --- src/core/param-spec.h | 37 ----- src/core/value-test.cc | 291 +++++++++++++++++++++++++++++++++ src/core/value.cc | 110 ++++++++++++- src/core/value.h | 304 ++++++++++++++++++++++++++++++++++- src/core/wscript | 2 - 11 files changed, 759 insertions(+), 142 deletions(-) delete mode 100644 src/core/param-spec.cc delete mode 100644 src/core/param-spec.h create mode 100644 src/core/value-test.cc diff --git a/src/core/boolean-value.cc b/src/core/boolean-value.cc index 05fe3a85f..75e36def2 100644 --- a/src/core/boolean-value.cc +++ b/src/core/boolean-value.cc @@ -16,10 +16,10 @@ BooleanValue::Get (void) const { return m_value; } -Ptr +PValue BooleanValue::Copy (void) const { - return Create (*this); + return PValue::Create (*this); } std::string BooleanValue::SerializeToString (Ptr spec) const @@ -57,22 +57,18 @@ BooleanValue::DeserializeFromString (std::string value, Ptr spe return false; } } -BooleanValue::BooleanValue (Ptr value) +BooleanValue::BooleanValue (PValue value) { - const BooleanValue *v = dynamic_cast (PeekPointer (value)); + const BooleanValue *v = value.DynCast (); if (v == 0) { NS_FATAL_ERROR ("assigning non-Boolean value to Boolean value."); } m_value = v->m_value; } -BooleanValue::operator Ptr () const +BooleanValue::operator PValue () const { - return Create (*this); -} -BooleanValue::operator Ptr () const -{ - return Create (*this); + return PValue::Create (*this); } } // namespace ns3 diff --git a/src/core/boolean-value.h b/src/core/boolean-value.h index 0f9ee9d23..a9f1a4dd0 100644 --- a/src/core/boolean-value.h +++ b/src/core/boolean-value.h @@ -2,7 +2,6 @@ #define BOOLEAN_VALUE_H #include "value.h" -#include "param-spec.h" #include "param-spec-helper.h" #include "ptr.h" @@ -15,13 +14,12 @@ public: void Set (bool value); bool Get (void) const; - virtual Ptr Copy (void) const; + virtual PValue Copy (void) const; virtual std::string SerializeToString (Ptr spec) const; virtual bool DeserializeFromString (std::string value, Ptr spec); - BooleanValue (Ptr value); - operator Ptr () const; - operator Ptr () const; + BooleanValue (PValue value); + operator PValue () const; private: bool m_value; }; diff --git a/src/core/object.cc b/src/core/object.cc index 658f9b160..c15400f86 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -23,7 +23,6 @@ #include "singleton.h" #include "trace-resolver.h" #include "value.h" -#include "param-spec.h" #include "log.h" #include #include @@ -62,7 +61,6 @@ public: uint32_t GetParametersN (uint16_t uid) const; std::string GetParameterName (uint16_t uid, uint32_t i) const; uint32_t GetParameterFlags (uint16_t uid, uint32_t i) const; - const ns3::Value *GetParameterInitialValue (uint16_t uid, uint32_t i) const; uint32_t GetParameterUid (uint16_t uid, uint32_t i) const; ns3::Ptr GetParameterParamSpec (uint16_t uid, uint32_t i) const; private: @@ -594,7 +592,7 @@ Parameters::Parameters (const Parameters &o) { struct Param param; param.spec = i->spec; - param.value = i->value->Copy (); + param.value = i->value.Copy (); m_parameters.push_back (param); } } @@ -606,7 +604,7 @@ Parameters::operator = (const Parameters &o) { struct Param param; param.spec = i->spec; - param.value = i->value->Copy (); + param.value = i->value.Copy (); m_parameters.push_back (param); } return *this; @@ -617,7 +615,7 @@ Parameters::~Parameters () } bool -Parameters::Set (std::string name, Ptr value) +Parameters::Set (std::string name, PValue value) { Ptr spec = TypeId::LookupParamSpecByFullName (name); bool ok = DoSet (spec, value); @@ -637,7 +635,7 @@ Parameters::SetWithTid (TypeId tid, std::string name, std::string value) DoSet (spec, value); } void -Parameters::SetWithTid (TypeId tid, std::string name, Ptr value) +Parameters::SetWithTid (TypeId tid, std::string name, PValue value) { Ptr spec = tid.LookupParamSpecByName (name); DoSet (spec, value); @@ -649,14 +647,14 @@ Parameters::SetWithTid (TypeId tid, uint32_t position, std::string value) DoSet (spec, value); } void -Parameters::SetWithTid (TypeId tid, uint32_t position, Ptr value) +Parameters::SetWithTid (TypeId tid, uint32_t position, PValue value) { Ptr spec = tid.LookupParamSpecByPosition (position); DoSet (spec, value); } void -Parameters::DoSetOne (Ptr spec, Ptr value) +Parameters::DoSetOne (Ptr spec, PValue value) { // get rid of any previous value stored in this // vector of values. @@ -671,11 +669,11 @@ Parameters::DoSetOne (Ptr spec, Ptr value) // store the new value. struct Param p; p.spec = spec; - p.value = value->Copy (); + p.value = value.Copy (); m_parameters.push_back (p); } bool -Parameters::DoSet (Ptr spec, Ptr value) +Parameters::DoSet (Ptr spec, PValue value) { if (spec == 0) { @@ -696,8 +694,8 @@ Parameters::DoSet (Ptr spec, std::string value) { return false; } - Ptr v = spec->CreateInitialValue (); - bool ok = v->DeserializeFromString (value, spec); + PValue v = spec->CreateInitialValue (); + bool ok = v.DeserializeFromString (value, spec); if (!ok) { return false; @@ -747,7 +745,7 @@ Parameters::SerializeToString (void) const for (Params::const_iterator i = m_parameters.begin (); i != m_parameters.end (); i++) { std::string name = LookupParameterFullNameByParamSpec (i->spec); - oss << name << "=" << i->value->SerializeToString (PeekPointer (i->spec)); + oss << name << "=" << i->value.SerializeToString (PeekPointer (i->spec)); if (i != m_parameters.end ()) { oss << "|"; @@ -792,8 +790,8 @@ Parameters::DeserializeFromString (std::string str) value = str.substr (equal+1, next - (equal+1)); cur++; } - Ptr val = spec->CreateInitialValue (); - bool ok = val->DeserializeFromString (value, spec); + PValue val = spec->CreateInitialValue (); + bool ok = val.DeserializeFromString (value, spec); if (!ok) { // XXX invalid value @@ -892,7 +890,7 @@ Object::Construct (const Parameters ¶meters) if (!found) { // No matching parameter value so we set the default value. - Ptr initial = paramSpec->CreateInitialValue (); + PValue initial = paramSpec->CreateInitialValue (); paramSpec->Set (this, initial); NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\" from local"); @@ -903,7 +901,7 @@ Object::Construct (const Parameters ¶meters) NotifyConstructionCompleted (); } bool -Object::DoSet (std::string name, Ptr value) +Object::DoSet (std::string name, PValue value) { Ptr spec = m_tid.LookupParamSpecByName (name); if (spec == 0) @@ -918,7 +916,7 @@ Object::DoSet (std::string name, Ptr value) return true; } bool -Object::Set (std::string name, Ptr value) +Object::Set (std::string name, PValue value) { return DoSet (name, value); } @@ -930,13 +928,13 @@ Object::Set (std::string name, std::string value) { return false; } - Ptr parameter = spec->CreateInitialValue (); - bool ok = parameter->DeserializeFromString (value, spec); + PValue v = spec->CreateInitialValue (); + bool ok = v.DeserializeFromString (value, spec); if (!ok) { return false; } - spec->Set (this, parameter); + spec->Set (this, v); if (!ok) { return false; @@ -951,34 +949,34 @@ Object::Get (std::string name, std::string &value) const { return false; } - Ptr parameter = paramSpec->CreateInitialValue (); - bool ok = paramSpec->Get (this, parameter); + PValue v = paramSpec->CreateInitialValue (); + bool ok = paramSpec->Get (this, v); if (ok) { - value = parameter->SerializeToString (paramSpec); + value = v.SerializeToString (paramSpec); } return ok; } -Ptr +PValue Object::Get (std::string name) const { Ptr paramSpec = m_tid.LookupParamSpecByName (name); if (paramSpec == 0) { - return 0; + return PValue (); } - Ptr value = paramSpec->CreateInitialValue (); + PValue value = paramSpec->CreateInitialValue (); bool ok = paramSpec->Get (this, value); if (!ok) { - return 0; + return PValue (); } return value; } bool -Object::DoGet (std::string name, Ptr parameter) const +Object::DoGet (std::string name, PValue parameter) const { Ptr paramSpec = m_tid.LookupParamSpecByName (name); if (paramSpec == 0) diff --git a/src/core/object.h b/src/core/object.h index 03436ce6a..a7cf37d66 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -287,12 +287,12 @@ public: * the program terminates with a message. */ bool Set (std::string name, std::string value); - bool Set (std::string name, Ptr value); + bool Set (std::string name, PValue value); void SetWithTid (TypeId tid, std::string name, std::string value); - void SetWithTid (TypeId tid, std::string name, Ptr value); + void SetWithTid (TypeId tid, std::string name, PValue value); void SetWithTid (TypeId tid, uint32_t position, std::string value); - void SetWithTid (TypeId tid, uint32_t position, Ptr value); + void SetWithTid (TypeId tid, uint32_t position, PValue value); /** * Clear the content of this instance. @@ -317,7 +317,7 @@ private: friend class Object; struct Param { Ptr spec; - Ptr value; + PValue value; }; typedef std::vector Params; typedef Params::iterator Iterator; @@ -325,9 +325,9 @@ private: - bool DoSet (Ptr spec, Ptr param); + bool DoSet (Ptr spec, PValue param); bool DoSet (Ptr spec, std::string value); - void DoSetOne (Ptr spec, Ptr param); + void DoSetOne (Ptr spec, PValue param); std::string LookupParameterFullNameByParamSpec (Ptr spec) const; Params m_parameters; @@ -353,7 +353,7 @@ public: * Set a single parameter. */ bool Set (std::string name, std::string value); - bool Set (std::string name, Ptr value); + bool Set (std::string name, PValue value); /** * \param name the name of the parameter to read * \param value a reference to the string where the value of the @@ -367,7 +367,7 @@ public: * parameter should be stored. * \returns true if the requested parameter was found, false otherwise. */ - Ptr Get (std::string name) const; + PValue Get (std::string name) const; /** * Increment the reference count. This method should not be called @@ -473,8 +473,8 @@ private: void DoTraceAll (std::ostream &os, const TraceContext &context) const; bool Check (void) const; bool CheckLoose (void) const; - bool DoSet (std::string name, Ptr value); - bool DoGet (std::string name, Ptr value) const; + bool DoSet (std::string name, PValue value); + bool DoGet (std::string name, PValue value) const; /** * Attempt to delete this object. This method iterates * over all aggregated objects to check if they all @@ -730,8 +730,8 @@ Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) template Ptr -CreateObjectWith (std::string n1, Ptr v1, - std::string n2 = "", Ptr v2 = 0) +CreateObjectWith (std::string n1, PValue v1, + std::string n2 = "", PValue v2 = PValue ()) { Parameters parameters; diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index 5a6162c46..c5de1fd95 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -8,10 +8,10 @@ class ParamSpecHelper : public ParamSpec { public: ParamSpecHelper (V initialValue); - virtual bool Set (ObjectBase * object, Ptr parameter) const; - virtual bool Get (const ObjectBase * object, Ptr parameter) const; - virtual bool Check (Ptr parameter) const; - virtual Ptr CreateInitialValue (void) const; + virtual bool Set (ObjectBase * object, PValue value) const; + virtual bool Get (const ObjectBase * object, PValue value) const; + virtual bool Check (PValue value) const; + virtual PValue CreateInitialValue (void) const; private: virtual void DoSet (T *object, const V &v) const = 0; @@ -87,9 +87,9 @@ ParamSpecHelper::ParamSpecHelper (V initialValue) {} template bool -ParamSpecHelper::Set (ObjectBase * object, Ptr parameter) const +ParamSpecHelper::Set (ObjectBase * object, PValue val) const { - const U *value = dynamic_cast (PeekPointer (parameter)); + const U *value = val.DynCast (); if (value == 0) { return false; @@ -105,9 +105,9 @@ ParamSpecHelper::Set (ObjectBase * object, Ptr parameter) co } template bool -ParamSpecHelper::Get (const ObjectBase * object, Ptr parameter) const +ParamSpecHelper::Get (const ObjectBase * object, PValue val) const { - U *value = dynamic_cast (PeekPointer (parameter)); + U *value = val.DynCast (); if (value == 0) { return false; @@ -123,14 +123,14 @@ ParamSpecHelper::Get (const ObjectBase * object, Ptr parameter) co } template bool -ParamSpecHelper::Check (Ptr parameter) const +ParamSpecHelper::Check (PValue value) const { - const U *value = dynamic_cast (PeekPointer (parameter)); - if (value == 0) + const U *val = value.DynCast (); + if (val == 0) { return false; } - V v = value->Get (); + V v = val->Get (); DoCheck (v); return true; } @@ -141,10 +141,10 @@ ParamSpecHelper::DoCheck (const V &v) const return true; } template -Ptr +PValue ParamSpecHelper::CreateInitialValue (void) const { - return Create (m_initialValue); + return PValue::Create (m_initialValue); } diff --git a/src/core/param-spec.cc b/src/core/param-spec.cc deleted file mode 100644 index c99543c0c..000000000 --- a/src/core/param-spec.cc +++ /dev/null @@ -1,25 +0,0 @@ -#include "param-spec.h" - -namespace ns3 { - -ParamSpec::ParamSpec () - : m_count (1) -{} -void -ParamSpec::Ref (void) const -{ - m_count++; -} -void -ParamSpec::Unref (void) const -{ - m_count--; - if (m_count == 0) - { - delete this; - } -} -ParamSpec::~ParamSpec () -{} - -} // namespace ns3 diff --git a/src/core/param-spec.h b/src/core/param-spec.h deleted file mode 100644 index 32cb42bd5..000000000 --- a/src/core/param-spec.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef PARAM_SPEC_H -#define PARAM_SPEC_H - -#include -#include -#include "object-base.h" -#include "ptr.h" - -namespace ns3 { - -class Value; - -class ParamSpec : public ObjectBase -{ -public: - ParamSpec (); - void Ref (void) const; - void Unref (void) const; - virtual ~ParamSpec (); - - /** - * \param object the object instance to set the value in - * \param value the value to set - * \returns true if the value is valid and it could be set - * successfully, false otherwise. - */ - virtual bool Set (ObjectBase * object, Ptr parameter) const = 0; - virtual bool Get (const ObjectBase * object, Ptr parameter) const = 0; - virtual bool Check (Ptr parameter) const = 0; - virtual Ptr CreateInitialValue (void) const = 0; -private: - mutable uint32_t m_count; -}; - -} // namespace ns3 - -#endif /* PARAM_SPEC_H */ diff --git a/src/core/value-test.cc b/src/core/value-test.cc new file mode 100644 index 000000000..993a2de2e --- /dev/null +++ b/src/core/value-test.cc @@ -0,0 +1,291 @@ +#ifdef RUN_SELF_TESTS +#include "test.h" +#include "object.h" +#include "boolean-value.h" +#if 0 +#include "int-value.h" +#include "uint-value.h" +#include "fp-value.h" +#include "enum-value.h" +#endif +namespace ns3 { + +class ParamSpecTest : public Test +{ +public: + ParamSpecTest (); + virtual bool RunTests (void); +private: +}; + +class Derived : public Object +{ +public: + static TypeId GetTypeId (void) { + static TypeId tid = TypeId ("Derived") + .SetParent () + ; + return tid; + } +}; + +class ParamSpecObjectTest : public Object +{ +public: + enum TestEnum { + TEST_A, + TEST_B, + TEST_C + }; + static TypeId GetTypeId (void) { + static TypeId tid = TypeId ("ParamSpecObjectTest") + .SetParent () + .AddParameter ("TestBoolName", "help text", + MakeBooleanParamSpec (&ParamSpecObjectTest::m_boolTest, false)) + .AddParameter ("TestBoolA", "help text", + MakeBooleanParamSpec (&ParamSpecObjectTest::DoSetTestB, + &ParamSpecObjectTest::DoGetTestB, + false)) + .AddParameter ("TestPtr", "help text", + MakePtrParamSpec (&ParamSpecObjectTest::m_derived)) +#if 0 + .AddParameter ("TestInt16", "help text", + MakeIntParamSpec (-2, &ParamSpecObjectTest::m_int16)) + .AddParameter ("TestInt16WithBounds", "help text", + MakeIntParamSpec (-2, -5, 10, + &ParamSpecObjectTest::m_int16WithBounds)) + .AddParameter ("TestInt16SetGet", "help text", + MakeIntParamSpec (6, &ParamSpecObjectTest::DoSetInt16, + &ParamSpecObjectTest::DoGetInt16)) + .AddParameter ("TestUint8", "help text", + MakeUintParamSpec (1, &ParamSpecObjectTest::m_uint8)) + .AddParameter ("TestFloat", "help text", + MakeFpParamSpec (-1.1, &ParamSpecObjectTest::m_float)) + .AddParameter ("TestEnum", "help text", + MakeEnumParamSpec (&ParamSpecObjectTest::m_enum, + TEST_A, "TestA", + TEST_B, "TestB", + TEST_C, "TestC")) +#endif + ; + + return tid; + } + +private: + void DoSetTestB (bool v) { + m_boolTestA = v; + } + bool DoGetTestB (void) const { + return m_boolTestA; + } + int16_t DoGetInt16 (void) const { + return m_int16SetGet; + } + void DoSetInt16 (int16_t v) { + m_int16SetGet = v; + } + bool m_boolTestA; + bool m_boolTest; + Ptr m_derived; + int16_t m_int16; + int16_t m_int16WithBounds; + int16_t m_int16SetGet; + uint8_t m_uint8; + float m_float; + enum TestEnum m_enum; +}; + + +#define CHECK_GET_STR(p,name,value) \ + { \ + std::string expected = value; \ + std::string got; \ + bool ok = p->Get (name, got); \ + NS_TEST_ASSERT (ok); \ + NS_TEST_ASSERT_EQUAL (got, expected); \ + } +#define CHECK_GET_PARAM(p,name,type,value) \ + { \ + const type expected = value; \ + type got = value; \ + PValue v = p->Get (name); \ + NS_TEST_ASSERT (v.DynCast () != 0); \ + got = v; \ + NS_TEST_ASSERT_EQUAL (got.Get (), expected.Get ()); \ + } + + +NS_OBJECT_ENSURE_REGISTERED (ParamSpecObjectTest); + +ParamSpecTest::ParamSpecTest () + : Test ("ParamSpec") +{} +bool +ParamSpecTest::RunTests (void) +{ + bool result = true; + + Parameters params; + Ptr p; + NS_TEST_ASSERT (params.Set ("ParamSpecObjectTest::TestBoolName", "false")); + p = CreateObject (params); + CHECK_GET_STR (p, "TestBoolName", "false"); + CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false); + + NS_TEST_ASSERT (p->Set ("TestBoolName", "true")); + CHECK_GET_STR (p, "TestBoolName", "true"); + CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true); + + NS_TEST_ASSERT (p->Set ("TestBoolName", BooleanValue (false))); + CHECK_GET_STR (p, "TestBoolName", "false"); + CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false); + + p = CreateObjectWith ("TestBoolName", "true"); + CHECK_GET_STR (p, "TestBoolName", "true"); + CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true); + + p = CreateObjectWith ("TestBoolName", BooleanValue (true)); + CHECK_GET_STR (p, "TestBoolName", "true"); + CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true); + + NS_TEST_ASSERT (p->Set ("TestBoolA", "false")); + CHECK_GET_STR (p, "TestBoolA", "false"); + CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, false); + + NS_TEST_ASSERT (p->Set ("TestBoolA", "true")); + CHECK_GET_STR (p, "TestBoolA", "true"); + CHECK_GET_PARAM (p, "TestBoolA", BooleanValue, true); + + + Ptr derived = p->Get ("TestPtr"); + NS_TEST_ASSERT (derived == 0); + derived = Create (); + NS_TEST_ASSERT (p->Set ("TestPtr", derived)); + Ptr stored = p->Get ("TestPtr"); + NS_TEST_ASSERT (stored == derived); + Ptr storedBase = p->Get ("TestPtr"); + NS_TEST_ASSERT (stored == storedBase); + Ptr x = p->Get ("TestPtr"); + NS_TEST_ASSERT (x == 0); + + p = CreateObjectWith ("TestPtr", Create ()); + NS_TEST_ASSERT (p != 0); + derived = 0; + derived = p->Get ("TestPtr"); + NS_TEST_ASSERT (derived != 0); + +#if 0 + CHECK_GET_STR (p, "TestInt16", "-2"); + CHECK_GET_PARAM (p, "TestInt16", IntValue, -2); + + NS_TEST_ASSERT (p->Set ("TestInt16", "-5")); + CHECK_GET_STR (p, "TestInt16", "-5"); + CHECK_GET_PARAM (p, "TestInt16", IntValue, -5); + + NS_TEST_ASSERT (p->Set ("TestInt16", IntValue (+2))); + CHECK_GET_STR (p, "TestInt16", "2"); + CHECK_GET_PARAM (p, "TestInt16", IntValue, +2); + + NS_TEST_ASSERT (p->Set ("TestInt16", IntValue (-32768))); + CHECK_GET_STR (p, "TestInt16", "-32768"); + CHECK_GET_PARAM (p, "TestInt16", IntValue, -32768); + + NS_TEST_ASSERT (!p->Set ("TestInt16", IntValue (-32769))); + CHECK_GET_STR (p, "TestInt16", "-32768"); + CHECK_GET_PARAM (p, "TestInt16", IntValue, -32768); + + NS_TEST_ASSERT (p->Set ("TestInt16", IntValue (32767))); + CHECK_GET_STR (p, "TestInt16", "32767"); + CHECK_GET_PARAM (p, "TestInt16", IntValue, 32767); + + NS_TEST_ASSERT (!p->Set ("TestInt16", IntValue (32768))); + CHECK_GET_STR (p, "TestInt16", "32767"); + CHECK_GET_PARAM (p, "TestInt16", IntValue, 32767); + + NS_TEST_ASSERT (p->Set ("TestInt16WithBounds", IntValue (10))); + CHECK_GET_STR (p, "TestInt16WithBounds", "10"); + CHECK_GET_PARAM (p, "TestInt16WithBounds", IntValue, 10); + NS_TEST_ASSERT (!p->Set ("TestInt16WithBounds", IntValue (11))); + CHECK_GET_STR (p, "TestInt16WithBounds", "10"); + CHECK_GET_PARAM (p, "TestInt16WithBounds", IntValue, 10); + + NS_TEST_ASSERT (p->Set ("TestInt16WithBounds", IntValue (-5))); + CHECK_GET_STR (p, "TestInt16WithBounds", "-5"); + CHECK_GET_PARAM (p, "TestInt16WithBounds", IntValue, -5); + NS_TEST_ASSERT (!p->Set ("TestInt16WithBounds", IntValue (-6))); + CHECK_GET_STR (p, "TestInt16WithBounds", "-5"); + CHECK_GET_PARAM (p, "TestInt16WithBounds", IntValue, -5); + + CHECK_GET_STR (p, "TestInt16SetGet", "6"); + CHECK_GET_PARAM (p, "TestInt16SetGet", IntValue, 6); + NS_TEST_ASSERT (p->Set ("TestInt16SetGet", IntValue (0))); + CHECK_GET_STR (p, "TestInt16SetGet", "0"); + CHECK_GET_PARAM (p, "TestInt16SetGet", IntValue, 0); + + CHECK_GET_STR (p, "TestUint8", "1"); + CHECK_GET_PARAM (p, "TestUint8", UintValue, 1); + NS_TEST_ASSERT (p->Set ("TestUint8", UintValue (0))); + CHECK_GET_STR (p, "TestUint8", "0"); + CHECK_GET_PARAM (p, "TestUint8", UintValue, 0); + NS_TEST_ASSERT (p->Set ("TestUint8", UintValue (255))); + CHECK_GET_STR (p, "TestUint8", "255"); + CHECK_GET_PARAM (p, "TestUint8", UintValue, 255); + NS_TEST_ASSERT (p->Set ("TestUint8", "255")); + CHECK_GET_STR (p, "TestUint8", "255"); + CHECK_GET_PARAM (p, "TestUint8", UintValue, 255); + NS_TEST_ASSERT (!p->Set ("TestUint8", "256")); + CHECK_GET_STR (p, "TestUint8", "255"); + CHECK_GET_PARAM (p, "TestUint8", UintValue, 255); + NS_TEST_ASSERT (!p->Set ("TestUint8", "-1")); + CHECK_GET_STR (p, "TestUint8", "255"); + CHECK_GET_PARAM (p, "TestUint8", UintValue, 255); + NS_TEST_ASSERT (!p->Set ("TestUint8", UintValue (-1))); + CHECK_GET_STR (p, "TestUint8", "255"); + CHECK_GET_PARAM (p, "TestUint8", UintValue, 255); + + CHECK_GET_STR (p, "TestFloat", "-1.1"); + NS_TEST_ASSERT (p->Set ("TestFloat", FpValue ((float)+2.3))); + CHECK_GET_PARAM (p, "TestFloat", FpValue, (float)+2.3); + + CHECK_GET_STR (p, "TestEnum", "TestA"); + CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_A); + NS_TEST_ASSERT (p->Set ("TestEnum", EnumValue (ParamSpecObjectTest::TEST_C))); + CHECK_GET_STR (p, "TestEnum", "TestC"); + CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_C); + NS_TEST_ASSERT (p->Set ("TestEnum", "TestB")); + CHECK_GET_STR (p, "TestEnum", "TestB"); + CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_B); + NS_TEST_ASSERT (!p->Set ("TestEnum", "TestD")); + CHECK_GET_STR (p, "TestEnum", "TestB"); + CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_B); + NS_TEST_ASSERT (!p->Set ("TestEnum", EnumValue (5))); + CHECK_GET_STR (p, "TestEnum", "TestB"); + CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_B); +#endif + +#if 0 + p->Set ("TestBoolName", "true"); + NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "true"); + p->Set ("TestBoolName", "false"); + NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "false"); + + Parameters::GetGlobal ()->Set ("TestBoolName", "true"); + p = CreateObjectWith (); + NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "true"); + + Parameters::GetGlobal ()->Set ("TestBoolName", "false"); + p = CreateObjectWith (); + NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "false"); +#endif + return result; +} + + + +static ParamSpecTest g_parameterTest; + +} // namespace ns3 + + +#endif /* RUN_SELF_TESTS */ diff --git a/src/core/value.cc b/src/core/value.cc index 83931ad36..a60c06c84 100644 --- a/src/core/value.cc +++ b/src/core/value.cc @@ -1,17 +1,106 @@ #include "value.h" +#include "log.h" +#include + +NS_LOG_COMPONENT_DEFINE ("Value"); namespace ns3 { Value::Value () : m_count (1) {} +Value::Value (const Value &o) + : m_count (1) +{} +Value & +Value::operator = (const Value &o) +{ + return *this; +} +Value::~Value () +{} + +PValue::PValue () + : m_value (0) +{} +PValue::PValue (const PValue &o) + : m_value (o.m_value) +{ + if (m_value != 0) + { + m_value->m_count++; + NS_LOG_DEBUG ("this="< Copy (void) const = 0; + virtual PValue Copy (void) const = 0; virtual std::string SerializeToString (Ptr spec) const = 0; virtual bool DeserializeFromString (std::string value, Ptr spec) = 0; +private: + friend class PValue; + uint32_t m_count; +}; + +class PValue +{ +public: + PValue (); + PValue (const PValue &o); + PValue &operator = (const PValue &o); + ~PValue (); + + PValue Copy (void) const; + std::string SerializeToString (Ptr spec) const; + bool DeserializeFromString (std::string value, Ptr spec); + + template + static PValue Create (void); + template + static PValue Create (T1 a1); + + template + T DynCast (void) const; + + template + PValue (Ptr pointer); + template + operator Ptr (); + +private: + PValue (Value *value); + Value *m_value; +}; + +class ParamSpec : public ObjectBase +{ +public: + ParamSpec (); + void Ref (void) const; + void Unref (void) const; + virtual ~ParamSpec (); + + /** + * \param object the object instance to set the value in + * \param value the value to set + * \returns true if the value is valid and it could be set + * successfully, false otherwise. + */ + virtual bool Set (ObjectBase * object, PValue value) const = 0; + virtual bool Get (const ObjectBase * object, PValue value) const = 0; + virtual bool Check (PValue value) const = 0; + virtual PValue CreateInitialValue (void) const = 0; private: mutable uint32_t m_count; }; + +template +Ptr +MakePtrParamSpec (Ptr T::*memberVariable); + +template +Ptr +MakePtrParamSpec (void (T::*setter) (Ptr)); +template +Ptr +MakePtrParamSpec (Ptr (T::*getter) (void) const); + + +} // namespace ns3 + +namespace ns3 { + +/******************************************************** + * The class used to access the pointer stored in a + * PtrValue Value instance. + ********************************************************/ + +class PtrValueBase : public Value +{ +public: + virtual ObjectBase *PeekObjectBase (void) const = 0; + virtual bool SetObjectBase (ObjectBase *object) = 0; + virtual std::string SerializeToString (Ptr spec) const; + virtual bool DeserializeFromString (std::string value, Ptr spec); +}; + +/******************************************************** + * Store the content of a Ptr in a Value + ********************************************************/ + +template +class PtrValue : public PtrValueBase +{ +public: + PtrValue (Ptr pointer) + : m_pointer (pointer) {} + + virtual ObjectBase *PeekObjectBase (void) const { + return PeekPointer (m_pointer); + } + virtual bool SetObjectBase (ObjectBase *object) { + T *ptr = dynamic_cast (object); + if (ptr == 0) + { + return false; + } + m_pointer = ptr; + return true; + } + virtual PValue Copy (void) const { + return PValue::Create > (*this); + } +private: + Ptr m_pointer; +}; + +/******************************************************** + * The implementation of the PValue + * class template methods. + ********************************************************/ + + +template +PValue +PValue::Create (void) +{ + return PValue (new T ()); +} +template +PValue +PValue::Create (T1 a1) +{ + return PValue (new T (a1)); +} + +template +T +PValue::DynCast (void) const +{ + return dynamic_cast (m_value); +} + +template +PValue::PValue (Ptr pointer) + : m_value (new PtrValue (pointer)) +{} +template +PValue::operator Ptr () +{ + PtrValueBase *value = DynCast (); + if (value == 0) + { + return 0; + } + ObjectBase *objectBase = value->PeekObjectBase (); + T *obj = dynamic_cast (objectBase); + if (obj == 0) + { + return 0; + } + return obj; +} + +/******************************************************** + * The ParamSpec associated to + * PtrValue + ********************************************************/ + +template +class PtrParamSpec : public ParamSpec +{ +public: + virtual ~PtrParamSpec () {} + virtual bool Set (ObjectBase * object, PValue val) const { + T *obj = dynamic_cast (object); + if (obj == 0) + { + return false; + } + const PtrValueBase *value = val.DynCast (); + if (value == 0) + { + return false; + } + Ptr ptr = dynamic_cast (value->PeekObjectBase ()); + if (ptr == 0) + { + return false; + } + DoSet (obj, ptr); + return true; + } + virtual bool Get (const ObjectBase * object, PValue val) const { + const T *obj = dynamic_cast (object); + if (obj == 0) + { + return false; + } + PtrValueBase *value = val.DynCast (); + if (value == 0) + { + return false; + } + return value->SetObjectBase (PeekPointer (DoGet (obj))); + } + virtual bool Check (PValue val) const { + const PtrValueBase *value = val.DynCast (); + if (value == 0) + { + return false; + } + if (value->PeekObjectBase () == 0) + { + return true; + } + U *ptr = dynamic_cast (value->PeekObjectBase ()); + if (ptr == 0) + { + return false; + } + return true; + } + virtual PValue CreateInitialValue (void) const { + return PValue::Create > (Ptr (0)); + } +private: + virtual void DoSet (T *object, Ptr value) const = 0; + virtual Ptr DoGet (const T *object) const = 0; +}; + + +template +Ptr +MakePtrParamSpec (Ptr T::*memberVariable) +{ + struct MemberVariable : public PtrParamSpec + { + Ptr T::*m_memberVariable; + virtual MemberVariable *Copy (void) const { + return new MemberVariable (*this); + } + virtual void DoSet (T *object, Ptr value) const { + (object->*m_memberVariable) = value; + } + virtual Ptr DoGet (const T *object) const { + return object->*m_memberVariable; + } + } *spec = new MemberVariable (); + spec->m_memberVariable = memberVariable; + return Ptr (spec, false); +} + +template +Ptr +MakePtrParamSpec (void (T::*setter) (Ptr)) +{ + struct MemberMethod : public PtrParamSpec + { + void (T::*m_setter) (Ptr); + virtual MemberMethod *Copy (void) const { + return new MemberMethod (*this); + } + virtual void DoSet (T *object, Ptr value) const { + (object->*m_setter) (value); + } + virtual Ptr DoGet (const T *object) const { + return 0; + //return (object->*m_getter) (); + } + } *spec = new MemberMethod (); + spec->m_setter = setter; + return Ptr (spec, false); +} + +template +Ptr +MakePtrParamSpec (Ptr (T::*getter) (void) const) +{ + struct MemberMethod : public PtrParamSpec + { + void (T::*m_getter) (Ptr); + virtual MemberMethod *Copy (void) const { + return new MemberMethod (*this); + } + virtual void DoSet (T *object, Ptr value) const { + //(object->*m_setter) (value); + } + virtual Ptr DoGet (const T *object) const { + return (object->*m_getter) (); + } + } *spec = new MemberMethod (); + spec->m_getter = getter; + return Ptr (spec, false); +} + } // namespace ns3 #endif /* VALUE_H */ diff --git a/src/core/wscript b/src/core/wscript index daa6cd27c..7105cae14 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -54,7 +54,6 @@ def build(bld): 'array-trace-resolver.cc', 'type-id-default-value.cc', 'value.cc', - 'param-spec.cc', 'boolean-value.cc', 'value-test.cc', ] @@ -102,6 +101,5 @@ def build(bld): 'int-to-type.h', 'type-id-default-value.h', 'value.h', - 'param-spec.h', ] From 220bf4846d97571bb664fdac7edf0cb35d48f8a6 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 4 Feb 2008 22:18:07 +0100 Subject: [PATCH 006/130] add back IntValue support --- src/core/boolean-value.h | 8 +- src/core/int-value.cc | 75 ++++++++ src/core/int-value.h | 105 +++++++++++ src/core/object.cc | 10 +- src/core/param-spec-helper.h | 333 +++++++++++++++++++---------------- src/core/value-test.cc | 20 ++- src/core/wscript | 1 + 7 files changed, 382 insertions(+), 170 deletions(-) create mode 100644 src/core/int-value.cc create mode 100644 src/core/int-value.h diff --git a/src/core/boolean-value.h b/src/core/boolean-value.h index a9f1a4dd0..a8473e7cb 100644 --- a/src/core/boolean-value.h +++ b/src/core/boolean-value.h @@ -49,7 +49,7 @@ namespace ns3 { template Ptr MakeBooleanParamSpec (bool T::*v, bool initialValue) { - return Create > (v, initialValue); + return MakeMemberVariableParamSpec (v, BooleanValue (initialValue)); } template @@ -57,21 +57,21 @@ Ptr MakeBooleanParamSpec (void (T::*setter) (bool), bool (T::*getter) (void) const, bool initialValue) { - return Create > (setter, getter, initialValue); + return MakeMemberMethodParamSpec (setter, getter, BooleanValue (initialValue)); } template Ptr MakeBooleanParamSpec (bool (T::*getter) (void) const, bool initialValue) { - return Create > (getter, initialValue); + return MakeMemberMethodGetterParamSpec (getter, BooleanValue (initialValue)); } template Ptr MakeBooleanParamSpec (void (T::*setter) (bool), bool initialValue) { - return Create > (setter, initialValue); + return MakeMemberMethodSetterParamSpec (setter, BooleanValue (initialValue)); } } // namespace ns3 diff --git a/src/core/int-value.cc b/src/core/int-value.cc new file mode 100644 index 000000000..2ea001a61 --- /dev/null +++ b/src/core/int-value.cc @@ -0,0 +1,75 @@ +#include "int-value.h" +#include "object.h" +#include + +namespace ns3 { + +IntValue::IntValue (int64_t value) + : m_value (value) +{} +PValue +IntValue::Copy (void) const +{ + return PValue::Create (*this); +} + +void +IntValue::Set (int64_t value) +{ + m_value = value; +} +int64_t +IntValue::Get (void) const +{ + return m_value; +} +std::string +IntValue::SerializeToString (Ptr spec) const +{ + std::ostringstream oss; + oss << m_value; + return oss.str (); +} +bool +IntValue::DeserializeFromString (std::string value, Ptr spec) +{ + int64_t v; + std::istringstream iss; + iss.str (value); + iss >> v; + bool ok = !iss.bad () && !iss.fail (); + if (ok) + { + m_value = v; + } + return ok; +} + +IntValue::IntValue (PValue value) +{ + const IntValue *v = value.DynCast (); + if (v == 0) + { + NS_FATAL_ERROR ("assigning non-Int value to Int value."); + } + m_value = v->m_value; +} +IntValue::operator PValue () const +{ + return PValue::Create (*this); +} + + +IntValueChecker::IntValueChecker (int64_t minValue, int64_t maxValue) + : m_minValue (minValue), + m_maxValue (maxValue) +{} +bool +IntValueChecker::Check (const int64_t &value) const +{ + return value >= m_minValue && value <= m_maxValue; +} + + + +} // namespace ns3 diff --git a/src/core/int-value.h b/src/core/int-value.h new file mode 100644 index 000000000..eaf98768d --- /dev/null +++ b/src/core/int-value.h @@ -0,0 +1,105 @@ +#ifndef INT_VALUE_H +#define INT_VALUE_H + +#include "value.h" +#include "param-spec-helper.h" +#include + +namespace ns3 { + +class IntValue : public Value +{ +public: + IntValue (int64_t value); + void Set (int64_t value); + int64_t Get (void) const; + + virtual PValue Copy (void) const; + virtual std::string SerializeToString (Ptr spec) const; + virtual bool DeserializeFromString (std::string value, Ptr spec); + + IntValue (PValue value); + operator PValue () const; +private: + int64_t m_value; +}; + +template +Ptr MakeIntParamSpec (U T::*memberVariable, + int64_t initialValue); +template +Ptr MakeIntParamSpec (U T::*memberVariable, + int64_t initialValue, + int64_t minValue, + int64_t maxValue); +template +Ptr MakeIntParamSpec (void (T::*setter) (U), + U (T::*getter) (void) const, + int64_t initialValue); +template +Ptr MakeIntParamSpec (void (T::*setter) (U), + U (T::*getter) (void) const, + int64_t initialValue, + int64_t minValue, + int64_t maxValue); + +} // namespace ns3 + +namespace ns3 { + +class IntValueChecker +{ +public: + IntValueChecker (int64_t minValue, int64_t maxValue); + bool Check (const int64_t &value) const; +private: + int64_t m_minValue; + int64_t m_maxValue; +}; + + +template +Ptr +MakeIntParamSpec (U T::*memberVariable, + int64_t initialValue) +{ + int64_t minValue = std::numeric_limits::min (); + int64_t maxValue = std::numeric_limits::max (); + return MakeMemberVariableParamSpecWithChecker (memberVariable, IntValue (initialValue), + IntValueChecker (minValue, maxValue)); +} + +template +Ptr MakeIntParamSpec (U T::*memberVariable, + int64_t initialValue, + int64_t minValue, + int64_t maxValue) +{ + return MakeMemberVariableParamSpecWithChecker (memberVariable, IntValue (initialValue), + IntValueChecker (minValue, maxValue)); +} +template +Ptr MakeIntParamSpec (void (T::*setter) (U), + U (T::*getter) (void) const, + int64_t initialValue) +{ + int64_t minValue = std::numeric_limits::min (); + int64_t maxValue = std::numeric_limits::max (); + return MakeMemberMethodParamSpecWithChecker (setter, getter, IntValue (initialValue), + IntValueChecker (minValue, maxValue)); +} +template +Ptr MakeIntParamSpec (void (T::*setter) (U), + U (T::*getter) (void) const, + int64_t initialValue, + int64_t minValue, + int64_t maxValue) +{ + return MakeMemberMethodParamSpecWithChecker (setter, getter, IntValue (initialValue), + IntValueChecker (minValue, maxValue)); +} + + +} // namespace ns3 + +#endif /* INT_VALUE_H */ diff --git a/src/core/object.cc b/src/core/object.cc index c15400f86..dac166c7d 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -908,12 +908,13 @@ Object::DoSet (std::string name, PValue value) { return false; } - bool ok = spec->Set (this, value); + bool ok = spec->Check (value); if (!ok) { return false; } - return true; + ok = spec->Set (this, value); + return ok; } bool Object::Set (std::string name, PValue value) @@ -934,12 +935,13 @@ Object::Set (std::string name, std::string value) { return false; } - spec->Set (this, v); + ok = spec->Check (v); if (!ok) { return false; } - return true; + ok = spec->Set (this, v); + return ok; } bool Object::Get (std::string name, std::string &value) const diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index c5de1fd95..5e024893e 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -4,90 +4,202 @@ namespace ns3 { template +Ptr +MakeMemberVariableParamSpec (U T::*memberVariable, V initialValue); + +template +Ptr +MakeMemberMethodParamSpec (void (T::*setter) (U), + U (T::*getter) (void) const, + V initialValue); + + +} // namespace ns3 + + +/*************************************************************** + * The implementation of the above class functions + ***************************************************************/ + +namespace ns3 { + +template class ParamSpecHelper : public ParamSpec { public: - ParamSpecHelper (V initialValue); + ParamSpecHelper (PValue initialValue, CHECKER checker); virtual bool Set (ObjectBase * object, PValue value) const; virtual bool Get (const ObjectBase * object, PValue value) const; virtual bool Check (PValue value) const; virtual PValue CreateInitialValue (void) const; private: - virtual void DoSet (T *object, const V &v) const = 0; - virtual V DoGet (const T *object) const = 0; - virtual bool DoCheck (const V &v) const; - V m_initialValue; + virtual void DoSet (T *object, const U *v) const = 0; + virtual void DoGet (const T *object, U *v) const = 0; + PValue m_initialValue; + CHECKER m_checker; }; -template -class MemberVariableParamSpecHelper : public ParamSpecHelper +template +class ParamSpecHelperSimpleChecker { public: - MemberVariableParamSpecHelper (V T::*memberVariable, V initialValue); -private: - virtual void DoSet (T *object, const V &v) const; - virtual V DoGet (const T *object) const; - - V T::*m_memberVariable; + bool Check (const T &value) const {return true;} }; -template -class MemberMethodParamSpecHelper : public ParamSpecHelper + + +template +Ptr +MakeMemberVariableParamSpecWithChecker (U T::*memberVariable, V initialValue, CHECKER checker) { -public: - MemberMethodParamSpecHelper (void (T::*setter) (V), - V (T::*getter) (void) const, - V initialValue); -private: - virtual void DoSet (T *object, const V &v) const; - virtual V DoGet (const T *object) const; - - void (T::*m_setter) (V); - V (T::*m_getter) (void) const; -}; + class MemberVariable : public ParamSpecHelper + { + public: + MemberVariable (U T::*memberVariable, V initialValue, CHECKER checker) + : ParamSpecHelper (initialValue, checker), + m_memberVariable (memberVariable) + {} + private: + virtual void DoSet (T *object, const V *v) const { + (object->*m_memberVariable) = v->Get (); + } + virtual void DoGet (const T *object, V *v) const { + v->Set (object->*m_memberVariable); + } + + U T::*m_memberVariable; + }; + return Ptr (new MemberVariable (memberVariable, initialValue, checker), false); +} template -class MemberMethodSetterParamSpecHelper : public ParamSpecHelper +Ptr +MakeMemberVariableParamSpec (U T::*memberVariable, V initialValue) { -public: - MemberMethodSetterParamSpecHelper (void (T::*setter) (V), - V initialValue); -private: - virtual void DoSet (T *object, const V &v) const; - virtual V DoGet (const T *object) const; + return MakeMemberVariableParamSpecWithChecker (memberVariable, initialValue, + ParamSpecHelperSimpleChecker ()); +} - void (T::*m_setter) (V); -}; - -template -class MemberMethodGetterParamSpecHelper : public ParamSpecHelper +template +Ptr +MakeMemberMethodParamSpecWithChecker (void (T::*setter) (U), + U (T::*getter) (void) const, + V initialValue, CHECKER checker) { -public: - MemberMethodGetterParamSpecHelper (V (T::*getter) (void) const); -private: - virtual void DoSet (T *object, const V &v) const; - virtual V DoGet (const T *object) const; + class MemberMethod : public ParamSpecHelper + { + public: + MemberMethod (void (T::*setter) (U), + U (T::*getter) (void) const, + V initialValue, CHECKER checker) + : ParamSpecHelper (initialValue, checker), + m_setter (setter), + m_getter (getter) + {} + private: + virtual void DoSet (T *object, const V *v) const { + (object->*m_setter) (v->Get ()); + } + virtual void DoGet (const T *object, V *v) const { + v->Set ((object->*m_getter) ()); + } + void (T::*m_setter) (U); + U (T::*m_getter) (void) const; + }; + return Ptr (new MemberMethod (setter, getter, initialValue, checker), false); +} - V (T::*m_getter) (void) const; -}; - -} // namespace ns3 - - -/*************************************************************** - * The implementation of the above class templates. - ***************************************************************/ - -namespace ns3 { template -ParamSpecHelper::ParamSpecHelper (V initialValue) - : m_initialValue (initialValue) +Ptr +MakeMemberMethodParamSpec (void (T::*setter) (U), + U (T::*getter) (void) const, + V initialValue) +{ + return MakeMemberMethodParamSpecWithChecker (setter, getter, initialValue, + ParamSpecHelperSimpleChecker ()); +} + + +template +Ptr +MakeMemberMethodGetterParamSpecWithChecker (U (T::*getter) (void) const, + V initialValue, CHECKER checker) +{ + class MemberMethod : public ParamSpecHelper + { + public: + MemberMethod (U (T::*getter) (void) const, + V initialValue, CHECKER checker) + : ParamSpecHelper (initialValue, checker), + m_getter (getter) + {} + private: + virtual void DoSet (T *object, const V *v) const {} + virtual void DoGet (const T *object, V *v) const { + v->Set ((object->*m_getter) ()); + } + U (T::*m_getter) (void) const; + }; + return Ptr (new MemberMethod (getter, initialValue, checker), false); +} + + +template +Ptr +MakeMemberMethodGetterParamSpec (U (T::*getter) (void) const, + V initialValue) +{ + return MakeMemberMethodParamSpecWithChecker (getter, initialValue, + ParamSpecHelperSimpleChecker ()); +} + + +template +Ptr +MakeMemberMethodSetterParamSpecWithChecker (void (T::*setter) (U), + V initialValue, CHECKER checker) +{ + class MemberMethod : public ParamSpecHelper + { + public: + MemberMethod (void (T::*setter) (U), + V initialValue, CHECKER checker) + : ParamSpecHelper (initialValue, checker), + m_setter (setter) + {} + private: + virtual void DoSet (T *object, const V *v) const { + (object->*m_setter) (v->Get ()); + } + virtual void DoGet (const T *object, V *v) const { + v->Set ((object->*m_setter) ()); + } + void (T::*m_setter) (U); + }; + return Ptr (new MemberMethod (setter, initialValue, checker), false); +} + + +template +Ptr +MakeMemberMethodParamSpec (void (T::*setter) (U), + V initialValue) +{ + return MakeMemberMethodSetterParamSpecWithChecker (setter, initialValue, + ParamSpecHelperSimpleChecker ()); +} + + +template +ParamSpecHelper::ParamSpecHelper (PValue initialValue, CHECKER checker) + : m_initialValue (initialValue), + m_checker (checker) {} -template +template bool -ParamSpecHelper::Set (ObjectBase * object, PValue val) const +ParamSpecHelper::Set (ObjectBase * object, PValue val) const { const U *value = val.DynCast (); if (value == 0) @@ -99,13 +211,12 @@ ParamSpecHelper::Set (ObjectBase * object, PValue val) const { return false; } - V v = value->Get (); - DoSet (obj, v); + DoSet (obj, value); return true; } -template +template bool -ParamSpecHelper::Get (const ObjectBase * object, PValue val) const +ParamSpecHelper::Get (const ObjectBase * object, PValue val) const { U *value = val.DynCast (); if (value == 0) @@ -117,111 +228,27 @@ ParamSpecHelper::Get (const ObjectBase * object, PValue val) const { return false; } - V v = DoGet (obj); - value->Set (v); + DoGet (obj, value); return true; } -template +template bool -ParamSpecHelper::Check (PValue value) const +ParamSpecHelper::Check (PValue value) const { const U *val = value.DynCast (); if (val == 0) { return false; } - V v = val->Get (); - DoCheck (v); - return true; + return m_checker.Check (val->Get ()); } -template -bool -ParamSpecHelper::DoCheck (const V &v) const -{ - return true; -} -template +template PValue -ParamSpecHelper::CreateInitialValue (void) const +ParamSpecHelper::CreateInitialValue (void) const { - return PValue::Create (m_initialValue); + return m_initialValue; } - -template -MemberVariableParamSpecHelper::MemberVariableParamSpecHelper (V T::*memberVariable, V initialValue) - : ParamSpecHelper (initialValue), - m_memberVariable (memberVariable) -{} -template -void -MemberVariableParamSpecHelper::DoSet (T *object, const V &v) const -{ - (object->*m_memberVariable) = v; -} -template -V -MemberVariableParamSpecHelper::DoGet (const T *object) const -{ - return (object->*m_memberVariable); -} - -template -MemberMethodParamSpecHelper::MemberMethodParamSpecHelper (void (T::*setter) (V), - V (T::*getter) (void) const, - V initialValue) - : ParamSpecHelper (initialValue), - m_setter (setter), - m_getter (getter) -{} -template -void -MemberMethodParamSpecHelper::DoSet (T *object, const V &v) const -{ - (object->*m_setter) (v); -} -template -V -MemberMethodParamSpecHelper::DoGet (const T *object) const -{ - return (object->*m_getter) (); -} - -template -MemberMethodSetterParamSpecHelper::MemberMethodSetterParamSpecHelper (void (T::*setter) (V), - V initialValue) - : ParamSpecHelper (initialValue), - m_setter (setter) -{} -template -void -MemberMethodSetterParamSpecHelper::DoSet (T *object, const V &v) const -{ - (object->m_setter) (v); -} -template -V -MemberMethodSetterParamSpecHelper::DoGet (const T *object) const -{ - return V (); -} - -template -MemberMethodGetterParamSpecHelper::MemberMethodGetterParamSpecHelper (V (T::*getter) (void) const) - : m_getter (getter) -{} -template -void -MemberMethodGetterParamSpecHelper::DoSet (T *object, const V &v) const -{} -template -V -MemberMethodGetterParamSpecHelper::DoGet (const T *object) const -{ - return (object->*m_getter) (); -} - - } // namespace ns3 #endif /* PARAM_SPEC_HELPER_H */ diff --git a/src/core/value-test.cc b/src/core/value-test.cc index 993a2de2e..c1786539f 100644 --- a/src/core/value-test.cc +++ b/src/core/value-test.cc @@ -2,8 +2,8 @@ #include "test.h" #include "object.h" #include "boolean-value.h" -#if 0 #include "int-value.h" +#if 0 #include "uint-value.h" #include "fp-value.h" #include "enum-value.h" @@ -48,15 +48,17 @@ public: false)) .AddParameter ("TestPtr", "help text", MakePtrParamSpec (&ParamSpecObjectTest::m_derived)) -#if 0 .AddParameter ("TestInt16", "help text", - MakeIntParamSpec (-2, &ParamSpecObjectTest::m_int16)) + MakeIntParamSpec (&ParamSpecObjectTest::m_int16, + -2)) .AddParameter ("TestInt16WithBounds", "help text", - MakeIntParamSpec (-2, -5, 10, - &ParamSpecObjectTest::m_int16WithBounds)) + MakeIntParamSpec (&ParamSpecObjectTest::m_int16WithBounds, + -2, -5, 10)) .AddParameter ("TestInt16SetGet", "help text", - MakeIntParamSpec (6, &ParamSpecObjectTest::DoSetInt16, - &ParamSpecObjectTest::DoGetInt16)) + MakeIntParamSpec (&ParamSpecObjectTest::DoSetInt16, + &ParamSpecObjectTest::DoGetInt16, + 6)) +#if 0 .AddParameter ("TestUint8", "help text", MakeUintParamSpec (1, &ParamSpecObjectTest::m_uint8)) .AddParameter ("TestFloat", "help text", @@ -175,7 +177,6 @@ ParamSpecTest::RunTests (void) derived = p->Get ("TestPtr"); NS_TEST_ASSERT (derived != 0); -#if 0 CHECK_GET_STR (p, "TestInt16", "-2"); CHECK_GET_PARAM (p, "TestInt16", IntValue, -2); @@ -222,7 +223,8 @@ ParamSpecTest::RunTests (void) NS_TEST_ASSERT (p->Set ("TestInt16SetGet", IntValue (0))); CHECK_GET_STR (p, "TestInt16SetGet", "0"); CHECK_GET_PARAM (p, "TestInt16SetGet", IntValue, 0); - + +#if 0 CHECK_GET_STR (p, "TestUint8", "1"); CHECK_GET_PARAM (p, "TestUint8", UintValue, 1); NS_TEST_ASSERT (p->Set ("TestUint8", UintValue (0))); diff --git a/src/core/wscript b/src/core/wscript index 7105cae14..2c51687d7 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -56,6 +56,7 @@ def build(bld): 'value.cc', 'boolean-value.cc', 'value-test.cc', + 'int-value.cc', ] if sys.platform == 'win32': From eeaae105d6279791d47d6fcbfc51254cc5c50209 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 4 Feb 2008 22:19:20 +0100 Subject: [PATCH 007/130] weird leftover from the ClassId changes. --- utils/mobility-visualizer-model.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils/mobility-visualizer-model.cc b/utils/mobility-visualizer-model.cc index aaa3fae63..2abec2202 100644 --- a/utils/mobility-visualizer-model.cc +++ b/utils/mobility-visualizer-model.cc @@ -14,6 +14,7 @@ #include "ns3/node.h" #include "ns3/node-list.h" #include "ns3/rectangle-default-value.h" +#include "ns3/type-id-default-value.h" #include "mobility-visualizer.h" @@ -100,7 +101,7 @@ int model_init (int argc, char *argv[], double *x1, double *y1, double *x2, doub Simulator::Schedule (g_sampleInterval, Sample); - ClassId mobType = DefaultValueListGet ("RandomTopologyMobilityType")->GetValue (); + TypeId mobType = DefaultValueListGet ("RandomTopologyMobilityType")->GetValue (); if (mobType.GetName () == "RandomWalk2dMobilityModel") { Rectangle bounds = DefaultValueListGet ("RandomWalk2dBounds")->GetValue (); From ff9e4a98033560ed68a60d41303b946381275764 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 4 Feb 2008 22:32:13 +0100 Subject: [PATCH 008/130] remove un-needed object.h include. --- src/core/int-value.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/int-value.cc b/src/core/int-value.cc index 2ea001a61..2d3ea6dfa 100644 --- a/src/core/int-value.cc +++ b/src/core/int-value.cc @@ -1,5 +1,5 @@ #include "int-value.h" -#include "object.h" +#include "fatal-error.h" #include namespace ns3 { From 91dc6ddb2de119f4af0534566e00e173df924160 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 4 Feb 2008 22:32:21 +0100 Subject: [PATCH 009/130] add back UintValue --- src/core/value-test.cc | 10 +++++----- src/core/wscript | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/core/value-test.cc b/src/core/value-test.cc index c1786539f..8ae421e4e 100644 --- a/src/core/value-test.cc +++ b/src/core/value-test.cc @@ -3,8 +3,8 @@ #include "object.h" #include "boolean-value.h" #include "int-value.h" -#if 0 #include "uint-value.h" +#if 0 #include "fp-value.h" #include "enum-value.h" #endif @@ -58,9 +58,10 @@ public: MakeIntParamSpec (&ParamSpecObjectTest::DoSetInt16, &ParamSpecObjectTest::DoGetInt16, 6)) -#if 0 .AddParameter ("TestUint8", "help text", - MakeUintParamSpec (1, &ParamSpecObjectTest::m_uint8)) + MakeUintParamSpec (&ParamSpecObjectTest::m_uint8, + 1)) +#if 0 .AddParameter ("TestFloat", "help text", MakeFpParamSpec (-1.1, &ParamSpecObjectTest::m_float)) .AddParameter ("TestEnum", "help text", @@ -224,7 +225,6 @@ ParamSpecTest::RunTests (void) CHECK_GET_STR (p, "TestInt16SetGet", "0"); CHECK_GET_PARAM (p, "TestInt16SetGet", IntValue, 0); -#if 0 CHECK_GET_STR (p, "TestUint8", "1"); CHECK_GET_PARAM (p, "TestUint8", UintValue, 1); NS_TEST_ASSERT (p->Set ("TestUint8", UintValue (0))); @@ -245,7 +245,7 @@ ParamSpecTest::RunTests (void) NS_TEST_ASSERT (!p->Set ("TestUint8", UintValue (-1))); CHECK_GET_STR (p, "TestUint8", "255"); CHECK_GET_PARAM (p, "TestUint8", UintValue, 255); - +#if 0 CHECK_GET_STR (p, "TestFloat", "-1.1"); NS_TEST_ASSERT (p->Set ("TestFloat", FpValue ((float)+2.3))); CHECK_GET_PARAM (p, "TestFloat", FpValue, (float)+2.3); diff --git a/src/core/wscript b/src/core/wscript index 2c51687d7..5f440ecbd 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -57,6 +57,7 @@ def build(bld): 'boolean-value.cc', 'value-test.cc', 'int-value.cc', + 'uint-value.cc', ] if sys.platform == 'win32': From eda53e48a7ba44a4b03b7dfe7872b524bcb1dc1a Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 4 Feb 2008 22:47:26 +0100 Subject: [PATCH 010/130] forgot to add new files --- src/core/uint-value.cc | 75 ++++++++++++++++++++++++++++ src/core/uint-value.h | 108 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 src/core/uint-value.cc create mode 100644 src/core/uint-value.h 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 */ From c82d9bfe06cb3adfc282f16af2e5d69b72d02d2c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 4 Feb 2008 22:48:26 +0100 Subject: [PATCH 011/130] split ParamSpec::CreateInitialValue -> ParamSpec::GetInitialValue + ParamSpec::CreateValue --- src/core/object.cc | 12 ++++++------ src/core/param-spec-helper.h | 12 ++++++++++-- src/core/value.h | 8 ++++++-- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/core/object.cc b/src/core/object.cc index dac166c7d..e608b181f 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -694,7 +694,7 @@ Parameters::DoSet (Ptr spec, std::string value) { return false; } - PValue v = spec->CreateInitialValue (); + PValue v = spec->CreateValue (); bool ok = v.DeserializeFromString (value, spec); if (!ok) { @@ -790,7 +790,7 @@ Parameters::DeserializeFromString (std::string str) value = str.substr (equal+1, next - (equal+1)); cur++; } - PValue val = spec->CreateInitialValue (); + PValue val = spec->CreateValue (); bool ok = val.DeserializeFromString (value, spec); if (!ok) { @@ -890,7 +890,7 @@ Object::Construct (const Parameters ¶meters) if (!found) { // No matching parameter value so we set the default value. - PValue initial = paramSpec->CreateInitialValue (); + PValue initial = paramSpec->GetInitialValue (); paramSpec->Set (this, initial); NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\" from local"); @@ -929,7 +929,7 @@ Object::Set (std::string name, std::string value) { return false; } - PValue v = spec->CreateInitialValue (); + PValue v = spec->CreateValue (); bool ok = v.DeserializeFromString (value, spec); if (!ok) { @@ -951,7 +951,7 @@ Object::Get (std::string name, std::string &value) const { return false; } - PValue v = paramSpec->CreateInitialValue (); + PValue v = paramSpec->CreateValue (); bool ok = paramSpec->Get (this, v); if (ok) { @@ -968,7 +968,7 @@ Object::Get (std::string name) const { return PValue (); } - PValue value = paramSpec->CreateInitialValue (); + PValue value = paramSpec->CreateValue (); bool ok = paramSpec->Get (this, value); if (!ok) { diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index 5e024893e..77851e632 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -31,7 +31,8 @@ public: virtual bool Set (ObjectBase * object, PValue value) const; virtual bool Get (const ObjectBase * object, PValue value) const; virtual bool Check (PValue value) const; - virtual PValue CreateInitialValue (void) const; + virtual PValue GetInitialValue (void) const; + virtual PValue CreateValue (void) const; private: virtual void DoSet (T *object, const U *v) const = 0; @@ -244,11 +245,18 @@ ParamSpecHelper::Check (PValue value) const } template PValue -ParamSpecHelper::CreateInitialValue (void) const +ParamSpecHelper::GetInitialValue (void) const { return m_initialValue; } +template +PValue +ParamSpecHelper::CreateValue (void) const +{ + return m_initialValue.Copy (); +} + } // namespace ns3 #endif /* PARAM_SPEC_HELPER_H */ diff --git a/src/core/value.h b/src/core/value.h index 652d5e1f1..db5bf22b6 100644 --- a/src/core/value.h +++ b/src/core/value.h @@ -74,7 +74,8 @@ public: virtual bool Set (ObjectBase * object, PValue value) const = 0; virtual bool Get (const ObjectBase * object, PValue value) const = 0; virtual bool Check (PValue value) const = 0; - virtual PValue CreateInitialValue (void) const = 0; + virtual PValue GetInitialValue (void) const = 0; + virtual PValue CreateValue (void) const = 0; private: mutable uint32_t m_count; }; @@ -246,7 +247,10 @@ public: } return true; } - virtual PValue CreateInitialValue (void) const { + virtual PValue GetInitialValue (void) const { + return CreateValue (); + } + virtual PValue CreateValue (void) const { return PValue::Create > (Ptr (0)); } private: From ac9843d8ff9208fbc4cb467763b6615d411d9771 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 4 Feb 2008 23:13:54 +0100 Subject: [PATCH 012/130] add back EnumValue --- src/core/enum-value.cc | 145 ++++++++++++++++++++++++++++++++++ src/core/enum-value.h | 172 +++++++++++++++++++++++++++++++++++++++++ src/core/value-test.cc | 11 ++- src/core/wscript | 1 + 4 files changed, 323 insertions(+), 6 deletions(-) create mode 100644 src/core/enum-value.cc create mode 100644 src/core/enum-value.h diff --git a/src/core/enum-value.cc b/src/core/enum-value.cc new file mode 100644 index 000000000..8cc1c110a --- /dev/null +++ b/src/core/enum-value.cc @@ -0,0 +1,145 @@ +#include "enum-value.h" +#include "fatal-error.h" +#include + +namespace ns3 { + + +EnumValue::EnumValue (int v) + : m_v (v) +{} +void +EnumValue::Set (int v) +{ + m_v = v; +} +int +EnumValue::Get (void) const +{ + return m_v; +} + +PValue +EnumValue::Copy (void) const +{ + return PValue::Create (*this); +} +std::string +EnumValue::SerializeToString (Ptr spec) const +{ + const EnumParamSpec *p = dynamic_cast (PeekPointer (spec)); + NS_ASSERT (p != 0); + for (EnumParamSpec::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++) + { + if (i->first == m_v) + { + return i->second; + } + } + + NS_FATAL_ERROR ("The user has set an invalid C++ value in this EnumValue"); + // quiet compiler. + return ""; +} +bool +EnumValue::DeserializeFromString (std::string value, Ptr spec) +{ + const EnumParamSpec *p = dynamic_cast (PeekPointer (spec)); + NS_ASSERT (p != 0); + for (EnumParamSpec::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++) + { + if (i->second == value) + { + m_v = i->first; + return true; + } + } + return false; +} + +EnumValue::EnumValue (PValue value) +{ + const EnumValue *v = value.DynCast (); + if (v == 0) + { + NS_FATAL_ERROR ("assigning non-Enum value to Enum value."); + } + m_v = v->m_v; +} +EnumValue::operator PValue () const +{ + return PValue::Create (*this); +} + + + +EnumParamSpec::EnumParamSpec () +{} + +void +EnumParamSpec::AddDefault (int v, std::string name) +{ + m_valueSet.push_front (std::make_pair (v, name)); +} +void +EnumParamSpec::Add (int v, std::string name) +{ + m_valueSet.push_back (std::make_pair (v, name)); +} + +bool +EnumParamSpec::Set (ObjectBase * object, PValue value) const +{ + const EnumValue *p = value.DynCast (); + if (p == 0) + { + return false; + } + DoSet (object, p); + return true; +} +bool +EnumParamSpec::Get (const ObjectBase * object, PValue value) const +{ + EnumValue *p = value.DynCast (); + if (p == 0) + { + return false; + } + DoGet (object, p); + return true; +} +bool +EnumParamSpec::Check (PValue value) const +{ + const EnumValue *p = value.DynCast (); + if (p == 0) + { + return false; + } + for (ValueSet::const_iterator i = m_valueSet.begin (); i != m_valueSet.end (); i++) + { + if (i->first == p->Get ()) + { + return true; + } + } + return false; +} + +PValue +EnumParamSpec::GetInitialValue (void) const +{ + NS_ASSERT (!m_valueSet.empty ()); + return PValue::Create (m_valueSet.front ().first); +} + +PValue +EnumParamSpec::CreateValue (void) const +{ + return PValue::Create (m_valueSet.front ().first); +} + + + +} // namespace ns3 diff --git a/src/core/enum-value.h b/src/core/enum-value.h new file mode 100644 index 000000000..65361f819 --- /dev/null +++ b/src/core/enum-value.h @@ -0,0 +1,172 @@ +#ifndef ENUM_VALUE_H +#define ENUM_VALUE_H + +#include "value.h" +#include "param-spec-helper.h" +#include + +namespace ns3 { + +class EnumValue : public Value +{ +public: + EnumValue (int v); + void Set (int v); + int Get (void) const; + + virtual PValue Copy (void) const; + virtual std::string SerializeToString (Ptr spec) const; + virtual bool DeserializeFromString (std::string value, Ptr spec); + + EnumValue (PValue value); + operator PValue () const; +private: + int m_v; +}; + +class EnumParamSpec : public ParamSpec +{ +public: + EnumParamSpec (); + + void AddDefault (int v, std::string name); + void Add (int v, std::string name); + + virtual bool Set (ObjectBase* object, PValue value) const; + virtual bool Get (const ObjectBase* object, PValue value) const; + virtual bool Check (PValue value) const; + + virtual PValue GetInitialValue (void) const; + virtual PValue CreateValue (void) const; +private: + virtual bool DoSet (ObjectBase *object, const EnumValue *value) const = 0; + virtual bool DoGet (const ObjectBase *object, EnumValue *value) const = 0; + friend class EnumValue; + typedef std::list > ValueSet; + ValueSet m_valueSet; +}; + +template +Ptr MakeEnumParamSpec (U T::*v, + int v1, std::string n1, + int v2 = 0, std::string n2 = "", + int v3 = 0, std::string n3 = "", + int v4 = 0, std::string n4 = "", + int v5 = 0, std::string n5 = "", + int v6 = 0, std::string n6 = "", + int v7 = 0, std::string n7 = "", + int v8 = 0, std::string n8 = "", + int v9 = 0, std::string n9 = "", + int v10 = 0, std::string n10 = "", + int v11 = 0, std::string n11 = "", + int v12 = 0, std::string n12 = ""); + + +} // namespace ns3 + +namespace ns3 { + +template +Ptr MakeEnumParamSpec (U T::*v, + int v1, std::string n1, + int v2 = 0, std::string n2 = "", + int v3 = 0, std::string n3 = "", + int v4 = 0, std::string n4 = "", + int v5 = 0, std::string n5 = "", + int v6 = 0, std::string n6 = "", + int v7 = 0, std::string n7 = "", + int v8 = 0, std::string n8 = "", + int v9 = 0, std::string n9 = "", + int v10 = 0, std::string n10 = "", + int v11 = 0, std::string n11 = "", + int v12 = 0, std::string n12 = "") +{ + class MemberVariable : public EnumParamSpec + { + private: + U T::*m_memberVariable; + public: + MemberVariable (U T::*memberVariable) + : m_memberVariable (memberVariable) {} + virtual bool DoSet (ObjectBase *object, const EnumValue *value) const { + T *o = dynamic_cast (object); + if (o == 0) + { + return false; + } + (o->*m_memberVariable) = U (value->Get ()); + return true; + } + virtual bool DoGet (const ObjectBase *object, EnumValue *value) const { + const T *o = dynamic_cast (object); + if (o == 0) + { + return false; + } + value->Set (o->*m_memberVariable); + return true; + } + }; + Ptr spec = Ptr (new MemberVariable (v), false); + spec->AddDefault (v1, n1); + if (n2 == "") + { + return spec; + } + spec->Add (v2, n2); + if (n3 == "") + { + return spec; + } + spec->Add (v3, n3); + if (n4 == "") + { + return spec; + } + spec->Add (v4, n4); + if (n5 == "") + { + return spec; + } + spec->Add (v5, n5); + if (n6 == "") + { + return spec; + } + spec->Add (v6, n6); + if (n7 == "") + { + return spec; + } + spec->Add (v7, n7); + if (n8 == "") + { + return spec; + } + spec->Add (v8, n8); + if (n9 == "") + { + return spec; + } + spec->Add (v9, n9); + if (n10 == "") + { + return spec; + } + spec->Add (v10, n10); + if (n11 == "") + { + return spec; + } + spec->Add (v11, n11); + if (n12 == "") + { + return spec; + } + spec->Add (v12, n12); + return spec; +} + +} // namespace ns3 + +#endif /* ENUM_VALUE_H */ diff --git a/src/core/value-test.cc b/src/core/value-test.cc index 8ae421e4e..c7d8fe24d 100644 --- a/src/core/value-test.cc +++ b/src/core/value-test.cc @@ -4,9 +4,9 @@ #include "boolean-value.h" #include "int-value.h" #include "uint-value.h" +#include "enum-value.h" #if 0 #include "fp-value.h" -#include "enum-value.h" #endif namespace ns3 { @@ -61,14 +61,14 @@ public: .AddParameter ("TestUint8", "help text", MakeUintParamSpec (&ParamSpecObjectTest::m_uint8, 1)) -#if 0 - .AddParameter ("TestFloat", "help text", - MakeFpParamSpec (-1.1, &ParamSpecObjectTest::m_float)) .AddParameter ("TestEnum", "help text", MakeEnumParamSpec (&ParamSpecObjectTest::m_enum, TEST_A, "TestA", TEST_B, "TestB", TEST_C, "TestC")) +#if 0 + .AddParameter ("TestFloat", "help text", + MakeFpParamSpec (-1.1, &ParamSpecObjectTest::m_float)) #endif ; @@ -249,7 +249,7 @@ ParamSpecTest::RunTests (void) CHECK_GET_STR (p, "TestFloat", "-1.1"); NS_TEST_ASSERT (p->Set ("TestFloat", FpValue ((float)+2.3))); CHECK_GET_PARAM (p, "TestFloat", FpValue, (float)+2.3); - +#endif CHECK_GET_STR (p, "TestEnum", "TestA"); CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_A); NS_TEST_ASSERT (p->Set ("TestEnum", EnumValue (ParamSpecObjectTest::TEST_C))); @@ -264,7 +264,6 @@ ParamSpecTest::RunTests (void) NS_TEST_ASSERT (!p->Set ("TestEnum", EnumValue (5))); CHECK_GET_STR (p, "TestEnum", "TestB"); CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_B); -#endif #if 0 p->Set ("TestBoolName", "true"); diff --git a/src/core/wscript b/src/core/wscript index 5f440ecbd..95cb7076a 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -58,6 +58,7 @@ def build(bld): 'value-test.cc', 'int-value.cc', 'uint-value.cc', + 'enum-value.cc', ] if sys.platform == 'win32': From 18df50f58d888e9e30899a58fc795bac4d4e9a0c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 6 Feb 2008 18:31:15 +0100 Subject: [PATCH 013/130] enforce TypeId::PARAM_* flags --- src/core/object.cc | 173 ++++++++++++++++++++++++++------------------- src/core/object.h | 24 ++++--- 2 files changed, 113 insertions(+), 84 deletions(-) diff --git a/src/core/object.cc b/src/core/object.cc index e608b181f..ef051223a 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -385,8 +385,8 @@ TypeId::LookupByName (std::string name) NS_ASSERT (uid != 0); return TypeId (uid); } -Ptr -TypeId::LookupParamSpecByFullName (std::string fullName) +bool +TypeId::LookupParameterByFullName (std::string fullName, struct TypeId::ParameterInfo *info) { std::string::size_type pos = fullName.find ("::"); if (pos == std::string::npos) @@ -396,7 +396,7 @@ TypeId::LookupParamSpecByFullName (std::string fullName) std::string tidName = fullName.substr (0, pos); std::string paramName = fullName.substr (pos+2, fullName.size () - (pos+2)); TypeId tid = LookupByName (tidName); - return tid.LookupParamSpecByName (paramName); + return tid.LookupParameterByName (paramName, info); } uint32_t TypeId::GetRegisteredN (void) @@ -409,21 +409,29 @@ TypeId::GetRegistered (uint32_t i) return TypeId (Singleton::Get ()->GetRegistered (i)); } -Ptr -TypeId::LookupParamSpecByName (std::string name) const +bool +TypeId::LookupParameterByName (std::string name, struct TypeId::ParameterInfo *info) const { - for (uint32_t i = 0; i < GetParametersN (); i++) - { - std::string paramName = GetParameterName (i); - if (paramName == name) - { - return GetParameterParamSpec (i); - } - } - return 0; + TypeId tid = TypeId (0); + TypeId nextTid = *this; + do { + tid = nextTid; + for (uint32_t i = 0; i < GetParametersN (); i++) + { + std::string paramName = GetParameterName (i); + if (paramName == name) + { + info->spec = GetParameterParamSpec (i); + info->flags = GetParameterFlags (i); + return true; + } + } + nextTid = tid.GetParent (); + } while (nextTid != tid); + return false; } -Ptr -TypeId::LookupParamSpecByPosition (uint32_t i) const +bool +TypeId::LookupParameterByPosition (uint32_t i, struct TypeId::ParameterInfo *info) const { uint32_t cur = 0; TypeId tid = TypeId (0); @@ -434,13 +442,15 @@ TypeId::LookupParamSpecByPosition (uint32_t i) const { if (cur == i) { - return tid.GetParameterParamSpec (j); + info->spec = tid.GetParameterParamSpec (j); + info->flags = tid.GetParameterFlags (j); + return true; } cur++; } nextTid = tid.GetParent (); } while (nextTid != tid); - return 0; + return false; } @@ -506,7 +516,7 @@ TypeId::AddParameter (std::string name, std::string help, Ptr param) { - Singleton::Get ()->AddParameter (m_tid, name, help, 0, param); + Singleton::Get ()->AddParameter (m_tid, name, help, PARAM_SGC, param); return *this; } @@ -567,6 +577,13 @@ TypeId::GetParameterParamSpec (uint32_t i) const Ptr param = Singleton::Get ()->GetParameterParamSpec (m_tid, i); return param; } +uint32_t +TypeId::GetParameterFlags (uint32_t i) const +{ + // Used exclusively by the Object class. + uint32_t flags = Singleton::Get ()->GetParameterFlags (m_tid, i); + return flags; +} bool operator == (TypeId a, TypeId b) @@ -617,40 +634,46 @@ Parameters::~Parameters () bool Parameters::Set (std::string name, PValue value) { - Ptr spec = TypeId::LookupParamSpecByFullName (name); - bool ok = DoSet (spec, value); + struct TypeId::ParameterInfo info; + TypeId::LookupParameterByFullName (name, &info); + bool ok = DoSet (info.spec, value); return ok; } bool Parameters::Set (std::string name, std::string value) { - Ptr spec = TypeId::LookupParamSpecByFullName (name); - bool ok = DoSet (spec,value); + struct TypeId::ParameterInfo info; + TypeId::LookupParameterByFullName (name, &info); + bool ok = DoSet (info.spec,value); return ok; } void Parameters::SetWithTid (TypeId tid, std::string name, std::string value) { - Ptr spec = tid.LookupParamSpecByName (name); - DoSet (spec, value); + struct TypeId::ParameterInfo info; + tid.LookupParameterByName (name, &info); + DoSet (info.spec, value); } void Parameters::SetWithTid (TypeId tid, std::string name, PValue value) { - Ptr spec = tid.LookupParamSpecByName (name); - DoSet (spec, value); + struct TypeId::ParameterInfo info; + tid.LookupParameterByName (name, &info); + DoSet (info.spec, value); } void Parameters::SetWithTid (TypeId tid, uint32_t position, std::string value) { - Ptr spec = tid.LookupParamSpecByPosition (position); - DoSet (spec, value); + struct TypeId::ParameterInfo info; + tid.LookupParameterByPosition (position, &info); + DoSet (info.spec, value); } void Parameters::SetWithTid (TypeId tid, uint32_t position, PValue value) { - Ptr spec = tid.LookupParamSpecByPosition (position); - DoSet (spec, value); + struct TypeId::ParameterInfo info; + tid.LookupParameterByPosition (position, &info); + DoSet (info.spec, value); } void @@ -770,8 +793,8 @@ Parameters::DeserializeFromString (std::string str) else { std::string name = str.substr (cur, equal-cur); - Ptr spec = TypeId::LookupParamSpecByFullName (name); - if (spec == 0) + struct TypeId::ParameterInfo info; + if (!TypeId::LookupParameterByFullName (name, &info)) { // XXX invalid name. break; @@ -790,8 +813,8 @@ Parameters::DeserializeFromString (std::string str) value = str.substr (equal+1, next - (equal+1)); cur++; } - PValue val = spec->CreateValue (); - bool ok = val.DeserializeFromString (value, spec); + PValue val = info.spec->CreateValue (); + bool ok = val.DeserializeFromString (value, info.spec); if (!ok) { // XXX invalid value @@ -799,7 +822,7 @@ Parameters::DeserializeFromString (std::string str) } else { - DoSetOne (spec, val); + DoSetOne (info.spec, val); } } } @@ -855,6 +878,10 @@ Object::Construct (const Parameters ¶meters) Ptr paramSpec = tid.GetParameterParamSpec (i); NS_LOG_DEBUG ("try to construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\""); + if (!(tid.GetParameterFlags (i) & TypeId::PARAM_CONSTRUCT)) + { + continue; + } bool found = false; // is this parameter stored in this Parameters instance ? for (Parameters::Params::const_iterator j = parameters.m_parameters.begin (); @@ -901,61 +928,68 @@ Object::Construct (const Parameters ¶meters) NotifyConstructionCompleted (); } bool -Object::DoSet (std::string name, PValue value) +Object::Set (std::string name, PValue value) { - Ptr spec = m_tid.LookupParamSpecByName (name); - if (spec == 0) + struct TypeId::ParameterInfo info; + if (!m_tid.LookupParameterByName (name, &info)) { return false; } - bool ok = spec->Check (value); + if (!(info.flags & TypeId::PARAM_SET)) + { + return false; + } + bool ok = info.spec->Check (value); if (!ok) { return false; } - ok = spec->Set (this, value); + ok = info.spec->Set (this, value); return ok; } bool -Object::Set (std::string name, PValue value) -{ - return DoSet (name, value); -} -bool Object::Set (std::string name, std::string value) { - Ptr spec = m_tid.LookupParamSpecByName (name); - if (spec == 0) + struct TypeId::ParameterInfo info; + if (!m_tid.LookupParameterByName (name, &info)) { return false; } - PValue v = spec->CreateValue (); - bool ok = v.DeserializeFromString (value, spec); + if (!(info.flags & TypeId::PARAM_SET)) + { + return false; + } + PValue v = info.spec->CreateValue (); + bool ok = v.DeserializeFromString (value, info.spec); if (!ok) { return false; } - ok = spec->Check (v); + ok = info.spec->Check (v); if (!ok) { return false; } - ok = spec->Set (this, v); + ok = info.spec->Set (this, v); return ok; } bool Object::Get (std::string name, std::string &value) const { - Ptr paramSpec = m_tid.LookupParamSpecByName (name); - if (paramSpec == 0) + struct TypeId::ParameterInfo info; + if (!m_tid.LookupParameterByName (name, &info)) { return false; } - PValue v = paramSpec->CreateValue (); - bool ok = paramSpec->Get (this, v); + if (!(info.flags & TypeId::PARAM_GET)) + { + return false; + } + PValue v = info.spec->CreateValue (); + bool ok = info.spec->Get (this, v); if (ok) { - value = v.SerializeToString (paramSpec); + value = v.SerializeToString (info.spec); } return ok; } @@ -963,13 +997,17 @@ Object::Get (std::string name, std::string &value) const PValue Object::Get (std::string name) const { - Ptr paramSpec = m_tid.LookupParamSpecByName (name); - if (paramSpec == 0) + struct TypeId::ParameterInfo info; + if (!m_tid.LookupParameterByName (name, &info)) { return PValue (); } - PValue value = paramSpec->CreateValue (); - bool ok = paramSpec->Get (this, value); + if (!(info.flags & TypeId::PARAM_GET)) + { + return PValue (); + } + PValue value = info.spec->CreateValue (); + bool ok = info.spec->Get (this, value); if (!ok) { return PValue (); @@ -977,19 +1015,6 @@ Object::Get (std::string name) const return value; } -bool -Object::DoGet (std::string name, PValue parameter) const -{ - Ptr paramSpec = m_tid.LookupParamSpecByName (name); - if (paramSpec == 0) - { - return false; - } - bool ok = paramSpec->Get (this, parameter); - return ok; -} - - Ptr Object::DoQueryInterface (TypeId tid) const { diff --git a/src/core/object.h b/src/core/object.h index a7cf37d66..7da6ae27c 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -61,12 +61,12 @@ class TypeId { public: enum { - PARAM_READ = 1<<0, - PARAM_WRITE = 1<<1, + PARAM_GET = 1<<0, + PARAM_SET = 1<<1, PARAM_CONSTRUCT = 1<<2, - PARAM_RWC = PARAM_READ | PARAM_WRITE | PARAM_CONSTRUCT, - PARAM_NO_READ = 0, - PARAM_NO_WRITE = 0, + PARAM_SGC = PARAM_GET | PARAM_SET | PARAM_CONSTRUCT, + PARAM_NO_GET = 0, + PARAM_NO_SET = 0, PARAM_NO_CONSTRUCT = 0, }; @@ -240,26 +240,32 @@ private: friend bool operator == (TypeId a, TypeId b); friend bool operator != (TypeId a, TypeId b); + struct ParameterInfo { + Ptr spec; + uint32_t flags; + }; + /** * \param name the name of the requested parameter * \returns the ParamSpec associated to the requested parameter */ - Ptr LookupParamSpecByName (std::string name) const; + bool LookupParameterByName (std::string name, struct ParameterInfo *info) const; /** * \param i the position of the requested parameter * \returns the ParamSpec associated to the requested parameter */ - Ptr LookupParamSpecByPosition (uint32_t i) const; + bool LookupParameterByPosition (uint32_t i, struct ParameterInfo *info) const; /** * \param fullName the full name of the requested parameter * \returns the ParamSpec associated to the requested parameter */ - static Ptr LookupParamSpecByFullName (std::string fullName); + static bool LookupParameterByFullName (std::string fullName, struct ParameterInfo *info); explicit TypeId (uint16_t tid); void DoAddConstructor (CallbackBase callback, uint32_t nArguments); CallbackBase LookupConstructor (uint32_t nArguments) const; Ptr GetParameterParamSpec (uint32_t i) const; + uint32_t GetParameterFlags (uint32_t i) const; uint16_t m_tid; }; @@ -473,8 +479,6 @@ private: void DoTraceAll (std::ostream &os, const TraceContext &context) const; bool Check (void) const; bool CheckLoose (void) const; - bool DoSet (std::string name, PValue value); - bool DoGet (std::string name, PValue value) const; /** * Attempt to delete this object. This method iterates * over all aggregated objects to check if they all From b7c68e1e694c8cceda790a480fdcf343099e20ff Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 7 Feb 2008 19:57:21 +0100 Subject: [PATCH 014/130] bug 133: automate RandomVariable memory management. --- src/applications/onoff/onoff-application.cc | 14 +- src/applications/onoff/onoff-application.h | 5 +- src/common/error-model.cc | 12 +- src/common/error-model.h | 4 +- src/core/random-variable-default-value.cc | 14 +- src/core/random-variable-default-value.h | 6 +- src/core/random-variable.cc | 1166 +++++++++++++---- src/core/random-variable.h | 293 +---- src/devices/wifi/propagation-delay-model.cc | 10 +- src/devices/wifi/propagation-delay-model.h | 5 +- src/devices/wifi/propagation-loss-model.cc | 10 +- src/devices/wifi/propagation-loss-model.h | 5 +- .../random-direction-2d-mobility-model.cc | 25 +- .../random-direction-2d-mobility-model.h | 7 +- src/mobility/random-position.cc | 38 +- src/mobility/random-position.h | 11 +- src/mobility/random-walk-2d-mobility-model.cc | 21 +- src/mobility/random-walk-2d-mobility-model.h | 6 +- .../random-waypoint-mobility-model.cc | 22 +- src/mobility/random-waypoint-mobility-model.h | 7 +- src/node/application.cc | 10 +- 21 files changed, 1091 insertions(+), 600 deletions(-) diff --git a/src/applications/onoff/onoff-application.cc b/src/applications/onoff/onoff-application.cc index af5409893..f068ca429 100644 --- a/src/applications/onoff/onoff-application.cc +++ b/src/applications/onoff/onoff-application.cc @@ -91,8 +91,8 @@ OnOffApplication::Construct (Ptr n, m_socket = 0; m_peer = remote; m_connected = false; - m_onTime = onTime.Copy (); - m_offTime = offTime.Copy (); + m_onTime = onTime; + m_offTime = offTime; m_pktSize = size; m_residualBits = 0; m_lastStartTime = Seconds (0); @@ -136,12 +136,6 @@ OnOffApplication::DoDispose (void) NS_LOG_FUNCTION; m_socket = 0; - delete m_onTime; - delete m_offTime; - - m_onTime = 0; - m_offTime = 0; - // chain up Application::DoDispose (); } @@ -222,7 +216,7 @@ void OnOffApplication::ScheduleStartEvent() { // Schedules the event to start sending data (switch to the "On" state) NS_LOG_FUNCTION; - Time offInterval = Seconds(m_offTime->GetValue()); + Time offInterval = Seconds(m_offTime.GetValue()); NS_LOG_LOGIC ("start at " << offInterval); m_startStopEvent = Simulator::Schedule(offInterval, &OnOffApplication::StartSending, this); } @@ -231,7 +225,7 @@ void OnOffApplication::ScheduleStopEvent() { // Schedules the event to stop sending data (switch to "Off" state) NS_LOG_FUNCTION; - Time onInterval = Seconds(m_onTime->GetValue()); + Time onInterval = Seconds(m_onTime.GetValue()); Simulator::Schedule(onInterval, &OnOffApplication::StopSending, this); } diff --git a/src/applications/onoff/onoff-application.h b/src/applications/onoff/onoff-application.h index 31ebb3c08..89a792cc5 100644 --- a/src/applications/onoff/onoff-application.h +++ b/src/applications/onoff/onoff-application.h @@ -30,6 +30,7 @@ #include "ns3/ptr.h" #include "ns3/data-rate.h" #include "ns3/callback-trace-source.h" +#include "ns3/random-variable.h" namespace ns3 { @@ -124,8 +125,8 @@ private: Ptr m_socket; // Associated socket Address m_peer; // Peer address bool m_connected; // True if connected - RandomVariable* m_onTime; // rng for On Time - RandomVariable* m_offTime; // rng for Off Time + RandomVariable m_onTime; // rng for On Time + RandomVariable m_offTime; // rng for Off Time DataRate m_cbrRate; // Rate that data is generated uint32_t m_pktSize; // Size of packets uint32_t m_residualBits; // Number of generated, but not sent, bits diff --git a/src/common/error-model.cc b/src/common/error-model.cc index c512aefb2..bfd18ec87 100644 --- a/src/common/error-model.cc +++ b/src/common/error-model.cc @@ -140,13 +140,12 @@ RateErrorModel::RateErrorModel () : { NS_LOG_FUNCTION; // Assume a uniform random variable if user does not specify - m_ranvar = new UniformVariable (); + m_ranvar = UniformVariable (); } RateErrorModel::~RateErrorModel () { NS_LOG_FUNCTION; - delete m_ranvar; } enum ErrorUnit @@ -181,8 +180,7 @@ void RateErrorModel::SetRandomVariable (const RandomVariable &ranvar) { NS_LOG_FUNCTION; - delete m_ranvar; - m_ranvar = ranvar.Copy (); + m_ranvar = ranvar; } bool @@ -212,7 +210,7 @@ bool RateErrorModel::DoCorruptPkt (Ptr p) { NS_LOG_FUNCTION; - return (m_ranvar->GetValue () < m_rate); + return (m_ranvar.GetValue () < m_rate); } bool @@ -221,7 +219,7 @@ RateErrorModel::DoCorruptByte (Ptr p) NS_LOG_FUNCTION; // compute pkt error rate, assume uniformly distributed byte error double per = 1 - pow (1.0 - m_rate, p->GetSize ()); - return (m_ranvar->GetValue () < per); + return (m_ranvar.GetValue () < per); } bool @@ -230,7 +228,7 @@ RateErrorModel::DoCorruptBit(Ptr p) NS_LOG_FUNCTION; // compute pkt error rate, assume uniformly distributed bit error double per = 1 - pow (1.0 - m_rate, (8 * p->GetSize ()) ); - return (m_ranvar->GetValue () < per); + return (m_ranvar.GetValue () < per); } void diff --git a/src/common/error-model.h b/src/common/error-model.h index 1791e56e5..5cd050548 100644 --- a/src/common/error-model.h +++ b/src/common/error-model.h @@ -23,11 +23,11 @@ #include #include "ns3/object.h" +#include "ns3/random-variable.h" namespace ns3 { class Packet; -class RandomVariable; /** * \brief General error model that can be used to corrupt packets @@ -174,7 +174,7 @@ private: enum ErrorUnit m_unit; double m_rate; - RandomVariable* m_ranvar; + RandomVariable m_ranvar; }; /** diff --git a/src/core/random-variable-default-value.cc b/src/core/random-variable-default-value.cc index 58597c545..395cfcfec 100644 --- a/src/core/random-variable-default-value.cc +++ b/src/core/random-variable-default-value.cc @@ -39,17 +39,17 @@ RandomVariableDefaultValue::RandomVariableDefaultValue (std::string name, DefaultValueList::Add (this); } -RandomVariable * -RandomVariableDefaultValue::GetCopy (void) +RandomVariable +RandomVariableDefaultValue::Get (void) const { - RandomVariable *variable; + RandomVariable variable; bool ok; ok = Parse (m_value, true, &variable); NS_ASSERT (ok); return variable; } double -RandomVariableDefaultValue::ReadAsDouble (std::string value, bool &ok) +RandomVariableDefaultValue::ReadAsDouble (std::string value, bool &ok) const { double v; std::istringstream iss; @@ -60,7 +60,7 @@ RandomVariableDefaultValue::ReadAsDouble (std::string value, bool &ok) } bool RandomVariableDefaultValue::Parse (const std::string &value, - bool mustCreate, RandomVariable **pVariable) + bool mustCreate, RandomVariable *pVariable) const { std::string::size_type pos = value.find_first_of(":"); if (pos == std::string::npos) @@ -76,7 +76,7 @@ RandomVariableDefaultValue::Parse (const std::string &value, if (mustCreate) { NS_LOG_LOGIC ("create Constant constant=" << constant); - *pVariable = new ConstantVariable (constant); + *pVariable = ConstantVariable (constant); } else { @@ -100,7 +100,7 @@ RandomVariableDefaultValue::Parse (const std::string &value, if (mustCreate) { NS_LOG_LOGIC ("create Uniform min=" << min << ", max=" << max); - *pVariable = new UniformVariable (minVal, maxVal); + *pVariable = UniformVariable (minVal, maxVal); } else { diff --git a/src/core/random-variable-default-value.h b/src/core/random-variable-default-value.h index 782c62331..43d77e74d 100644 --- a/src/core/random-variable-default-value.h +++ b/src/core/random-variable-default-value.h @@ -33,10 +33,10 @@ class RandomVariableDefaultValue : public DefaultValueBase std::string help, std::string defaultValue); - RandomVariable *GetCopy (void); + RandomVariable Get (void) const; private: - bool Parse (const std::string &value, bool mustCreate, RandomVariable **pVariable); - double ReadAsDouble (const std::string value, bool &ok); + bool Parse (const std::string &value, bool mustCreate, RandomVariable *pVariable) const; + double ReadAsDouble (const std::string value, bool &ok) const; virtual bool DoParseValue (const std::string &value); virtual std::string DoGetType (void) const; virtual std::string DoGetDefaultValue (void) const; diff --git a/src/core/random-variable.cc b/src/core/random-variable.cc index 2190b90e1..1b29fe312 100644 --- a/src/core/random-variable.cc +++ b/src/core/random-variable.cc @@ -30,6 +30,7 @@ #include +#include "assert.h" #include "random-variable.h" #include "rng-stream.h" #include "fatal-error.h" @@ -40,16 +41,51 @@ namespace ns3{ //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -// RandomVariable methods +// RandomVariableBase methods -bool RandomVariable::initialized = false; // True if RngStream seed set -bool RandomVariable::useDevRandom = false; // True if use /dev/random -bool RandomVariable::globalSeedSet = false; // True if GlobalSeed called -int RandomVariable::devRandom = -1; -uint32_t RandomVariable::globalSeed[6]; -unsigned long RandomVariable::heuristic_sequence; -RngStream* RandomVariable::m_static_generator = 0; -uint32_t RandomVariable::runNumber = 0; + +class RandomVariableBase +{ +public: + RandomVariableBase (); + RandomVariableBase (const RandomVariableBase &o); + virtual ~RandomVariableBase(); + virtual double GetValue() = 0; + virtual uint32_t GetIntValue(); + virtual RandomVariableBase* Copy(void) const = 0; + virtual void GetSeed(uint32_t seed[6]); + + static void UseDevRandom(bool udr = true); + static void UseGlobalSeed(uint32_t s0, uint32_t s1, uint32_t s2, + uint32_t s3, uint32_t s4, uint32_t s5); + static void SetRunNumber(uint32_t n); +private: + static void GetRandomSeeds(uint32_t seeds[6]); +private: + static bool useDevRandom; // True if using /dev/random desired + static bool globalSeedSet; // True if global seed has been specified + static int devRandom; // File handle for /dev/random + static uint32_t globalSeed[6]; // The global seed to use + friend class RandomVariableInitializer; +protected: + static unsigned long heuristic_sequence; + static RngStream* m_static_generator; + static uint32_t runNumber; + static void Initialize(); // Initialize the RNG system + static bool initialized; // True if package seed is set + RngStream* m_generator; //underlying generator being wrapped +}; + + + +bool RandomVariableBase::initialized = false; // True if RngStream seed set +bool RandomVariableBase::useDevRandom = false; // True if use /dev/random +bool RandomVariableBase::globalSeedSet = false; // True if GlobalSeed called +int RandomVariableBase::devRandom = -1; +uint32_t RandomVariableBase::globalSeed[6]; +unsigned long RandomVariableBase::heuristic_sequence; +RngStream* RandomVariableBase::m_static_generator = 0; +uint32_t RandomVariableBase::runNumber = 0; //the static object random_variable_initializer initializes the static members //of RandomVariable @@ -58,25 +94,25 @@ static class RandomVariableInitializer public: RandomVariableInitializer() { -// RandomVariable::Initialize(); // sets the static package seed -// RandomVariable::m_static_generator = new RngStream(); -// RandomVariable::m_static_generator->InitializeStream(); +// RandomVariableBase::Initialize(); // sets the static package seed +// RandomVariableBase::m_static_generator = new RngStream(); +// RandomVariableBase::m_static_generator->InitializeStream(); } ~RandomVariableInitializer() { - delete RandomVariable::m_static_generator; + delete RandomVariableBase::m_static_generator; } } random_variable_initializer; -RandomVariable::RandomVariable() +RandomVariableBase::RandomVariableBase() : m_generator(NULL) { // m_generator = new RngStream(); // m_generator->InitializeStream(); -// m_generator->ResetNthSubstream(RandomVariable::runNumber); +// m_generator->ResetNthSubstream(RandomVariableBase::runNumber); } -RandomVariable::RandomVariable(const RandomVariable& r) +RandomVariableBase::RandomVariableBase(const RandomVariableBase& r) :m_generator(0) { if(r.m_generator) @@ -85,61 +121,61 @@ RandomVariable::RandomVariable(const RandomVariable& r) } } -RandomVariable::~RandomVariable() +RandomVariableBase::~RandomVariableBase() { delete m_generator; } -uint32_t RandomVariable::GetIntValue() +uint32_t RandomVariableBase::GetIntValue() { return (uint32_t)GetValue(); } -void RandomVariable::UseDevRandom(bool udr) +void RandomVariableBase::UseDevRandom(bool udr) { - RandomVariable::useDevRandom = udr; + RandomVariableBase::useDevRandom = udr; } -void RandomVariable::GetSeed(uint32_t seed[6]) +void RandomVariableBase::GetSeed(uint32_t seed[6]) { if(!m_generator) { m_generator = new RngStream(); m_generator->InitializeStream(); - m_generator->ResetNthSubstream(RandomVariable::runNumber); + m_generator->ResetNthSubstream(RandomVariableBase::runNumber); } m_generator->GetState(seed); } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -// RandomVariable static methods -void RandomVariable::UseGlobalSeed(uint32_t s0, uint32_t s1, uint32_t s2, +// RandomVariableBase static methods +void RandomVariableBase::UseGlobalSeed(uint32_t s0, uint32_t s1, uint32_t s2, uint32_t s3, uint32_t s4, uint32_t s5) { - if (RandomVariable::globalSeedSet) + if (RandomVariableBase::globalSeedSet) { cerr << "Random number generator already initialized!" << endl; - cerr << "Call to RandomVariable::UseGlobalSeed() ignored" << endl; + cerr << "Call to RandomVariableBase::UseGlobalSeed() ignored" << endl; return; } - RandomVariable::globalSeed[0] = s0; - RandomVariable::globalSeed[1] = s1; - RandomVariable::globalSeed[2] = s2; - RandomVariable::globalSeed[3] = s3; - RandomVariable::globalSeed[4] = s4; - RandomVariable::globalSeed[5] = s5; - if (!RngStream::CheckSeed(RandomVariable::globalSeed)) + RandomVariableBase::globalSeed[0] = s0; + RandomVariableBase::globalSeed[1] = s1; + RandomVariableBase::globalSeed[2] = s2; + RandomVariableBase::globalSeed[3] = s3; + RandomVariableBase::globalSeed[4] = s4; + RandomVariableBase::globalSeed[5] = s5; + if (!RngStream::CheckSeed(RandomVariableBase::globalSeed)) NS_FATAL_ERROR("Invalid seed"); - RandomVariable::globalSeedSet = true; + RandomVariableBase::globalSeedSet = true; } -void RandomVariable::Initialize() +void RandomVariableBase::Initialize() { - if (RandomVariable::initialized) return; // Already initialized and seeded - RandomVariable::initialized = true; - if (!RandomVariable::globalSeedSet) + if (RandomVariableBase::initialized) return; // Already initialized and seeded + RandomVariableBase::initialized = true; + if (!RandomVariableBase::globalSeedSet) { // No global seed, try a random one GetRandomSeeds(globalSeed); } @@ -147,20 +183,20 @@ void RandomVariable::Initialize() RngStream::SetPackageSeed(globalSeed); } -void RandomVariable::GetRandomSeeds(uint32_t seeds[6]) +void RandomVariableBase::GetRandomSeeds(uint32_t seeds[6]) { // Check if /dev/random exists - if (RandomVariable::useDevRandom && RandomVariable::devRandom < 0) + if (RandomVariableBase::useDevRandom && RandomVariableBase::devRandom < 0) { - RandomVariable::devRandom = open("/dev/random", O_RDONLY); + RandomVariableBase::devRandom = open("/dev/random", O_RDONLY); } - if (RandomVariable::devRandom > 0) + if (RandomVariableBase::devRandom > 0) { // Use /dev/random while(true) { for (int i = 0; i < 6; ++i) { - read(RandomVariable::devRandom, &seeds[i], sizeof(seeds[i])); + read(RandomVariableBase::devRandom, &seeds[i], sizeof(seeds[i])); } if (RngStream::CheckSeed(seeds)) break; // Got a valid one } @@ -194,205 +230,537 @@ void RandomVariable::GetRandomSeeds(uint32_t seeds[6]) } } -void RandomVariable::SetRunNumber(uint32_t n) +void RandomVariableBase::SetRunNumber(uint32_t n) { runNumber = n; } + + +RandomVariable::RandomVariable() + : m_variable (0) +{} +RandomVariable::RandomVariable(const RandomVariable&o) + : m_variable (o.m_variable->Copy ()) +{} +RandomVariable::RandomVariable (const RandomVariableBase &variable) + : m_variable (variable.Copy ()) +{} +RandomVariable & +RandomVariable::operator = (const RandomVariable &o) +{ + delete m_variable; + m_variable = o.m_variable->Copy (); + return *this; +} +RandomVariable::~RandomVariable() +{ + delete m_variable; +} +double +RandomVariable::GetValue (void) const +{ + return m_variable->GetValue (); +} + +uint32_t +RandomVariable::GetIntValue (void) const +{ + return m_variable->GetIntValue (); +} +void +RandomVariable::GetSeed(uint32_t seed[6]) const +{ + return m_variable->GetSeed (seed); +} +void +RandomVariable::UseDevRandom(bool udr) +{ + RandomVariableBase::UseDevRandom (udr); +} +void +RandomVariable::UseGlobalSeed(uint32_t s0, uint32_t s1, uint32_t s2, + uint32_t s3, uint32_t s4, uint32_t s5) +{ + RandomVariableBase::UseGlobalSeed (s0, s1, s2, s3, s4, s5); +} +void +RandomVariable::SetRunNumber(uint32_t n) +{ + RandomVariableBase::SetRunNumber (n); +} +RandomVariableBase * +RandomVariable::Peek (void) +{ + return m_variable; +} + + //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -// UniformVariable -UniformVariable::UniformVariable() +// UniformVariableImpl + +class UniformVariableImpl : public RandomVariableBase { +public: + /** + * Creates a uniform random number generator in the + * range [0.0 .. 1.0). + */ + UniformVariableImpl(); + + /** + * Creates a uniform random number generator with the specified range + * \param s Low end of the range + * \param l High end of the range + */ + UniformVariableImpl(double s, double l); + + UniformVariableImpl(const UniformVariableImpl& c); + + /** + * \return A value between low and high values specified by the constructor + */ + virtual double GetValue(); + virtual RandomVariableBase* Copy(void) const; + +public: + /** + * \param s Low end of the range + * \param l High end of the range + * \return A uniformly distributed random number between s and l + */ + static double GetSingleValue(double s, double l); +private: + double m_min; + double m_max; +}; + +UniformVariableImpl::UniformVariableImpl() : m_min(0), m_max(1.0) { } -UniformVariable::UniformVariable(double s, double l) +UniformVariableImpl::UniformVariableImpl(double s, double l) : m_min(s), m_max(l) { } -UniformVariable::UniformVariable(const UniformVariable& c) - : RandomVariable(c), m_min(c.m_min), m_max(c.m_max) { } +UniformVariableImpl::UniformVariableImpl(const UniformVariableImpl& c) + : RandomVariableBase(c), m_min(c.m_min), m_max(c.m_max) { } -double UniformVariable::GetValue() +double UniformVariableImpl::GetValue() { - if(!RandomVariable::initialized) + if(!RandomVariableBase::initialized) { - RandomVariable::Initialize(); + RandomVariableBase::Initialize(); } if(!m_generator) { m_generator = new RngStream(); m_generator->InitializeStream(); - m_generator->ResetNthSubstream(RandomVariable::runNumber); + m_generator->ResetNthSubstream(RandomVariableBase::runNumber); } return m_min + m_generator->RandU01() * (m_max - m_min); } -RandomVariable* UniformVariable::Copy() const +RandomVariableBase* UniformVariableImpl::Copy() const { - return new UniformVariable(*this); + return new UniformVariableImpl(*this); } -double UniformVariable::GetSingleValue(double s, double l) +double UniformVariableImpl::GetSingleValue(double s, double l) { - if(!RandomVariable::m_static_generator) + if(!RandomVariableBase::m_static_generator) { - RandomVariable::Initialize(); // sets the static package seed - RandomVariable::m_static_generator = new RngStream(); - RandomVariable::m_static_generator->InitializeStream(); + RandomVariableBase::Initialize(); // sets the static package seed + RandomVariableBase::m_static_generator = new RngStream(); + RandomVariableBase::m_static_generator->InitializeStream(); } return s + m_static_generator->RandU01() * (l - s);; } +UniformVariable::UniformVariable() + : RandomVariable (UniformVariableImpl ()) +{} +UniformVariable::UniformVariable(double s, double l) + : RandomVariable (UniformVariableImpl (s, l)) +{} +double +UniformVariable::GetSingleValue(double s, double l) +{ + return UniformVariableImpl::GetSingleValue (s, l); +} + + //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -// ConstantVariable methods -ConstantVariable::ConstantVariable() +// ConstantVariableImpl methods + +class ConstantVariableImpl : public RandomVariableBase { + +public: + /** + * Construct a ConstantVariableImpl RNG that returns zero every sample + */ + ConstantVariableImpl(); + + /** + * Construct a ConstantVariableImpl RNG that returns the specified value + * every sample. + * \param c Unchanging value for this RNG. + */ + ConstantVariableImpl(double c); + + + ConstantVariableImpl(const ConstantVariableImpl& c) ; + + /** + * \brief Specify a new constant RNG for this generator. + * \param c New constant value for this RNG. + */ + void NewConstant(double c); + + /** + * \return The constant value specified + */ + virtual double GetValue(); + virtual uint32_t GetIntValue(); + virtual RandomVariableBase* Copy(void) const; +private: + double m_const; +}; + +ConstantVariableImpl::ConstantVariableImpl() : m_const(0) { } -ConstantVariable::ConstantVariable(double c) +ConstantVariableImpl::ConstantVariableImpl(double c) : m_const(c) { }; -ConstantVariable::ConstantVariable(const ConstantVariable& c) - : RandomVariable(c), m_const(c.m_const) { } +ConstantVariableImpl::ConstantVariableImpl(const ConstantVariableImpl& c) + : RandomVariableBase(c), m_const(c.m_const) { } -void ConstantVariable::NewConstant(double c) +void ConstantVariableImpl::NewConstant(double c) { m_const = c;} -double ConstantVariable::GetValue() +double ConstantVariableImpl::GetValue() { return m_const; } -uint32_t ConstantVariable::GetIntValue() +uint32_t ConstantVariableImpl::GetIntValue() { return (uint32_t)m_const; } -RandomVariable* ConstantVariable::Copy() const -{ - return new ConstantVariable(*this); -} -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -// SequentialVariable methods -SequentialVariable::SequentialVariable(double f, double l, double i, uint32_t c) - : m_min(f), m_max(l), m_increment(ConstantVariable(i).Copy()), m_consecutive(c), - m_current(f), m_currentConsecutive(0) +RandomVariableBase* ConstantVariableImpl::Copy() const { + return new ConstantVariableImpl(*this); } -SequentialVariable::SequentialVariable(double f, double l, const RandomVariable& i, uint32_t c) - : m_min(f), m_max(l), m_increment(i.Copy()), m_consecutive(c), - m_current(f), m_currentConsecutive(0) +ConstantVariable::ConstantVariable() + : RandomVariable (ConstantVariableImpl ()) +{} +ConstantVariable::ConstantVariable(double c) + : RandomVariable (ConstantVariableImpl (c)) +{} +void +ConstantVariable::SetConstant(double c) { + *this = ConstantVariable (c); } -SequentialVariable::SequentialVariable(const SequentialVariable& c) - : RandomVariable(c), m_min(c.m_min), m_max(c.m_max), - m_increment(c.m_increment->Copy()), m_consecutive(c.m_consecutive), +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +// SequentialVariableImpl methods + + +class SequentialVariableImpl : public RandomVariableBase { + +public: + /** + * \brief Constructor for the SequentialVariableImpl RNG. + * + * The four parameters define the sequence. For example + * SequentialVariableImpl(0,5,1,2) creates a RNG that has the sequence + * 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 0, 0 ... + * \param f First value of the sequence. + * \param l One more than the last value of the sequence. + * \param i Increment between sequence values + * \param c Number of times each member of the sequence is repeated + */ + SequentialVariableImpl(double f, double l, double i = 1, uint32_t c = 1); + + /** + * \brief Constructor for the SequentialVariableImpl RNG. + * + * Differs from the first only in that the increment parameter is a + * random variable + * \param f First value of the sequence. + * \param l One more than the last value of the sequence. + * \param i Reference to a RandomVariableBase for the sequence increment + * \param c Number of times each member of the sequence is repeated + */ + SequentialVariableImpl(double f, double l, const RandomVariable& i, uint32_t c = 1); + + SequentialVariableImpl(const SequentialVariableImpl& c); + + ~SequentialVariableImpl(); + /** + * \return The next value in the Sequence + */ + virtual double GetValue(); + virtual RandomVariableBase* Copy(void) const; +private: + double m_min; + double m_max; + RandomVariable m_increment; + uint32_t m_consecutive; + double m_current; + uint32_t m_currentConsecutive; +}; + +SequentialVariableImpl::SequentialVariableImpl(double f, double l, double i, uint32_t c) + : m_min(f), m_max(l), m_increment(ConstantVariable(i)), m_consecutive(c), + m_current(f), m_currentConsecutive(0) +{} + +SequentialVariableImpl::SequentialVariableImpl(double f, double l, const RandomVariable& i, uint32_t c) + : m_min(f), m_max(l), m_increment(i), m_consecutive(c), + m_current(f), m_currentConsecutive(0) +{} + +SequentialVariableImpl::SequentialVariableImpl(const SequentialVariableImpl& c) + : RandomVariableBase(c), m_min(c.m_min), m_max(c.m_max), + m_increment(c.m_increment), m_consecutive(c.m_consecutive), m_current(c.m_current), m_currentConsecutive(c.m_currentConsecutive) -{ -} +{} -SequentialVariable::~SequentialVariable() -{ - delete m_increment; -} +SequentialVariableImpl::~SequentialVariableImpl() +{} -double SequentialVariable::GetValue() +double SequentialVariableImpl::GetValue() { // Return a sequential series of values double r = m_current; if (++m_currentConsecutive == m_consecutive) { // Time to advance to next m_currentConsecutive = 0; - m_current += m_increment->GetValue(); + m_current += m_increment.GetValue(); if (m_current >= m_max) m_current = m_min + (m_current - m_max); } return r; } -RandomVariable* SequentialVariable::Copy() const +RandomVariableBase* SequentialVariableImpl::Copy() const { - return new SequentialVariable(*this); + return new SequentialVariableImpl(*this); } + +SequentialVariable::SequentialVariable(double f, double l, double i, uint32_t c) + : RandomVariable (SequentialVariableImpl (f, l, i, c)) +{} +SequentialVariable::SequentialVariable(double f, double l, const RandomVariable& i, uint32_t c) + : RandomVariable (SequentialVariableImpl (f, l, i, c)) +{} + //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -// ExponentialVariable methods -ExponentialVariable::ExponentialVariable() +// ExponentialVariableImpl methods + +class ExponentialVariableImpl : public RandomVariableBase { +public: + /** + * Constructs an exponential random variable with a mean + * value of 1.0. + */ + ExponentialVariableImpl(); + + /** + * \brief Constructs an exponential random variable with a specified mean + * \param m Mean value for the random variable + */ + explicit ExponentialVariableImpl(double m); + + /** + * \brief Constructs an exponential random variable with spefified + * \brief mean and upper limit. + * + * Since exponential distributions can theoretically return unbounded values, + * it is sometimes useful to specify a fixed upper limit. Note however when + * the upper limit is specified, the true mean of the distribution is + * slightly smaller than the mean value specified. + * \param m Mean value of the random variable + * \param b Upper bound on returned values + */ + ExponentialVariableImpl(double m, double b); + + ExponentialVariableImpl(const ExponentialVariableImpl& c); + + /** + * \return A random value from this exponential distribution + */ + virtual double GetValue(); + virtual RandomVariableBase* Copy(void) const; +public: + /** + * \param m The mean of the distribution from which the return value is drawn + * \param b The upper bound value desired, beyond which values get clipped + * \return A random number from an exponential distribution with mean m + */ + static double GetSingleValue(double m, double b=0); +private: + double m_mean; // Mean value of RV + double m_bound; // Upper bound on value (if non-zero) +}; + +ExponentialVariableImpl::ExponentialVariableImpl() : m_mean(1.0), m_bound(0) { } -ExponentialVariable::ExponentialVariable(double m) +ExponentialVariableImpl::ExponentialVariableImpl(double m) : m_mean(m), m_bound(0) { } -ExponentialVariable::ExponentialVariable(double m, double b) +ExponentialVariableImpl::ExponentialVariableImpl(double m, double b) : m_mean(m), m_bound(b) { } -ExponentialVariable::ExponentialVariable(const ExponentialVariable& c) - : RandomVariable(c), m_mean(c.m_mean), m_bound(c.m_bound) { } +ExponentialVariableImpl::ExponentialVariableImpl(const ExponentialVariableImpl& c) + : RandomVariableBase(c), m_mean(c.m_mean), m_bound(c.m_bound) { } -double ExponentialVariable::GetValue() +double ExponentialVariableImpl::GetValue() { - if(!RandomVariable::initialized) + if(!RandomVariableBase::initialized) { - RandomVariable::Initialize(); + RandomVariableBase::Initialize(); } if(!m_generator) { m_generator = new RngStream(); m_generator->InitializeStream(); - m_generator->ResetNthSubstream(RandomVariable::runNumber); + m_generator->ResetNthSubstream(RandomVariableBase::runNumber); } double r = -m_mean*log(m_generator->RandU01()); if (m_bound != 0 && r > m_bound) return m_bound; return r; } -RandomVariable* ExponentialVariable::Copy() const +RandomVariableBase* ExponentialVariableImpl::Copy() const { - return new ExponentialVariable(*this); + return new ExponentialVariableImpl(*this); } -double ExponentialVariable::GetSingleValue(double m, double b/*=0*/) +double ExponentialVariableImpl::GetSingleValue(double m, double b/*=0*/) { - if(!RandomVariable::m_static_generator) + if(!RandomVariableBase::m_static_generator) { - RandomVariable::Initialize(); // sets the static package seed - RandomVariable::m_static_generator = new RngStream(); - RandomVariable::m_static_generator->InitializeStream(); + RandomVariableBase::Initialize(); // sets the static package seed + RandomVariableBase::m_static_generator = new RngStream(); + RandomVariableBase::m_static_generator->InitializeStream(); } double r = -m*log(m_static_generator->RandU01()); if (b != 0 && r > b) return b; return r; } + +ExponentialVariable::ExponentialVariable() + : RandomVariable (ExponentialVariableImpl ()) +{} +ExponentialVariable::ExponentialVariable(double m) + : RandomVariable (ExponentialVariableImpl (m)) +{} +ExponentialVariable::ExponentialVariable(double m, double b) + : RandomVariable (ExponentialVariableImpl (m, b)) +{} +double +ExponentialVariable::GetSingleValue(double m, double b) +{ + return ExponentialVariableImpl::GetSingleValue (m, b); +} + //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -// ParetoVariable methods -ParetoVariable::ParetoVariable() +// ParetoVariableImpl methods +class ParetoVariableImpl : public RandomVariableBase { +public: + /** + * Constructs a pareto random variable with a mean of 1 and a shape + * parameter of 1.5 + */ + ParetoVariableImpl(); + + /** + * Constructs a pareto random variable with specified mean and shape + * parameter of 1.5 + * \param m Mean value of the distribution + */ + explicit ParetoVariableImpl(double m); + + /** + * Constructs a pareto random variable with the specified mean value and + * shape parameter. + * \param m Mean value of the distribution + * \param s Shape parameter for the distribution + */ + ParetoVariableImpl(double m, double s); + + /** + * \brief Constructs a pareto random variable with the specified mean + * \brief value, shape (alpha), and upper bound. + * + * Since pareto distributions can theoretically return unbounded values, + * it is sometimes useful to specify a fixed upper limit. Note however + * when the upper limit is specified, the true mean of the distribution + * is slightly smaller than the mean value specified. + * \param m Mean value + * \param s Shape parameter + * \param b Upper limit on returned values + */ + ParetoVariableImpl(double m, double s, double b); + + ParetoVariableImpl(const ParetoVariableImpl& c); + + /** + * \return A random value from this Pareto distribution + */ + virtual double GetValue(); + virtual RandomVariableBase* Copy() const; +public: + /** + * \param m The mean value of the distribution from which the return value + * is drawn. + * \param s The shape parameter of the distribution from which the return + * value is drawn. + * \param b The upper bound to which to restrict return values + * \return A random number from a Pareto distribution with mean m and shape + * parameter s. + */ + static double GetSingleValue(double m, double s, double b=0); +private: + double m_mean; // Mean value of RV + double m_shape; // Shape parameter + double m_bound; // Upper bound on value (if non-zero) +}; + +ParetoVariableImpl::ParetoVariableImpl() : m_mean(1.0), m_shape(1.5), m_bound(0) { } -ParetoVariable::ParetoVariable(double m) +ParetoVariableImpl::ParetoVariableImpl(double m) : m_mean(m), m_shape(1.5), m_bound(0) { } -ParetoVariable::ParetoVariable(double m, double s) +ParetoVariableImpl::ParetoVariableImpl(double m, double s) : m_mean(m), m_shape(s), m_bound(0) { } -ParetoVariable::ParetoVariable(double m, double s, double b) +ParetoVariableImpl::ParetoVariableImpl(double m, double s, double b) : m_mean(m), m_shape(s), m_bound(b) { } -ParetoVariable::ParetoVariable(const ParetoVariable& c) - : RandomVariable(c), m_mean(c.m_mean), m_shape(c.m_shape), +ParetoVariableImpl::ParetoVariableImpl(const ParetoVariableImpl& c) + : RandomVariableBase(c), m_mean(c.m_mean), m_shape(c.m_shape), m_bound(c.m_bound) { } -double ParetoVariable::GetValue() +double ParetoVariableImpl::GetValue() { - if(!RandomVariable::initialized) + if(!RandomVariableBase::initialized) { - RandomVariable::Initialize(); + RandomVariableBase::Initialize(); } if(!m_generator) { m_generator = new RngStream(); m_generator->InitializeStream(); - m_generator->ResetNthSubstream(RandomVariable::runNumber); + m_generator->ResetNthSubstream(RandomVariableBase::runNumber); } double scale = m_mean * ( m_shape - 1.0) / m_shape; double r = (scale * ( 1.0 / pow(m_generator->RandU01(), 1.0 / m_shape))); @@ -400,49 +768,127 @@ double ParetoVariable::GetValue() return r; } -RandomVariable* ParetoVariable::Copy() const +RandomVariableBase* ParetoVariableImpl::Copy() const { - return new ParetoVariable(*this); + return new ParetoVariableImpl(*this); } -double ParetoVariable::GetSingleValue(double m, double s, double b/*=0*/) +double ParetoVariableImpl::GetSingleValue(double m, double s, double b/*=0*/) { - if(!RandomVariable::m_static_generator) + if(!RandomVariableBase::m_static_generator) { - RandomVariable::Initialize(); // sets the static package seed - RandomVariable::m_static_generator = new RngStream(); - RandomVariable::m_static_generator->InitializeStream(); + RandomVariableBase::Initialize(); // sets the static package seed + RandomVariableBase::m_static_generator = new RngStream(); + RandomVariableBase::m_static_generator->InitializeStream(); } double scale = m * ( s - 1.0) / s; double r = (scale * ( 1.0 / pow(m_static_generator->RandU01(), 1.0 / s))); if (b != 0 && r > b) return b; return r; } + +ParetoVariable::ParetoVariable () + : RandomVariable (ParetoVariableImpl ()) +{} +ParetoVariable::ParetoVariable(double m) + : RandomVariable (ParetoVariableImpl (m)) +{} +ParetoVariable::ParetoVariable(double m, double s) + : RandomVariable (ParetoVariableImpl (m, s)) +{} +ParetoVariable::ParetoVariable(double m, double s, double b) + : RandomVariable (ParetoVariableImpl (m, s, b)) +{} +double +ParetoVariable::GetSingleValue(double m, double s, double b) +{ + return ParetoVariableImpl::GetSingleValue (m, s, b); +} + //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -// WeibullVariable methods -WeibullVariable::WeibullVariable() : m_mean(1.0), m_alpha(1), m_bound(0) { } -WeibullVariable::WeibullVariable(double m) +// WeibullVariableImpl methods + +class WeibullVariableImpl : public RandomVariableBase { +public: + /** + * Constructs a weibull random variable with a mean + * value of 1.0 and a shape (alpha) parameter of 1 + */ + WeibullVariableImpl(); + + + /** + * Constructs a weibull random variable with the specified mean + * value and a shape (alpha) parameter of 1.5. + * \param m mean value of the distribution + */ + WeibullVariableImpl(double m) ; + + /** + * Constructs a weibull random variable with the specified mean + * value and a shape (alpha). + * \param m Mean value for the distribution. + * \param s Shape (alpha) parameter for the distribution. + */ + WeibullVariableImpl(double m, double s); + + /** + * \brief Constructs a weibull random variable with the specified mean + * \brief value, shape (alpha), and upper bound. + * Since WeibullVariableImpl distributions can theoretically return unbounded values, + * it is sometimes usefull to specify a fixed upper limit. Note however + * that when the upper limit is specified, the true mean of the distribution + * is slightly smaller than the mean value specified. + * \param m Mean value for the distribution. + * \param s Shape (alpha) parameter for the distribution. + * \param b Upper limit on returned values + */ + WeibullVariableImpl(double m, double s, double b); + + WeibullVariableImpl(const WeibullVariableImpl& c); + + /** + * \return A random value from this Weibull distribution + */ + virtual double GetValue(); + virtual RandomVariableBase* Copy(void) const; +public: + /** + * \param m Mean value for the distribution. + * \param s Shape (alpha) parameter for the distribution. + * \param b Upper limit on returned values + * \return Random number from a distribution specified by m,s, and b + */ + static double GetSingleValue(double m, double s, double b=0); +private: + double m_mean; // Mean value of RV + double m_alpha; // Shape parameter + double m_bound; // Upper bound on value (if non-zero) +}; + +WeibullVariableImpl::WeibullVariableImpl() : m_mean(1.0), m_alpha(1), m_bound(0) { } +WeibullVariableImpl::WeibullVariableImpl(double m) : m_mean(m), m_alpha(1), m_bound(0) { } -WeibullVariable::WeibullVariable(double m, double s) +WeibullVariableImpl::WeibullVariableImpl(double m, double s) : m_mean(m), m_alpha(s), m_bound(0) { } -WeibullVariable::WeibullVariable(double m, double s, double b) +WeibullVariableImpl::WeibullVariableImpl(double m, double s, double b) : m_mean(m), m_alpha(s), m_bound(b) { }; -WeibullVariable::WeibullVariable(const WeibullVariable& c) - : RandomVariable(c), m_mean(c.m_mean), m_alpha(c.m_alpha), +WeibullVariableImpl::WeibullVariableImpl(const WeibullVariableImpl& c) + : RandomVariableBase(c), m_mean(c.m_mean), m_alpha(c.m_alpha), m_bound(c.m_bound) { } -double WeibullVariable::GetValue() +double WeibullVariableImpl::GetValue() { - if(!RandomVariable::initialized) + if(!RandomVariableBase::initialized) { - RandomVariable::Initialize(); + RandomVariableBase::Initialize(); } if(!m_generator) { m_generator = new RngStream(); m_generator->InitializeStream(); - m_generator->ResetNthSubstream(RandomVariable::runNumber); + m_generator->ResetNthSubstream(RandomVariableBase::runNumber); } double exponent = 1.0 / m_alpha; double r = m_mean * pow( -log(m_generator->RandU01()), exponent); @@ -450,52 +896,114 @@ double WeibullVariable::GetValue() return r; } -RandomVariable* WeibullVariable::Copy() const +RandomVariableBase* WeibullVariableImpl::Copy() const { - return new WeibullVariable(*this); + return new WeibullVariableImpl(*this); } -double WeibullVariable::GetSingleValue(double m, double s, double b/*=0*/) +double WeibullVariableImpl::GetSingleValue(double m, double s, double b/*=0*/) { - if(!RandomVariable::m_static_generator) + if(!RandomVariableBase::m_static_generator) { - RandomVariable::Initialize(); // sets the static package seed - RandomVariable::m_static_generator = new RngStream(); - RandomVariable::m_static_generator->InitializeStream(); + RandomVariableBase::Initialize(); // sets the static package seed + RandomVariableBase::m_static_generator = new RngStream(); + RandomVariableBase::m_static_generator->InitializeStream(); } double exponent = 1.0 / s; double r = m * pow( -log(m_static_generator->RandU01()), exponent); if (b != 0 && r > b) return b; return r; } -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -// NormalVariable methods -bool NormalVariable::m_static_nextValid = false; -double NormalVariable::m_static_next; -const double NormalVariable::INFINITE_VALUE = 1e307; -NormalVariable::NormalVariable() +WeibullVariable::WeibullVariable() + : RandomVariable (WeibullVariableImpl ()) +{} +WeibullVariable::WeibullVariable(double m) + : RandomVariable (WeibullVariableImpl (m)) +{} +WeibullVariable::WeibullVariable(double m, double s) + : RandomVariable (WeibullVariableImpl (m, s)) +{} +WeibullVariable::WeibullVariable(double m, double s, double b) + : RandomVariable (WeibullVariableImpl (m, s, b)) +{} +double +WeibullVariable::GetSingleValue(double m, double s, double b) +{ + return WeibullVariableImpl::GetSingleValue (m, s, b); +} +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +// NormalVariableImpl methods + +class NormalVariableImpl : public RandomVariableBase { // Normally Distributed random var + +public: + static const double INFINITE_VALUE; + /** + * Constructs an normal random variable with a mean + * value of 0 and variance of 1. + */ + NormalVariableImpl(); + + /** + * \brief Construct a normal random variable with specified mean and variance + * \param m Mean value + * \param v Variance + * \param b Bound. The NormalVariableImpl is bounded within +-bound. + */ + NormalVariableImpl(double m, double v, double b = INFINITE_VALUE); + + NormalVariableImpl(const NormalVariableImpl& c); + + /** + * \return A value from this normal distribution + */ + virtual double GetValue(); + virtual RandomVariableBase* Copy(void) const; +public: + /** + * \param m Mean value + * \param v Variance + * \param b Bound. The NormalVariableImpl is bounded within +-bound. + * \return A random number from a distribution specified by m,v, and b. + */ + static double GetSingleValue(double m, double v, double b = INFINITE_VALUE); +private: + double m_mean; // Mean value of RV + double m_variance; // Mean value of RV + double m_bound; // Bound on value (absolute value) + bool m_nextValid; // True if next valid + double m_next; // The algorithm produces two values at a time + static bool m_static_nextValid; + static double m_static_next; +}; + +bool NormalVariableImpl::m_static_nextValid = false; +double NormalVariableImpl::m_static_next; +const double NormalVariableImpl::INFINITE_VALUE = 1e307; + +NormalVariableImpl::NormalVariableImpl() : m_mean(0.0), m_variance(1.0), m_bound(INFINITE_VALUE), m_nextValid(false){} -NormalVariable::NormalVariable(double m, double v, double b/*=INFINITE_VALUE*/) +NormalVariableImpl::NormalVariableImpl(double m, double v, double b/*=INFINITE_VALUE*/) : m_mean(m), m_variance(v), m_bound(b), m_nextValid(false) { } -NormalVariable::NormalVariable(const NormalVariable& c) - : RandomVariable(c), m_mean(c.m_mean), m_variance(c.m_variance), +NormalVariableImpl::NormalVariableImpl(const NormalVariableImpl& c) + : RandomVariableBase(c), m_mean(c.m_mean), m_variance(c.m_variance), m_bound(c.m_bound) { } -double NormalVariable::GetValue() +double NormalVariableImpl::GetValue() { - if(!RandomVariable::initialized) + if(!RandomVariableBase::initialized) { - RandomVariable::Initialize(); + RandomVariableBase::Initialize(); } if(!m_generator) { m_generator = new RngStream(); m_generator->InitializeStream(); - m_generator->ResetNthSubstream(RandomVariable::runNumber); + m_generator->ResetNthSubstream(RandomVariableBase::runNumber); } if (m_nextValid) { // use previously generated @@ -523,18 +1031,18 @@ double NormalVariable::GetValue() } } -RandomVariable* NormalVariable::Copy() const +RandomVariableBase* NormalVariableImpl::Copy() const { - return new NormalVariable(*this); + return new NormalVariableImpl(*this); } -double NormalVariable::GetSingleValue(double m, double v, double b) +double NormalVariableImpl::GetSingleValue(double m, double v, double b) { - if(!RandomVariable::m_static_generator) + if(!RandomVariableBase::m_static_generator) { - RandomVariable::Initialize(); // sets the static package seed - RandomVariable::m_static_generator = new RngStream(); - RandomVariable::m_static_generator->InitializeStream(); + RandomVariableBase::Initialize(); // sets the static package seed + RandomVariableBase::m_static_generator = new RngStream(); + RandomVariableBase::m_static_generator->InitializeStream(); } if (m_static_nextValid) { // use previously generated @@ -562,39 +1070,89 @@ double NormalVariable::GetSingleValue(double m, double v, double b) } } +NormalVariable::NormalVariable() + : RandomVariable (NormalVariableImpl ()) +{} +NormalVariable::NormalVariable(double m, double v, double b) + : RandomVariable (NormalVariableImpl (m, v, b)) +{} +double +NormalVariable::GetSingleValue(double m, double v, double b) +{ + return NormalVariableImpl::GetSingleValue (m, v, b); +} + + //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- +class EmpiricalVariableImpl : public RandomVariableBase { +public: + /** + * Constructor for the EmpiricalVariableImpl random variables. + */ + explicit EmpiricalVariableImpl(); + + virtual ~EmpiricalVariableImpl(); + EmpiricalVariableImpl(const EmpiricalVariableImpl& c); + /** + * \return A value from this empirical distribution + */ + virtual double GetValue(); + virtual RandomVariableBase* Copy(void) const; + /** + * \brief Specifies a point in the empirical distribution + * \param v The function value for this point + * \param c Probability that the function is less than or equal to v + */ + virtual void CDF(double v, double c); // Value, prob <= Value + +private: + class ValueCDF { + public: + ValueCDF(); + ValueCDF(double v, double c); + ValueCDF(const ValueCDF& c); + double value; + double cdf; + }; + virtual void Validate(); // Insure non-decreasing emiprical values + virtual double Interpolate(double, double, double, double, double); + bool validated; // True if non-decreasing validated + std::vector emp; // Empicical CDF +}; + + // ValueCDF methods -EmpiricalVariable::ValueCDF::ValueCDF() +EmpiricalVariableImpl::ValueCDF::ValueCDF() : value(0.0), cdf(0.0){ } -EmpiricalVariable::ValueCDF::ValueCDF(double v, double c) +EmpiricalVariableImpl::ValueCDF::ValueCDF(double v, double c) : value(v), cdf(c) { } -EmpiricalVariable::ValueCDF::ValueCDF(const ValueCDF& c) +EmpiricalVariableImpl::ValueCDF::ValueCDF(const ValueCDF& c) : value(c.value), cdf(c.cdf) { } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -// EmpiricalVariable methods -EmpiricalVariable::EmpiricalVariable() +// EmpiricalVariableImpl methods +EmpiricalVariableImpl::EmpiricalVariableImpl() : validated(false) { } -EmpiricalVariable::EmpiricalVariable(const EmpiricalVariable& c) - : RandomVariable(c), validated(c.validated), emp(c.emp) { } +EmpiricalVariableImpl::EmpiricalVariableImpl(const EmpiricalVariableImpl& c) + : RandomVariableBase(c), validated(c.validated), emp(c.emp) { } -EmpiricalVariable::~EmpiricalVariable() { } +EmpiricalVariableImpl::~EmpiricalVariableImpl() { } -double EmpiricalVariable::GetValue() +double EmpiricalVariableImpl::GetValue() { // Return a value from the empirical distribution // This code based (loosely) on code by Bruce Mah (Thanks Bruce!) - if(!RandomVariable::initialized) + if(!RandomVariableBase::initialized) { - RandomVariable::Initialize(); + RandomVariableBase::Initialize(); } if(!m_generator) { m_generator = new RngStream(); m_generator->InitializeStream(); - m_generator->ResetNthSubstream(RandomVariable::runNumber); + m_generator->ResetNthSubstream(RandomVariableBase::runNumber); } if (emp.size() == 0) return 0.0; // HuH? No empirical data if (!validated) Validate(); // Insure in non-decreasing @@ -619,18 +1177,18 @@ double EmpiricalVariable::GetValue() } } -RandomVariable* EmpiricalVariable::Copy() const +RandomVariableBase* EmpiricalVariableImpl::Copy() const { - return new EmpiricalVariable(*this); + return new EmpiricalVariableImpl(*this); } -void EmpiricalVariable::CDF(double v, double c) +void EmpiricalVariableImpl::CDF(double v, double c) { // Add a new empirical datapoint to the empirical cdf // NOTE. These MUST be inserted in non-decreasing order emp.push_back(ValueCDF(v, c)); } -void EmpiricalVariable::Validate() +void EmpiricalVariableImpl::Validate() { ValueCDF prior; for (std::vector::size_type i = 0; i < emp.size(); ++i) @@ -650,66 +1208,156 @@ void EmpiricalVariable::Validate() validated = true; } -double EmpiricalVariable::Interpolate(double c1, double c2, +double EmpiricalVariableImpl::Interpolate(double c1, double c2, double v1, double v2, double r) { // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2) return (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1)); } -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -// Integer EmpiricalVariable methods -IntEmpiricalVariable::IntEmpiricalVariable() { } +EmpiricalVariable::EmpiricalVariable() + : RandomVariable (EmpiricalVariableImpl ()) +{} +EmpiricalVariable::EmpiricalVariable (const RandomVariableBase &variable) + : RandomVariable (variable) +{} +void +EmpiricalVariable::CDF(double v, double c) +{ + EmpiricalVariableImpl *impl = dynamic_cast (Peek ()); + NS_ASSERT (impl); + impl->CDF (v, c); +} -uint32_t IntEmpiricalVariable::GetIntValue() + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +// Integer EmpiricalVariableImpl methods +class IntEmpiricalVariableImpl : public EmpiricalVariableImpl { +public: + + IntEmpiricalVariableImpl(); + + virtual RandomVariableBase* Copy(void) const; + /** + * \return An integer value from this empirical distribution + */ + virtual uint32_t GetIntValue(); +private: + virtual double Interpolate(double, double, double, double, double); +}; + + +IntEmpiricalVariableImpl::IntEmpiricalVariableImpl() { } + +uint32_t IntEmpiricalVariableImpl::GetIntValue() { return (uint32_t)GetValue(); } -RandomVariable* IntEmpiricalVariable::Copy() const +RandomVariableBase* IntEmpiricalVariableImpl::Copy() const { - return new IntEmpiricalVariable(*this); + return new IntEmpiricalVariableImpl(*this); } -double IntEmpiricalVariable::Interpolate(double c1, double c2, +double IntEmpiricalVariableImpl::Interpolate(double c1, double c2, double v1, double v2, double r) { // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2) return ceil(v1 + ((v2 - v1) / (c2 - c1)) * (r - c1)); } +IntEmpiricalVariable::IntEmpiricalVariable() + : EmpiricalVariable (IntEmpiricalVariableImpl ()) +{} //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -// DeterministicVariable -DeterministicVariable::DeterministicVariable(double* d, uint32_t c) +// DeterministicVariableImpl +class DeterministicVariableImpl : public RandomVariableBase +{ + +public: + /** + * \brief Constructor + * + * Creates a generator that returns successive elements of the d array + * on successive calls to ::Value(). Note that the d pointer is copied + * for use by the generator (shallow-copy), not its contents, so the + * contents of the array d points to have to remain unchanged for the use + * of DeterministicVariableImpl to be meaningful. + * \param d Pointer to array of random values to return in sequence + * \param c Number of values in the array + */ + explicit DeterministicVariableImpl(double* d, uint32_t c); + + virtual ~DeterministicVariableImpl(); + /** + * \return The next value in the deterministic sequence + */ + virtual double GetValue(); + virtual RandomVariableBase* Copy(void) const; +private: + uint32_t count; + uint32_t next; + double* data; +}; + +DeterministicVariableImpl::DeterministicVariableImpl(double* d, uint32_t c) : count(c), next(c), data(d) { // Nothing else needed } -DeterministicVariable::~DeterministicVariable() { } +DeterministicVariableImpl::~DeterministicVariableImpl() { } -double DeterministicVariable::GetValue() +double DeterministicVariableImpl::GetValue() { if (next == count) next = 0; return data[next++]; } -RandomVariable* DeterministicVariable::Copy() const +RandomVariableBase* DeterministicVariableImpl::Copy() const { - return new DeterministicVariable(*this); + return new DeterministicVariableImpl(*this); } +DeterministicVariable::DeterministicVariable(double* d, uint32_t c) + : RandomVariable (DeterministicVariableImpl (d, c)) +{} //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -// LogNormalVariable +// LogNormalVariableImpl +class LogNormalVariableImpl : public RandomVariableBase { +public: + /** + * \param mu mu parameter of the lognormal distribution + * \param sigma sigma parameter of the lognormal distribution + */ + LogNormalVariableImpl (double mu, double sigma); -RandomVariable* LogNormalVariable::Copy () const + /** + * \return A random value from this distribution + */ + virtual double GetValue (); + virtual RandomVariableBase* Copy(void) const; +public: + /** + * \param mu mu parameter of the underlying normal distribution + * \param sigma sigma parameter of the underlying normal distribution + * \return A random number from the distribution specified by mu and sigma + */ + static double GetSingleValue(double mu, double sigma); +private: + double m_mu; + double m_sigma; +}; + + +RandomVariableBase* LogNormalVariableImpl::Copy () const { - return new LogNormalVariable (m_mu, m_sigma); + return new LogNormalVariableImpl (m_mu, m_sigma); } -LogNormalVariable::LogNormalVariable (double mu, double sigma) +LogNormalVariableImpl::LogNormalVariableImpl (double mu, double sigma) :m_mu(mu), m_sigma(sigma) { } @@ -741,17 +1389,17 @@ LogNormalVariable::LogNormalVariable (double mu, double sigma) for x > 0. Lognormal random numbers are the exponentials of gaussian random numbers */ double -LogNormalVariable::GetValue () +LogNormalVariableImpl::GetValue () { - if(!RandomVariable::initialized) + if(!RandomVariableBase::initialized) { - RandomVariable::Initialize(); + RandomVariableBase::Initialize(); } if(!m_generator) { m_generator = new RngStream(); m_generator->InitializeStream(); - m_generator->ResetNthSubstream(RandomVariable::runNumber); + m_generator->ResetNthSubstream(RandomVariableBase::runNumber); } double u, v, r2, normal, z; @@ -774,13 +1422,13 @@ LogNormalVariable::GetValue () return z; } -double LogNormalVariable::GetSingleValue (double mu, double sigma) +double LogNormalVariableImpl::GetSingleValue (double mu, double sigma) { - if(!RandomVariable::m_static_generator) + if(!RandomVariableBase::m_static_generator) { - RandomVariable::Initialize(); // sets the static package seed - RandomVariable::m_static_generator = new RngStream(); - RandomVariable::m_static_generator->InitializeStream(); + RandomVariableBase::Initialize(); // sets the static package seed + RandomVariableBase::m_static_generator = new RngStream(); + RandomVariableBase::m_static_generator->InitializeStream(); } double u, v, r2, normal, z; do @@ -801,29 +1449,78 @@ double LogNormalVariable::GetSingleValue (double mu, double sigma) return z; } +LogNormalVariable::LogNormalVariable (double mu, double sigma) + : RandomVariable (LogNormalVariableImpl (mu, sigma)) +{} +double +LogNormalVariable::GetSingleValue(double mu, double sigma) +{ + return LogNormalVariableImpl::GetSingleValue (mu, sigma); +} + + //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -// TriangularVariable methods -TriangularVariable::TriangularVariable() +// TriangularVariableImpl methods +class TriangularVariableImpl : public RandomVariableBase { +public: + /** + * Creates a triangle distribution random number generator in the + * range [0.0 .. 1.0), with mean of 0.5 + */ + TriangularVariableImpl(); + + /** + * Creates a triangle distribution random number generator with the specified + * range + * \param s Low end of the range + * \param l High end of the range + * \param mean mean of the distribution + */ + TriangularVariableImpl(double s, double l, double mean); + + TriangularVariableImpl(const TriangularVariableImpl& c); + + /** + * \return A value from this distribution + */ + virtual double GetValue(); + virtual RandomVariableBase* Copy(void) const; +public: + /** + * \param s Low end of the range + * \param l High end of the range + * \param mean mean of the distribution + * \return A triangularly distributed random number between s and l + */ + static double GetSingleValue(double s, double l, double mean); +private: + double m_min; + double m_max; + double m_mode; //easier to work with the mode internally instead of the mean + //they are related by the simple: mean = (min+max+mode)/3 +}; + +TriangularVariableImpl::TriangularVariableImpl() : m_min(0), m_max(1), m_mode(0.5) { } -TriangularVariable::TriangularVariable(double s, double l, double mean) +TriangularVariableImpl::TriangularVariableImpl(double s, double l, double mean) : m_min(s), m_max(l), m_mode(3.0*mean-s-l) { } -TriangularVariable::TriangularVariable(const TriangularVariable& c) - : RandomVariable(c), m_min(c.m_min), m_max(c.m_max), m_mode(c.m_mode) { } +TriangularVariableImpl::TriangularVariableImpl(const TriangularVariableImpl& c) + : RandomVariableBase(c), m_min(c.m_min), m_max(c.m_max), m_mode(c.m_mode) { } -double TriangularVariable::GetValue() +double TriangularVariableImpl::GetValue() { - if(!RandomVariable::initialized) + if(!RandomVariableBase::initialized) { - RandomVariable::Initialize(); + RandomVariableBase::Initialize(); } if(!m_generator) { m_generator = new RngStream(); m_generator->InitializeStream(); - m_generator->ResetNthSubstream(RandomVariable::runNumber); + m_generator->ResetNthSubstream(RandomVariableBase::runNumber); } double u = m_generator->RandU01(); if(u <= (m_mode - m_min) / (m_max - m_min) ) @@ -832,18 +1529,18 @@ double TriangularVariable::GetValue() return m_max - sqrt( (1-u) * (m_max - m_min) * (m_max - m_mode) ); } -RandomVariable* TriangularVariable::Copy() const +RandomVariableBase* TriangularVariableImpl::Copy() const { - return new TriangularVariable(*this); + return new TriangularVariableImpl(*this); } -double TriangularVariable::GetSingleValue(double s, double l, double mean) +double TriangularVariableImpl::GetSingleValue(double s, double l, double mean) { - if(!RandomVariable::m_static_generator) + if(!RandomVariableBase::m_static_generator) { - RandomVariable::Initialize(); // sets the static package seed - RandomVariable::m_static_generator = new RngStream(); - RandomVariable::m_static_generator->InitializeStream(); + RandomVariableBase::Initialize(); // sets the static package seed + RandomVariableBase::m_static_generator = new RngStream(); + RandomVariableBase::m_static_generator->InitializeStream(); } double mode = 3.0*mean-s-l; double u = m_static_generator->RandU01(); @@ -853,6 +1550,19 @@ double TriangularVariable::GetSingleValue(double s, double l, double mean) return l - sqrt( (1-u) * (l - s) * (l - mode) ); } +TriangularVariable::TriangularVariable() + : RandomVariable (TriangularVariableImpl ()) +{} +TriangularVariable::TriangularVariable(double s, double l, double mean) + : RandomVariable (TriangularVariableImpl (s,l,mean)) +{} +double +TriangularVariable::GetSingleValue(double s, double l, double mean) +{ + return TriangularVariableImpl::GetSingleValue (s,l,mean); +} + + }//namespace ns3 diff --git a/src/core/random-variable.h b/src/core/random-variable.h index 67661f7d3..dd0280745 100644 --- a/src/core/random-variable.h +++ b/src/core/random-variable.h @@ -33,7 +33,7 @@ namespace ns3{ -class RngStream; +class RandomVariableBase; /** * \brief The basic RNG for NS-3. @@ -44,46 +44,31 @@ class RngStream; * the University of Montreal. * * NS-3 has a rich set of random number generators. - * Class RandomVariable defines the base class functionalty + * Class RandomVariableBase defines the base class functionalty * required for all random number generators. By default, the underlying * generator is seeded with the time of day, and then deterministically * creates a sequence of seeds for each subsequent generator that is created. * The rest of the documentation outlines how to change this behavior. */ -class RandomVariable { - +class RandomVariable +{ public: - /** - * \brief Constructor for a random number generator with a random seed. - */ RandomVariable(); - - /** - * \brief Copy constructor - */ - RandomVariable(const RandomVariable&); - - /** - * \brief Destructor for a random number generator with a random seed. - */ - virtual ~RandomVariable(); + RandomVariable(const RandomVariable&o); + RandomVariable &operator = (const RandomVariable &o); + ~RandomVariable(); /** * \brief Returns a random double from the underlying distribution * \return A floating point random value */ - virtual double GetValue() = 0; + double GetValue (void) const; /** * \brief Returns a random integer integer from the underlying distribution * \return Integer cast of ::GetValue() */ - virtual uint32_t GetIntValue(); - - /** - * \return A copy of this object - */ - virtual RandomVariable* Copy() const = 0; + uint32_t GetIntValue (void) const; /** * \brief Get the internal state of the RNG @@ -94,7 +79,7 @@ public: * \param seed Output parameter; gets overwritten with the internal state of * of the RNG. */ - virtual void GetSeed(uint32_t seed[6]); + void GetSeed(uint32_t seed[6]) const; /** * \brief Set seeding behavior @@ -104,7 +89,7 @@ public: * generator is seeded with data from /dev/random instead of * being seeded based upon the time of day. For this to be effective, * it must be called before the creation of the first instance of a - * RandomVariable or subclass. Example: + * RandomVariableBase or subclass. Example: * \code * RandomVariable::UseDevRandom(); * UniformVariable x(2,3); //these are seeded randomly @@ -174,23 +159,12 @@ public: */ static void SetRunNumber(uint32_t n); private: - static void GetRandomSeeds(uint32_t seeds[6]); -private: - static bool useDevRandom; // True if using /dev/random desired - static bool globalSeedSet; // True if global seed has been specified - static int devRandom; // File handle for /dev/random - static uint32_t globalSeed[6]; // The global seed to use - friend class RandomVariableInitializer; + RandomVariableBase *m_variable; protected: - static unsigned long heuristic_sequence; - static RngStream* m_static_generator; - static uint32_t runNumber; - static void Initialize(); // Initialize the RNG system - static bool initialized; // True if package seed is set - RngStream* m_generator; //underlying generator being wrapped + RandomVariable (const RandomVariableBase &variable); + RandomVariableBase *Peek (void); }; - /** * \brief The uniform distribution RNG for NS-3. * \ingroup randomvariable @@ -207,7 +181,8 @@ protected: * UniformVariable::GetSingleValue(100,1000); //returns a value [100,1000) * \endcode */ -class UniformVariable : public RandomVariable { +class UniformVariable : public RandomVariable +{ public: /** * Creates a uniform random number generator in the @@ -221,14 +196,6 @@ public: * \param l High end of the range */ UniformVariable(double s, double l); - - UniformVariable(const UniformVariable& c); - - /** - * \return A value between low and high values specified by the constructor - */ - virtual double GetValue(); - virtual RandomVariable* Copy() const; public: /** * \param s Low end of the range @@ -236,50 +203,36 @@ public: * \return A uniformly distributed random number between s and l */ static double GetSingleValue(double s, double l); -private: - double m_min; - double m_max; }; /** * \brief A random variable that returns a constant * \ingroup randomvariable * - * Class ConstantVariable defines a random number generator that + * Class ConstantVariableImpl defines a random number generator that * returns the same value every sample. */ class ConstantVariable : public RandomVariable { public: /** - * Construct a ConstantVariable RNG that returns zero every sample + * Construct a ConstantVariableImpl RNG that returns zero every sample */ ConstantVariable(); /** - * Construct a ConstantVariable RNG that returns the specified value + * Construct a ConstantVariableImpl RNG that returns the specified value * every sample. * \param c Unchanging value for this RNG. */ ConstantVariable(double c); - - ConstantVariable(const ConstantVariable& c) ; - /** * \brief Specify a new constant RNG for this generator. * \param c New constant value for this RNG. */ - void NewConstant(double c); + void SetConstant(double c); - /** - * \return The constant value specified - */ - virtual double GetValue(); - virtual uint32_t GetIntValue(); - virtual RandomVariable* Copy() const; -private: - double m_const; }; /** @@ -291,14 +244,14 @@ private: * increases for a period, then wraps around to the low value * and begins monotonicaly increasing again. */ -class SequentialVariable : public RandomVariable { - +class SequentialVariable : public RandomVariable +{ public: /** - * \brief Constructor for the SequentialVariable RNG. + * \brief Constructor for the SequentialVariableImpl RNG. * * The four parameters define the sequence. For example - * SequentialVariable(0,5,1,2) creates a RNG that has the sequence + * SequentialVariableImpl(0,5,1,2) creates a RNG that has the sequence * 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 0, 0 ... * \param f First value of the sequence. * \param l One more than the last value of the sequence. @@ -308,32 +261,17 @@ public: SequentialVariable(double f, double l, double i = 1, uint32_t c = 1); /** - * \brief Constructor for the SequentialVariable RNG. + * \brief Constructor for the SequentialVariableImpl RNG. * * Differs from the first only in that the increment parameter is a * random variable * \param f First value of the sequence. * \param l One more than the last value of the sequence. - * \param i Reference to a RandomVariable for the sequence increment + * \param i Reference to a RandomVariableBase for the sequence increment * \param c Number of times each member of the sequence is repeated */ SequentialVariable(double f, double l, const RandomVariable& i, uint32_t c = 1); - SequentialVariable(const SequentialVariable& c); - - ~SequentialVariable(); - /** - * \return The next value in the Sequence - */ - virtual double GetValue(); - virtual RandomVariable* Copy() const; -private: - double m_min; - double m_max; - RandomVariable* m_increment; - uint32_t m_consecutive; - double m_current; - uint32_t m_currentConsecutive; }; /** @@ -353,14 +291,15 @@ private: * \f$ \left\{ \begin{array}{cl} \alpha e^{-\alpha x} & x < bound \\ bound & x > bound \end{array}\right. \f$ * * \code - * ExponentialVariable x(3.14); + * ExponentialVariableImpl x(3.14); * x.GetValue(); //will always return with mean 3.14 - * ExponentialVariable::GetSingleValue(20.1); //returns with mean 20.1 - * ExponentialVariable::GetSingleValue(108); //returns with mean 108 + * ExponentialVariableImpl::GetSingleValue(20.1); //returns with mean 20.1 + * ExponentialVariableImpl::GetSingleValue(108); //returns with mean 108 * \endcode * */ -class ExponentialVariable : public RandomVariable { +class ExponentialVariable : public RandomVariable +{ public: /** * Constructs an exponential random variable with a mean @@ -387,27 +326,16 @@ public: */ ExponentialVariable(double m, double b); - ExponentialVariable(const ExponentialVariable& c); - - /** - * \return A random value from this exponential distribution - */ - virtual double GetValue(); - virtual RandomVariable* Copy() const; -public: /** * \param m The mean of the distribution from which the return value is drawn * \param b The upper bound value desired, beyond which values get clipped * \return A random number from an exponential distribution with mean m */ static double GetSingleValue(double m, double b=0); -private: - double m_mean; // Mean value of RV - double m_bound; // Upper bound on value (if non-zero) }; /** - * \brief ParetoVariable distributed random var + * \brief ParetoVariableImpl distributed random var * \ingroup randomvariable * * This class supports the creation of objects that return random numbers @@ -422,19 +350,20 @@ private: * with the equation \f$ x_m = mean \frac{k-1}{k}, k > 1\f$. * * \code - * ParetoVariable x(3.14); + * ParetoVariableImpl x(3.14); * x.GetValue(); //will always return with mean 3.14 - * ParetoVariable::GetSingleValue(20.1); //returns with mean 20.1 - * ParetoVariable::GetSingleValue(108); //returns with mean 108 + * ParetoVariableImpl::GetSingleValue(20.1); //returns with mean 20.1 + * ParetoVariableImpl::GetSingleValue(108); //returns with mean 108 * \endcode */ -class ParetoVariable : public RandomVariable { +class ParetoVariable : public RandomVariable +{ public: /** * Constructs a pareto random variable with a mean of 1 and a shape * parameter of 1.5 */ - ParetoVariable(); + ParetoVariable (); /** * Constructs a pareto random variable with specified mean and shape @@ -465,14 +394,6 @@ public: */ ParetoVariable(double m, double s, double b); - ParetoVariable(const ParetoVariable& c); - - /** - * \return A random value from this Pareto distribution - */ - virtual double GetValue(); - virtual RandomVariable* Copy() const; -public: /** * \param m The mean value of the distribution from which the return value * is drawn. @@ -483,14 +404,10 @@ public: * parameter s. */ static double GetSingleValue(double m, double s, double b=0); -private: - double m_mean; // Mean value of RV - double m_shape; // Shape parameter - double m_bound; // Upper bound on value (if non-zero) }; /** - * \brief WeibullVariable distributed random var + * \brief WeibullVariableImpl distributed random var * \ingroup randomvariable * * This class supports the creation of objects that return random numbers @@ -530,7 +447,7 @@ public: /** * \brief Constructs a weibull random variable with the specified mean * \brief value, shape (alpha), and upper bound. - * Since WeibullVariable distributions can theoretically return unbounded values, + * Since WeibullVariableImpl distributions can theoretically return unbounded values, * it is sometimes usefull to specify a fixed upper limit. Note however * that when the upper limit is specified, the true mean of the distribution * is slightly smaller than the mean value specified. @@ -539,15 +456,6 @@ public: * \param b Upper limit on returned values */ WeibullVariable(double m, double s, double b); - - WeibullVariable(const WeibullVariable& c); - - /** - * \return A random value from this Weibull distribution - */ - virtual double GetValue(); - virtual RandomVariable* Copy() const; -public: /** * \param m Mean value for the distribution. * \param s Shape (alpha) parameter for the distribution. @@ -555,14 +463,10 @@ public: * \return Random number from a distribution specified by m,s, and b */ static double GetSingleValue(double m, double s, double b=0); -private: - double m_mean; // Mean value of RV - double m_alpha; // Shape parameter - double m_bound; // Upper bound on value (if non-zero) }; /** - * \brief Class NormalVariable defines a random variable with a + * \brief Class NormalVariableImpl defines a random variable with a * normal (Gaussian) distribution. * \ingroup randomvariable * @@ -575,8 +479,8 @@ private: * where \f$ mean = \mu \f$ and \f$ variance = \sigma^2 \f$ * */ -class NormalVariable : public RandomVariable { // Normally Distributed random var - +class NormalVariable : public RandomVariable +{ public: static const double INFINITE_VALUE; /** @@ -589,37 +493,20 @@ public: * \brief Construct a normal random variable with specified mean and variance * \param m Mean value * \param v Variance - * \param b Bound. The NormalVariable is bounded within +-bound. + * \param b Bound. The NormalVariableImpl is bounded within +-bound. */ NormalVariable(double m, double v, double b = INFINITE_VALUE); - - NormalVariable(const NormalVariable& c); - - /** - * \return A value from this normal distribution - */ - virtual double GetValue(); - virtual RandomVariable* Copy() const; -public: /** * \param m Mean value * \param v Variance - * \param b Bound. The NormalVariable is bounded within +-bound. + * \param b Bound. The NormalVariableImpl is bounded within +-bound. * \return A random number from a distribution specified by m,v, and b. */ static double GetSingleValue(double m, double v, double b = INFINITE_VALUE); -private: - double m_mean; // Mean value of RV - double m_variance; // Mean value of RV - double m_bound; // Bound on value (absolute value) - bool m_nextValid; // True if next valid - double m_next; // The algorithm produces two values at a time - static bool m_static_nextValid; - static double m_static_next; }; /** - * \brief EmpiricalVariable distribution random var + * \brief EmpiricalVariableImpl distribution random var * \ingroup randomvariable * * Defines a random variable that has a specified, empirical @@ -634,37 +521,18 @@ private: class EmpiricalVariable : public RandomVariable { public: /** - * Constructor for the EmpiricalVariable random variables. + * Constructor for the EmpiricalVariableImpl random variables. */ explicit EmpiricalVariable(); - virtual ~EmpiricalVariable(); - EmpiricalVariable(const EmpiricalVariable& c); - /** - * \return A value from this empirical distribution - */ - virtual double GetValue(); - virtual RandomVariable* Copy() const; /** * \brief Specifies a point in the empirical distribution * \param v The function value for this point * \param c Probability that the function is less than or equal to v */ - virtual void CDF(double v, double c); // Value, prob <= Value - -private: - class ValueCDF { - public: - ValueCDF(); - ValueCDF(double v, double c); - ValueCDF(const ValueCDF& c); - double value; - double cdf; - }; - virtual void Validate(); // Insure non-decreasing emiprical values - virtual double Interpolate(double, double, double, double, double); - bool validated; // True if non-decreasing validated - std::vector emp; // Empicical CDF + void CDF(double v, double c); // Value, prob <= Value +protected: + EmpiricalVariable (const RandomVariableBase &variable); }; /** @@ -672,20 +540,13 @@ private: * \ingroup randomvariable * * Defines an empirical distribution where all values are integers. - * Indentical to EmpiricalVariable, but with slightly different + * Indentical to EmpiricalVariableImpl, but with slightly different * interpolation between points. */ -class IntEmpiricalVariable : public EmpiricalVariable { +class IntEmpiricalVariable : public EmpiricalVariable +{ public: - IntEmpiricalVariable(); - - virtual RandomVariable* Copy() const; - /** - * \return An integer value from this empirical distribution - */ - virtual uint32_t GetIntValue(); - virtual double Interpolate(double, double, double, double, double); }; /** @@ -697,8 +558,8 @@ public: * the RNG to return a known sequence, perhaps to * compare NS-3 to some other simulator */ -class DeterministicVariable : public RandomVariable { - +class DeterministicVariable : public RandomVariable +{ public: /** * \brief Constructor @@ -707,22 +568,11 @@ public: * on successive calls to ::Value(). Note that the d pointer is copied * for use by the generator (shallow-copy), not its contents, so the * contents of the array d points to have to remain unchanged for the use - * of DeterministicVariable to be meaningful. + * of DeterministicVariableImpl to be meaningful. * \param d Pointer to array of random values to return in sequence * \param c Number of values in the array */ explicit DeterministicVariable(double* d, uint32_t c); - - virtual ~DeterministicVariable(); - /** - * \return The next value in the deterministic sequence - */ - virtual double GetValue(); - virtual RandomVariable* Copy() const; -private: - uint32_t count; - uint32_t next; - double* data; }; @@ -730,7 +580,7 @@ private: * \brief Log-normal Distributed random var * \ingroup randomvariable * - * LogNormalVariable defines a random variable with log-normal + * LogNormalVariableImpl defines a random variable with log-normal * distribution. If one takes the natural logarithm of random * variable following the log-normal distribution, the obtained values * follow a normal distribution. @@ -748,7 +598,8 @@ private: * \f$ \mu = ln(mean) - \frac{1}{2}ln\left(1+\frac{stddev}{mean^2}\right)\f$, and, * \f$ \sigma = \sqrt{ln\left(1+\frac{stddev}{mean^2}\right)}\f$ */ -class LogNormalVariable : public RandomVariable { +class LogNormalVariable : public RandomVariable +{ public: /** * \param mu mu parameter of the lognormal distribution @@ -756,21 +607,12 @@ public: */ LogNormalVariable (double mu, double sigma); - /** - * \return A random value from this distribution - */ - virtual double GetValue (); - virtual RandomVariable* Copy() const; -public: /** * \param mu mu parameter of the underlying normal distribution * \param sigma sigma parameter of the underlying normal distribution * \return A random number from the distribution specified by mu and sigma */ static double GetSingleValue(double mu, double sigma); -private: - double m_mu; - double m_sigma; }; /** @@ -780,7 +622,8 @@ private: * This distribution is a triangular distribution. The probablility density * is in the shape of a triangle. */ -class TriangularVariable : public RandomVariable { +class TriangularVariable : public RandomVariable +{ public: /** * Creates a triangle distribution random number generator in the @@ -796,15 +639,6 @@ public: * \param mean mean of the distribution */ TriangularVariable(double s, double l, double mean); - - TriangularVariable(const TriangularVariable& c); - - /** - * \return A value from this distribution - */ - virtual double GetValue(); - virtual RandomVariable* Copy() const; -public: /** * \param s Low end of the range * \param l High end of the range @@ -812,11 +646,6 @@ public: * \return A triangularly distributed random number between s and l */ static double GetSingleValue(double s, double l, double mean); -private: - double m_min; - double m_max; - double m_mode; //easier to work with the mode internally instead of the mean - //they are related by the simple: mean = (min+max+mode)/3 }; }//namespace ns3 diff --git a/src/devices/wifi/propagation-delay-model.cc b/src/devices/wifi/propagation-delay-model.cc index e62edf12c..b55154766 100644 --- a/src/devices/wifi/propagation-delay-model.cc +++ b/src/devices/wifi/propagation-delay-model.cc @@ -69,20 +69,18 @@ PropagationDelayModel::CreateDefault (void) } RandomPropagationDelayModel::RandomPropagationDelayModel () - : m_variable (g_random.GetCopy ()) + : m_variable (g_random.Get ()) {} RandomPropagationDelayModel::RandomPropagationDelayModel (const RandomVariable &variable) - : m_variable (variable.Copy ()) + : m_variable (variable) {} RandomPropagationDelayModel::~RandomPropagationDelayModel () -{ - delete m_variable; -} +{} Time RandomPropagationDelayModel::GetDelay (Ptr a, Ptr b) const { - return Seconds (m_variable->GetValue ()); + return Seconds (m_variable.GetValue ()); } ConstantSpeedPropagationDelayModel::ConstantSpeedPropagationDelayModel (double speed) diff --git a/src/devices/wifi/propagation-delay-model.h b/src/devices/wifi/propagation-delay-model.h index e1fd752f2..4fb80f685 100644 --- a/src/devices/wifi/propagation-delay-model.h +++ b/src/devices/wifi/propagation-delay-model.h @@ -23,13 +23,12 @@ #include "ns3/ptr.h" #include "ns3/object.h" #include "ns3/nstime.h" +#include "ns3/random-variable.h" namespace ns3 { class MobilityModel; -class RandomVariable; - /** * \brief calculate a propagation delay. */ @@ -71,7 +70,7 @@ public: virtual ~RandomPropagationDelayModel (); virtual Time GetDelay (Ptr a, Ptr b) const; private: - RandomVariable *m_variable; + RandomVariable m_variable; }; /** diff --git a/src/devices/wifi/propagation-loss-model.cc b/src/devices/wifi/propagation-loss-model.cc index c74e258b7..29f3475e8 100644 --- a/src/devices/wifi/propagation-loss-model.cc +++ b/src/devices/wifi/propagation-loss-model.cc @@ -104,23 +104,21 @@ PropagationLossModel::CreateDefault (void) } } RandomPropagationLossModel::RandomPropagationLossModel () - : m_variable (g_random.GetCopy ()) + : m_variable (g_random.Get ()) {} RandomPropagationLossModel::RandomPropagationLossModel (const RandomVariable &variable) - : m_variable (variable.Copy ()) + : m_variable (variable) {} RandomPropagationLossModel::~RandomPropagationLossModel () -{ - delete m_variable; -} +{} double RandomPropagationLossModel::GetRxPower (double txPowerDbm, Ptr a, Ptr b) const { - double rxPower = txPowerDbm - m_variable->GetValue (); + double rxPower = txPowerDbm - m_variable.GetValue (); NS_LOG_DEBUG ("tx power="< a, Ptr b) const; private: - RandomVariable *m_variable; + RandomVariable m_variable; }; /** diff --git a/src/mobility/random-direction-2d-mobility-model.cc b/src/mobility/random-direction-2d-mobility-model.cc index 9f13588dc..fb6703d3d 100644 --- a/src/mobility/random-direction-2d-mobility-model.cc +++ b/src/mobility/random-direction-2d-mobility-model.cc @@ -54,8 +54,8 @@ static RectangleDefaultValue RandomDirection2dMobilityModelParameters::RandomDirection2dMobilityModelParameters () : m_bounds (g_bounds.GetValue ()), - m_speedVariable (g_speedVariable.GetCopy ()), - m_pauseVariable (g_pauseVariable.GetCopy ()) + m_speedVariable (g_speedVariable.Get ()), + m_pauseVariable (g_pauseVariable.Get ()) {} RandomDirection2dMobilityModelParameters::RandomDirection2dMobilityModelParameters @@ -63,29 +63,22 @@ RandomDirection2dMobilityModelParameters::RandomDirection2dMobilityModelParamete const RandomVariable &speedVariable, const RandomVariable &pauseVariable) : m_bounds (bounds), - m_speedVariable (speedVariable.Copy ()), - m_pauseVariable (pauseVariable.Copy ()) + m_speedVariable (speedVariable), + m_pauseVariable (pauseVariable) {} RandomDirection2dMobilityModelParameters::~RandomDirection2dMobilityModelParameters () -{ - delete m_speedVariable; - delete m_pauseVariable; - m_speedVariable = 0; - m_pauseVariable = 0; -} +{} void RandomDirection2dMobilityModelParameters::SetSpeed (const RandomVariable &speedVariable) { - delete m_speedVariable; - m_speedVariable = speedVariable.Copy (); + m_speedVariable = speedVariable; } void RandomDirection2dMobilityModelParameters::SetPause (const RandomVariable &pauseVariable) { - delete m_pauseVariable; - m_pauseVariable = pauseVariable.Copy (); + m_pauseVariable = pauseVariable; } void RandomDirection2dMobilityModelParameters::SetBounds (const Rectangle &bounds) @@ -149,7 +142,7 @@ RandomDirection2dMobilityModel::Start (void) void RandomDirection2dMobilityModel::BeginPause (void) { - Time pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); + Time pause = Seconds (m_parameters->m_pauseVariable.GetValue ()); m_helper.Pause (); m_event = Simulator::Schedule (pause, &RandomDirection2dMobilityModel::ResetDirectionAndSpeed, this); NotifyCourseChange (); @@ -159,7 +152,7 @@ void RandomDirection2dMobilityModel::SetDirectionAndSpeed (double direction) { NS_LOG_FUNCTION; - double speed = m_parameters->m_speedVariable->GetValue (); + double speed = m_parameters->m_speedVariable.GetValue (); const Vector vector (std::cos (direction) * speed, std::sin (direction) * speed, 0.0); diff --git a/src/mobility/random-direction-2d-mobility-model.h b/src/mobility/random-direction-2d-mobility-model.h index 14fd06611..f12e5b8cb 100644 --- a/src/mobility/random-direction-2d-mobility-model.h +++ b/src/mobility/random-direction-2d-mobility-model.h @@ -26,13 +26,12 @@ #include "ns3/nstime.h" #include "ns3/event-id.h" #include "ns3/rectangle.h" +#include "ns3/random-variable.h" #include "mobility-model.h" #include "static-speed-helper.h" namespace ns3 { -class RandomVariable; - /** * \brief the parameters to control a RandomDirection mobility model. */ @@ -73,8 +72,8 @@ class RandomDirection2dMobilityModelParameters : public Object static Ptr GetCurrent (void); Rectangle m_bounds; - RandomVariable *m_speedVariable; - RandomVariable *m_pauseVariable; + RandomVariable m_speedVariable; + RandomVariable m_pauseVariable; }; /** diff --git a/src/mobility/random-position.cc b/src/mobility/random-position.cc index 8823b2262..5db9f89f0 100644 --- a/src/mobility/random-position.cc +++ b/src/mobility/random-position.cc @@ -88,26 +88,21 @@ RandomRectanglePosition::GetTypeId (void) } RandomRectanglePosition::RandomRectanglePosition () - : m_x (g_rectangleX.GetCopy ()), - m_y (g_rectangleY.GetCopy ()) + : m_x (g_rectangleX.Get ()), + m_y (g_rectangleY.Get ()) {} RandomRectanglePosition::RandomRectanglePosition (const RandomVariable &x, const RandomVariable &y) - : m_x (x.Copy ()), - m_y (y.Copy ()) + : m_x (x), + m_y (y) {} RandomRectanglePosition::~RandomRectanglePosition () -{ - delete m_x; - delete m_y; - m_x = 0; - m_y = 0; -} +{} Vector RandomRectanglePosition::Get (void) const { - double x = m_x->GetValue (); - double y = m_y->GetValue (); + double x = m_x.GetValue (); + double y = m_y.GetValue (); return Vector (x, y, 0.0); } @@ -123,31 +118,26 @@ RandomDiscPosition::GetTypeId (void) } RandomDiscPosition::RandomDiscPosition () - : m_theta (g_discTheta.GetCopy ()), - m_rho (g_discRho.GetCopy ()), + : m_theta (g_discTheta.Get ()), + m_rho (g_discRho.Get ()), m_x (g_discX.GetValue ()), m_y (g_discY.GetValue ()) {} RandomDiscPosition::RandomDiscPosition (const RandomVariable &theta, const RandomVariable &rho, double x, double y) - : m_theta (theta.Copy ()), - m_rho (rho.Copy ()), + : m_theta (theta), + m_rho (rho), m_x (0.0), m_y (0.0) {} RandomDiscPosition::~RandomDiscPosition () -{ - delete m_theta; - delete m_rho; - m_theta = 0; - m_rho = 0; -} +{} Vector RandomDiscPosition::Get (void) const { - double theta = m_theta->GetValue (); - double rho = m_rho->GetValue (); + double theta = m_theta.GetValue (); + double rho = m_rho.GetValue (); double x = m_x + std::cos (theta) * rho; double y = m_y + std::sin (theta) * rho; NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y); diff --git a/src/mobility/random-position.h b/src/mobility/random-position.h index c9f0c65f8..2e4fc28cc 100644 --- a/src/mobility/random-position.h +++ b/src/mobility/random-position.h @@ -21,12 +21,11 @@ #define RANDOM_POSITION_H #include "ns3/object.h" +#include "ns3/random-variable.h" #include "vector.h" namespace ns3 { -class RandomVariable; - /** * \brief choose a position at random. * @@ -69,8 +68,8 @@ public: virtual ~RandomRectanglePosition (); virtual Vector Get (void) const; private: - RandomVariable *m_x; - RandomVariable *m_y; + RandomVariable m_x; + RandomVariable m_y; }; /** @@ -107,8 +106,8 @@ public: virtual ~RandomDiscPosition (); virtual Vector Get (void) const; private: - RandomVariable *m_theta; - RandomVariable *m_rho; + RandomVariable m_theta; + RandomVariable m_rho; double m_x; double m_y; }; diff --git a/src/mobility/random-walk-2d-mobility-model.cc b/src/mobility/random-walk-2d-mobility-model.cc index 5210f9d7d..bf488ece6 100644 --- a/src/mobility/random-walk-2d-mobility-model.cc +++ b/src/mobility/random-walk-2d-mobility-model.cc @@ -69,30 +69,23 @@ RandomWalk2dMobilityModelParameters::RandomWalk2dMobilityModelParameters () : m_mode (g_mode.GetValue ()), m_modeDistance (g_modeDistance.GetValue ()), m_modeTime (g_modeTime.GetValue ()), - m_speed (g_speed.GetCopy ()), - m_direction (g_direction.GetCopy ()), + m_speed (g_speed.Get ()), + m_direction (g_direction.Get ()), m_bounds (g_rectangle.GetValue ()) {} RandomWalk2dMobilityModelParameters::~RandomWalk2dMobilityModelParameters () -{ - delete m_speed; - delete m_direction; - m_speed = 0; - m_direction = 0; -} +{} void RandomWalk2dMobilityModelParameters::SetSpeed (const RandomVariable &speed) { - delete m_speed; - m_speed = speed.Copy (); + m_speed = speed; } void RandomWalk2dMobilityModelParameters::SetDirection (const RandomVariable &direction) { - delete m_direction; - m_direction = direction.Copy (); + m_direction = direction; } void RandomWalk2dMobilityModelParameters::SetModeDistance (double distance) @@ -154,8 +147,8 @@ RandomWalk2dMobilityModel::RandomWalk2dMobilityModel (Ptrm_speed->GetValue (); - double direction = m_parameters->m_direction->GetValue (); + double speed = m_parameters->m_speed.GetValue (); + double direction = m_parameters->m_direction.GetValue (); Vector vector (std::cos (direction) * speed, std::sin (direction) * speed, 0.0); diff --git a/src/mobility/random-walk-2d-mobility-model.h b/src/mobility/random-walk-2d-mobility-model.h index 376152d52..600ed1435 100644 --- a/src/mobility/random-walk-2d-mobility-model.h +++ b/src/mobility/random-walk-2d-mobility-model.h @@ -25,12 +25,12 @@ #include "ns3/nstime.h" #include "ns3/event-id.h" #include "ns3/rectangle.h" +#include "ns3/random-variable.h" #include "mobility-model.h" #include "static-speed-helper.h" namespace ns3 { -class RandomVariable; /** * \brief parameters to control a random walk 2d model @@ -93,8 +93,8 @@ class RandomWalk2dMobilityModelParameters : public Object enum Mode m_mode; double m_modeDistance; Time m_modeTime; - RandomVariable *m_speed; - RandomVariable *m_direction; + RandomVariable m_speed; + RandomVariable m_direction; Rectangle m_bounds; }; diff --git a/src/mobility/random-waypoint-mobility-model.cc b/src/mobility/random-waypoint-mobility-model.cc index 408a6f38d..1fb9bb91a 100644 --- a/src/mobility/random-waypoint-mobility-model.cc +++ b/src/mobility/random-waypoint-mobility-model.cc @@ -47,16 +47,16 @@ g_position ("RandomWaypointPosition", RandomWaypointMobilityModelParameters::RandomWaypointMobilityModelParameters () - : m_speed (g_speed.GetCopy ()), - m_pause (g_pause.GetCopy ()) + : m_speed (g_speed.Get ()), + m_pause (g_pause.Get ()) { m_position = g_position.GetValue ().CreateObject ()->QueryInterface (); } RandomWaypointMobilityModelParameters::RandomWaypointMobilityModelParameters (Ptr randomPosition, const RandomVariable &speed, const RandomVariable &pause) - : m_speed (speed.Copy ()), - m_pause (pause.Copy ()), + : m_speed (speed), + m_pause (pause), m_position (randomPosition) {} void @@ -67,23 +67,17 @@ RandomWaypointMobilityModelParameters::SetWaypointPositionModel (Ptr @@ -128,7 +122,7 @@ RandomWaypointMobilityModel::BeginWalk (void) { Vector m_current = m_helper.GetCurrentPosition (); Vector destination = m_parameters->m_position->Get (); - double speed = m_parameters->m_speed->GetValue (); + double speed = m_parameters->m_speed.GetValue (); double dx = (destination.x - m_current.x); double dy = (destination.y - m_current.y); double dz = (destination.z - m_current.z); @@ -144,7 +138,7 @@ RandomWaypointMobilityModel::BeginWalk (void) void RandomWaypointMobilityModel::Start (void) { - Time pause = Seconds (m_parameters->m_pause->GetValue ()); + Time pause = Seconds (m_parameters->m_pause.GetValue ()); m_helper.Pause (); NotifyCourseChange (); m_event = Simulator::Schedule (pause, &RandomWaypointMobilityModel::BeginWalk, this); diff --git a/src/mobility/random-waypoint-mobility-model.h b/src/mobility/random-waypoint-mobility-model.h index d6dd0ac53..7b3288177 100644 --- a/src/mobility/random-waypoint-mobility-model.h +++ b/src/mobility/random-waypoint-mobility-model.h @@ -25,11 +25,10 @@ #include "mobility-model.h" #include "random-position.h" #include "ns3/ptr.h" +#include "ns3/random-variable.h" namespace ns3 { -class RandomVariable; - /** * \brief the parameters which control the behavior of a random waypoint * mobility model. @@ -66,8 +65,8 @@ private: friend class RandomWaypointMobilityModel; static Ptr GetCurrent (void); virtual void DoDispose (void); - RandomVariable *m_speed; - RandomVariable *m_pause; + RandomVariable m_speed; + RandomVariable m_pause; Ptr m_position; }; diff --git a/src/node/application.cc b/src/node/application.cc index 3346da3c2..73d8b47ed 100644 --- a/src/node/application.cc +++ b/src/node/application.cc @@ -59,9 +59,8 @@ void Application::Start(const Time& startTime) void Application::Start(const RandomVariable& startVar) { - RandomVariable *v = startVar.Copy (); - ScheduleStart (Seconds (v->GetValue ())); - delete v; + RandomVariable v = startVar; + ScheduleStart (Seconds (v.GetValue ())); } @@ -72,9 +71,8 @@ void Application::Stop(const Time& stopTime) void Application::Stop(const RandomVariable& stopVar) { - RandomVariable *v = stopVar.Copy (); - ScheduleStop (Seconds (v->GetValue ())); - delete v; + RandomVariable v = stopVar; + ScheduleStop (Seconds (v.GetValue ())); } Ptr Application::GetNode() const From 41ebb83abb74a670287555b635be424ebe5571a2 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 7 Feb 2008 22:40:46 +0100 Subject: [PATCH 015/130] add Value support to RandomVariable. --- src/core/random-variable.cc | 108 +++++++++++++++++++++++++++++++++++- src/core/random-variable.h | 43 +++++++++++++- src/core/value-test.cc | 10 ++++ 3 files changed, 159 insertions(+), 2 deletions(-) diff --git a/src/core/random-variable.cc b/src/core/random-variable.cc index 1b29fe312..f67f6eb29 100644 --- a/src/core/random-variable.cc +++ b/src/core/random-variable.cc @@ -28,6 +28,7 @@ #include #include #include +#include #include "assert.h" @@ -249,6 +250,10 @@ RandomVariable::RandomVariable (const RandomVariableBase &variable) RandomVariable & RandomVariable::operator = (const RandomVariable &o) { + if (&o == this) + { + return *this; + } delete m_variable; m_variable = o.m_variable->Copy (); return *this; @@ -290,10 +295,24 @@ RandomVariable::SetRunNumber(uint32_t n) RandomVariableBase::SetRunNumber (n); } RandomVariableBase * -RandomVariable::Peek (void) +RandomVariable::Peek (void) const { return m_variable; } +RandomVariable::RandomVariable (PValue value) +{ + 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 (); + +} +RandomVariable::operator PValue () const +{ + return PValue::Create (*this); +} //----------------------------------------------------------------------------- @@ -316,6 +335,9 @@ public: UniformVariableImpl(double s, double l); UniformVariableImpl(const UniformVariableImpl& c); + + double GetMin (void) const; + double GetMax (void) const; /** * \return A value between low and high values specified by the constructor @@ -344,6 +366,18 @@ UniformVariableImpl::UniformVariableImpl(double s, double l) UniformVariableImpl::UniformVariableImpl(const UniformVariableImpl& c) : RandomVariableBase(c), m_min(c.m_min), m_max(c.m_max) { } +double +UniformVariableImpl::GetMin (void) const +{ + return m_min; +} +double +UniformVariableImpl::GetMax (void) const +{ + return m_max; +} + + double UniformVariableImpl::GetValue() { if(!RandomVariableBase::initialized) @@ -1563,6 +1597,78 @@ TriangularVariable::GetSingleValue(double s, double l, double mean) } +RandomVariableValue::RandomVariableValue (RandomVariable variable) + : m_variable (variable) +{} +void +RandomVariableValue::Set (RandomVariable variable) +{ + m_variable = variable; +} +RandomVariable +RandomVariableValue::Get (void) const +{ + 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 (); + ConstantVariableImpl *constant = dynamic_cast (base); + if (constant != 0) + { + oss << "Constant:" << constant->GetValue (); + return oss.str (); + } + UniformVariableImpl *uniform = dynamic_cast (base); + if (uniform != 0) + { + oss << "Uniform:" << uniform->GetMin () << ":" << uniform->GetMax (); + return oss.str (); + } + // XXX: support other distributions + return ""; +} +bool +RandomVariableValue::DeserializeFromString (std::string value, Ptr spec) +{ + std::string::size_type tmp; + tmp = value.find (":"); + if (tmp == std::string::npos) + { + return false; + } + std::string type = value.substr (0, tmp); + if (value == "Constant") + { + // XXX + return true; + } + else if (value == "Uniform") + { + // XXX + return true; + } + // XXX: support other distributions. + return false; +} +RandomVariableValue::RandomVariableValue (PValue value) + : m_variable (value) +{} +RandomVariableValue::operator PValue () const +{ + return m_variable; +} + + + }//namespace ns3 diff --git a/src/core/random-variable.h b/src/core/random-variable.h index dd0280745..370ff9e79 100644 --- a/src/core/random-variable.h +++ b/src/core/random-variable.h @@ -24,6 +24,7 @@ #include #include #include +#include "value.h" /** * \ingroup core @@ -158,11 +159,17 @@ public: * \endcode */ static void SetRunNumber(uint32_t n); + + + RandomVariable (PValue value); + operator PValue () const; + private: + friend class RandomVariableValue; RandomVariableBase *m_variable; protected: RandomVariable (const RandomVariableBase &variable); - RandomVariableBase *Peek (void); + RandomVariableBase *Peek (void) const; }; /** @@ -648,5 +655,39 @@ public: 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; + + 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; +}; + +template +Ptr MakeRandomVariableParamSpec (RandomVariable T::*m_memberVariable, + RandomVariable initialValue); + }//namespace ns3 + + +namespace ns3 { + +template +Ptr MakeRandomVariableParamSpec (RandomVariable T::*memberVariable, + RandomVariable initialValue) +{ + return MakeMemberVariableParamSpec (memberVariable, RandomVariableValue (initialValue)); +} + +} // namespace ns3 #endif diff --git a/src/core/value-test.cc b/src/core/value-test.cc index c7d8fe24d..dc31c45b1 100644 --- a/src/core/value-test.cc +++ b/src/core/value-test.cc @@ -5,6 +5,7 @@ #include "int-value.h" #include "uint-value.h" #include "enum-value.h" +#include "random-variable.h" #if 0 #include "fp-value.h" #endif @@ -66,6 +67,9 @@ public: TEST_A, "TestA", TEST_B, "TestB", TEST_C, "TestC")) + .AddParameter ("TestRandom", "help text", + MakeRandomVariableParamSpec (&ParamSpecObjectTest::m_random, + ConstantVariable (1.0))) #if 0 .AddParameter ("TestFloat", "help text", MakeFpParamSpec (-1.1, &ParamSpecObjectTest::m_float)) @@ -97,6 +101,7 @@ private: uint8_t m_uint8; float m_float; enum TestEnum m_enum; + RandomVariable m_random; }; @@ -265,6 +270,11 @@ ParamSpecTest::RunTests (void) CHECK_GET_STR (p, "TestEnum", "TestB"); CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_B); + RandomVariable ran = p->Get ("TestRandom"); + NS_TEST_ASSERT (p->Set ("TestRandom", UniformVariable (0.0, 1.0))); + NS_TEST_ASSERT (p->Set ("TestRandom", ConstantVariable (10.0))); + + #if 0 p->Set ("TestBoolName", "true"); NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "true"); From b2fc672bb22f9fdf8e2ac509883b335f590ebf2f Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 7 Feb 2008 23:23:33 +0100 Subject: [PATCH 016/130] add back FpValue support --- src/core/fp-value.cc | 74 +++++++++++++++++++++++++++++++++++++++ src/core/fp-value.h | 78 ++++++++++++++++++++++++++++++++++++++++++ src/core/value-test.cc | 10 ++---- src/core/wscript | 1 + 4 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 src/core/fp-value.cc create mode 100644 src/core/fp-value.h diff --git a/src/core/fp-value.cc b/src/core/fp-value.cc new file mode 100644 index 000000000..71e2e3583 --- /dev/null +++ b/src/core/fp-value.cc @@ -0,0 +1,74 @@ +#include "fp-value.h" +#include "object.h" +#include + +namespace ns3 { + +FpValue::FpValue (double value) + : m_value (value) +{} +PValue +FpValue::Copy (void) const +{ + return PValue::Create (*this); +} + +void +FpValue::Set (double value) +{ + m_value = value; +} +double +FpValue::Get (void) const +{ + return m_value; +} +std::string +FpValue::SerializeToString (Ptr spec) const +{ + std::ostringstream oss; + oss << m_value; + return oss.str (); +} +bool +FpValue::DeserializeFromString (std::string value, Ptr spec) +{ + double v; + std::istringstream iss; + iss.str (value); + iss >> v; + bool ok = !iss.bad () && !iss.fail (); + if (ok) + { + m_value = v; + } + return ok; +} + +FpValue::FpValue (PValue value) +{ + const FpValue *v = value.DynCast (); + if (v == 0) + { + NS_FATAL_ERROR ("assigning non-Fp value to Fp value."); + } + m_value = v->m_value; +} +FpValue::operator PValue () const +{ + return PValue::Create (*this); +} + + + +FpValueChecker::FpValueChecker (double min, double max) + : m_min (min), + m_max (max) +{} +bool +FpValueChecker::Check (double v) const +{ + return v >= m_min && v <= m_max; +} + +} // namespace ns3 diff --git a/src/core/fp-value.h b/src/core/fp-value.h new file mode 100644 index 000000000..f4d7da3ce --- /dev/null +++ b/src/core/fp-value.h @@ -0,0 +1,78 @@ +#ifndef FP_VALUE_H +#define FP_VALUE_H + +#include "value.h" +#include "param-spec-helper.h" +#include + +namespace ns3 { + +class FpValue : public Value +{ +public: + FpValue (double value); + + virtual PValue Copy (void) const; + virtual std::string SerializeToString (Ptr spec) const; + virtual bool DeserializeFromString (std::string value, Ptr spec); + + void Set (double value); + double Get (void) const; + + FpValue (PValue value); + operator PValue () const; +private: + double m_value; +}; + + +template +Ptr MakeFpParamSpec (U T::*memberVariable, + double initialValue); +template +Ptr MakeFpParamSpec (U T::*memberVariable, + double initialValue, + double minValue, + double maxValue); + +} // namespace ns3 + +namespace ns3 { + +class FpValueChecker +{ +public: + FpValueChecker (double min, double max); + bool Check (double v) const; +private: + double m_min; + double m_max; +}; + +template +Ptr +MakeFpParamSpec (U T::*memberVariable, + double initialValue) +{ + double minValue = -std::numeric_limits::max (); + double maxValue = std::numeric_limits::max (); + return MakeMemberVariableParamSpecWithChecker (memberVariable, + FpValue (initialValue), + FpValueChecker (minValue, maxValue)); +} + +template +Ptr +MakeFpParamSpec (U T::*memberVariable, + double initialValue, + double minValue, + double maxValue) +{ + return MakeMemberVariableParamSpecWithChecker (memberVariable, + FpValue (initialValue), + FpValueChecker (minValue, maxValue)); +} + +} // namespace ns3 + +#endif /* FP_VALUE_H */ diff --git a/src/core/value-test.cc b/src/core/value-test.cc index dc31c45b1..83319abf8 100644 --- a/src/core/value-test.cc +++ b/src/core/value-test.cc @@ -6,9 +6,7 @@ #include "uint-value.h" #include "enum-value.h" #include "random-variable.h" -#if 0 #include "fp-value.h" -#endif namespace ns3 { class ParamSpecTest : public Test @@ -70,10 +68,8 @@ public: .AddParameter ("TestRandom", "help text", MakeRandomVariableParamSpec (&ParamSpecObjectTest::m_random, ConstantVariable (1.0))) -#if 0 .AddParameter ("TestFloat", "help text", - MakeFpParamSpec (-1.1, &ParamSpecObjectTest::m_float)) -#endif + MakeFpParamSpec (&ParamSpecObjectTest::m_float, -1.1)) ; return tid; @@ -250,11 +246,11 @@ ParamSpecTest::RunTests (void) NS_TEST_ASSERT (!p->Set ("TestUint8", UintValue (-1))); CHECK_GET_STR (p, "TestUint8", "255"); CHECK_GET_PARAM (p, "TestUint8", UintValue, 255); -#if 0 + CHECK_GET_STR (p, "TestFloat", "-1.1"); NS_TEST_ASSERT (p->Set ("TestFloat", FpValue ((float)+2.3))); CHECK_GET_PARAM (p, "TestFloat", FpValue, (float)+2.3); -#endif + CHECK_GET_STR (p, "TestEnum", "TestA"); CHECK_GET_PARAM (p, "TestEnum", EnumValue, ParamSpecObjectTest::TEST_A); NS_TEST_ASSERT (p->Set ("TestEnum", EnumValue (ParamSpecObjectTest::TEST_C))); diff --git a/src/core/wscript b/src/core/wscript index 95cb7076a..ac96b2ba6 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -59,6 +59,7 @@ def build(bld): 'int-value.cc', 'uint-value.cc', 'enum-value.cc', + 'fp-value.cc', ] if sys.platform == 'win32': From 9f35179db067acc21a3b52218670768f4aa21777 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 7 Feb 2008 23:55:23 +0100 Subject: [PATCH 017/130] make param-spec-helper.h public --- src/core/wscript | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/wscript b/src/core/wscript index ac96b2ba6..afb4be869 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -105,5 +105,6 @@ def build(bld): 'int-to-type.h', 'type-id-default-value.h', 'value.h', + 'param-spec-helper.h', ] From d7fb6f58d0ae5d977714f7a1199575f70467583e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 7 Feb 2008 23:55:43 +0100 Subject: [PATCH 018/130] add Value support to Vector --- src/mobility/vector.cc | 62 ++++++++++++++++++++++++++++++++++++++++++ src/mobility/vector.h | 40 +++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/src/mobility/vector.cc b/src/mobility/vector.cc index 79917c16e..c69e6bbf5 100644 --- a/src/mobility/vector.cc +++ b/src/mobility/vector.cc @@ -18,7 +18,9 @@ * Author: Mathieu Lacage */ #include "vector.h" +#include "ns3/fatal-error.h" #include +#include namespace ns3 { @@ -35,6 +37,21 @@ Vector::Vector () z (0.0) {} +Vector::Vector (PValue value) +{ + const VectorValue *v = value.DynCast (); + if (v == 0) + { + NS_FATAL_ERROR ("Expected value of type Vector"); + } + *this = v->Get (); +} +Vector::operator PValue () const +{ + return PValue::Create (*this); +} + + double CalculateDistance (const Vector &a, const Vector &b) { @@ -45,4 +62,49 @@ CalculateDistance (const Vector &a, const Vector &b) return distance; } + + +VectorValue::VectorValue (const Vector &vector) + : m_vector (vector) +{} + +void +VectorValue::Set (const Vector &vector) +{ + m_vector = vector; +} +Vector +VectorValue::Get (void) const +{ + return m_vector; +} + +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 --git a/src/mobility/vector.h b/src/mobility/vector.h index 6bc277f27..38bdeb2ad 100644 --- a/src/mobility/vector.h +++ b/src/mobility/vector.h @@ -20,6 +20,9 @@ #ifndef VECTOR_H #define VECTOR_H +#include "ns3/value.h" +#include "ns3/param-spec-helper.h" + namespace ns3 { /** @@ -54,10 +57,47 @@ public: * z coordinate of vector vector */ double z; + + Vector (PValue value); + operator PValue () const; }; 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; + + 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; +}; + +template +Ptr +MakeVectorParamSpec (Vector T::*memberVariable, const Vector &initialValue); + +} // namespace ns3 + +namespace ns3 { + +template +Ptr +MakeVectorParamSpec (Vector T::*memberVariable, const Vector &initialValue) +{ + return MakeMemberVariableParamSpec (memberVariable, VectorValue (initialValue)); +} + + } // namespace ns3 #endif /* VECTOR_H */ From 14ea56069ca26c978a09519b7a90a84dac422c2c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 7 Feb 2008 23:56:03 +0100 Subject: [PATCH 019/130] 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 */ From ab95f30c9c8e4997fab22a5f104a211efa01d9a1 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 7 Feb 2008 23:57:07 +0100 Subject: [PATCH 020/130] make headers public. --- src/core/wscript | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/core/wscript b/src/core/wscript index afb4be869..9c7f8e174 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -106,5 +106,9 @@ def build(bld): 'type-id-default-value.h', 'value.h', 'param-spec-helper.h', + 'int-value.h', + 'uint-value.h', + 'fp-value.h', + 'enum-value.h', ] From 624b741c2ff4a6009bc199e8f4a0c296ea54e4fd Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 00:52:32 +0100 Subject: [PATCH 021/130] add Value support to Time --- src/simulator/nstime.h | 46 +++++++++++++++++++- src/simulator/time.cc | 97 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 140 insertions(+), 3 deletions(-) diff --git a/src/simulator/nstime.h b/src/simulator/nstime.h index 6b6660f70..707fb1f30 100644 --- a/src/simulator/nstime.h +++ b/src/simulator/nstime.h @@ -20,9 +20,11 @@ #ifndef TIME_H #define TIME_H +#include "ns3/assert.h" +#include "ns3/value.h" +#include "ns3/param-spec-helper.h" #include #include -#include "ns3/assert.h" #include #include "high-precision.h" #include "cairo-wideint-private.h" @@ -334,6 +336,8 @@ TimeUnit Min (TimeUnit const &ta, TimeUnit const &tb) // additional methods that should not be available for N!=1 // \class TimeUnit<1> +class TimeValue; + template <> class TimeUnit<1> { @@ -431,6 +435,9 @@ public: static uint64_t UnitsToTimestep (uint64_t unitValue, uint64_t unitFactor); + + TimeUnit (PValue value); + operator PValue () const; private: HighPrecision m_data; @@ -662,6 +669,41 @@ typedef TimeUnit<-1> TimeInvert; typedef TimeUnit<2> TimeSquare; -}; // namespace ns3 +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; +}; + +template +Ptr MakeTimeParamSpec (Time T::*memberVariable, + Time initialValue); + +} // namespace ns3 + +namespace ns3 { + +template +Ptr MakeTimeParamSpec (Time T::*memberVariable, + Time initialValue) +{ + return MakeMemberVariableParamSpec (memberVariable, TimeValue (initialValue)); +} + + +} // namespace ns3 #endif /* TIME_H */ diff --git a/src/simulator/time.cc b/src/simulator/time.cc index fc5507906..852fb9dc9 100644 --- a/src/simulator/time.cc +++ b/src/simulator/time.cc @@ -22,6 +22,7 @@ #include "nstime.h" #include "ns3/fatal-error.h" #include "ns3/default-value.h" +#include "ns3/object.h" #include namespace ns3 { @@ -241,6 +242,20 @@ TimeUnit<1>::UnitsToTimestep (uint64_t unitValue, return unitValue; } +TimeUnit<1>::TimeUnit (PValue value) +{ + const TimeValue *v = value.DynCast (); + if (v == 0) + { + NS_FATAL_ERROR ("Expected value of type TimeValue."); + } + *this = v->Get (); +} +TimeUnit<1>::operator PValue () const +{ + return PValue::Create (*this); +} + Time MilliSeconds (uint64_t ms) { uint64_t ts = TimeUnit<1>::UnitsToTimestep(ms, TimeStepPrecision::MS_FACTOR); @@ -268,6 +283,86 @@ Time FemtoSeconds (uint64_t fs) uint64_t ts = TimeUnit<1>::UnitsToTimestep(fs, TimeStepPrecision::FS_FACTOR); 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; +} + + /* * The timestep value passed to this function must be of the precision * of TimeStepPrecision::Get @@ -287,7 +382,7 @@ TimeUnit<0>::GetDouble (void) const return GetHighPrecision ().GetDouble (); } -}; // namespace ns3 +} // namespace ns3 #ifdef RUN_SELF_TESTS From 5c2e40d9fc536508a19535b0cc7fcf2402125491 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 00:52:57 +0100 Subject: [PATCH 022/130] fix Value support --- src/mobility/rectangle.cc | 2 +- src/mobility/rectangle.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mobility/rectangle.cc b/src/mobility/rectangle.cc index 01f2ea653..24d2fb8c2 100644 --- a/src/mobility/rectangle.cc +++ b/src/mobility/rectangle.cc @@ -23,7 +23,7 @@ #include "ns3/fatal-error.h" #include #include -#include +#include namespace ns3 { diff --git a/src/mobility/rectangle.h b/src/mobility/rectangle.h index 94a721d23..5cb9e578e 100644 --- a/src/mobility/rectangle.h +++ b/src/mobility/rectangle.h @@ -100,7 +100,7 @@ Ptr MakeRectangleParamSpec (Rectangle T::*memberVariable, Rectangle initialValue) { - return MakeMemberVariableParamSpec (memberVariable, initialValue); + return MakeMemberVariableParamSpec (memberVariable, RectangleValue (initialValue)); } From 703b1dba085d8b5ff9bf817aa04fca13f9728b17 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 00:53:16 +0100 Subject: [PATCH 023/130] add missing Make functions --- src/mobility/vector.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/mobility/vector.h b/src/mobility/vector.h index 38bdeb2ad..af3976fad 100644 --- a/src/mobility/vector.h +++ b/src/mobility/vector.h @@ -86,6 +86,18 @@ template Ptr MakeVectorParamSpec (Vector T::*memberVariable, const Vector &initialValue); +template +Ptr +MakeVectorParamSpec (void (T::*setter) (const Vector &), + Vector (T::*getter) (void) const, + const Vector &initialValue); + +template +Ptr +MakeVectorParamSpec (Vector (T::*getter) (void) const, + const Vector &initialValue); + + } // namespace ns3 namespace ns3 { @@ -97,6 +109,25 @@ MakeVectorParamSpec (Vector T::*memberVariable, const Vector &initialValue) return MakeMemberVariableParamSpec (memberVariable, VectorValue (initialValue)); } +template +Ptr +MakeVectorParamSpec (void (T::*setter) (const Vector &), + Vector (T::*getter) (void) const, + const Vector &initialValue) +{ + return MakeMemberMethodParamSpec (setter, getter, VectorValue (initialValue)); +} + +template +Ptr +MakeVectorParamSpec (Vector (T::*getter) (void) const, + const Vector &initialValue) +{ + return MakeMemberMethodGetterParamSpec (getter, VectorValue (initialValue)); +} + + + } // namespace ns3 From b155b7ad342c9b66e925e222e33b45f1006f2635 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 00:53:40 +0100 Subject: [PATCH 024/130] untested templates rarely work. --- src/core/param-spec-helper.h | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index 77851e632..5240c9acb 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -82,51 +82,51 @@ MakeMemberVariableParamSpec (U T::*memberVariable, V initialValue) ParamSpecHelperSimpleChecker ()); } -template +template Ptr MakeMemberMethodParamSpecWithChecker (void (T::*setter) (U), - U (T::*getter) (void) const, - V initialValue, CHECKER checker) + V (T::*getter) (void) const, + W initialValue, CHECKER checker) { - class MemberMethod : public ParamSpecHelper + class MemberMethod : public ParamSpecHelper { public: MemberMethod (void (T::*setter) (U), - U (T::*getter) (void) const, - V initialValue, CHECKER checker) - : ParamSpecHelper (initialValue, checker), + V (T::*getter) (void) const, + W initialValue, CHECKER checker) + : ParamSpecHelper (initialValue, checker), m_setter (setter), m_getter (getter) {} private: - virtual void DoSet (T *object, const V *v) const { + virtual void DoSet (T *object, const W *v) const { (object->*m_setter) (v->Get ()); } - virtual void DoGet (const T *object, V *v) const { + virtual void DoGet (const T *object, W *v) const { v->Set ((object->*m_getter) ()); } void (T::*m_setter) (U); - U (T::*m_getter) (void) const; + V (T::*m_getter) (void) const; }; return Ptr (new MemberMethod (setter, getter, initialValue, checker), false); } -template +template Ptr MakeMemberMethodParamSpec (void (T::*setter) (U), - U (T::*getter) (void) const, - V initialValue) + V (T::*getter) (void) const, + W initialValue) { return MakeMemberMethodParamSpecWithChecker (setter, getter, initialValue, - ParamSpecHelperSimpleChecker ()); + ParamSpecHelperSimpleChecker ()); } template Ptr MakeMemberMethodGetterParamSpecWithChecker (U (T::*getter) (void) const, - V initialValue, CHECKER checker) + V initialValue, CHECKER checker) { class MemberMethod : public ParamSpecHelper { @@ -152,8 +152,8 @@ Ptr MakeMemberMethodGetterParamSpec (U (T::*getter) (void) const, V initialValue) { - return MakeMemberMethodParamSpecWithChecker (getter, initialValue, - ParamSpecHelperSimpleChecker ()); + return MakeMemberMethodGetterParamSpecWithChecker (getter, initialValue, + ParamSpecHelperSimpleChecker ()); } From 06270d7eed9ce2514d49a5939c77ac2b50bcb4a7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 02:21:23 +0100 Subject: [PATCH 025/130] add a default constructor for the TypeId class --- src/core/object.cc | 4 ++++ src/core/object.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/core/object.cc b/src/core/object.cc index ef051223a..86baa8b9a 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -365,6 +365,10 @@ TypeIdTraceResolver::TraceAll (std::ostream &os, const TraceContext &context) * The TypeId class *********************************************************************/ +TypeId::TypeId () + : m_tid (0) +{} + TypeId::TypeId (std::string name) { uint16_t uid = Singleton::Get ()->AllocateUid (name); diff --git a/src/core/object.h b/src/core/object.h index 7da6ae27c..4c874fd5f 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -233,6 +233,8 @@ public: uint32_t flags, Ptr spec); + // construct an invalid TypeId. + TypeId (); ~TypeId (); private: friend class Object; From 816736f7239f73a441a322085e40839694cec3ee Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 02:22:04 +0100 Subject: [PATCH 026/130] a pretty simple wrapper around TypeId+Parameters: ObjectFactory --- src/core/object-factory.cc | 44 ++++++++++++++++++++++++++++++++++++++ src/core/object-factory.h | 30 ++++++++++++++++++++++++++ src/core/wscript | 2 ++ 3 files changed, 76 insertions(+) create mode 100644 src/core/object-factory.cc create mode 100644 src/core/object-factory.h diff --git a/src/core/object-factory.cc b/src/core/object-factory.cc new file mode 100644 index 000000000..70503c46b --- /dev/null +++ b/src/core/object-factory.cc @@ -0,0 +1,44 @@ +#include "object-factory.h" +#include + +namespace ns3 { + +ObjectFactory::ObjectFactory () +{} + +void +ObjectFactory::SetTypeId (TypeId tid) +{ + m_tid = tid; +} +void +ObjectFactory::SetTypeId (std::string tid) +{ + m_tid = TypeId::LookupByName (tid); +} +void +ObjectFactory::Set (std::string name, PValue value) +{ + m_parameters.SetWithTid (m_tid, name, value); +} + +void +ObjectFactory::Set (std::string name, std::string value) +{ + m_parameters.SetWithTid (m_tid, name, value); +} + +TypeId +ObjectFactory::GetTypeId (void) const +{ + return m_tid; +} + +Ptr +ObjectFactory::Create (void) const +{ + Ptr object = m_tid.CreateObject (m_parameters); + return object; +} + +} // namespace ns3 diff --git a/src/core/object-factory.h b/src/core/object-factory.h new file mode 100644 index 000000000..518cbad64 --- /dev/null +++ b/src/core/object-factory.h @@ -0,0 +1,30 @@ +#ifndef OBJECT_FACTORY_H +#define OBJECT_FACTORY_H + +#include "value.h" +#include "object.h" + +namespace ns3 { + +class ObjectFactory +{ +public: + ObjectFactory (); + + void SetTypeId (TypeId tid); + void SetTypeId (std::string tid); + void Set (std::string name, PValue value); + void Set (std::string name, std::string value); + + TypeId GetTypeId (void) const; + + Ptr Create (void) const; + +private: + TypeId m_tid; + Parameters m_parameters; +}; + +} // namespace ns3 + +#endif /* OBJECT_FACTORY_H */ diff --git a/src/core/wscript b/src/core/wscript index 9c7f8e174..d748a4fe5 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -60,6 +60,7 @@ def build(bld): 'uint-value.cc', 'enum-value.cc', 'fp-value.cc', + 'object-factory.cc', ] if sys.platform == 'win32': @@ -110,5 +111,6 @@ def build(bld): 'uint-value.h', 'fp-value.h', 'enum-value.h', + 'object-factory.h', ] From 1a71f4e43a5e851690b5cf5b6d3653a0a1175619 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 02:22:36 +0100 Subject: [PATCH 027/130] implement missing methods --- src/simulator/time.cc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/simulator/time.cc b/src/simulator/time.cc index 852fb9dc9..e95e8d46e 100644 --- a/src/simulator/time.cc +++ b/src/simulator/time.cc @@ -362,6 +362,15 @@ TimeValue::DeserializeFromString (std::string value, Ptr spec) 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 From 0ec09b9cf4ae980053ac211c064e9948d5fcfa53 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 04:11:08 +0100 Subject: [PATCH 028/130] add automatic conversion from string to PValue. --- src/core/object.cc | 110 +++++++++++++-------------------------------- src/core/object.h | 18 +------- src/core/value.cc | 72 +++++++++++++++++++++++++++++ src/core/value.h | 27 +++++++++++ 4 files changed, 131 insertions(+), 96 deletions(-) diff --git a/src/core/object.cc b/src/core/object.cc index 86baa8b9a..4ab6cae76 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -643,21 +643,6 @@ Parameters::Set (std::string name, PValue value) bool ok = DoSet (info.spec, value); return ok; } -bool -Parameters::Set (std::string name, std::string value) -{ - struct TypeId::ParameterInfo info; - TypeId::LookupParameterByFullName (name, &info); - bool ok = DoSet (info.spec,value); - return ok; -} -void -Parameters::SetWithTid (TypeId tid, std::string name, std::string value) -{ - struct TypeId::ParameterInfo info; - tid.LookupParameterByName (name, &info); - DoSet (info.spec, value); -} void Parameters::SetWithTid (TypeId tid, std::string name, PValue value) { @@ -666,13 +651,6 @@ Parameters::SetWithTid (TypeId tid, std::string name, PValue value) DoSet (info.spec, value); } void -Parameters::SetWithTid (TypeId tid, uint32_t position, std::string value) -{ - struct TypeId::ParameterInfo info; - tid.LookupParameterByPosition (position, &info); - DoSet (info.spec, value); -} -void Parameters::SetWithTid (TypeId tid, uint32_t position, PValue value) { struct TypeId::ParameterInfo info; @@ -709,32 +687,16 @@ Parameters::DoSet (Ptr spec, PValue value) bool ok = spec->Check (value); if (!ok) { - return false; + PValue v = spec->CreateValue (); + ok = v.ConvertFrom (value, spec); + if (!ok) + { + return false; + } } DoSetOne (spec, value); return true; } -bool -Parameters::DoSet (Ptr spec, std::string value) -{ - if (spec == 0) - { - return false; - } - PValue v = spec->CreateValue (); - bool ok = v.DeserializeFromString (value, spec); - if (!ok) - { - return false; - } - ok = spec->Check (v); - if (!ok) - { - return false; - } - DoSetOne (spec, v); - return true; -} void Parameters::Reset (void) { @@ -894,7 +856,7 @@ Object::Construct (const Parameters ¶meters) if (j->spec == paramSpec) { // We have a matching parameter value. - paramSpec->Set (this, j->value); + DoSet (paramSpec, j->value); NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\""); found = true; @@ -910,7 +872,7 @@ Object::Construct (const Parameters ¶meters) if (j->spec == paramSpec) { // We have a matching parameter value. - paramSpec->Set (this, j->value); + DoSet (paramSpec, j->value); NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\" from global"); found = true; @@ -932,6 +894,28 @@ Object::Construct (const Parameters ¶meters) NotifyConstructionCompleted (); } bool +Object::DoSet (Ptr spec, PValue value) +{ + bool ok = spec->Check (value); + if (!ok) + { + PValue v = spec->CreateValue (); + ok = v.ConvertFrom (value, spec); + if (!ok) + { + return false; + } + value = v; + ok = spec->Check (value); + if (!ok) + { + return false; + } + } + ok = spec->Set (this, value); + return ok; +} +bool Object::Set (std::string name, PValue value) { struct TypeId::ParameterInfo info; @@ -943,39 +927,7 @@ Object::Set (std::string name, PValue value) { return false; } - bool ok = info.spec->Check (value); - if (!ok) - { - return false; - } - ok = info.spec->Set (this, value); - return ok; -} -bool -Object::Set (std::string name, std::string value) -{ - struct TypeId::ParameterInfo info; - if (!m_tid.LookupParameterByName (name, &info)) - { - return false; - } - if (!(info.flags & TypeId::PARAM_SET)) - { - return false; - } - PValue v = info.spec->CreateValue (); - bool ok = v.DeserializeFromString (value, info.spec); - if (!ok) - { - return false; - } - ok = info.spec->Check (v); - if (!ok) - { - return false; - } - ok = info.spec->Set (this, v); - return ok; + return DoSet (info.spec, value); } bool Object::Get (std::string name, std::string &value) const diff --git a/src/core/object.h b/src/core/object.h index 4c874fd5f..b5b9ef44e 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -294,12 +294,9 @@ public: * value of that parameter. If any of these checks fails, * the program terminates with a message. */ - bool Set (std::string name, std::string value); bool Set (std::string name, PValue value); - void SetWithTid (TypeId tid, std::string name, std::string value); void SetWithTid (TypeId tid, std::string name, PValue value); - void SetWithTid (TypeId tid, uint32_t position, std::string value); void SetWithTid (TypeId tid, uint32_t position, PValue value); /** @@ -334,7 +331,6 @@ private: bool DoSet (Ptr spec, PValue param); - bool DoSet (Ptr spec, std::string value); void DoSetOne (Ptr spec, PValue param); std::string LookupParameterFullNameByParamSpec (Ptr spec) const; @@ -360,7 +356,6 @@ public: * * Set a single parameter. */ - bool Set (std::string name, std::string value); bool Set (std::string name, PValue value); /** * \param name the name of the parameter to read @@ -475,6 +470,7 @@ private: friend Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7); + bool DoSet (Ptr spec, PValue value); Ptr DoQueryInterface (TypeId tid) const; void DoCollectSources (std::string path, const TraceContext &context, TraceResolver::SourceCollection *collection) const; @@ -746,18 +742,6 @@ CreateObjectWith (std::string n1, PValue v1, return CreateObject (parameters); } -template -Ptr -CreateObjectWith (std::string n1, std::string v1, - std::string n2 = "", std::string v2 = "") - -{ - Parameters parameters; - parameters.SetWithTid (T::GetTypeId (), n1, v1); - parameters.SetWithTid (T::GetTypeId (), n2, v2); - return CreateObject (parameters); -} - } // namespace ns3 #endif /* OBJECT_H */ diff --git a/src/core/value.cc b/src/core/value.cc index a60c06c84..7248b3022 100644 --- a/src/core/value.cc +++ b/src/core/value.cc @@ -1,5 +1,6 @@ #include "value.h" #include "log.h" +#include "fatal-error.h" #include NS_LOG_COMPONENT_DEFINE ("Value"); @@ -19,6 +20,11 @@ Value::operator = (const Value &o) } Value::~Value () {} +bool +Value::ConvertFrom (PValue value, Ptr spec) +{ + return false; +} PValue::PValue () : m_value (0) @@ -90,6 +96,24 @@ PValue::DeserializeFromString (std::string value, Ptr spec) { return m_value->DeserializeFromString (value, spec); } +bool +PValue::ConvertFrom (PValue value, Ptr spec) +{ + const StringValue *str = value.DynCast (); + if (str == 0) + { + return false; + } + return DeserializeFromString (str->Get (), spec); +} + +PValue::PValue (const char *value) + : m_value (new StringValue (value)) +{} +PValue::PValue (std::string value) + : m_value (new StringValue (value)) +{} + ParamSpec::ParamSpec () : m_count (1) @@ -111,6 +135,54 @@ ParamSpec::Unref (void) const ParamSpec::~ParamSpec () {} + +StringValue::StringValue (const char *value) + : m_value (value) +{} +StringValue::StringValue (std::string value) + : m_value (value) +{} +void +StringValue::Set (std::string value) +{ + m_value = value; +} +std::string +StringValue::Get (void) const +{ + return m_value; +} +PValue +StringValue::Copy (void) const +{ + return PValue::Create (*this); +} +std::string +StringValue::SerializeToString (Ptr spec) const +{ + return m_value; +} +bool +StringValue::DeserializeFromString (std::string value, Ptr spec) +{ + m_value = value; + return true; +} +StringValue::StringValue (PValue value) +{ + const StringValue *v = value.DynCast (); + if (v == 0) + { + NS_FATAL_ERROR ("Expected value of type String."); + } + m_value = v->Get (); +} +StringValue::operator PValue () const +{ + return PValue::Create (*this); +} + + std::string PtrValueBase::SerializeToString (Ptr spec) const { diff --git a/src/core/value.h b/src/core/value.h index db5bf22b6..c01b65cf4 100644 --- a/src/core/value.h +++ b/src/core/value.h @@ -22,6 +22,7 @@ public: virtual PValue Copy (void) const = 0; virtual std::string SerializeToString (Ptr spec) const = 0; virtual bool DeserializeFromString (std::string value, Ptr spec) = 0; + virtual bool ConvertFrom (PValue value, Ptr spec); private: friend class PValue; uint32_t m_count; @@ -38,6 +39,7 @@ public: PValue Copy (void) const; std::string SerializeToString (Ptr spec) const; bool DeserializeFromString (std::string value, Ptr spec); + bool ConvertFrom (PValue value, Ptr spec); template static PValue Create (void); @@ -52,6 +54,8 @@ public: template operator Ptr (); + PValue (const char *value); + PValue (std::string value); private: PValue (Value *value); Value *m_value; @@ -97,6 +101,29 @@ MakePtrParamSpec (Ptr (T::*getter) (void) const); namespace ns3 { +/******************************************************** + * A class used to hold std::string values. + ********************************************************/ + +class StringValue : public Value +{ +public: + StringValue (const char *value); + StringValue (std::string value); + void Set (std::string value); + std::string Get (void) const; + + virtual PValue Copy (void) const; + virtual std::string SerializeToString (Ptr spec) const; + virtual bool DeserializeFromString (std::string value, Ptr spec); + + StringValue (PValue value); + operator PValue () const; +private: + std::string m_value; +}; + + /******************************************************** * The class used to access the pointer stored in a * PtrValue Value instance. From 17136ba64c715662d1b194fb1eee94e4f1eab4a3 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 04:11:29 +0100 Subject: [PATCH 029/130] fix typo: non-matching delaration/implementation --- src/core/param-spec-helper.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index 5240c9acb..d3290bf54 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -7,11 +7,11 @@ template Ptr MakeMemberVariableParamSpec (U T::*memberVariable, V initialValue); -template +template Ptr MakeMemberMethodParamSpec (void (T::*setter) (U), - U (T::*getter) (void) const, - V initialValue); + V (T::*getter) (void) const, + W initialValue); } // namespace ns3 From d4c643c73c7c70c60aff0149da689c0c20ecd4dd Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 04:11:48 +0100 Subject: [PATCH 030/130] convert Mobility subsystem to Value framework --HG-- rename : src/mobility/random-position.cc => src/mobility/position-allocator.cc rename : src/mobility/random-position.h => src/mobility/position-allocator.h --- samples/main-grid-topology.cc | 27 ++- samples/main-random-topology.cc | 29 +-- samples/main-random-walk.cc | 25 +-- samples/wscript | 3 - src/devices/wifi/propagation-loss-model.cc | 8 +- src/mobility/mobility-helper.cc | 113 ++++++++++++ src/mobility/mobility-helper.h | 76 ++++++++ src/mobility/mobility-model.cc | 13 +- src/mobility/position-allocator.cc | 170 ++++++++++++++++++ src/mobility/position-allocator.h | 105 +++++++++++ .../random-direction-2d-mobility-model.cc | 102 ++--------- .../random-direction-2d-mobility-model.h | 53 +----- src/mobility/random-position.cc | 148 --------------- src/mobility/random-position.h | 117 ------------ src/mobility/random-walk-2d-mobility-model.cc | 152 ++++------------ src/mobility/random-walk-2d-mobility-model.h | 87 ++------- .../random-waypoint-mobility-model.cc | 95 ++-------- src/mobility/random-waypoint-mobility-model.h | 55 +----- src/mobility/static-mobility-model.cc | 5 +- src/mobility/static-mobility-model.h | 8 +- src/mobility/static-speed-mobility-model.cc | 11 +- src/mobility/static-speed-mobility-model.h | 13 -- src/mobility/wscript | 10 +- utils/mobility-visualizer-model.cc | 18 +- 24 files changed, 638 insertions(+), 805 deletions(-) create mode 100644 src/mobility/mobility-helper.cc create mode 100644 src/mobility/mobility-helper.h create mode 100644 src/mobility/position-allocator.cc create mode 100644 src/mobility/position-allocator.h delete mode 100644 src/mobility/random-position.cc delete mode 100644 src/mobility/random-position.h diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index 7dc850e4d..7a877b8a7 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -1,10 +1,13 @@ /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ #include "ns3/ptr.h" -#include "ns3/grid-topology.h" -#include "ns3/static-mobility-model.h" -#include "ns3/internet-node.h" +#include "ns3/node.h" #include "ns3/command-line.h" +#include "ns3/mobility-model.h" +#include "ns3/mobility-helper.h" +#include "ns3/uint-value.h" +#include "ns3/fp-value.h" +#include "ns3/enum-value.h" using namespace ns3; @@ -17,22 +20,30 @@ int main (int argc, char *argv[]) // create an array of empty nodes for testing purposes for (uint32_t i = 0; i < 120; i++) { - nodes.push_back (CreateObject ()); + nodes.push_back (CreateObject ()); } + MobilityHelper mobility; // setup the grid itself: objects are layed out // started from (-100,-100) with 20 objects per row, // the x interval between each object is 5 meters // and the y interval between each object is 20 meters - GridTopology grid (-100, -100, 20, 5, 20); - + mobility.SetPositionAllocator ("GridPositionAllocator", + "MinX", FpValue (-100.0), + "MinY", FpValue (-100.0), + "DeltaX", FpValue (5.0), + "DeltaY", FpValue (20.0), + "GridWidth", UintValue (20), + "LayoutType", "RowFirst"); // each object will be attached a static position. - grid.SetMobilityModel (StaticMobilityModel::GetTypeId ()); + // i.e., once set by the "position allocator", the + // position will never change. + mobility.SetMobilityModel ("StaticMobilityModel"); // finalize the setup by attaching to each object // in the input array a position and initializing // this position with the calculated coordinates. - grid.LayoutRowFirst (nodes.begin (), nodes.end ()); + mobility.Layout (nodes.begin (), nodes.end ()); // iterate our nodes and print their position. for (std::vector >::const_iterator j = nodes.begin (); diff --git a/samples/main-random-topology.cc b/samples/main-random-topology.cc index aaa38bed1..003b174ce 100644 --- a/samples/main-random-topology.cc +++ b/samples/main-random-topology.cc @@ -4,13 +4,13 @@ #include "ns3/ptr.h" #include "ns3/mobility-model.h" -#include "ns3/mobility-model-notifier.h" -#include "ns3/static-mobility-model.h" -#include "ns3/random-topology.h" #include "ns3/default-value.h" #include "ns3/command-line.h" #include "ns3/simulator.h" #include "ns3/nstime.h" +#include "ns3/node.h" +#include "ns3/mobility-helper.h" +#include "ns3/node-list.h" using namespace ns3; @@ -24,26 +24,27 @@ CourseChange (const TraceContext &context, Ptr position) int main (int argc, char *argv[]) { - DefaultValue::Bind ("RandomDiscPositionX", "100"); - DefaultValue::Bind ("RandomDiscPositionY", "50"); - DefaultValue::Bind ("RandomDiscPositionRho", "Uniform:0:30"); - - DefaultValue::Bind ("RandomTopologyPositionType", "RandomDiscPosition"); - DefaultValue::Bind ("RandomTopologyMobilityType", "StaticMobilityModel"); CommandLine::Parse (argc, argv); - RandomTopology topology; std::vector > objects; for (uint32_t i = 0; i < 10000; i++) { - Ptr notifier = CreateObject (); - notifier->TraceConnect ("/course-change", MakeCallback (&CourseChange)); - objects.push_back (notifier); + objects.push_back (CreateObject ()); } - topology.Layout (objects.begin (), objects.end ()); + MobilityHelper mobility; + mobility.EnableNotifier (); + mobility.SetPositionAllocator ("RandomDiscPositionAllocator", + "X", "100.0", + "Y", "100.0", + "Rho", "Uniform:0:30"); + mobility.SetMobilityModel ("StaticMobilityModel"); + mobility.Layout (objects.begin (), objects.end ()); + + NodeList::Connect ("/nodes/*/$MobilityModelNotifier/course-change", + MakeCallback (&CourseChange)); Simulator::StopAt (Seconds (100.0)); diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index 6be8b1f14..abdfe8b05 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -4,14 +4,14 @@ #include "ns3/ptr.h" #include "ns3/mobility-model.h" -#include "ns3/mobility-model-notifier.h" -#include "ns3/random-topology.h" +#include "ns3/position-allocator.h" #include "ns3/default-value.h" #include "ns3/command-line.h" #include "ns3/simulator.h" #include "ns3/nstime.h" #include "ns3/node.h" #include "ns3/node-list.h" +#include "ns3/mobility-helper.h" using namespace ns3; @@ -32,24 +32,27 @@ int main (int argc, char *argv[]) DefaultValue::Bind ("RandomWalk2dSpeed", "Constant:1.0"); DefaultValue::Bind ("RandomWalk2dBounds", "0:200:0:100"); - DefaultValue::Bind ("RandomDiscPositionX", "100"); - DefaultValue::Bind ("RandomDiscPositionY", "50"); - DefaultValue::Bind ("RandomDiscPositionRho", "Uniform:0:30"); - - DefaultValue::Bind ("RandomTopologyPositionType", "RandomDiscPosition"); DefaultValue::Bind ("RandomTopologyMobilityType", "RandomWalk2dMobilityModel"); CommandLine::Parse (argc, argv); - RandomTopology topology; - for (uint32_t i = 0; i < 100; i++) { Ptr node = CreateObject (); - node->AddInterface (CreateObject ()); } - topology.Layout (NodeList::Begin (), NodeList::End ()); + MobilityHelper mobility; + mobility.EnableNotifier (); + mobility.SetPositionAllocator ("RandomDiscPositionAllocator", + "X", "100.0", + "Y", "100.0", + "Rho", "Uniform:0:30"); + mobility.SetMobilityModel ("RandomWalk2dMobilityModel", + "Mode", "Time", + "Time", "2s", + "Speed", "Constant:1.0", + "Bounds", "0:200:0:100"); + mobility.Layout (NodeList::Begin (), NodeList::End ()); NodeList::Connect ("/nodes/*/$MobilityModelNotifier/course-change", MakeCallback (&CourseChange)); diff --git a/samples/wscript b/samples/wscript index c8a97de52..b03196067 100644 --- a/samples/wscript +++ b/samples/wscript @@ -4,9 +4,6 @@ def build(bld): obj = bld.create_ns3_program('main-callback') obj.source = 'main-callback.cc' - obj = bld.create_ns3_program('main-ptr') - obj.source = 'main-ptr.cc' - obj = bld.create_ns3_program('main-simulator') obj.source = 'main-simulator.cc' diff --git a/src/devices/wifi/propagation-loss-model.cc b/src/devices/wifi/propagation-loss-model.cc index 29f3475e8..407d25b08 100644 --- a/src/devices/wifi/propagation-loss-model.cc +++ b/src/devices/wifi/propagation-loss-model.cc @@ -286,8 +286,12 @@ LogDistancePropagationLossModel::GetRxPower (double txPowerDbm, * * rx = rx0(tx) - 10 * n * log (d/d0) */ - static Ptr zero = CreateObject (Vector (0.0, 0.0, 0.0)); - static Ptr reference = CreateObject (Vector (m_referenceDistance, 0.0, 0.0)); + static Ptr zero = + CreateObjectWith ("position", + Vector (0.0, 0.0, 0.0)); + static Ptr reference = + CreateObjectWith ("position", + Vector (m_referenceDistance, 0.0, 0.0)); double rx0 = m_reference->GetRxPower (txPowerDbm, zero, reference); double pathLossDb = 10 * m_exponent * log10 (distance / m_referenceDistance); double rxPowerDbm = rx0 - pathLossDb; diff --git a/src/mobility/mobility-helper.cc b/src/mobility/mobility-helper.cc new file mode 100644 index 000000000..d8ad0df1d --- /dev/null +++ b/src/mobility/mobility-helper.cc @@ -0,0 +1,113 @@ +#include "mobility-helper.h" +#include "mobility-model.h" +#include "mobility-model-notifier.h" +#include "position-allocator.h" + +namespace ns3 { + +MobilityHelper::MobilityHelper () + : m_notifierEnabled (false) +{} +void +MobilityHelper::EnableNotifier (void) +{ + m_notifierEnabled = true; +} +void +MobilityHelper::DisableNotifier (void) +{ + m_notifierEnabled = false; +} +void +MobilityHelper::SetPositionAllocator (Ptr allocator) +{ + m_position = allocator; +} +void +MobilityHelper::SetPositionAllocator (std::string type, + std::string n1, PValue v1, + std::string n2, PValue v2, + std::string n3, PValue v3, + std::string n4, PValue v4, + std::string n5, PValue v5, + std::string n6, PValue v6, + std::string n7, PValue v7, + std::string n8, PValue v8, + std::string n9, PValue v9) +{ + ObjectFactory pos; + pos.SetTypeId (type); + pos.Set (n1, v1); + pos.Set (n2, v2); + pos.Set (n3, v3); + pos.Set (n4, v4); + pos.Set (n5, v5); + pos.Set (n6, v6); + pos.Set (n7, v7); + pos.Set (n8, v8); + pos.Set (n9, v9); + m_position = pos.Create ()->QueryInterface (); +} + +void +MobilityHelper::SetMobilityModel (std::string type, + std::string n1, PValue v1, + std::string n2, PValue v2, + std::string n3, PValue v3, + std::string n4, PValue v4, + std::string n5, PValue v5, + std::string n6, PValue v6, + std::string n7, PValue v7, + std::string n8, PValue v8, + std::string n9, PValue v9) +{ + m_mobility.SetTypeId (type); + m_mobility.Set (n1, v1); + m_mobility.Set (n2, v2); + m_mobility.Set (n3, v3); + m_mobility.Set (n4, v4); + m_mobility.Set (n5, v5); + m_mobility.Set (n6, v6); + m_mobility.Set (n7, v7); + m_mobility.Set (n8, v8); + m_mobility.Set (n9, v9); +} + +std::string +MobilityHelper::GetMobilityModelType (void) const +{ + return m_mobility.GetTypeId ().GetName (); +} + +void +MobilityHelper::Layout (const std::vector > &objects) +{ + for (std::vector >::const_iterator i = objects.begin (); i != objects.end (); i++) + { + Ptr object = *i; + Ptr model = object->QueryInterface (); + if (model == 0) + { + model = m_mobility.Create ()->QueryInterface (); + if (model == 0) + { + NS_FATAL_ERROR ("The requested mobility model is not a mobility model: \""<< + m_mobility.GetTypeId ().GetName ()<<"\""); + } + object->AddInterface (model); + } + Vector position = m_position->GetNext (); + model->SetPosition (position); + if (m_notifierEnabled) + { + Ptr notifier = + object->QueryInterface (); + if (notifier == 0) + { + object->AddInterface (CreateObject ()); + } + } + } +} + +} // namespace ns3 diff --git a/src/mobility/mobility-helper.h b/src/mobility/mobility-helper.h new file mode 100644 index 000000000..627285039 --- /dev/null +++ b/src/mobility/mobility-helper.h @@ -0,0 +1,76 @@ +#ifndef MOBILITY_HELPER_H +#define MOBILITY_HELPER_H + +#include +#include "ns3/object-factory.h" +#include "ns3/value.h" +#include "position-allocator.h" + +namespace ns3 { + +class PositionAllocator; + +class MobilityHelper +{ +public: + MobilityHelper (); + + void EnableNotifier (void); + void DisableNotifier (void); + + void SetPositionAllocator (Ptr allocator); + + void SetPositionAllocator (std::string type, + std::string n1 = "", PValue v1 = PValue (), + std::string n2 = "", PValue v2 = PValue (), + std::string n3 = "", PValue v3 = PValue (), + std::string n4 = "", PValue v4 = PValue (), + std::string n5 = "", PValue v5 = PValue (), + std::string n6 = "", PValue v6 = PValue (), + std::string n7 = "", PValue v7 = PValue (), + std::string n8 = "", PValue v8 = PValue (), + std::string n9 = "", PValue v9 = PValue ()); + + void SetMobilityModel (std::string type, + std::string n1 = "", PValue v1 = PValue (), + std::string n2 = "", PValue v2 = PValue (), + std::string n3 = "", PValue v3 = PValue (), + std::string n4 = "", PValue v4 = PValue (), + std::string n5 = "", PValue v5 = PValue (), + std::string n6 = "", PValue v6 = PValue (), + std::string n7 = "", PValue v7 = PValue (), + std::string n8 = "", PValue v8 = PValue (), + std::string n9 = "", PValue v9 = PValue ()); + + std::string GetMobilityModelType (void) const; + + template + void Layout (T begin, T end); +private: + void Layout (const std::vector > &objects); + + bool m_notifierEnabled; + ObjectFactory m_mobility; + Ptr m_position; +}; + +} // namespace ns3 + +namespace ns3 { + +template +void +MobilityHelper::Layout (T begin, T end) +{ + std::vector > objects; + for (T i = begin; i != end; i++) + { + Ptr object = *i; + objects.push_back (object); + } + Layout (objects); +} + +} // namespace ns3 + +#endif /* MOBILITY_HELPER_H */ diff --git a/src/mobility/mobility-model.cc b/src/mobility/mobility-model.cc index a27a230ac..e72dd16fb 100644 --- a/src/mobility/mobility-model.cc +++ b/src/mobility/mobility-model.cc @@ -27,7 +27,18 @@ TypeId MobilityModel::GetTypeId (void) { static TypeId tid = TypeId ("MobilityModel") - .SetParent (); + .SetParent () + .AddParameter ("position", "The current position of the mobility model.", + TypeId::PARAM_SGC, + MakeVectorParamSpec (&MobilityModel::SetPosition, + &MobilityModel::GetPosition, + Vector (0.0, 0.0, 0.0))) + .AddParameter ("velocity", "The current velocity of the mobility model.", + TypeId::PARAM_GET, + MakeVectorParamSpec (&MobilityModel::GetVelocity, + Vector (0.0, 0.0, 0.0) // ignored initial value. + )) + ; return tid; } diff --git a/src/mobility/position-allocator.cc b/src/mobility/position-allocator.cc new file mode 100644 index 000000000..03ace15ba --- /dev/null +++ b/src/mobility/position-allocator.cc @@ -0,0 +1,170 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#include "position-allocator.h" +#include "ns3/random-variable.h" +#include "ns3/fp-value.h" +#include "ns3/int-value.h" +#include "ns3/enum-value.h" +#include "ns3/log.h" +#include + +NS_LOG_COMPONENT_DEFINE ("PositionAllocator"); + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (PositionAllocator); + +TypeId +PositionAllocator::GetTypeId (void) +{ + static TypeId tid = TypeId ("PositionAllocator") + .SetParent (); + return tid; +} + +PositionAllocator::PositionAllocator () +{ +} + +PositionAllocator::~PositionAllocator () +{} + +TypeId +GridPositionAllocator::GetTypeId (void) +{ + static TypeId tid = TypeId ("GridPositionAllocator") + .SetParent () + .SetGroupName ("Mobility") + .AddConstructor () + .AddParameter ("GridWidth", "The number of objects layed out on a line.", + MakeIntParamSpec (&GridPositionAllocator::m_n, 10)) + .AddParameter ("MinX", "The x coordinate where the grid starts.", + MakeFpParamSpec (&GridPositionAllocator::m_xMin, 0.0)) + .AddParameter ("MinY", "The y coordinate where the grid starts.", + MakeFpParamSpec (&GridPositionAllocator::m_yMin, 0.0)) + .AddParameter ("DeltaX", "The x space between objects.", + MakeFpParamSpec (&GridPositionAllocator::m_deltaX, 1.0)) + .AddParameter ("DeltaY", "The y space between objects.", + MakeFpParamSpec (&GridPositionAllocator::m_deltaY, 1.0)) + .AddParameter ("LayoutType", "The type of layout.", + MakeEnumParamSpec (&GridPositionAllocator::m_layoutType, + ROW_FIRST, "RowFirst", + COLUMN_FIRST, "ColumnFirst")) + ; + return tid; +} +GridPositionAllocator::GridPositionAllocator () + : m_current (0) +{} + +Vector +GridPositionAllocator::GetNext (void) const +{ + double x, y; + switch (m_layoutType) { + case ROW_FIRST: + x = m_xMin + m_deltaX * (m_current % m_n); + y = m_yMin + m_deltaY * (m_current / m_n); + break; + case COLUMN_FIRST: + x = m_xMin + m_deltaX * (m_current / m_n); + y = m_yMin + m_deltaY * (m_current % m_n); + break; + } + m_current++; + return Vector (x, y, 0.0); +} + + +NS_OBJECT_ENSURE_REGISTERED (RandomRectanglePositionAllocator); + +TypeId +RandomRectanglePositionAllocator::GetTypeId (void) +{ + static TypeId tid = TypeId ("RandomRectanglePositionAllocator") + .SetParent () + .SetGroupName ("Mobility") + .AddConstructor () + .AddParameter ("X", + "A random variable which represents the x coordinate of a position in a random rectangle.", + MakeRandomVariableParamSpec (&RandomRectanglePositionAllocator::m_x, + UniformVariable (0.0, 1.0))) + .AddParameter ("Y", + "A random variable which represents the y coordinate of a position in a random rectangle.", + MakeRandomVariableParamSpec (&RandomRectanglePositionAllocator::m_y, + UniformVariable (0.0, 1.0))); + return tid; +} + +RandomRectanglePositionAllocator::RandomRectanglePositionAllocator () +{} +RandomRectanglePositionAllocator::~RandomRectanglePositionAllocator () +{} +Vector +RandomRectanglePositionAllocator::GetNext (void) const +{ + double x = m_x.GetValue (); + double y = m_y.GetValue (); + return Vector (x, y, 0.0); +} + +NS_OBJECT_ENSURE_REGISTERED (RandomDiscPositionAllocator); + +TypeId +RandomDiscPositionAllocator::GetTypeId (void) +{ + static TypeId tid = TypeId ("RandomDiscPositionAllocator") + .SetParent () + .SetGroupName ("Mobility") + .AddConstructor () + .AddParameter ("Theta", + "A random variable which represents the angle (gradients) of a position in a random disc.", + MakeRandomVariableParamSpec (&RandomDiscPositionAllocator::m_theta, + UniformVariable (0.0, 6.2830))) + .AddParameter ("Rho", + "A random variable which represents the radius of a position in a random disc.", + MakeRandomVariableParamSpec (&RandomDiscPositionAllocator::m_rho, + UniformVariable (0.0, 200.0))) + .AddParameter ("X", + "The x coordinate of the center of the random position disc.", + MakeFpParamSpec (&RandomDiscPositionAllocator::m_x, 0.0)) + .AddParameter ("Y", + "The y coordinate of the center of the random position disc.", + MakeFpParamSpec (&RandomDiscPositionAllocator::m_y, 0.0)); + return tid; +} + +RandomDiscPositionAllocator::RandomDiscPositionAllocator () +{} +RandomDiscPositionAllocator::~RandomDiscPositionAllocator () +{} +Vector +RandomDiscPositionAllocator::GetNext (void) const +{ + double theta = m_theta.GetValue (); + double rho = m_rho.GetValue (); + double x = m_x + std::cos (theta) * rho; + double y = m_y + std::sin (theta) * rho; + NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y); + return Vector (x, y, 0.0); +} + + +} // namespace ns3 diff --git a/src/mobility/position-allocator.h b/src/mobility/position-allocator.h new file mode 100644 index 000000000..483617ccf --- /dev/null +++ b/src/mobility/position-allocator.h @@ -0,0 +1,105 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#ifndef POSITION_ALLOCATOR_H +#define POSITION_ALLOCATOR_H + +#include "ns3/object.h" +#include "ns3/random-variable.h" +#include "vector.h" + +namespace ns3 { + +/** + * \brief choose a position at random. + * + * This is a pure abstract base class. + */ +class PositionAllocator : public Object +{ +public: + static TypeId GetTypeId (void); + PositionAllocator (); + virtual ~PositionAllocator (); + /** + * \returns the next randomly-choosen position. + */ + virtual Vector GetNext (void) const = 0; +}; + +class GridPositionAllocator : public Object +{ +public: + static TypeId GetTypeId (void); + + enum LayoutType { + ROW_FIRST, + COLUMN_FIRST + }; + + GridPositionAllocator (); + + virtual Vector GetNext (void) const; +private: + mutable uint32_t m_current; + enum LayoutType m_layoutType; + double m_xMin; + double m_yMin; + uint32_t m_n; + double m_deltaX; + double m_deltaY; +}; + +/** + * \brief allocate random positions within a rectangle + * according to a pair of random variables. + */ +class RandomRectanglePositionAllocator : public PositionAllocator +{ +public: + static TypeId GetTypeId (void); + RandomRectanglePositionAllocator (); + virtual ~RandomRectanglePositionAllocator (); + virtual Vector GetNext (void) const; +private: + RandomVariable m_x; + RandomVariable m_y; +}; + +/** + * \brief allocate random positions within a disc + * according to a pair of random variables. + */ +class RandomDiscPositionAllocator : public PositionAllocator +{ +public: + static TypeId GetTypeId (void); + RandomDiscPositionAllocator (); + virtual ~RandomDiscPositionAllocator (); + virtual Vector GetNext (void) const; +private: + RandomVariable m_theta; + RandomVariable m_rho; + double m_x; + double m_y; +}; + +} // namespace ns3 + +#endif /* RANDOM_POSITION_H */ diff --git a/src/mobility/random-direction-2d-mobility-model.cc b/src/mobility/random-direction-2d-mobility-model.cc index fb6703d3d..8e0d090ce 100644 --- a/src/mobility/random-direction-2d-mobility-model.cc +++ b/src/mobility/random-direction-2d-mobility-model.cc @@ -19,7 +19,6 @@ * Author: Mathieu Lacage */ #include "ns3/random-variable-default-value.h" -#include "ns3/rectangle-default-value.h" #include "ns3/simulator.h" #include #include @@ -35,100 +34,33 @@ const double RandomDirection2dMobilityModel::PI = 3.14159265358979323846; NS_OBJECT_ENSURE_REGISTERED (RandomDirection2dMobilityModel); -static RandomVariableDefaultValue - g_speedVariable ("RandomDirection2dSpeed", - "A random variable to control the speed of a RandomDirection2d mobility model.", - "Uniform:1:2"); - -static RandomVariableDefaultValue - g_pauseVariable ("RandomDirection2dPause", - "A random variable to control the duration " - "of the pause of a RandomDiretion mobility model.", - "Constant:2"); - -static RectangleDefaultValue - g_bounds ("RandomDirection2dArea", - "The bounding area for the RandomDirection2d model.", - -100, 100, -100, 100); - - -RandomDirection2dMobilityModelParameters::RandomDirection2dMobilityModelParameters () - : m_bounds (g_bounds.GetValue ()), - m_speedVariable (g_speedVariable.Get ()), - m_pauseVariable (g_pauseVariable.Get ()) - -{} -RandomDirection2dMobilityModelParameters::RandomDirection2dMobilityModelParameters -(const Rectangle &bounds, - const RandomVariable &speedVariable, - const RandomVariable &pauseVariable) - : m_bounds (bounds), - m_speedVariable (speedVariable), - m_pauseVariable (pauseVariable) -{} - -RandomDirection2dMobilityModelParameters::~RandomDirection2dMobilityModelParameters () -{} - -void -RandomDirection2dMobilityModelParameters::SetSpeed (const RandomVariable &speedVariable) -{ - m_speedVariable = speedVariable; -} -void -RandomDirection2dMobilityModelParameters::SetPause (const RandomVariable &pauseVariable) -{ - m_pauseVariable = pauseVariable; -} -void -RandomDirection2dMobilityModelParameters::SetBounds (const Rectangle &bounds) -{ - m_bounds = bounds; -} - -Ptr -RandomDirection2dMobilityModelParameters::GetCurrent (void) -{ - static Ptr parameters = 0; - if (parameters == 0 || - g_bounds.IsDirty () || - g_speedVariable.IsDirty () || - g_pauseVariable.IsDirty ()) - { - parameters = CreateObject (); - g_bounds.ClearDirtyFlag (); - g_speedVariable.ClearDirtyFlag (); - g_pauseVariable.ClearDirtyFlag (); - } - return parameters; -} - TypeId RandomDirection2dMobilityModel::GetTypeId (void) { static TypeId tid = TypeId ("RandomDirection2dMobilityModel") .SetParent () + .SetGroupName ("Mobility") .AddConstructor () - .AddConstructor > (); + .AddParameter ("bounds", "The 2d bounding area", + MakeRectangleParamSpec (&RandomDirection2dMobilityModel::m_bounds, + Rectangle (-100, 100, -100, 100))) + .AddParameter ("speed", "A random variable to control the speed (m/s).", + MakeRandomVariableParamSpec (&RandomDirection2dMobilityModel::m_speed, + UniformVariable (1.0, 2.0))) + .AddParameter ("pause", "A random variable to control the pause (s).", + MakeRandomVariableParamSpec (&RandomDirection2dMobilityModel::m_pause, + ConstantVariable (2.0))); return tid; } RandomDirection2dMobilityModel::RandomDirection2dMobilityModel () - : m_parameters (RandomDirection2dMobilityModelParameters::GetCurrent ()) -{ - m_event = Simulator::ScheduleNow (&RandomDirection2dMobilityModel::Start, this); -} -RandomDirection2dMobilityModel::RandomDirection2dMobilityModel -(Ptr parameters) - : m_parameters (parameters) { m_event = Simulator::ScheduleNow (&RandomDirection2dMobilityModel::Start, this); } void RandomDirection2dMobilityModel::DoDispose (void) { - m_parameters = 0; // chain up. MobilityModel::DoDispose (); } @@ -142,7 +74,7 @@ RandomDirection2dMobilityModel::Start (void) void RandomDirection2dMobilityModel::BeginPause (void) { - Time pause = Seconds (m_parameters->m_pauseVariable.GetValue ()); + Time pause = Seconds (m_pause.GetValue ()); m_helper.Pause (); m_event = Simulator::Schedule (pause, &RandomDirection2dMobilityModel::ResetDirectionAndSpeed, this); NotifyCourseChange (); @@ -152,13 +84,13 @@ void RandomDirection2dMobilityModel::SetDirectionAndSpeed (double direction) { NS_LOG_FUNCTION; - double speed = m_parameters->m_speedVariable.GetValue (); + double speed = m_speed.GetValue (); const Vector vector (std::cos (direction) * speed, std::sin (direction) * speed, 0.0); - Vector position = m_helper.GetCurrentPosition (m_parameters->m_bounds); + Vector position = m_helper.GetCurrentPosition (m_bounds); m_helper.Reset (vector); - Vector next = m_parameters->m_bounds.CalculateIntersection (position, vector); + Vector next = m_bounds.CalculateIntersection (position, vector); Time delay = Seconds (CalculateDistance (position, next) / speed); m_event = Simulator::Schedule (delay, &RandomDirection2dMobilityModel::BeginPause, this); @@ -169,8 +101,8 @@ RandomDirection2dMobilityModel::ResetDirectionAndSpeed (void) { double direction = UniformVariable::GetSingleValue (0, PI); - Vector position = m_helper.GetCurrentPosition (m_parameters->m_bounds); - switch (m_parameters->m_bounds.GetClosestSide (position)) + Vector position = m_helper.GetCurrentPosition (m_bounds); + switch (m_bounds.GetClosestSide (position)) { case Rectangle::RIGHT: direction += PI / 2; @@ -190,7 +122,7 @@ RandomDirection2dMobilityModel::ResetDirectionAndSpeed (void) Vector RandomDirection2dMobilityModel::DoGetPosition (void) const { - return m_helper.GetCurrentPosition (m_parameters->m_bounds); + return m_helper.GetCurrentPosition (m_bounds); } void RandomDirection2dMobilityModel::DoSetPosition (const Vector &position) diff --git a/src/mobility/random-direction-2d-mobility-model.h b/src/mobility/random-direction-2d-mobility-model.h index f12e5b8cb..83eca421a 100644 --- a/src/mobility/random-direction-2d-mobility-model.h +++ b/src/mobility/random-direction-2d-mobility-model.h @@ -32,50 +32,6 @@ namespace ns3 { -/** - * \brief the parameters to control a RandomDirection mobility model. - */ -class RandomDirection2dMobilityModelParameters : public Object -{ - public: - /** - * Create from \valueref{RandomDirection2dSpeed}, - * \valueref{RandomDirection2dPause}, and, - * \valueref{RandomDirection2dArea}. - */ - RandomDirection2dMobilityModelParameters (); - /** - * \param bounds the 2d bounds of the mobility model - * \param speedVariable the random variable used to pick a random speed - * \param pauseVariable the random variable used to pick a random pause delay - */ - RandomDirection2dMobilityModelParameters (const Rectangle &bounds, - const RandomVariable &speedVariable, - const RandomVariable &pauseVariable); - virtual ~RandomDirection2dMobilityModelParameters (); - - /** - * \param speedVariable the random variable used to pick a random speed. - */ - void SetSpeed (const RandomVariable &speedVariable); - /** - * \param pauseVariable the random variable used to pick a random pause delay. - */ - void SetPause (const RandomVariable &pauseVariable); - /** - * \param bounds the 2d bounds of the mobility model. - */ - void SetBounds (const Rectangle &bounds); - private: - friend class RandomDirection2dMobilityModel; - - static Ptr GetCurrent (void); - - Rectangle m_bounds; - RandomVariable m_speedVariable; - RandomVariable m_pauseVariable; -}; - /** * \brief a RandomDirection mobility model * @@ -96,11 +52,6 @@ class RandomDirection2dMobilityModel : public MobilityModel * \valueref{RandomDirection2dArea}. */ RandomDirection2dMobilityModel (); - /** - * \param parameters the parameters which control the behavior of the model. - * Create a RandomDirection model using the parameters specified. - */ - RandomDirection2dMobilityModel (Ptr parameters); private: void Start (void); void ResetDirectionAndSpeed (void); @@ -113,7 +64,9 @@ class RandomDirection2dMobilityModel : public MobilityModel virtual Vector DoGetVelocity (void) const; static const double PI; - Ptr m_parameters; + Rectangle m_bounds; + RandomVariable m_speed; + RandomVariable m_pause; EventId m_event; StaticSpeedHelper m_helper; }; diff --git a/src/mobility/random-position.cc b/src/mobility/random-position.cc deleted file mode 100644 index 5db9f89f0..000000000 --- a/src/mobility/random-position.cc +++ /dev/null @@ -1,148 +0,0 @@ -/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 INRIA - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation; - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author: Mathieu Lacage - */ -#include "random-position.h" -#include "ns3/random-variable.h" -#include "ns3/default-value.h" -#include "ns3/random-variable-default-value.h" -#include "ns3/log.h" -#include - -NS_LOG_COMPONENT_DEFINE ("RandomPosition"); - -namespace ns3 { - -static RandomVariableDefaultValue -g_rectangleX ("RandomRectanglePositionX", - "A random variable which represents the x position of a position in a random rectangle.", - "Uniform:0:200"); - -static RandomVariableDefaultValue -g_rectangleY ("RandomRectanglePositionY", - "A random variable which represents the y position of a position in a random rectangle.", - "Uniform:0:200"); - -static RandomVariableDefaultValue -g_discTheta ("RandomDiscPositionTheta", - "A random variable which represents the angle (gradients) of a position in a random disc.", - "Uniform:0:6.2830"); - -static RandomVariableDefaultValue -g_discRho ("RandomDiscPositionRho", - "A random variable which represents the radius of a position in a random disc.", - "Uniform:0:200"); - -static NumericDefaultValue -g_discX ("RandomDiscPositionX", - "The x coordinate of the center of the random position disc.", - 0.0); - -static NumericDefaultValue -g_discY ("RandomDiscPositionY", - "The y coordinate of the center of the random position disc.", - 0.0); - -NS_OBJECT_ENSURE_REGISTERED (RandomPosition); - -TypeId -RandomPosition::GetTypeId (void) -{ - static TypeId tid = TypeId ("RandomPosition") - .SetParent (); - return tid; -} - -RandomPosition::RandomPosition () -{ -} - -RandomPosition::~RandomPosition () -{} - -NS_OBJECT_ENSURE_REGISTERED (RandomRectanglePosition); - -TypeId -RandomRectanglePosition::GetTypeId (void) -{ - static TypeId tid = TypeId ("RandomRectanglePosition") - .SetParent () - .AddConstructor () - .AddConstructor (); - return tid; -} - -RandomRectanglePosition::RandomRectanglePosition () - : m_x (g_rectangleX.Get ()), - m_y (g_rectangleY.Get ()) -{} -RandomRectanglePosition::RandomRectanglePosition (const RandomVariable &x, - const RandomVariable &y) - : m_x (x), - m_y (y) -{} -RandomRectanglePosition::~RandomRectanglePosition () -{} -Vector -RandomRectanglePosition::Get (void) const -{ - double x = m_x.GetValue (); - double y = m_y.GetValue (); - return Vector (x, y, 0.0); -} - -NS_OBJECT_ENSURE_REGISTERED (RandomDiscPosition); - -TypeId -RandomDiscPosition::GetTypeId (void) -{ - static TypeId tid = TypeId ("RandomDiscPosition") - .SetParent () - .AddConstructor (); - return tid; -} - -RandomDiscPosition::RandomDiscPosition () - : m_theta (g_discTheta.Get ()), - m_rho (g_discRho.Get ()), - m_x (g_discX.GetValue ()), - m_y (g_discY.GetValue ()) -{} -RandomDiscPosition::RandomDiscPosition (const RandomVariable &theta, - const RandomVariable &rho, - double x, double y) - : m_theta (theta), - m_rho (rho), - m_x (0.0), - m_y (0.0) -{} -RandomDiscPosition::~RandomDiscPosition () -{} -Vector -RandomDiscPosition::Get (void) const -{ - double theta = m_theta.GetValue (); - double rho = m_rho.GetValue (); - double x = m_x + std::cos (theta) * rho; - double y = m_y + std::sin (theta) * rho; - NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y); - return Vector (x, y, 0.0); -} - - -} // namespace ns3 diff --git a/src/mobility/random-position.h b/src/mobility/random-position.h deleted file mode 100644 index 2e4fc28cc..000000000 --- a/src/mobility/random-position.h +++ /dev/null @@ -1,117 +0,0 @@ -/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 INRIA - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation; - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Author: Mathieu Lacage - */ -#ifndef RANDOM_POSITION_H -#define RANDOM_POSITION_H - -#include "ns3/object.h" -#include "ns3/random-variable.h" -#include "vector.h" - -namespace ns3 { - -/** - * \brief choose a position at random. - * - * This is a pure abstract base class. - */ -class RandomPosition : public Object -{ -public: - static TypeId GetTypeId (void); - RandomPosition (); - virtual ~RandomPosition (); - /** - * \returns the next randomly-choosen position. - */ - virtual Vector Get (void) const = 0; -}; - -/** - * \brief allocate random positions within a rectangle - * according to a pair of random variables. - */ -class RandomRectanglePosition : public RandomPosition -{ -public: - static TypeId GetTypeId (void); - /** - * Create a random position model with construction - * values from \valueref{RandomRectanglePositionX}, and - * \valueref{RandomRectanglePositionY} - */ - RandomRectanglePosition (); - /** - * \param x the random variable which is used to choose - * the x coordinates. - * \param y the random variable which is used to choose - * the y coordinates. - */ - RandomRectanglePosition (const RandomVariable &x, - const RandomVariable &y); - virtual ~RandomRectanglePosition (); - virtual Vector Get (void) const; -private: - RandomVariable m_x; - RandomVariable m_y; -}; - -/** - * \brief allocate random positions within a disc - * according to a pair of random variables. - */ -class RandomDiscPosition : public RandomPosition -{ -public: - static TypeId GetTypeId (void); - /** - * Create a random position model with construction - * values from \valueref{RandomDiscPositionTheta}, - * \valueref{RandomDiscPositionRho}, - * \valueref{RandomDiscPositionX}, and, - * \valueref{RandomDiscPositionY}. - */ - RandomDiscPosition (); - /** - * \param theta the random variable used to pick - * the angle of the random position in polar - * coordinates. - * \param rho the random variable used to pick the - * radius of the random position in polar - * coordinates. - * \param x the x coordinate of the center of the - * polar coodinate system. - * \param y the y coordinate of the center of the - * polar coodinate system. - */ - RandomDiscPosition (const RandomVariable &theta, - const RandomVariable &rho, - double x, double y); - virtual ~RandomDiscPosition (); - virtual Vector Get (void) const; -private: - RandomVariable m_theta; - RandomVariable m_rho; - double m_x; - double m_y; -}; - -} // namespace ns3 - -#endif /* RANDOM_POSITION_H */ diff --git a/src/mobility/random-walk-2d-mobility-model.cc b/src/mobility/random-walk-2d-mobility-model.cc index bf488ece6..4e11fedf8 100644 --- a/src/mobility/random-walk-2d-mobility-model.cc +++ b/src/mobility/random-walk-2d-mobility-model.cc @@ -19,10 +19,7 @@ * Author: Mathieu Lacage */ #include "random-walk-2d-mobility-model.h" -#include "ns3/default-value.h" -#include "ns3/time-default-value.h" -#include "ns3/rectangle-default-value.h" -#include "ns3/random-variable-default-value.h" +#include "ns3/enum-value.h" #include "ns3/simulator.h" #include "ns3/log.h" #include @@ -33,113 +30,43 @@ namespace ns3 { NS_OBJECT_ENSURE_REGISTERED (RandomWalk2dMobilityModel); -static EnumDefaultValue -g_mode ("RandomWalk2dMode", - "The mode indicates the condition used to " - "change the current speed and direction", - RandomWalk2dMobilityModelParameters::MODE_DISTANCE, "Distance", - RandomWalk2dMobilityModelParameters::MODE_TIME, "Time", - 0, (void*)0); - -static NumericDefaultValue -g_modeDistance ("RandomWalk2dDistance", - "Change current direction and speed after moving this distance.", - 2.0); - -static TimeDefaultValue -g_modeTime ("RandomWalk2dTime", - "Change current direction and speed after moving for this delay.", - Seconds (1.0)); - -static RandomVariableDefaultValue -g_speed ("RandomWalk2dSpeed", - "A random variable used to pick the speed.", - "Uniform:2:4"); -static RandomVariableDefaultValue -g_direction ("RandomWalk2dDirection", - "A random variable used to pick the direction (gradients).", - "Uniform:0.0:6.283184"); - -static RectangleDefaultValue -g_rectangle ("RandomWalk2dBounds", - "Bounds of the area to cruise.", - 0.0, 0.0, 100.0, 100.0); - -RandomWalk2dMobilityModelParameters::RandomWalk2dMobilityModelParameters () - : m_mode (g_mode.GetValue ()), - m_modeDistance (g_modeDistance.GetValue ()), - m_modeTime (g_modeTime.GetValue ()), - m_speed (g_speed.Get ()), - m_direction (g_direction.Get ()), - m_bounds (g_rectangle.GetValue ()) -{} - -RandomWalk2dMobilityModelParameters::~RandomWalk2dMobilityModelParameters () -{} - -void -RandomWalk2dMobilityModelParameters::SetSpeed (const RandomVariable &speed) -{ - m_speed = speed; -} -void -RandomWalk2dMobilityModelParameters::SetDirection (const RandomVariable &direction) -{ - m_direction = direction; -} -void -RandomWalk2dMobilityModelParameters::SetModeDistance (double distance) -{ - m_mode = RandomWalk2dMobilityModelParameters::MODE_DISTANCE; - m_modeDistance = distance; -} -void -RandomWalk2dMobilityModelParameters::SetModeTime (Time time) -{ - m_mode = RandomWalk2dMobilityModelParameters::MODE_TIME; - m_modeTime = time; -} -void -RandomWalk2dMobilityModelParameters::SetBounds (const Rectangle &bounds) -{ - m_bounds = bounds; -} - -Ptr -RandomWalk2dMobilityModelParameters::GetCurrent (void) -{ - static Ptr parameters = 0; - if (parameters == 0 || - g_speed.IsDirty () || - g_direction.IsDirty () || - g_mode.IsDirty () || - g_modeDistance.IsDirty () || - g_modeTime.IsDirty () || - g_rectangle.IsDirty ()) - { - parameters = CreateObject (); - } - return parameters; -} - TypeId RandomWalk2dMobilityModel::GetTypeId (void) { static TypeId tid = TypeId ("RandomWalkMobilityModel") .SetParent () + .SetGroupName ("Mobility") .AddConstructor () - .AddConstructor > (); + .AddParameter ("bounds", + "Bounds of the area to cruise.", + MakeRectangleParamSpec (&RandomWalk2dMobilityModel::m_bounds, + Rectangle (0.0, 0.0, 100.0, 100.0))) + .AddParameter ("time", + "Change current direction and speed after moving for this delay.", + MakeTimeParamSpec (&RandomWalk2dMobilityModel::m_modeTime, + Seconds (1.0))) + .AddParameter ("distance", + "Change current direction and speed after moving for this distance.", + MakeTimeParamSpec (&RandomWalk2dMobilityModel::m_modeTime, + Seconds (1.0))) + .AddParameter ("mode", + "The mode indicates the condition used to " + "change the current speed and direction", + MakeEnumParamSpec (&RandomWalk2dMobilityModel::m_mode, + RandomWalk2dMobilityModel::MODE_DISTANCE, "Distance", + RandomWalk2dMobilityModel::MODE_TIME, "Time")) + .AddParameter ("direction", + "A random variable used to pick the direction (gradients).", + MakeRandomVariableParamSpec (&RandomWalk2dMobilityModel::m_direction, + UniformVariable (0.0, 6.283184))) + .AddParameter ("speed", + "A random variable used to pick the speed (m/s).", + MakeRandomVariableParamSpec (&RandomWalk2dMobilityModel::m_speed, + UniformVariable (2.0, 4.0))); return tid; } RandomWalk2dMobilityModel::RandomWalk2dMobilityModel () - : m_parameters (RandomWalk2dMobilityModelParameters::GetCurrent ()) -{ - m_event = Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); -} - -RandomWalk2dMobilityModel::RandomWalk2dMobilityModel (Ptr parameters) - : m_parameters (parameters) { m_event = Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); } @@ -147,21 +74,21 @@ RandomWalk2dMobilityModel::RandomWalk2dMobilityModel (Ptrm_speed.GetValue (); - double direction = m_parameters->m_direction.GetValue (); + double speed = m_speed.GetValue (); + double direction = m_direction.GetValue (); Vector vector (std::cos (direction) * speed, std::sin (direction) * speed, 0.0); m_helper.Reset (vector); Time delayLeft; - if (m_parameters->m_mode == RandomWalk2dMobilityModelParameters::MODE_TIME) + if (m_mode == RandomWalk2dMobilityModel::MODE_TIME) { - delayLeft = m_parameters->m_modeTime; + delayLeft = m_modeTime; } else { - delayLeft = Seconds (m_parameters->m_modeDistance / speed); + delayLeft = Seconds (m_modeDistance / speed); } DoWalk (delayLeft); } @@ -174,13 +101,13 @@ RandomWalk2dMobilityModel::DoWalk (Time delayLeft) Vector nextPosition = position; nextPosition.x += speed.x * delayLeft.GetSeconds (); nextPosition.y += speed.y * delayLeft.GetSeconds (); - if (m_parameters->m_bounds.IsInside (nextPosition)) + if (m_bounds.IsInside (nextPosition)) { m_event = Simulator::Schedule (delayLeft, &RandomWalk2dMobilityModel::Start, this); } else { - nextPosition = m_parameters->m_bounds.CalculateIntersection (position, speed); + nextPosition = m_bounds.CalculateIntersection (position, speed); Time delay = Seconds ((nextPosition.x - position.x) / speed.x); m_event = Simulator::Schedule (delay, &RandomWalk2dMobilityModel::Rebound, this, delayLeft - delay); @@ -191,9 +118,9 @@ RandomWalk2dMobilityModel::DoWalk (Time delayLeft) void RandomWalk2dMobilityModel::Rebound (Time delayLeft) { - Vector position = m_helper.GetCurrentPosition (m_parameters->m_bounds); + Vector position = m_helper.GetCurrentPosition (m_bounds); Vector speed = m_helper.GetVelocity (); - switch (m_parameters->m_bounds.GetClosestSide (position)) + switch (m_bounds.GetClosestSide (position)) { case Rectangle::RIGHT: case Rectangle::LEFT: @@ -211,19 +138,18 @@ RandomWalk2dMobilityModel::Rebound (Time delayLeft) void RandomWalk2dMobilityModel::DoDispose (void) { - m_parameters = 0; // chain up MobilityModel::DoDispose (); } Vector RandomWalk2dMobilityModel::DoGetPosition (void) const { - return m_helper.GetCurrentPosition (m_parameters->m_bounds); + return m_helper.GetCurrentPosition (m_bounds); } void RandomWalk2dMobilityModel::DoSetPosition (const Vector &position) { - NS_ASSERT (m_parameters->m_bounds.IsInside (position)); + NS_ASSERT (m_bounds.IsInside (position)); m_helper.InitializePosition (position); Simulator::Remove (m_event); m_event = Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); diff --git a/src/mobility/random-walk-2d-mobility-model.h b/src/mobility/random-walk-2d-mobility-model.h index 600ed1435..c46f1b3d6 100644 --- a/src/mobility/random-walk-2d-mobility-model.h +++ b/src/mobility/random-walk-2d-mobility-model.h @@ -32,72 +32,6 @@ namespace ns3 { -/** - * \brief parameters to control a random walk 2d model - * - * A single parameter object can be shared by multiple random - * walk models. - */ -class RandomWalk2dMobilityModelParameters : public Object -{ - public: - /** - * Instantiate a set of RandomWalk parameters initialized - * with construction values from \valueref{RandomWalk2dMode}, - * \valueref{RandomWalk2dDistance}, \valueref{RandomWalk2dTime}, - * \valueref{RandomWalk2dSpeed}, \valueref{RandomWalk2dDirection}, - * and, \valueref{RandomWalk2dBounds}. - */ - RandomWalk2dMobilityModelParameters (); - virtual ~RandomWalk2dMobilityModelParameters (); - /** - * \param speed the random variable used to pick a new - * speed when the direction is changed. - * - */ - void SetSpeed (const RandomVariable &speed); - /** - * \param direction the random variable used to pick a new - * direction. - */ - void SetDirection (const RandomVariable &direction); - /** - * \param distance the distance before a direction change - * - * Unit is meters. - * "time" mode is incompatible with "distance" mode. - */ - void SetModeDistance (double distance); - /** - * \param time the delay before a direction change. - * - * "time" mode is incompatible with "distance" mode. - */ - void SetModeTime (Time time); - - /** - * \param bounds the bounds of the random walk - */ - void SetBounds (const Rectangle &bounds); - - - // needed public for internal default value code. - enum Mode { - MODE_DISTANCE, - MODE_TIME - }; - private: - friend class RandomWalk2dMobilityModel; - static Ptr GetCurrent (void); - - enum Mode m_mode; - double m_modeDistance; - Time m_modeTime; - RandomVariable m_speed; - RandomVariable m_direction; - Rectangle m_bounds; -}; - /** * \brief a 2D random walk position model * @@ -116,6 +50,12 @@ class RandomWalk2dMobilityModel : public MobilityModel { public: static TypeId GetTypeId (void); + + enum Mode { + MODE_DISTANCE, + MODE_TIME + }; + /** * Instantiate a set of RandomWalk parameters initialized * with construction values from \valueref{RandomWalk2dMode}, @@ -126,14 +66,6 @@ class RandomWalk2dMobilityModel : public MobilityModel * The default position is (0,0,0) */ RandomWalk2dMobilityModel (); - /** - * \param parameters the parameters to use to control - * the movement of this mobile object. - * - * Create a new position object located at position (0,0,0) with - * the specified parameters. - */ - RandomWalk2dMobilityModel (Ptr parameters); private: void Start (void); @@ -146,7 +78,12 @@ class RandomWalk2dMobilityModel : public MobilityModel StaticSpeedHelper m_helper; EventId m_event; - Ptr m_parameters; + enum Mode m_mode; + double m_modeDistance; + Time m_modeTime; + RandomVariable m_speed; + RandomVariable m_direction; + Rectangle m_bounds; }; diff --git a/src/mobility/random-waypoint-mobility-model.cc b/src/mobility/random-waypoint-mobility-model.cc index 1fb9bb91a..bf926a26d 100644 --- a/src/mobility/random-waypoint-mobility-model.cc +++ b/src/mobility/random-waypoint-mobility-model.cc @@ -23,106 +23,45 @@ #include "ns3/random-variable-default-value.h" #include "ns3/type-id-default-value.h" #include "random-waypoint-mobility-model.h" -#include "random-position.h" +#include "position-allocator.h" namespace ns3 { NS_OBJECT_ENSURE_REGISTERED (RandomWaypointMobilityModel); -static RandomVariableDefaultValue -g_speed ("RandomWaypointSpeed", - "A random variable used to pick the speed of a random waypoint model.", - "Uniform:0.3:0.7"); - -static RandomVariableDefaultValue -g_pause ("RandomWaypointPause", - "A random variable used to pick the pause of a random waypoint model.", - "Constant:2"); - -static TypeIdDefaultValue -g_position ("RandomWaypointPosition", - "A random position model used to pick the next waypoint position.", - RandomPosition::GetTypeId (), - "RandomRectanglePosition"); - - -RandomWaypointMobilityModelParameters::RandomWaypointMobilityModelParameters () - : m_speed (g_speed.Get ()), - m_pause (g_pause.Get ()) -{ - m_position = g_position.GetValue ().CreateObject ()->QueryInterface (); -} -RandomWaypointMobilityModelParameters::RandomWaypointMobilityModelParameters (Ptr randomPosition, - const RandomVariable &speed, - const RandomVariable &pause) - : m_speed (speed), - m_pause (pause), - m_position (randomPosition) -{} -void -RandomWaypointMobilityModelParameters::SetWaypointPositionModel (Ptr randomPosition) -{ - m_position = randomPosition; -} -void -RandomWaypointMobilityModelParameters::SetSpeed (const RandomVariable &speed) -{ - m_speed = speed; -} -void -RandomWaypointMobilityModelParameters::SetPause (const RandomVariable &pause) -{ - m_pause = pause; -} -void -RandomWaypointMobilityModelParameters::DoDispose (void) -{ - m_position = 0; -} - -Ptr -RandomWaypointMobilityModelParameters::GetCurrent (void) -{ - static Ptr parameters = 0; - if (parameters == 0 || - g_position.IsDirty () || - g_pause.IsDirty () || - g_speed.IsDirty ()) - { - parameters = CreateObject (); - } - return parameters; -} - TypeId RandomWaypointMobilityModel::GetTypeId (void) { static TypeId tid = TypeId ("RandomWaypointMobilityModel") .SetParent () + .SetGroupName ("Mobility") .AddConstructor () - .AddConstructor > (); + .AddParameter ("speed", + "A random variable used to pick the speed of a random waypoint model.", + MakeRandomVariableParamSpec (&RandomWaypointMobilityModel::m_speed, + UniformVariable (0.3, 0.7))) + .AddParameter ("pause", + "A random variable used to pick the pause of a random waypoint model.", + MakeRandomVariableParamSpec (&RandomWaypointMobilityModel::m_pause, + ConstantVariable (2.0))) + .AddParameter ("position", + "The position model used to pick a destination point.", + MakePtrParamSpec (&RandomWaypointMobilityModel::m_position)); + return tid; } RandomWaypointMobilityModel::RandomWaypointMobilityModel () - : m_parameters (RandomWaypointMobilityModelParameters::GetCurrent ()) { Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this); } -RandomWaypointMobilityModel::RandomWaypointMobilityModel (Ptr parameters) - : m_parameters (parameters) -{ - Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this); - NotifyCourseChange (); -} - void RandomWaypointMobilityModel::BeginWalk (void) { Vector m_current = m_helper.GetCurrentPosition (); - Vector destination = m_parameters->m_position->Get (); - double speed = m_parameters->m_speed.GetValue (); + Vector destination = m_position->GetNext (); + double speed = m_speed.GetValue (); double dx = (destination.x - m_current.x); double dy = (destination.y - m_current.y); double dz = (destination.z - m_current.z); @@ -138,7 +77,7 @@ RandomWaypointMobilityModel::BeginWalk (void) void RandomWaypointMobilityModel::Start (void) { - Time pause = Seconds (m_parameters->m_pause.GetValue ()); + Time pause = Seconds (m_pause.GetValue ()); m_helper.Pause (); NotifyCourseChange (); m_event = Simulator::Schedule (pause, &RandomWaypointMobilityModel::BeginWalk, this); diff --git a/src/mobility/random-waypoint-mobility-model.h b/src/mobility/random-waypoint-mobility-model.h index 7b3288177..66d50cd53 100644 --- a/src/mobility/random-waypoint-mobility-model.h +++ b/src/mobility/random-waypoint-mobility-model.h @@ -23,53 +23,12 @@ #include "static-speed-helper.h" #include "mobility-model.h" -#include "random-position.h" +#include "position-allocator.h" #include "ns3/ptr.h" #include "ns3/random-variable.h" namespace ns3 { -/** - * \brief the parameters which control the behavior of a random waypoint - * mobility model. - */ -class RandomWaypointMobilityModelParameters : public Object -{ -public: - /** - * Default parameters from \valueref{RandomWaypointPause}, - * and, \valueref{RandomWaypointPosition}. - */ - RandomWaypointMobilityModelParameters (); - /** - * \param randomPosition a random position model to choose the position of waypoints. - * \param speed a random variable to choose the speed - * \param pause a random variable to choose the pause delay - */ - RandomWaypointMobilityModelParameters (Ptr randomPosition, - const RandomVariable &speed, - const RandomVariable &pause); - /** - * \param randomPosition a random position model to choose the position of waypoints. - */ - void SetWaypointPositionModel (Ptr randomPosition); - /** - * \param speed a random variable to choose the speed - */ - void SetSpeed (const RandomVariable &speed); - /** - * \param pause a random variable to choose the pause delay - */ - void SetPause (const RandomVariable &pause); -private: - friend class RandomWaypointMobilityModel; - static Ptr GetCurrent (void); - virtual void DoDispose (void); - RandomVariable m_speed; - RandomVariable m_pause; - Ptr m_position; -}; - /** * \brief a random waypoint mobility model * @@ -86,15 +45,7 @@ class RandomWaypointMobilityModel : public MobilityModel { public: static TypeId GetTypeId (void); - /** - * Default parameters from \valueref{RandomWaypointPause}, - * and, \valueref{RandomWaypointPosition}. - */ RandomWaypointMobilityModel (); - /** - * \param parameters the parameters which control the behavior of this model. - */ - RandomWaypointMobilityModel (Ptr parameters); private: void Start (void); void BeginWalk (void); @@ -103,7 +54,9 @@ private: virtual Vector DoGetVelocity (void) const; StaticSpeedHelper m_helper; - Ptr m_parameters; + Ptr m_position; + RandomVariable m_speed; + RandomVariable m_pause; EventId m_event; }; diff --git a/src/mobility/static-mobility-model.cc b/src/mobility/static-mobility-model.cc index f9c0a81d1..239e0000a 100644 --- a/src/mobility/static-mobility-model.cc +++ b/src/mobility/static-mobility-model.cc @@ -29,15 +29,12 @@ StaticMobilityModel::GetTypeId (void) static TypeId tid = TypeId ("StaticMobilityModel") .SetParent () .AddConstructor () - .AddConstructor (); + ; return tid; } StaticMobilityModel::StaticMobilityModel () {} -StaticMobilityModel::StaticMobilityModel (const Vector &position) - : m_position (position) -{} StaticMobilityModel::~StaticMobilityModel () {} diff --git a/src/mobility/static-mobility-model.h b/src/mobility/static-mobility-model.h index 56358f366..f7b936cc2 100644 --- a/src/mobility/static-mobility-model.h +++ b/src/mobility/static-mobility-model.h @@ -21,6 +21,7 @@ #define STATIC_MOBILITY_MODEL_H #include "mobility-model.h" +#include "vector.h" namespace ns3 { @@ -37,13 +38,6 @@ public: * Create a position located at coordinates (0,0,0) */ StaticMobilityModel (); - /** - * \param position the initial position. - * - * Create a position located at coordinates (x,y,z). - * Unit is meters - */ - StaticMobilityModel (const Vector &position); virtual ~StaticMobilityModel (); private: diff --git a/src/mobility/static-speed-mobility-model.cc b/src/mobility/static-speed-mobility-model.cc index 11c641a28..720f05bd4 100644 --- a/src/mobility/static-speed-mobility-model.cc +++ b/src/mobility/static-speed-mobility-model.cc @@ -28,21 +28,12 @@ TypeId StaticSpeedMobilityModel::GetTypeId (void) { static TypeId tid = TypeId ("StaticSpeedMobilityModel") .SetParent () - .AddConstructor () - .AddConstructor () - .AddConstructor (); + .AddConstructor (); return tid; } StaticSpeedMobilityModel::StaticSpeedMobilityModel () {} -StaticSpeedMobilityModel::StaticSpeedMobilityModel (const Vector &position) - : m_helper (position) -{} -StaticSpeedMobilityModel::StaticSpeedMobilityModel (const Vector &position, - const Vector &speed) - : m_helper (position, speed) -{} StaticSpeedMobilityModel::~StaticSpeedMobilityModel () {} diff --git a/src/mobility/static-speed-mobility-model.h b/src/mobility/static-speed-mobility-model.h index 27a0fb4aa..3da5b466e 100644 --- a/src/mobility/static-speed-mobility-model.h +++ b/src/mobility/static-speed-mobility-model.h @@ -41,19 +41,6 @@ public: * speed (0,0,0). */ StaticSpeedMobilityModel (); - /** - * Create a position located at coordinates (x,y,z) with - * speed (0,0,0). - */ - StaticSpeedMobilityModel (const Vector &position); - /** - * - * Create a position located at coordinates (x,y,z) with - * speed (dx,dy,dz). - * Unit is meters and meters/s - */ - StaticSpeedMobilityModel (const Vector &position, - const Vector &speed); virtual ~StaticSpeedMobilityModel (); /** diff --git a/src/mobility/wscript b/src/mobility/wscript index 8b5256a77..63f9db563 100644 --- a/src/mobility/wscript +++ b/src/mobility/wscript @@ -4,12 +4,10 @@ def build(bld): mobility = bld.create_ns3_module('mobility', ['core', 'simulator']) mobility.source = [ 'vector.cc', - 'grid-topology.cc', 'hierarchical-mobility-model.cc', 'mobility-model.cc', 'mobility-model-notifier.cc', - 'random-position.cc', - 'random-topology.cc', + 'position-allocator.cc', 'rectangle.cc', 'rectangle-default-value.cc', 'static-mobility-model.cc', @@ -19,17 +17,16 @@ def build(bld): 'random-walk-2d-mobility-model.cc', 'random-direction-2d-mobility-model.cc', 'ns2-mobility-file-topology.cc', + 'mobility-helper.cc', ] headers = bld.create_obj('ns3header') headers.source = [ 'vector.h', - 'grid-topology.h', 'hierarchical-mobility-model.h', 'mobility-model.h', 'mobility-model-notifier.h', - 'random-position.h', - 'random-topology.h', + 'position-allocator.h', 'rectangle.h', 'rectangle-default-value.h', 'static-mobility-model.h', @@ -39,4 +36,5 @@ def build(bld): 'random-walk-2d-mobility-model.h', 'random-direction-2d-mobility-model.h', 'ns2-mobility-file-topology.h', + 'mobility-helper.h', ] diff --git a/utils/mobility-visualizer-model.cc b/utils/mobility-visualizer-model.cc index 2abec2202..0bba20612 100644 --- a/utils/mobility-visualizer-model.cc +++ b/utils/mobility-visualizer-model.cc @@ -5,7 +5,7 @@ #include "ns3/ptr.h" #include "ns3/mobility-model.h" #include "ns3/mobility-model-notifier.h" -#include "ns3/random-topology.h" +#include "ns3/position-allocator.h" #include "ns3/default-value.h" #include "ns3/command-line.h" #include "ns3/command-line.h" @@ -15,6 +15,7 @@ #include "ns3/node-list.h" #include "ns3/rectangle-default-value.h" #include "ns3/type-id-default-value.h" +#include "ns3/mobility-helper.h" #include "mobility-visualizer.h" @@ -89,20 +90,19 @@ int model_init (int argc, char *argv[], double *x1, double *y1, double *x2, doub CommandLine::Parse (argc, argv); - RandomTopology topology; + MobilityHelper mobility; for (uint32_t i = 0; i < g_numNodes; i++) { Ptr node = CreateObject (); - node->AddInterface (CreateObject ()); } - topology.Layout (NodeList::Begin (), NodeList::End ()); + mobility.EnableNotifier (); + mobility.Layout (NodeList::Begin (), NodeList::End ()); Simulator::Schedule (g_sampleInterval, Sample); - TypeId mobType = DefaultValueListGet ("RandomTopologyMobilityType")->GetValue (); - if (mobType.GetName () == "RandomWalk2dMobilityModel") + if (mobility.GetMobilityModelType () == "RandomWalk2dMobilityModel") { Rectangle bounds = DefaultValueListGet ("RandomWalk2dBounds")->GetValue (); *x1 = bounds.xMin; @@ -112,7 +112,7 @@ int model_init (int argc, char *argv[], double *x1, double *y1, double *x2, doub std::cout << "RECT " << bounds.xMin << " " << bounds.xMax << " " << bounds.yMin << " " << bounds.yMax << std::endl; } - else if (mobType.GetName () == "RandomDirection2dMobilityModel") + else if (mobility.GetMobilityModelType () == "RandomDirection2dMobilityModel") { Rectangle bounds = DefaultValueListGet ("RandomDirection2dArea")->GetValue (); *x1 = bounds.xMin; @@ -122,7 +122,7 @@ int model_init (int argc, char *argv[], double *x1, double *y1, double *x2, doub std::cout << "RECT " << bounds.xMin << " " << bounds.xMax << " " << bounds.yMin << " " << bounds.yMax << std::endl; } - else if (mobType.GetName () == "RandomWaypointMobilityModel") + else if (mobility.GetMobilityModelType () == "RandomWaypointMobilityModel") { std::cerr << "bounds for RandomWaypointMobilityModel not implemented" << std::endl; //ClassId posType = DefaultValueList::Get ("RandomWaypointPosition")->GetValue (); @@ -130,7 +130,7 @@ int model_init (int argc, char *argv[], double *x1, double *y1, double *x2, doub } else { - NS_FATAL_ERROR ("mobility type " << mobType.GetName () << " not supported"); + NS_FATAL_ERROR ("mobility type " << mobility.GetMobilityModelType () << " not supported"); } std::cerr << g_sampleInterval << std::endl; From 58679140f24d589fc34fb74d75ba55f418123de3 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 17:27:38 +0100 Subject: [PATCH 031/130] rework slightly ns2 mobility support --HG-- rename : src/mobility/ns2-mobility-file-topology.cc => src/mobility/ns2-mobility-helper.cc rename : src/mobility/ns2-mobility-file-topology.h => src/mobility/ns2-mobility-helper.h --- ...ile-topology.cc => ns2-mobility-helper.cc} | 33 +++++++++++++++---- ...-file-topology.h => ns2-mobility-helper.h} | 10 ++++-- src/mobility/wscript | 4 +-- 3 files changed, 35 insertions(+), 12 deletions(-) rename src/mobility/{ns2-mobility-file-topology.cc => ns2-mobility-helper.cc} (82%) rename src/mobility/{ns2-mobility-file-topology.h => ns2-mobility-helper.h} (93%) diff --git a/src/mobility/ns2-mobility-file-topology.cc b/src/mobility/ns2-mobility-helper.cc similarity index 82% rename from src/mobility/ns2-mobility-file-topology.cc rename to src/mobility/ns2-mobility-helper.cc index 5f804f769..c79b55264 100644 --- a/src/mobility/ns2-mobility-file-topology.cc +++ b/src/mobility/ns2-mobility-helper.cc @@ -24,21 +24,34 @@ #include "ns3/simulator.h" #include "ns3/node-list.h" #include "ns3/node.h" -#include "ns2-mobility-file-topology.h" +#include "ns2-mobility-helper.h" #include "static-speed-mobility-model.h" +#include "mobility-model-notifier.h" -NS_LOG_COMPONENT_DEFINE ("Ns2MobilityFileTopology"); +NS_LOG_COMPONENT_DEFINE ("Ns2MobilityHelper"); namespace ns3 { -Ns2MobilityFileTopology::Ns2MobilityFileTopology (std::string filename) +Ns2MobilityHelper::Ns2MobilityHelper (std::string filename) : m_filename (filename) {} +void +Ns2MobilityHelper::EnableNotifier (void) +{ + m_notifierEnabled = true; +} +void +Ns2MobilityHelper::DisableNotifier (void) +{ + m_notifierEnabled = false; +} + + Ptr -Ns2MobilityFileTopology::GetMobilityModel (std::string idString, const ObjectStore &store) const +Ns2MobilityHelper::GetMobilityModel (std::string idString, const ObjectStore &store) const { std::istringstream iss; iss.str (idString); @@ -55,11 +68,17 @@ Ns2MobilityFileTopology::GetMobilityModel (std::string idString, const ObjectSto model = CreateObject (); object->AddInterface (model); } + Ptr notifier = object->QueryInterface (); + if (notifier == 0) + { + notifier = CreateObject (); + object->AddInterface (notifier); + } return model; } double -Ns2MobilityFileTopology::ReadDouble (std::string valueString) const +Ns2MobilityHelper::ReadDouble (std::string valueString) const { std::istringstream iss; iss.str (valueString); @@ -69,7 +88,7 @@ Ns2MobilityFileTopology::ReadDouble (std::string valueString) const } void -Ns2MobilityFileTopology::LayoutObjectStore (const ObjectStore &store) const +Ns2MobilityHelper::LayoutObjectStore (const ObjectStore &store) const { std::ifstream file (m_filename.c_str (), std::ios::in); if (file.is_open()) @@ -136,7 +155,7 @@ Ns2MobilityFileTopology::LayoutObjectStore (const ObjectStore &store) const } void -Ns2MobilityFileTopology::Layout (void) const +Ns2MobilityHelper::Layout (void) const { Layout (NodeList::Begin (), NodeList::End ()); } diff --git a/src/mobility/ns2-mobility-file-topology.h b/src/mobility/ns2-mobility-helper.h similarity index 93% rename from src/mobility/ns2-mobility-file-topology.h rename to src/mobility/ns2-mobility-helper.h index 1bcc1a5fe..7f7580861 100644 --- a/src/mobility/ns2-mobility-file-topology.h +++ b/src/mobility/ns2-mobility-helper.h @@ -33,14 +33,17 @@ namespace ns3 { * \brief a topology object which can read ns2's movement files * generated by the CMU setdest tool. */ -class Ns2MobilityFileTopology +class Ns2MobilityHelper { public: /** * \param filename filename of file which contains the * ns2 movement trace. */ - Ns2MobilityFileTopology (std::string filename); + Ns2MobilityHelper (std::string filename); + + void EnableNotifier (void); + void DisableNotifier (void); /** * Read the ns2 trace file and configure the movement @@ -74,6 +77,7 @@ private: Ptr GetMobilityModel (std::string idString, const ObjectStore &store) const; double ReadDouble (std::string valueString) const; std::string m_filename; + bool m_notifierEnabled; }; } // namespace ns3 @@ -82,7 +86,7 @@ namespace ns3 { template void -Ns2MobilityFileTopology::Layout (T begin, T end) const +Ns2MobilityHelper::Layout (T begin, T end) const { class MyObjectStore : public ObjectStore { diff --git a/src/mobility/wscript b/src/mobility/wscript index 63f9db563..20c16a76c 100644 --- a/src/mobility/wscript +++ b/src/mobility/wscript @@ -16,7 +16,7 @@ def build(bld): 'random-waypoint-mobility-model.cc', 'random-walk-2d-mobility-model.cc', 'random-direction-2d-mobility-model.cc', - 'ns2-mobility-file-topology.cc', + 'ns2-mobility-helper.cc', 'mobility-helper.cc', ] @@ -35,6 +35,6 @@ def build(bld): 'random-waypoint-mobility-model.h', 'random-walk-2d-mobility-model.h', 'random-direction-2d-mobility-model.h', - 'ns2-mobility-file-topology.h', + 'ns2-mobility-helper.h', 'mobility-helper.h', ] From af363eaa5ea2354aee20d422b0c2bababa4ecf94 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Feb 2008 17:52:20 +0100 Subject: [PATCH 032/130] add hierarchical support to MobilityHelper --- src/mobility/hierarchical-mobility-model.cc | 50 ++++++++++++++------- src/mobility/hierarchical-mobility-model.h | 8 ++-- src/mobility/mobility-helper.cc | 28 +++++++++++- src/mobility/mobility-helper.h | 5 +++ 4 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/mobility/hierarchical-mobility-model.cc b/src/mobility/hierarchical-mobility-model.cc index 46f0150b3..dc14d2efe 100644 --- a/src/mobility/hierarchical-mobility-model.cc +++ b/src/mobility/hierarchical-mobility-model.cc @@ -28,32 +28,48 @@ TypeId HierarchicalMobilityModel::GetTypeId (void) { static TypeId tid = TypeId ("HierarchicalMobilityModel") - .SetParent (); + .SetParent () + .AddConstructor () + .AddParameter ("child", "The child mobility model.", + MakePtrParamSpec (&HierarchicalMobilityModel::SetChild)) + .AddParameter ("parent", "The parent mobility model.", + MakePtrParamSpec (&HierarchicalMobilityModel::SetParent)) + ; return tid; } -HierarchicalMobilityModel::HierarchicalMobilityModel (Ptr child, Ptr parent) - : m_child (child), - m_parent (parent) +HierarchicalMobilityModel::HierarchicalMobilityModel () +{} + +void +HierarchicalMobilityModel::SetChild (Ptr model) { - Ptr childNotifier = + m_child = model; + Ptr notifier = m_child->QueryInterface (); - Ptr parentNotifier = - m_parent->QueryInterface (); - if (childNotifier == 0) + if (notifier == 0) { - childNotifier = CreateObject (); - child->AddInterface (childNotifier); + notifier = CreateObject (); + m_child->AddInterface (notifier); } - if (parentNotifier == 0) - { - parentNotifier = CreateObject (); - parent->AddInterface (parentNotifier); - } - childNotifier->TraceConnect ("/course-changed", MakeCallback (&HierarchicalMobilityModel::ChildChanged, this)); - parentNotifier->TraceConnect ("/course-changed", MakeCallback (&HierarchicalMobilityModel::ParentChanged, this)); + notifier->TraceConnect ("/course-changed", MakeCallback (&HierarchicalMobilityModel::ChildChanged, this)); } +void +HierarchicalMobilityModel::SetParent (Ptr model) +{ + m_parent = model; + Ptr notifier = + m_parent->QueryInterface (); + if (notifier == 0) + { + notifier = CreateObject (); + m_parent->AddInterface (notifier); + } + notifier->TraceConnect ("/course-changed", MakeCallback (&HierarchicalMobilityModel::ParentChanged, this)); +} + + Ptr HierarchicalMobilityModel::GetChild (void) const { diff --git a/src/mobility/hierarchical-mobility-model.h b/src/mobility/hierarchical-mobility-model.h index e45380bbd..91191222e 100644 --- a/src/mobility/hierarchical-mobility-model.h +++ b/src/mobility/hierarchical-mobility-model.h @@ -35,11 +35,7 @@ class HierarchicalMobilityModel : public MobilityModel public: static TypeId GetTypeId (void); - /** - * \param child the "relative" mobility model - * \param parent the "reference" mobility model - */ - HierarchicalMobilityModel (Ptr child, Ptr parent); + HierarchicalMobilityModel (); /** * \returns the child mobility model. @@ -62,6 +58,8 @@ private: virtual void DoSetPosition (const Vector &position); virtual Vector DoGetVelocity (void) const; + void SetChild (Ptr model); + void SetParent (Ptr model); void ParentChanged (const TraceContext &context, Ptr model); void ChildChanged (const TraceContext &context, Ptr model); diff --git a/src/mobility/mobility-helper.cc b/src/mobility/mobility-helper.cc index d8ad0df1d..341465738 100644 --- a/src/mobility/mobility-helper.cc +++ b/src/mobility/mobility-helper.cc @@ -2,6 +2,7 @@ #include "mobility-model.h" #include "mobility-model-notifier.h" #include "position-allocator.h" +#include "hierarchical-mobility-model.h" namespace ns3 { @@ -73,6 +74,19 @@ MobilityHelper::SetMobilityModel (std::string type, m_mobility.Set (n9, v9); } +void +MobilityHelper::PushReferenceMobilityModel (Ptr reference) +{ + Ptr mobility = reference->QueryInterface (); + m_mobilityStack.push_back (mobility); +} +void +MobilityHelper::PopReferenceMobilityModel (void) +{ + m_mobilityStack.pop_back (); +} + + std::string MobilityHelper::GetMobilityModelType (void) const { @@ -94,7 +108,19 @@ MobilityHelper::Layout (const std::vector > &objects) NS_FATAL_ERROR ("The requested mobility model is not a mobility model: \""<< m_mobility.GetTypeId ().GetName ()<<"\""); } - object->AddInterface (model); + if (m_mobilityStack.empty ()) + { + object->AddInterface (model); + } + else + { + // we need to setup a hierarchical mobility model + Ptr parent = m_mobilityStack.back (); + Ptr hierarchical = + CreateObjectWith ("child", model, + "parent", parent); + object->AddInterface (hierarchical); + } } Vector position = m_position->GetNext (); model->SetPosition (position); diff --git a/src/mobility/mobility-helper.h b/src/mobility/mobility-helper.h index 627285039..8a8d22f35 100644 --- a/src/mobility/mobility-helper.h +++ b/src/mobility/mobility-helper.h @@ -9,6 +9,7 @@ namespace ns3 { class PositionAllocator; +class MobilityModel; class MobilityHelper { @@ -42,6 +43,9 @@ public: std::string n8 = "", PValue v8 = PValue (), std::string n9 = "", PValue v9 = PValue ()); + void PushReferenceMobilityModel (Ptr reference); + void PopReferenceMobilityModel (void); + std::string GetMobilityModelType (void) const; template @@ -49,6 +53,7 @@ public: private: void Layout (const std::vector > &objects); + std::vector > m_mobilityStack; bool m_notifierEnabled; ObjectFactory m_mobility; Ptr m_position; From 1a801fa3bb1b5ba1876dec663805ff7f01e3bcaf Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sat, 9 Feb 2008 02:28:21 +0100 Subject: [PATCH 033/130] report more get/set errors --- src/core/param-spec-helper.h | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index d3290bf54..527743026 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -35,8 +35,8 @@ public: virtual PValue CreateValue (void) const; private: - virtual void DoSet (T *object, const U *v) const = 0; - virtual void DoGet (const T *object, U *v) const = 0; + virtual bool DoSet (T *object, const U *v) const = 0; + virtual bool DoGet (const T *object, U *v) const = 0; PValue m_initialValue; CHECKER m_checker; }; @@ -62,11 +62,13 @@ MakeMemberVariableParamSpecWithChecker (U T::*memberVariable, V initialValue, CH m_memberVariable (memberVariable) {} private: - virtual void DoSet (T *object, const V *v) const { + virtual bool DoSet (T *object, const V *v) const { (object->*m_memberVariable) = v->Get (); + return true; } - virtual void DoGet (const T *object, V *v) const { + virtual bool DoGet (const T *object, V *v) const { v->Set (object->*m_memberVariable); + return true; } U T::*m_memberVariable; @@ -99,11 +101,13 @@ MakeMemberMethodParamSpecWithChecker (void (T::*setter) (U), m_getter (getter) {} private: - virtual void DoSet (T *object, const W *v) const { + virtual bool DoSet (T *object, const W *v) const { (object->*m_setter) (v->Get ()); + return true; } - virtual void DoGet (const T *object, W *v) const { + virtual bool DoGet (const T *object, W *v) const { v->Set ((object->*m_getter) ()); + return true; } void (T::*m_setter) (U); V (T::*m_getter) (void) const; @@ -137,9 +141,12 @@ MakeMemberMethodGetterParamSpecWithChecker (U (T::*getter) (void) const, m_getter (getter) {} private: - virtual void DoSet (T *object, const V *v) const {} - virtual void DoGet (const T *object, V *v) const { + virtual bool DoSet (T *object, const V *v) const { + return false; + } + virtual bool DoGet (const T *object, V *v) const { v->Set ((object->*m_getter) ()); + return true; } U (T::*m_getter) (void) const; }; @@ -171,11 +178,12 @@ MakeMemberMethodSetterParamSpecWithChecker (void (T::*setter) (U), m_setter (setter) {} private: - virtual void DoSet (T *object, const V *v) const { + virtual bool DoSet (T *object, const V *v) const { (object->*m_setter) (v->Get ()); + return true; } virtual void DoGet (const T *object, V *v) const { - v->Set ((object->*m_setter) ()); + return false; } void (T::*m_setter) (U); }; @@ -212,8 +220,7 @@ ParamSpecHelper::Set (ObjectBase * object, PValue val) const { return false; } - DoSet (obj, value); - return true; + return DoSet (obj, value); } template bool @@ -229,8 +236,7 @@ ParamSpecHelper::Get (const ObjectBase * object, PValue val) const { return false; } - DoGet (obj, value); - return true; + return DoGet (obj, value); } template bool From c9ea3636e2b2cc7ddd2bca646081a1940acdecb7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sat, 9 Feb 2008 02:35:38 +0100 Subject: [PATCH 034/130] kill non-existant method --- src/core/object.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/core/object.h b/src/core/object.h index 5efc24093..79fadb993 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -140,13 +140,6 @@ public: * index is \i i. */ std::string GetParameterFullName (uint32_t i) const; - /** - * \param i index into parameter array - * \param value the value to validate - * \returns true if the input value is a valid value - * associated to the parameter whose index is \i i. - */ - bool CheckParameterValue (uint32_t i, std::string value) const; Ptr CreateObject (const Parameters ¶meters) const; From c1526b932025ab30940e5456781c4d59c0057d0a Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 11 Feb 2008 04:26:09 +0100 Subject: [PATCH 035/130] add ObjectVector value support. --- src/core/object-vector.cc | 115 +++++++++++++++++++++++++++++++ src/core/object-vector.h | 141 ++++++++++++++++++++++++++++++++++++++ src/core/value-test.cc | 69 ++++++++++++++++--- src/core/wscript | 1 + 4 files changed, 317 insertions(+), 9 deletions(-) create mode 100644 src/core/object-vector.cc create mode 100644 src/core/object-vector.h diff --git a/src/core/object-vector.cc b/src/core/object-vector.cc new file mode 100644 index 000000000..4dcebb3ce --- /dev/null +++ b/src/core/object-vector.cc @@ -0,0 +1,115 @@ +#include "object-vector.h" + +namespace ns3 { + +ObjectVector::ObjectVector () +{} + +ObjectVector::Iterator +ObjectVector::Begin (void) const +{ + return m_objects.begin (); +} +ObjectVector::Iterator +ObjectVector::End (void) const +{ + return m_objects.end (); +} +uint32_t +ObjectVector::GetN (void) const +{ + return m_objects.size (); +} +Ptr +ObjectVector::Get (uint32_t i) const +{ + return m_objects[i]; +} + +ObjectVector::ObjectVector (PValue value) +{ + const ObjectVectorValue *v = value.DynCast (); + if (v == 0) + { + NS_FATAL_ERROR ("Expected value of type ObjectVectorValue."); + } + *this = v->Get (); +} + +ObjectVectorValue::ObjectVectorValue () + : m_vector () +{} + +ObjectVectorValue::ObjectVectorValue (const ObjectVector &vector) + : m_vector (vector) +{} + +ObjectVector +ObjectVectorValue::Get (void) const +{ + return m_vector; +} + +PValue +ObjectVectorValue::Copy (void) const +{ + return PValue::Create (*this); +} +std::string +ObjectVectorValue::SerializeToString (Ptr spec) const +{ + return ""; +} +bool +ObjectVectorValue::DeserializeFromString (std::string value, Ptr spec) +{ + return true; +} + +bool +ObjectVectorParamSpec::Set (ObjectBase * object, PValue value) const +{ + // not allowed. + return false; +} +bool +ObjectVectorParamSpec::Get (const ObjectBase * object, PValue value) const +{ + ObjectVectorValue *v = value.DynCast (); + if (v == 0) + { + return false; + } + v->m_vector.m_objects.clear (); + uint32_t n; + bool ok = DoGetN (object, &n); + if (!ok) + { + return false; + } + for (uint32_t i = 0; i < n; i++) + { + Ptr o = DoGet (object, i); + v->m_vector.m_objects.push_back (o); + } + return true; +} +bool +ObjectVectorParamSpec::Check (PValue value) const +{ + const ObjectVectorValue *v = value.DynCast (); + return v != 0; +} +PValue +ObjectVectorParamSpec::GetInitialValue (void) const +{ + return PValue::Create (); +} +PValue +ObjectVectorParamSpec::CreateValue (void) const +{ + return PValue::Create (); +} + + +} // name diff --git a/src/core/object-vector.h b/src/core/object-vector.h new file mode 100644 index 000000000..dbd64733c --- /dev/null +++ b/src/core/object-vector.h @@ -0,0 +1,141 @@ +#ifndef OBJECT_VECTOR_H +#define OBJECT_VECTOR_H + +#include +#include "object.h" +#include "ptr.h" +#include "value.h" + +namespace ns3 { + +class ObjectVector +{ +public: + typedef std::vector >::const_iterator Iterator; + + ObjectVector (); + + Iterator Begin (void) const; + Iterator End (void) const; + uint32_t GetN (void) const; + Ptr Get (uint32_t i) const; + + ObjectVector (PValue value); +private: + friend class ObjectVectorParamSpec; + std::vector > m_objects; +}; + +template +Ptr +MakeObjectVectorParamSpec (U T::*memberVector); + +template +Ptr +MakeObjectVectorParamSpec (Ptr (T::*get) (INDEX) const, + INDEX (T::*getN) (void) const); + +template +Ptr +MakeObjectVectorParamSpec (INDEX (T::*getN) (void) const, + Ptr (T::*get) (INDEX) const); + + +} // namespace ns3 + +namespace ns3 { + +class ObjectVectorValue : public Value +{ +public: + ObjectVectorValue (); + ObjectVectorValue (const ObjectVector &vector); + + ObjectVector Get (void) const; + + virtual PValue Copy (void) const; + virtual std::string SerializeToString (Ptr spec) const; + virtual bool DeserializeFromString (std::string value, Ptr spec); + +private: + friend class ObjectVectorParamSpec; + ObjectVector m_vector; +}; + +class ObjectVectorParamSpec : public ParamSpec +{ +public: + virtual bool Set (ObjectBase * object, PValue value) const; + virtual bool Get (const ObjectBase * object, PValue value) const; + virtual bool Check (PValue value) const; + virtual PValue GetInitialValue (void) const; + virtual PValue CreateValue (void) const; +private: + virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const = 0; + virtual Ptr DoGet (const ObjectBase *object, uint32_t i) const = 0; +}; + +template +Ptr +MakeObjectVectorParamSpec (U T::*memberVector) +{ + struct MemberStdContainer : public ObjectVectorParamSpec + { + virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const { + const T *obj = dynamic_cast (object); + if (obj == 0) + { + return false; + } + *n = (obj->*m_memberVector).size (); + return true; + } + virtual Ptr DoGet (const ObjectBase *object, uint32_t i) const { + const T *obj = static_cast (object); + return (obj->*m_memberVector)[i]; + } + U T::*m_memberVector; + } *spec = new MemberStdContainer (); + spec->m_memberVector = memberVector; + return Ptr (spec, false); +} + +template +Ptr +MakeObjectVectorParamSpec (Ptr (T::*get) (INDEX) const, + INDEX (T::*getN) (void) const) +{ + struct MemberGetters : public ObjectVectorParamSpec + { + virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const { + const T *obj = dynamic_cast (object); + if (obj == 0) + { + return false; + } + *n = (obj->*m_getN) (); + return true; + } + virtual Ptr DoGet (const ObjectBase *object, uint32_t i) const { + const T *obj = static_cast (object); + return (obj->*m_get) (i); + } + Ptr (T::*m_get) (INDEX) const; + INDEX (T::*m_getN) (void) const; + } *spec = new MemberGetters (); + spec->m_get = get; + spec->m_getN = getN; + return Ptr (spec, false); +} + +template +Ptr +MakeObjectVectorParamSpec (INDEX (T::*getN) (void) const, + Ptr (T::*get) (INDEX) const) +{ + return MakeObjectVectorParamSpec (get, getN); +} + +} // namespace ns3 + +#endif /* OBJECT_VECTOR_H */ diff --git a/src/core/value-test.cc b/src/core/value-test.cc index 83319abf8..aab20b899 100644 --- a/src/core/value-test.cc +++ b/src/core/value-test.cc @@ -7,6 +7,8 @@ #include "enum-value.h" #include "random-variable.h" #include "fp-value.h" +#include "object-vector.h" + namespace ns3 { class ParamSpecTest : public Test @@ -70,11 +72,23 @@ public: ConstantVariable (1.0))) .AddParameter ("TestFloat", "help text", MakeFpParamSpec (&ParamSpecObjectTest::m_float, -1.1)) + .AddParameter ("TestVector1", "help text", + MakeObjectVectorParamSpec (&ParamSpecObjectTest::m_vector1)) + .AddParameter ("TestVector2", "help text", + MakeObjectVectorParamSpec (&ParamSpecObjectTest::DoGetVectorN, + &ParamSpecObjectTest::DoGetVector)) ; return tid; } + void AddToVector1 (void) { + m_vector1.push_back (CreateObject ()); + } + void AddToVector2 (void) { + m_vector2.push_back (CreateObject ()); + } + private: void DoSetTestB (bool v) { m_boolTestA = v; @@ -88,6 +102,12 @@ private: void DoSetInt16 (int16_t v) { m_int16SetGet = v; } + uint32_t DoGetVectorN (void) const { + return m_vector2.size (); + } + Ptr DoGetVector (uint32_t i) const { + return m_vector2[i]; + } bool m_boolTestA; bool m_boolTest; Ptr m_derived; @@ -98,6 +118,8 @@ private: float m_float; enum TestEnum m_enum; RandomVariable m_random; + std::vector > m_vector1; + std::vector > m_vector2; }; @@ -119,7 +141,6 @@ private: NS_TEST_ASSERT_EQUAL (got.Get (), expected.Get ()); \ } - NS_OBJECT_ENSURE_REGISTERED (ParamSpecObjectTest); ParamSpecTest::ParamSpecTest () @@ -269,21 +290,51 @@ ParamSpecTest::RunTests (void) RandomVariable ran = p->Get ("TestRandom"); NS_TEST_ASSERT (p->Set ("TestRandom", UniformVariable (0.0, 1.0))); NS_TEST_ASSERT (p->Set ("TestRandom", ConstantVariable (10.0))); + + { + ObjectVector vector = p->Get ("TestVector1"); + NS_TEST_ASSERT_EQUAL (vector.GetN (), 0); + p->AddToVector1 (); + NS_TEST_ASSERT_EQUAL (vector.GetN (), 0); + vector = p->Get ("TestVector1"); + NS_TEST_ASSERT_EQUAL (vector.GetN (), 1); + Ptr a = vector.Get (0); + NS_TEST_ASSERT_UNEQUAL (a, 0); + p->AddToVector1 (); + NS_TEST_ASSERT_EQUAL (vector.GetN (), 1); + vector = p->Get ("TestVector1"); + NS_TEST_ASSERT_EQUAL (vector.GetN (), 2); + } + + { + ObjectVector vector = p->Get ("TestVector2"); + NS_TEST_ASSERT_EQUAL (vector.GetN (), 0); + p->AddToVector2 (); + NS_TEST_ASSERT_EQUAL (vector.GetN (), 0); + vector = p->Get ("TestVector2"); + NS_TEST_ASSERT_EQUAL (vector.GetN (), 1); + Ptr a = vector.Get (0); + NS_TEST_ASSERT_UNEQUAL (a, 0); + p->AddToVector2 (); + NS_TEST_ASSERT_EQUAL (vector.GetN (), 1); + vector = p->Get ("TestVector2"); + NS_TEST_ASSERT_EQUAL (vector.GetN (), 2); + } #if 0 - p->Set ("TestBoolName", "true"); - NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "true"); - p->Set ("TestBoolName", "false"); - NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "false"); + p->Set ("TestBool"TestVector2"", "true"); + NS_TEST_ASSERT_EQUAL (p->Get ("TestBool"), "true"); + p->Set ("TestBool", "false"); + NS_TEST_ASSERT_EQUAL (p->Get ("TestBool"), "false"); - Parameters::GetGlobal ()->Set ("TestBoolName", "true"); + Parameters::GetGlobal ()->Set ("TestBool", "true"); p = CreateObjectWith (); - NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "true"); + NS_TEST_ASSERT_EQUAL (p->Get ("TestBool"), "true"); - Parameters::GetGlobal ()->Set ("TestBoolName", "false"); + Parameters::GetGlobal ()->Set ("TestBool", "false"); p = CreateObjectWith (); - NS_TEST_ASSERT_EQUAL (p->Get ("TestBoolName"), "false"); + NS_TEST_ASSERT_EQUAL (p->Get ("TestBool"), "false"); #endif return result; } diff --git a/src/core/wscript b/src/core/wscript index d748a4fe5..e5d71fb41 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -61,6 +61,7 @@ def build(bld): 'enum-value.cc', 'fp-value.cc', 'object-factory.cc', + 'object-vector.cc', ] if sys.platform == 'win32': From cd74dc9461635beb42f94dd745fb28dba61af018 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 11 Feb 2008 05:11:45 +0100 Subject: [PATCH 036/130] add big warning --- src/core/value.cc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/core/value.cc b/src/core/value.cc index 7248b3022..478a29b99 100644 --- a/src/core/value.cc +++ b/src/core/value.cc @@ -26,6 +26,27 @@ Value::ConvertFrom (PValue value, Ptr spec) return false; } +/*************************************************************** + * Big interesting warning. + * ------------------------ + * + * One might wonder why we re-implement a smart pointer below + * in the PValue class. This is a very good question and the answer + * is unfortunately pretty complicated. + * + * 1) We could have requested the user to use Ptr and save us + * a lot of pain. This, however, does not work because our smart + * pointer needs a special constructor which can be used to convert + * objects of type Ptr into a PtrValue to hold the pointer. + * + * 2) We could have made the m_value member variable below a Ptr + * rather than store a raw pointer. This, however, does not work + * because this would mean that the constructor PValue (Value *) + * should be morphed into PValue (Ptr) which, unfortunately, + * would conflict with the template constructor PValue (Ptr)... + * + * This is definitely not fun. + */ PValue::PValue () : m_value (0) {} From ee7111ff5e98c4c57fd8abe1b206569e082c7051 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 11 Feb 2008 05:11:55 +0100 Subject: [PATCH 037/130] add XXX markers. --- src/core/object-vector.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/object-vector.cc b/src/core/object-vector.cc index 4dcebb3ce..3cef5c8bb 100644 --- a/src/core/object-vector.cc +++ b/src/core/object-vector.cc @@ -58,11 +58,13 @@ ObjectVectorValue::Copy (void) const std::string ObjectVectorValue::SerializeToString (Ptr spec) const { + // XXX return ""; } bool ObjectVectorValue::DeserializeFromString (std::string value, Ptr spec) { + // XXX ?? Can we implement this correctly ?? I doubt it very much. return true; } From 0537be3a4798fed99a0661cd189413f596459ed3 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 13 Feb 2008 21:00:33 +0100 Subject: [PATCH 038/130] rework param-spec-helper code to avoid having to deal with pointers to member functions et al. --- src/core/boolean-value.h | 43 ++----- src/core/fp-value.h | 76 ++++++++---- src/core/int-value.h | 74 ++++++------ src/core/param-spec-helper.h | 227 +++++++++++++++++++++-------------- src/core/random-variable.h | 22 +++- src/core/uint-value.h | 111 ++++++++--------- src/mobility/rectangle.h | 23 ++-- src/mobility/vector.h | 39 ++---- src/simulator/nstime.h | 21 +++- 9 files changed, 355 insertions(+), 281 deletions(-) diff --git a/src/core/boolean-value.h b/src/core/boolean-value.h index a8473e7cb..37cd75b9a 100644 --- a/src/core/boolean-value.h +++ b/src/core/boolean-value.h @@ -25,20 +25,11 @@ private: }; -template -Ptr MakeBooleanParamSpec (bool T::*v, bool initialValue); +template +Ptr MakeBooleanParamSpec (T1 a1, bool initialValue); -template -Ptr MakeBooleanParamSpec (bool (T::*getter) (void) const, - bool initialValue); - -template -Ptr MakeBooleanParamSpec (bool (T::*getter) (void) const, - void (T::*setter) (bool), - bool initialValue); -template -Ptr MakeBooleanParamSpec (void (T::*setter) (bool), - bool initialValue); +template +Ptr MakeBooleanParamSpec (T1 a1, T2 a2, bool initialValue); } // namespace ns3 @@ -46,33 +37,19 @@ Ptr MakeBooleanParamSpec (void (T::*setter) (bool), // Implementation of template functions below. namespace ns3 { -template -Ptr MakeBooleanParamSpec (bool T::*v, bool initialValue) +template +Ptr MakeBooleanParamSpec (T1 a1, bool initialValue) { - return MakeMemberVariableParamSpec (v, BooleanValue (initialValue)); + return MakeParamSpecHelper (a1, BooleanValue (initialValue)); } -template -Ptr MakeBooleanParamSpec (void (T::*setter) (bool), - bool (T::*getter) (void) const, - bool initialValue) +template +Ptr MakeBooleanParamSpec (T1 a1, T2 a2, bool initialValue) { - return MakeMemberMethodParamSpec (setter, getter, BooleanValue (initialValue)); + return MakeParamSpecHelper (a1, a2, BooleanValue (initialValue)); } -template -Ptr MakeBooleanParamSpec (bool (T::*getter) (void) const, - bool initialValue) -{ - return MakeMemberMethodGetterParamSpec (getter, BooleanValue (initialValue)); -} -template -Ptr MakeBooleanParamSpec (void (T::*setter) (bool), - bool initialValue) -{ - return MakeMemberMethodSetterParamSpec (setter, BooleanValue (initialValue)); -} } // namespace ns3 diff --git a/src/core/fp-value.h b/src/core/fp-value.h index f4d7da3ce..eb0f6bc73 100644 --- a/src/core/fp-value.h +++ b/src/core/fp-value.h @@ -26,11 +26,11 @@ private: }; -template -Ptr MakeFpParamSpec (U T::*memberVariable, +template +Ptr MakeFpParamSpec (T1 a1, double initialValue); -template -Ptr MakeFpParamSpec (U T::*memberVariable, +template +Ptr MakeFpParamSpec (T1 a1, T2 a2, double initialValue, double minValue, double maxValue); @@ -42,37 +42,65 @@ namespace ns3 { class FpValueChecker { public: - FpValueChecker (double min, double max); - bool Check (double v) const; + FpValueChecker (double minValue, double maxValue); + bool Check (double value) const; + + template + static FpValueChecker Create (U T::*) { + return FpValueChecker (-std::numeric_limits::max (), + std::numeric_limits::max ()); + } + template + static FpValueChecker Create (U (T::*) (void) const) { + return FpValueChecker (-std::numeric_limits::max (), + std::numeric_limits::max ()); + } + template + static FpValueChecker Create (void (T::*) (U)) { + return FpValueChecker (-std::numeric_limits::max (), + std::numeric_limits::max ()); + } private: double m_min; double m_max; }; -template -Ptr -MakeFpParamSpec (U T::*memberVariable, - double initialValue) +template +Ptr +MakeFpParamSpec (T1 a1, + double initialValue) { - double minValue = -std::numeric_limits::max (); - double maxValue = std::numeric_limits::max (); - return MakeMemberVariableParamSpecWithChecker (memberVariable, - FpValue (initialValue), - FpValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, FpValue (initialValue), + FpValueChecker::Create (a1)); } -template -Ptr -MakeFpParamSpec (U T::*memberVariable, - double initialValue, - double minValue, - double maxValue) +template +Ptr MakeFpParamSpec (T1 a1, + double initialValue, + double minValue, + double maxValue) { - return MakeMemberVariableParamSpecWithChecker (memberVariable, - FpValue (initialValue), - FpValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, FpValue (initialValue), + FpValueChecker (minValue, maxValue)); +} +template +Ptr MakeFpParamSpec (T1 a1, T2 a2, + double initialValue) +{ + return MakeParamSpecHelperWithChecker (a1, a2, FpValue (initialValue), + FpValueChecker::Create (a1)); +} +template +Ptr MakeFpParamSpec (T1 a1, T2 a2, + double initialValue, + double minValue, + double maxValue) +{ + return MakeParamSpecHelperWithChecker (a1, a2, FpValue (initialValue), + FpValueChecker (minValue, maxValue)); } } // namespace ns3 + #endif /* FP_VALUE_H */ diff --git a/src/core/int-value.h b/src/core/int-value.h index eaf98768d..3bbf73db4 100644 --- a/src/core/int-value.h +++ b/src/core/int-value.h @@ -24,21 +24,19 @@ private: int64_t m_value; }; -template -Ptr MakeIntParamSpec (U T::*memberVariable, +template +Ptr MakeIntParamSpec (T1 a1, int64_t initialValue); -template -Ptr MakeIntParamSpec (U T::*memberVariable, +template +Ptr MakeIntParamSpec (T1 a1, int64_t initialValue, int64_t minValue, int64_t maxValue); -template -Ptr MakeIntParamSpec (void (T::*setter) (U), - U (T::*getter) (void) const, +template +Ptr MakeIntParamSpec (T1 a1, T2 a2, int64_t initialValue); -template -Ptr MakeIntParamSpec (void (T::*setter) (U), - U (T::*getter) (void) const, +template +Ptr MakeIntParamSpec (T1 a1, T2 a2, int64_t initialValue, int64_t minValue, int64_t maxValue); @@ -52,54 +50,62 @@ class IntValueChecker public: IntValueChecker (int64_t minValue, int64_t maxValue); bool Check (const int64_t &value) const; + + template + static IntValueChecker Create (U T::*) { + return IntValueChecker (std::numeric_limits::min (), + std::numeric_limits::max ()); + } + template + static IntValueChecker Create (U (T::*) (void) const) { + return IntValueChecker (std::numeric_limits::min (), + std::numeric_limits::max ()); + } + template + static IntValueChecker Create (void (T::*) (U)) { + return IntValueChecker (std::numeric_limits::min (), + std::numeric_limits::max ()); + } private: int64_t m_minValue; int64_t m_maxValue; }; - -template +template Ptr -MakeIntParamSpec (U T::*memberVariable, +MakeIntParamSpec (T1 a1, int64_t initialValue) { - int64_t minValue = std::numeric_limits::min (); - int64_t maxValue = std::numeric_limits::max (); - return MakeMemberVariableParamSpecWithChecker (memberVariable, IntValue (initialValue), - IntValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, IntValue (initialValue), + IntValueChecker::Create (a1)); } -template -Ptr MakeIntParamSpec (U T::*memberVariable, +template +Ptr MakeIntParamSpec (T1 a1, int64_t initialValue, int64_t minValue, int64_t maxValue) { - return MakeMemberVariableParamSpecWithChecker (memberVariable, IntValue (initialValue), - IntValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, IntValue (initialValue), + IntValueChecker (minValue, maxValue)); } -template -Ptr MakeIntParamSpec (void (T::*setter) (U), - U (T::*getter) (void) const, +template +Ptr MakeIntParamSpec (T1 a1, T2 a2, int64_t initialValue) { - int64_t minValue = std::numeric_limits::min (); - int64_t maxValue = std::numeric_limits::max (); - return MakeMemberMethodParamSpecWithChecker (setter, getter, IntValue (initialValue), - IntValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, a2, IntValue (initialValue), + IntValueChecker::Create (a1)); } -template -Ptr MakeIntParamSpec (void (T::*setter) (U), - U (T::*getter) (void) const, +template +Ptr MakeIntParamSpec (T1 a1, T2 a2, int64_t initialValue, int64_t minValue, int64_t maxValue) { - return MakeMemberMethodParamSpecWithChecker (setter, getter, IntValue (initialValue), - IntValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, a2, IntValue (initialValue), + IntValueChecker (minValue, maxValue)); } - } // namespace ns3 #endif /* INT_VALUE_H */ diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index 527743026..e2d3ffb1c 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -3,24 +3,30 @@ namespace ns3 { -template +template Ptr -MakeMemberVariableParamSpec (U T::*memberVariable, V initialValue); +MakeParamSpecHelper (T1 a1, V initialValue); -template +template Ptr -MakeMemberMethodParamSpec (void (T::*setter) (U), - V (T::*getter) (void) const, - W initialValue); +MakeParamSpecHelper (T1 a1, T2 a2, V initialValue); +template +Ptr +MakeParamSpecHelperWithChecker (T1 a1, V initialValue, CHECKER checker); + +template +Ptr +MakeParamSpecHelperWithChecker (T1 a1, T2 a2, V initialValue, CHECKER checker); } // namespace ns3 - /*************************************************************** - * The implementation of the above class functions + * The implementation of the above functions. ***************************************************************/ +#include "type-traits.h" + namespace ns3 { template @@ -48,11 +54,9 @@ public: bool Check (const T &value) const {return true;} }; - - template Ptr -MakeMemberVariableParamSpecWithChecker (U T::*memberVariable, V initialValue, CHECKER checker) +DoMakeParamSpecHelperOne (U T::*memberVariable, V initialValue, CHECKER checker) { class MemberVariable : public ParamSpecHelper { @@ -75,20 +79,92 @@ MakeMemberVariableParamSpecWithChecker (U T::*memberVariable, V initialValue, CH }; return Ptr (new MemberVariable (memberVariable, initialValue, checker), false); } +template +Ptr +DoMakeParamSpecHelperOne (U T::*memberVariable, V initialValue) +{ + return DoMakeParamSpecHelperOne (memberVariable, initialValue, ParamSpecHelperSimpleChecker ()); +} + + +template +Ptr +DoMakeParamSpecHelperOne (U (T::*getter) (void) const, + V initialValue, CHECKER checker) +{ + class MemberMethod : public ParamSpecHelper + { + public: + MemberMethod (U (T::*getter) (void) const, + V initialValue, CHECKER checker) + : ParamSpecHelper (initialValue, checker), + m_getter (getter) + {} + private: + virtual bool DoSet (T *object, const V *v) const { + return false; + } + virtual bool DoGet (const T *object, V *v) const { + v->Set ((object->*m_getter) ()); + return true; + } + U (T::*m_getter) (void) const; + }; + return Ptr (new MemberMethod (getter, initialValue, checker), false); +} template Ptr -MakeMemberVariableParamSpec (U T::*memberVariable, V initialValue) +DoMakeParamSpecHelperOne (U (T::*getter) (void) const, + V initialValue) { - return MakeMemberVariableParamSpecWithChecker (memberVariable, initialValue, - ParamSpecHelperSimpleChecker ()); + return DoMakeParamSpecHelperOne (getter, initialValue, ParamSpecHelperSimpleChecker ()); } + + +template +Ptr +DoMakeParamSpecHelperOne (void (T::*setter) (U), + V initialValue, CHECKER checker) +{ + class MemberMethod : public ParamSpecHelper + { + public: + MemberMethod (void (T::*setter) (U), + V initialValue, CHECKER checker) + : ParamSpecHelper (initialValue, checker), + m_setter (setter) + {} + private: + virtual bool DoSet (T *object, const V *v) const { + (object->*m_setter) (v->Get ()); + return true; + } + virtual void DoGet (const T *object, V *v) const { + return false; + } + void (T::*m_setter) (U); + }; + return Ptr (new MemberMethod (setter, initialValue, checker), false); +} + +template +Ptr +DoMakeParamSpecHelperOne (void (T::*setter) (U), + V initialValue) +{ + return DoMakeParamSpecHelperOne (setter, initialValue, + ParamSpecHelperSimpleChecker::ReferencedType> ()); +} + + template Ptr -MakeMemberMethodParamSpecWithChecker (void (T::*setter) (U), - V (T::*getter) (void) const, - W initialValue, CHECKER checker) +DoMakeParamSpecHelperTwo (void (T::*setter) (U), + V (T::*getter) (void) const, + W initialValue, + CHECKER checker = ParamSpecHelperSimpleChecker ()) { class MemberMethod : public ParamSpecHelper { @@ -115,89 +191,32 @@ MakeMemberMethodParamSpecWithChecker (void (T::*setter) (U), return Ptr (new MemberMethod (setter, getter, initialValue, checker), false); } - template Ptr -MakeMemberMethodParamSpec (void (T::*setter) (U), - V (T::*getter) (void) const, - W initialValue) +DoMakeParamSpecHelperTwo (void (T::*setter) (U), + V (T::*getter) (void) const, + W initialValue) { - return MakeMemberMethodParamSpecWithChecker (setter, getter, initialValue, - ParamSpecHelperSimpleChecker ()); + return DoMakeParamSpecHelperTwo (setter, getter, initialValue, ParamSpecHelperSimpleChecker ()); } - -template +template Ptr -MakeMemberMethodGetterParamSpecWithChecker (U (T::*getter) (void) const, - V initialValue, CHECKER checker) +DoMakeParamSpecHelperTwo (V (T::*getter) (void) const, + void (T::*setter) (U), + W initialValue, + CHECKER checker) { - class MemberMethod : public ParamSpecHelper - { - public: - MemberMethod (U (T::*getter) (void) const, - V initialValue, CHECKER checker) - : ParamSpecHelper (initialValue, checker), - m_getter (getter) - {} - private: - virtual bool DoSet (T *object, const V *v) const { - return false; - } - virtual bool DoGet (const T *object, V *v) const { - v->Set ((object->*m_getter) ()); - return true; - } - U (T::*m_getter) (void) const; - }; - return Ptr (new MemberMethod (getter, initialValue, checker), false); + return DoMakeParamSpecHelperTwo (setter, getter, initialValue, checker); } - -template +template Ptr -MakeMemberMethodGetterParamSpec (U (T::*getter) (void) const, - V initialValue) +DoMakeParamSpecHelperTwo (V (T::*getter) (void) const, + void (T::*setter) (U), + W initialValue) { - return MakeMemberMethodGetterParamSpecWithChecker (getter, initialValue, - ParamSpecHelperSimpleChecker ()); -} - - -template -Ptr -MakeMemberMethodSetterParamSpecWithChecker (void (T::*setter) (U), - V initialValue, CHECKER checker) -{ - class MemberMethod : public ParamSpecHelper - { - public: - MemberMethod (void (T::*setter) (U), - V initialValue, CHECKER checker) - : ParamSpecHelper (initialValue, checker), - m_setter (setter) - {} - private: - virtual bool DoSet (T *object, const V *v) const { - (object->*m_setter) (v->Get ()); - return true; - } - virtual void DoGet (const T *object, V *v) const { - return false; - } - void (T::*m_setter) (U); - }; - return Ptr (new MemberMethod (setter, initialValue, checker), false); -} - - -template -Ptr -MakeMemberMethodParamSpec (void (T::*setter) (U), - V initialValue) -{ - return MakeMemberMethodSetterParamSpecWithChecker (setter, initialValue, - ParamSpecHelperSimpleChecker ()); + return DoMakeParamSpecHelperTwo (setter, getter, initialValue); } @@ -263,6 +282,36 @@ ParamSpecHelper::CreateValue (void) const return m_initialValue.Copy (); } + + +template +Ptr +MakeParamSpecHelper (T1 a1, V initialValue) +{ + return DoMakeParamSpecHelperOne (a1, initialValue); +} + +template +Ptr +MakeParamSpecHelper (T1 a1, T2 a2, V initialValue) +{ + return DoMakeParamSpecHelperTwo (a1, a2, initialValue); +} + +template +Ptr +MakeParamSpecHelperWithChecker (T1 a1, V initialValue, CHECKER checker) +{ + return DoMakeParamSpecHelperOne (a1, initialValue, checker); +} + +template +Ptr +MakeParamSpecHelperWithChecker (T1 a1, T2 a2, V initialValue, CHECKER checker) +{ + return DoMakeParamSpecHelperTwo (a1, a2, initialValue, checker); +} + } // namespace ns3 #endif /* PARAM_SPEC_HELPER_H */ diff --git a/src/core/random-variable.h b/src/core/random-variable.h index 370ff9e79..b7ea9d14d 100644 --- a/src/core/random-variable.h +++ b/src/core/random-variable.h @@ -673,21 +673,33 @@ private: RandomVariable m_variable; }; -template -Ptr MakeRandomVariableParamSpec (RandomVariable T::*m_memberVariable, +template +Ptr MakeRandomVariableParamSpec (T1 a1, RandomVariable initialValue); +template +Ptr MakeRandomVariableParamSpec (T1 a1, T2 a2, + RandomVariable initialValue); + }//namespace ns3 namespace ns3 { -template -Ptr MakeRandomVariableParamSpec (RandomVariable T::*memberVariable, +template +Ptr MakeRandomVariableParamSpec (T1 a1, RandomVariable initialValue) { - return MakeMemberVariableParamSpec (memberVariable, RandomVariableValue (initialValue)); + return MakeParamSpecHelper (a1, RandomVariableValue (initialValue)); } +template +Ptr MakeRandomVariableParamSpec (T1 a1, T2 a2, + RandomVariable initialValue) +{ + return MakeParamSpecHelper (a1, a2, RandomVariableValue (initialValue)); +} + + } // namespace ns3 #endif diff --git a/src/core/uint-value.h b/src/core/uint-value.h index f53177c7f..1a85e9c33 100644 --- a/src/core/uint-value.h +++ b/src/core/uint-value.h @@ -24,24 +24,22 @@ 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); +template +Ptr MakeUintParamSpec (T1 a1, + uint64_t initialValue); +template +Ptr MakeUintParamSpec (T1 a1, + uint64_t initialValue, + uint64_t minValue, + uint64_t maxValue); +template +Ptr MakeUintParamSpec (T1 a1, T2 a2, + uint64_t initialValue); +template +Ptr MakeUintParamSpec (T1 a1, T2 a2, + uint64_t initialValue, + uint64_t minValue, + uint64_t maxValue); } // namespace ns3 @@ -52,57 +50,62 @@ class UintValueChecker public: UintValueChecker (uint64_t minValue, uint64_t maxValue); bool Check (const uint64_t &value) const; + + template + static UintValueChecker Create (U T::*) { + return UintValueChecker (std::numeric_limits::min (), + std::numeric_limits::max ()); + } + template + static UintValueChecker Create (U (T::*) (void) const) { + return UintValueChecker (std::numeric_limits::min (), + std::numeric_limits::max ()); + } + template + static UintValueChecker Create (void (T::*) (U)) { + return UintValueChecker (std::numeric_limits::min (), + std::numeric_limits::max ()); + } private: uint64_t m_minValue; uint64_t m_maxValue; }; -template -Ptr -MakeUintParamSpec (U T::*memberVariable, - uint64_t initialValue) +template +Ptr +MakeUintParamSpec (T1 a1, + 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)); + return MakeParamSpecHelperWithChecker (a1, UintValue (initialValue), + UintValueChecker::Create (a1)); } -template -Ptr -MakeUintParamSpec (U T::*memberVariable, - uint64_t initialValue, - uint64_t minValue, - uint64_t maxValue) +template +Ptr MakeUintParamSpec (T1 a1, + uint64_t initialValue, + uint64_t minValue, + uint64_t maxValue) { - return MakeMemberVariableParamSpecWithChecker (memberVariable, UintValue (initialValue), - UintValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, UintValue (initialValue), + UintValueChecker (minValue, maxValue)); } - -template -Ptr -MakeUintParamSpec (void (T::*setter) (U), - U (T::*getter) (void) const, - uint64_t initialValue) +template +Ptr MakeUintParamSpec (T1 a1, T2 a2, + 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)); + return MakeParamSpecHelperWithChecker (a1, a2, UintValue (initialValue), + UintValueChecker::Create (a1)); } -template -Ptr -MakeUintParamSpec (void (T::*setter) (U), - U (T::*getter) (void) const, - uint64_t initialValue, - uint64_t minValue, - uint64_t maxValue) +template +Ptr MakeUintParamSpec (T1 a1, T2 a2, + uint64_t initialValue, + uint64_t minValue, + uint64_t maxValue) { - return MakeMemberMethodParamSpecWithChecker (setter, getter, UintValue (initialValue), - UintValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, a2, UintValue (initialValue), + UintValueChecker (minValue, maxValue)); } - } // namespace ns3 #endif /* UINT_PARAMETER_H */ diff --git a/src/mobility/rectangle.h b/src/mobility/rectangle.h index 5cb9e578e..87ea70d6d 100644 --- a/src/mobility/rectangle.h +++ b/src/mobility/rectangle.h @@ -87,22 +87,29 @@ private: Rectangle m_rectangle; }; -template -Ptr MakeRectangleParamSpec (Rectangle T::*memberVariable, +template +Ptr MakeRectangleParamSpec (T1 a1, + Rectangle initialValue); +template +Ptr MakeRectangleParamSpec (T1 a1, T2 a2, Rectangle initialValue); } // namespace ns3 namespace ns3 { -template -Ptr -MakeRectangleParamSpec (Rectangle T::*memberVariable, - Rectangle initialValue) +template +Ptr MakeRectangleParamSpec (T1 a1, + Rectangle initialValue) { - return MakeMemberVariableParamSpec (memberVariable, RectangleValue (initialValue)); + return MakeParamSpecHelper (a1, RectangleValue (initialValue)); +} +template +Ptr MakeRectangleParamSpec (T1 a1, T2 a2, + Rectangle initialValue) +{ + return MakeParamSpecHelper (a1, a2, RectangleValue (initialValue)); } - } // namespace ns3 diff --git a/src/mobility/vector.h b/src/mobility/vector.h index af3976fad..c941d6d5d 100644 --- a/src/mobility/vector.h +++ b/src/mobility/vector.h @@ -82,53 +82,34 @@ private: Vector m_vector; }; -template +template Ptr -MakeVectorParamSpec (Vector T::*memberVariable, const Vector &initialValue); +MakeVectorParamSpec (T1 a1, const Vector &initialValue); -template +template Ptr -MakeVectorParamSpec (void (T::*setter) (const Vector &), - Vector (T::*getter) (void) const, +MakeVectorParamSpec (T1 a1, T2 a2, const Vector &initialValue); -template -Ptr -MakeVectorParamSpec (Vector (T::*getter) (void) const, - const Vector &initialValue); - - } // namespace ns3 namespace ns3 { -template +template Ptr -MakeVectorParamSpec (Vector T::*memberVariable, const Vector &initialValue) +MakeVectorParamSpec (T1 a1, const Vector &initialValue) { - return MakeMemberVariableParamSpec (memberVariable, VectorValue (initialValue)); + return MakeParamSpecHelper (a1, VectorValue (initialValue)); } -template +template Ptr -MakeVectorParamSpec (void (T::*setter) (const Vector &), - Vector (T::*getter) (void) const, +MakeVectorParamSpec (T1 a1, T2 a2, const Vector &initialValue) { - return MakeMemberMethodParamSpec (setter, getter, VectorValue (initialValue)); + return MakeParamSpecHelper (a1, a2, VectorValue (initialValue)); } -template -Ptr -MakeVectorParamSpec (Vector (T::*getter) (void) const, - const Vector &initialValue) -{ - return MakeMemberMethodGetterParamSpec (getter, VectorValue (initialValue)); -} - - - - } // namespace ns3 #endif /* VECTOR_H */ diff --git a/src/simulator/nstime.h b/src/simulator/nstime.h index 707fb1f30..ee7ab55f2 100644 --- a/src/simulator/nstime.h +++ b/src/simulator/nstime.h @@ -688,19 +688,30 @@ private: Time m_time; }; -template -Ptr MakeTimeParamSpec (Time T::*memberVariable, +template +Ptr MakeTimeParamSpec (T1 a1, Time initialValue); +template +Ptr MakeTimeParamSpec (T1 a1, T2 a2, + Time initialValue); + } // namespace ns3 namespace ns3 { -template -Ptr MakeTimeParamSpec (Time T::*memberVariable, +template +Ptr MakeTimeParamSpec (T1 a1, Time initialValue) { - return MakeMemberVariableParamSpec (memberVariable, TimeValue (initialValue)); + return MakeParamSpecHelper (a1, TimeValue (initialValue)); +} + +template +Ptr MakeTimeParamSpec (T1 a1, T2 a2, + Time initialValue) +{ + return MakeParamSpecHelper (a1, a2, TimeValue (initialValue)); } From 540bb4c68df600ba360493d4df73e2dcf4a930c6 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 13 Feb 2008 22:39:17 +0100 Subject: [PATCH 039/130] convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper --- src/core/class-value-helper.h | 96 +++++++++++++++++++++ src/core/random-variable.cc | 78 ++++++----------- src/core/random-variable.h | 28 +++---- src/core/wscript | 1 + src/mobility/rectangle.cc | 103 ++++------------------- src/mobility/rectangle.h | 27 ++---- src/mobility/vector.cc | 61 ++++---------- src/mobility/vector.h | 25 ++---- src/simulator/nstime.h | 28 ++----- src/simulator/time.cc | 152 +++++++++++++--------------------- 10 files changed, 238 insertions(+), 361 deletions(-) create mode 100644 src/core/class-value-helper.h diff --git a/src/core/class-value-helper.h b/src/core/class-value-helper.h new file mode 100644 index 000000000..89df79cf7 --- /dev/null +++ b/src/core/class-value-helper.h @@ -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 --git a/src/core/random-variable.cc b/src/core/random-variable.cc index f67f6eb29..95aa4d4f4 100644 --- a/src/core/random-variable.cc +++ b/src/core/random-variable.cc @@ -300,18 +300,13 @@ RandomVariable::Peek (void) const 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 @@ TriangularVariable::GetSingleValue(double s, double l, double mean) } -RandomVariableValue::RandomVariableValue (RandomVariable variable) - : m_variable (variable) -{} -void -RandomVariableValue::Set (RandomVariable variable) +std::ostream &operator << (std::ostream &os, const RandomVariable &var) { - m_variable = variable; -} -RandomVariable -RandomVariableValue::Get (void) const -{ - 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 --git a/src/core/random-variable.h b/src/core/random-variable.h index b7ea9d14d..3cf219b11 100644 --- a/src/core/random-variable.h +++ b/src/core/random-variable.h @@ -24,7 +24,10 @@ #include #include #include +#include +#include #include "value.h" +#include "class-value-helper.h" /** * \ingroup core @@ -166,6 +169,9 @@ public: 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 @@ public: static double GetSingleValue(double s, double l, double mean); }; +std::ostream &operator << (std::ostream &os, const RandomVariable &var); +std::istream &operator >> (std::istream &os, RandomVariable &var); -class RandomVariableValue : public Value -{ -public: - RandomVariableValue (RandomVariable variable); - void Set (RandomVariable variable); - RandomVariable Get (void) const; - 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 @@ template 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 --git a/src/core/wscript b/src/core/wscript index e5d71fb41..e5cad486e 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -113,5 +113,6 @@ def build(bld): 'fp-value.h', 'enum-value.h', 'object-factory.h', + 'class-value-helper.h', ] diff --git a/src/mobility/rectangle.cc b/src/mobility/rectangle.cc index 24d2fb8c2..7fa815661 100644 --- a/src/mobility/rectangle.cc +++ b/src/mobility/rectangle.cc @@ -121,102 +121,31 @@ 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 (); + *this = ClassValueHelperExtractFrom (value); } Rectangle::operator PValue () const { - return PValue::Create (*this); + return ClassValueHelperConvertTo (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 (*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) +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 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; } diff --git a/src/mobility/rectangle.h b/src/mobility/rectangle.h index 87ea70d6d..2f4828d1a 100644 --- a/src/mobility/rectangle.h +++ b/src/mobility/rectangle.h @@ -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 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 @@ template 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 --git a/src/mobility/vector.cc b/src/mobility/vector.cc index c69e6bbf5..c44d5ca3c 100644 --- a/src/mobility/vector.cc +++ b/src/mobility/vector.cc @@ -39,16 +39,11 @@ Vector::Vector () 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 @@ 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 (*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 --git a/src/mobility/vector.h b/src/mobility/vector.h index c941d6d5d..24dbc54f9 100644 --- a/src/mobility/vector.h +++ b/src/mobility/vector.h @@ -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 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 @@ template Ptr MakeVectorParamSpec (T1 a1, const Vector &initialValue) { - return MakeParamSpecHelper (a1, VectorValue (initialValue)); + return MakeClassValueHelperParamSpec (a1, initialValue); } template @@ -107,7 +94,7 @@ Ptr MakeVectorParamSpec (T1 a1, T2 a2, const Vector &initialValue) { - return MakeParamSpecHelper (a1, a2, VectorValue (initialValue)); + return MakeClassValueHelperParamSpec (a1, a2, initialValue); } } // namespace ns3 diff --git a/src/simulator/nstime.h b/src/simulator/nstime.h index ee7ab55f2..283e33253 100644 --- a/src/simulator/nstime.h +++ b/src/simulator/nstime.h @@ -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 @@ private: 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<-1> TimeInvert; 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 @@ template 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 --git a/src/simulator/time.cc b/src/simulator/time.cc index e95e8d46e..a98072e00 100644 --- a/src/simulator/time.cc +++ b/src/simulator/time.cc @@ -189,7 +189,7 @@ TimeUnit<1>::GetTimeStep (void) const 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 @@ operator<< (std::ostream& os, Time const& time) 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>::UnitsToTimestep (uint64_t unitValue, 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 @@ Time FemtoSeconds (uint64_t fs) 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 From c5f411ca28db7c2584f98fac8d47714df7fa8dd8 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 13 Feb 2008 23:37:46 +0100 Subject: [PATCH 040/130] introduce top-level ParamSpec for bindings. --- src/core/boolean-value.h | 6 +- src/core/class-value-helper.h | 8 +- src/core/fp-value.h | 33 +++--- src/core/int-value.h | 18 ++-- src/core/param-spec-helper.h | 198 +++++++++++++++------------------- src/core/random-variable.h | 7 +- src/core/uint-value.h | 18 ++-- src/mobility/rectangle.h | 5 +- src/mobility/vector.h | 5 +- src/simulator/nstime.h | 5 +- 10 files changed, 147 insertions(+), 156 deletions(-) diff --git a/src/core/boolean-value.h b/src/core/boolean-value.h index 37cd75b9a..d4054fa2d 100644 --- a/src/core/boolean-value.h +++ b/src/core/boolean-value.h @@ -24,6 +24,8 @@ private: bool m_value; }; +class BooleanParamSpec : public ParamSpec {}; + template Ptr MakeBooleanParamSpec (T1 a1, bool initialValue); @@ -40,13 +42,13 @@ namespace ns3 { template Ptr MakeBooleanParamSpec (T1 a1, bool initialValue) { - return MakeParamSpecHelper (a1, BooleanValue (initialValue)); + return MakeParamSpecHelper (a1, BooleanValue (initialValue)); } template Ptr MakeBooleanParamSpec (T1 a1, T2 a2, bool initialValue) { - return MakeParamSpecHelper (a1, a2, BooleanValue (initialValue)); + return MakeParamSpecHelper (a1, a2, BooleanValue (initialValue)); } diff --git a/src/core/class-value-helper.h b/src/core/class-value-helper.h index 89df79cf7..3ad76e81e 100644 --- a/src/core/class-value-helper.h +++ b/src/core/class-value-helper.h @@ -76,19 +76,19 @@ ClassValueHelperConvertTo (const T *self) } -template +template Ptr MakeClassValueHelperParamSpec (T1 a1, const T &initialValue) { - return MakeParamSpecHelper (a1, ClassValue (initialValue)); + return MakeParamSpecHelper (a1, ClassValue (initialValue)); } -template +template Ptr MakeClassValueHelperParamSpec (T1 a1, T2 a2, const T &initialValue) { - return MakeParamSpecHelper (a1, a2, ClassValue (initialValue)); + return MakeParamSpecHelper (a1, a2, ClassValue (initialValue)); } } // namespace ns3 diff --git a/src/core/fp-value.h b/src/core/fp-value.h index eb0f6bc73..d733b06b9 100644 --- a/src/core/fp-value.h +++ b/src/core/fp-value.h @@ -25,6 +25,7 @@ private: double m_value; }; +class FpParamSpec : public ParamSpec {}; template Ptr MakeFpParamSpec (T1 a1, @@ -68,36 +69,36 @@ private: template Ptr MakeFpParamSpec (T1 a1, - double initialValue) + double initialValue) { - return MakeParamSpecHelperWithChecker (a1, FpValue (initialValue), - FpValueChecker::Create (a1)); + return MakeParamSpecHelperWithChecker (a1, FpValue (initialValue), + FpValueChecker::Create (a1)); } template Ptr MakeFpParamSpec (T1 a1, - double initialValue, - double minValue, - double maxValue) + double initialValue, + double minValue, + double maxValue) { - return MakeParamSpecHelperWithChecker (a1, FpValue (initialValue), - FpValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, FpValue (initialValue), + FpValueChecker (minValue, maxValue)); } template Ptr MakeFpParamSpec (T1 a1, T2 a2, - double initialValue) + double initialValue) { - return MakeParamSpecHelperWithChecker (a1, a2, FpValue (initialValue), - FpValueChecker::Create (a1)); + return MakeParamSpecHelperWithChecker (a1, a2, FpValue (initialValue), + FpValueChecker::Create (a1)); } template Ptr MakeFpParamSpec (T1 a1, T2 a2, - double initialValue, - double minValue, - double maxValue) + double initialValue, + double minValue, + double maxValue) { - return MakeParamSpecHelperWithChecker (a1, a2, FpValue (initialValue), - FpValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, a2, FpValue (initialValue), + FpValueChecker (minValue, maxValue)); } } // namespace ns3 diff --git a/src/core/int-value.h b/src/core/int-value.h index 3bbf73db4..2281336e0 100644 --- a/src/core/int-value.h +++ b/src/core/int-value.h @@ -24,6 +24,8 @@ private: int64_t m_value; }; +class IntParamSpec : public ParamSpec {}; + template Ptr MakeIntParamSpec (T1 a1, int64_t initialValue); @@ -76,8 +78,8 @@ Ptr MakeIntParamSpec (T1 a1, int64_t initialValue) { - return MakeParamSpecHelperWithChecker (a1, IntValue (initialValue), - IntValueChecker::Create (a1)); + return MakeParamSpecHelperWithChecker (a1, IntValue (initialValue), + IntValueChecker::Create (a1)); } template @@ -86,15 +88,15 @@ Ptr MakeIntParamSpec (T1 a1, int64_t minValue, int64_t maxValue) { - return MakeParamSpecHelperWithChecker (a1, IntValue (initialValue), - IntValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, IntValue (initialValue), + IntValueChecker (minValue, maxValue)); } template Ptr MakeIntParamSpec (T1 a1, T2 a2, int64_t initialValue) { - return MakeParamSpecHelperWithChecker (a1, a2, IntValue (initialValue), - IntValueChecker::Create (a1)); + return MakeParamSpecHelperWithChecker (a1, a2, IntValue (initialValue), + IntValueChecker::Create (a1)); } template Ptr MakeIntParamSpec (T1 a1, T2 a2, @@ -102,8 +104,8 @@ Ptr MakeIntParamSpec (T1 a1, T2 a2, int64_t minValue, int64_t maxValue) { - return MakeParamSpecHelperWithChecker (a1, a2, IntValue (initialValue), - IntValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, a2, IntValue (initialValue), + IntValueChecker (minValue, maxValue)); } } // namespace ns3 diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index e2d3ffb1c..696db23f1 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -3,19 +3,19 @@ namespace ns3 { -template +template Ptr MakeParamSpecHelper (T1 a1, V initialValue); -template +template Ptr MakeParamSpecHelper (T1 a1, T2 a2, V initialValue); -template +template Ptr MakeParamSpecHelperWithChecker (T1 a1, V initialValue, CHECKER checker); -template +template Ptr MakeParamSpecHelperWithChecker (T1 a1, T2 a2, V initialValue, CHECKER checker); @@ -29,16 +29,59 @@ MakeParamSpecHelperWithChecker (T1 a1, T2 a2, V initialValue, CHECKER checker); namespace ns3 { -template -class ParamSpecHelper : public ParamSpec +template +class ParamSpecHelper : public BASE { public: - ParamSpecHelper (PValue initialValue, CHECKER checker); - virtual bool Set (ObjectBase * object, PValue value) const; - virtual bool Get (const ObjectBase * object, PValue value) const; - virtual bool Check (PValue value) const; - virtual PValue GetInitialValue (void) const; - virtual PValue CreateValue (void) const; + ParamSpecHelper (PValue initialValue, CHECKER checker) + : m_initialValue (initialValue), + m_checker (checker) {} + + virtual bool Set (ObjectBase * object, PValue val) const { + const U *value = val.DynCast (); + if (value == 0) + { + return false; + } + T *obj = dynamic_cast (object); + if (obj == 0) + { + return false; + } + return DoSet (obj, value); + } + + virtual bool Get (const ObjectBase * object, PValue val) const { + U *value = val.DynCast (); + if (value == 0) + { + return false; + } + const T *obj = dynamic_cast (object); + if (obj == 0) + { + return false; + } + return DoGet (obj, value); + } + + virtual bool Check (PValue value) const { + const U *val = value.DynCast (); + if (val == 0) + { + return false; + } + return m_checker.Check (val->Get ()); + } + + virtual PValue GetInitialValue (void) const { + return m_initialValue; + } + + virtual PValue CreateValue (void) const { + return m_initialValue.Copy (); + } + private: virtual bool DoSet (T *object, const U *v) const = 0; @@ -54,15 +97,15 @@ public: bool Check (const T &value) const {return true;} }; -template +template Ptr DoMakeParamSpecHelperOne (U T::*memberVariable, V initialValue, CHECKER checker) { - class MemberVariable : public ParamSpecHelper + class MemberVariable : public ParamSpecHelper { public: MemberVariable (U T::*memberVariable, V initialValue, CHECKER checker) - : ParamSpecHelper (initialValue, checker), + : ParamSpecHelper (initialValue, checker), m_memberVariable (memberVariable) {} private: @@ -79,25 +122,25 @@ DoMakeParamSpecHelperOne (U T::*memberVariable, V initialValue, CHECKER checker) }; return Ptr (new MemberVariable (memberVariable, initialValue, checker), false); } -template +template Ptr DoMakeParamSpecHelperOne (U T::*memberVariable, V initialValue) { - return DoMakeParamSpecHelperOne (memberVariable, initialValue, ParamSpecHelperSimpleChecker ()); + return DoMakeParamSpecHelperOne (memberVariable, initialValue, ParamSpecHelperSimpleChecker ()); } -template +template Ptr DoMakeParamSpecHelperOne (U (T::*getter) (void) const, V initialValue, CHECKER checker) { - class MemberMethod : public ParamSpecHelper + class MemberMethod : public ParamSpecHelper { public: MemberMethod (U (T::*getter) (void) const, V initialValue, CHECKER checker) - : ParamSpecHelper (initialValue, checker), + : ParamSpecHelper (initialValue, checker), m_getter (getter) {} private: @@ -113,27 +156,27 @@ DoMakeParamSpecHelperOne (U (T::*getter) (void) const, return Ptr (new MemberMethod (getter, initialValue, checker), false); } -template +template Ptr DoMakeParamSpecHelperOne (U (T::*getter) (void) const, V initialValue) { - return DoMakeParamSpecHelperOne (getter, initialValue, ParamSpecHelperSimpleChecker ()); + return DoMakeParamSpecHelperOne (getter, initialValue, ParamSpecHelperSimpleChecker ()); } -template +template Ptr DoMakeParamSpecHelperOne (void (T::*setter) (U), V initialValue, CHECKER checker) { - class MemberMethod : public ParamSpecHelper + class MemberMethod : public ParamSpecHelper { public: MemberMethod (void (T::*setter) (U), V initialValue, CHECKER checker) - : ParamSpecHelper (initialValue, checker), + : ParamSpecHelper (initialValue, checker), m_setter (setter) {} private: @@ -149,30 +192,30 @@ DoMakeParamSpecHelperOne (void (T::*setter) (U), return Ptr (new MemberMethod (setter, initialValue, checker), false); } -template +template Ptr DoMakeParamSpecHelperOne (void (T::*setter) (U), V initialValue) { - return DoMakeParamSpecHelperOne (setter, initialValue, - ParamSpecHelperSimpleChecker::ReferencedType> ()); + return DoMakeParamSpecHelperOne (setter, initialValue, + ParamSpecHelperSimpleChecker::ReferencedType> ()); } -template +template Ptr DoMakeParamSpecHelperTwo (void (T::*setter) (U), V (T::*getter) (void) const, W initialValue, CHECKER checker = ParamSpecHelperSimpleChecker ()) { - class MemberMethod : public ParamSpecHelper + class MemberMethod : public ParamSpecHelper { public: MemberMethod (void (T::*setter) (U), V (T::*getter) (void) const, W initialValue, CHECKER checker) - : ParamSpecHelper (initialValue, checker), + : ParamSpecHelper (initialValue, checker), m_setter (setter), m_getter (getter) {} @@ -191,125 +234,60 @@ DoMakeParamSpecHelperTwo (void (T::*setter) (U), return Ptr (new MemberMethod (setter, getter, initialValue, checker), false); } -template +template Ptr DoMakeParamSpecHelperTwo (void (T::*setter) (U), V (T::*getter) (void) const, W initialValue) { - return DoMakeParamSpecHelperTwo (setter, getter, initialValue, ParamSpecHelperSimpleChecker ()); + return DoMakeParamSpecHelperTwo (setter, getter, initialValue, ParamSpecHelperSimpleChecker ()); } -template +template Ptr DoMakeParamSpecHelperTwo (V (T::*getter) (void) const, void (T::*setter) (U), W initialValue, CHECKER checker) { - return DoMakeParamSpecHelperTwo (setter, getter, initialValue, checker); + return DoMakeParamSpecHelperTwo (setter, getter, initialValue, checker); } -template +template Ptr DoMakeParamSpecHelperTwo (V (T::*getter) (void) const, void (T::*setter) (U), W initialValue) { - return DoMakeParamSpecHelperTwo (setter, getter, initialValue); + return DoMakeParamSpecHelperTwo (setter, getter, initialValue); } - -template -ParamSpecHelper::ParamSpecHelper (PValue initialValue, CHECKER checker) - : m_initialValue (initialValue), - m_checker (checker) -{} -template -bool -ParamSpecHelper::Set (ObjectBase * object, PValue val) const -{ - const U *value = val.DynCast (); - if (value == 0) - { - return false; - } - T *obj = dynamic_cast (object); - if (obj == 0) - { - return false; - } - return DoSet (obj, value); -} -template -bool -ParamSpecHelper::Get (const ObjectBase * object, PValue val) const -{ - U *value = val.DynCast (); - if (value == 0) - { - return false; - } - const T *obj = dynamic_cast (object); - if (obj == 0) - { - return false; - } - return DoGet (obj, value); -} -template -bool -ParamSpecHelper::Check (PValue value) const -{ - const U *val = value.DynCast (); - if (val == 0) - { - return false; - } - return m_checker.Check (val->Get ()); -} -template -PValue -ParamSpecHelper::GetInitialValue (void) const -{ - return m_initialValue; -} - -template -PValue -ParamSpecHelper::CreateValue (void) const -{ - return m_initialValue.Copy (); -} - - - -template +template Ptr MakeParamSpecHelper (T1 a1, V initialValue) { - return DoMakeParamSpecHelperOne (a1, initialValue); + return DoMakeParamSpecHelperOne (a1, initialValue); } -template +template Ptr MakeParamSpecHelper (T1 a1, T2 a2, V initialValue) { - return DoMakeParamSpecHelperTwo (a1, a2, initialValue); + return DoMakeParamSpecHelperTwo (a1, a2, initialValue); } -template +template Ptr MakeParamSpecHelperWithChecker (T1 a1, V initialValue, CHECKER checker) { - return DoMakeParamSpecHelperOne (a1, initialValue, checker); + return DoMakeParamSpecHelperOne (a1, initialValue, checker); } -template +template Ptr MakeParamSpecHelperWithChecker (T1 a1, T2 a2, V initialValue, CHECKER checker) { - return DoMakeParamSpecHelperTwo (a1, a2, initialValue, checker); + return DoMakeParamSpecHelperTwo (a1, a2, initialValue, checker); } } // namespace ns3 diff --git a/src/core/random-variable.h b/src/core/random-variable.h index 3cf219b11..e251f0a74 100644 --- a/src/core/random-variable.h +++ b/src/core/random-variable.h @@ -666,6 +666,7 @@ std::istream &operator >> (std::istream &os, RandomVariable &var); class RandomVariableValue : public Value {}; +class RandomVariableParamSpec : public ParamSpec {}; template Ptr MakeRandomVariableParamSpec (T1 a1, @@ -684,14 +685,16 @@ template Ptr MakeRandomVariableParamSpec (T1 a1, RandomVariable initialValue) { - return MakeClassValueHelperParamSpec (a1, initialValue); + return MakeClassValueHelperParamSpec (a1, initialValue); } template Ptr MakeRandomVariableParamSpec (T1 a1, T2 a2, RandomVariable initialValue) { - return MakeClassValueHelperParamSpec (a1, a2, initialValue); + return MakeClassValueHelperParamSpec (a1, a2, initialValue); } diff --git a/src/core/uint-value.h b/src/core/uint-value.h index 1a85e9c33..21ffad5dc 100644 --- a/src/core/uint-value.h +++ b/src/core/uint-value.h @@ -24,6 +24,8 @@ private: uint64_t m_value; }; +class UintParamSpec : public ParamSpec {}; + template Ptr MakeUintParamSpec (T1 a1, uint64_t initialValue); @@ -76,8 +78,8 @@ Ptr MakeUintParamSpec (T1 a1, uint64_t initialValue) { - return MakeParamSpecHelperWithChecker (a1, UintValue (initialValue), - UintValueChecker::Create (a1)); + return MakeParamSpecHelperWithChecker (a1, UintValue (initialValue), + UintValueChecker::Create (a1)); } template @@ -86,15 +88,15 @@ Ptr MakeUintParamSpec (T1 a1, uint64_t minValue, uint64_t maxValue) { - return MakeParamSpecHelperWithChecker (a1, UintValue (initialValue), - UintValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, UintValue (initialValue), + UintValueChecker (minValue, maxValue)); } template Ptr MakeUintParamSpec (T1 a1, T2 a2, uint64_t initialValue) { - return MakeParamSpecHelperWithChecker (a1, a2, UintValue (initialValue), - UintValueChecker::Create (a1)); + return MakeParamSpecHelperWithChecker (a1, a2, UintValue (initialValue), + UintValueChecker::Create (a1)); } template Ptr MakeUintParamSpec (T1 a1, T2 a2, @@ -102,8 +104,8 @@ Ptr MakeUintParamSpec (T1 a1, T2 a2, uint64_t minValue, uint64_t maxValue) { - return MakeParamSpecHelperWithChecker (a1, a2, UintValue (initialValue), - UintValueChecker (minValue, maxValue)); + return MakeParamSpecHelperWithChecker (a1, a2, UintValue (initialValue), + UintValueChecker (minValue, maxValue)); } } // namespace ns3 diff --git a/src/mobility/rectangle.h b/src/mobility/rectangle.h index 2f4828d1a..b4f4386af 100644 --- a/src/mobility/rectangle.h +++ b/src/mobility/rectangle.h @@ -71,6 +71,7 @@ std::istream &operator >> (std::istream &is, Rectangle &rectangle); class RectangleValue : public Value {}; +class RectangleParamSpec : public ParamSpec {}; template Ptr MakeRectangleParamSpec (T1 a1, @@ -87,13 +88,13 @@ template Ptr MakeRectangleParamSpec (T1 a1, Rectangle initialValue) { - return MakeClassValueHelperParamSpec (a1, initialValue); + return MakeClassValueHelperParamSpec (a1, initialValue); } template Ptr MakeRectangleParamSpec (T1 a1, T2 a2, Rectangle initialValue) { - return MakeClassValueHelperParamSpec (a1, a2, initialValue); + return MakeClassValueHelperParamSpec (a1, a2, initialValue); } } // namespace ns3 diff --git a/src/mobility/vector.h b/src/mobility/vector.h index 24dbc54f9..118448337 100644 --- a/src/mobility/vector.h +++ b/src/mobility/vector.h @@ -65,6 +65,7 @@ public: double CalculateDistance (const Vector &a, const Vector &b); class VectorValue : public Value {}; +class VectorParamSpec : public ParamSpec {}; std::ostream &operator << (std::ostream &os, const Vector &vector); std::istream &operator >> (std::istream &is, Vector &vector); @@ -86,7 +87,7 @@ template Ptr MakeVectorParamSpec (T1 a1, const Vector &initialValue) { - return MakeClassValueHelperParamSpec (a1, initialValue); + return MakeClassValueHelperParamSpec (a1, initialValue); } template @@ -94,7 +95,7 @@ Ptr MakeVectorParamSpec (T1 a1, T2 a2, const Vector &initialValue) { - return MakeClassValueHelperParamSpec (a1, a2, initialValue); + return MakeClassValueHelperParamSpec (a1, a2, initialValue); } } // namespace ns3 diff --git a/src/simulator/nstime.h b/src/simulator/nstime.h index 283e33253..4673100db 100644 --- a/src/simulator/nstime.h +++ b/src/simulator/nstime.h @@ -671,6 +671,7 @@ typedef TimeUnit<2> TimeSquare; class TimeValue : public Value {}; +class TimeParamSpec : public ParamSpec {}; template Ptr MakeTimeParamSpec (T1 a1, @@ -688,14 +689,14 @@ template Ptr MakeTimeParamSpec (T1 a1, Time initialValue) { - return MakeClassValueHelperParamSpec (a1, initialValue); + return MakeClassValueHelperParamSpec (a1, initialValue); } template Ptr MakeTimeParamSpec (T1 a1, T2 a2, Time initialValue) { - return MakeClassValueHelperParamSpec (a1, a2, initialValue); + return MakeClassValueHelperParamSpec (a1, a2, initialValue); } From 4e1ab534f030ee30fedeeaaee5cdae315f42cce7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 14 Feb 2008 00:36:14 +0100 Subject: [PATCH 041/130] add Value support to Mac48Address --- src/node/mac48-address.cc | 17 +++++++++++++++++ src/node/mac48-address.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/node/mac48-address.cc b/src/node/mac48-address.cc index cb6c43228..0c4013da1 100644 --- a/src/node/mac48-address.cc +++ b/src/node/mac48-address.cc @@ -149,6 +149,17 @@ Mac48Address::GetBroadcast (void) static Mac48Address broadcast = Mac48Address ("ff:ff:ff:ff:ff:ff"); return broadcast; } + +Mac48Address::Mac48Address (PValue value) +{ + *this = ClassValueHelperExtractFrom (value); +} +Mac48Address::operator PValue () const +{ + return ClassValueHelperConvertTo (this); +} + + bool operator == (const Mac48Address &a, const Mac48Address &b) { return memcmp (a.m_address, b.m_address, 6) == 0; @@ -197,5 +208,11 @@ std::ostream& operator<< (std::ostream& os, const Mac48Address & address) return os; } +std::istream& operator>> (std::istream& is, const Mac48Address & address) +{ + // XXX ! + return is; +} + } // namespace ns3 diff --git a/src/node/mac48-address.h b/src/node/mac48-address.h index 255557e3d..91068506d 100644 --- a/src/node/mac48-address.h +++ b/src/node/mac48-address.h @@ -22,6 +22,8 @@ #include #include +#include "ns3/value.h" +#include "ns3/class-value-helper.h" namespace ns3 { @@ -93,6 +95,9 @@ public: * \returns the broadcast address */ static Mac48Address GetBroadcast (void); + + Mac48Address (PValue value); + operator PValue () const; private: /** * \returns a new Address instance @@ -111,6 +116,36 @@ bool operator == (const Mac48Address &a, const Mac48Address &b); bool operator != (const Mac48Address &a, const Mac48Address &b); bool operator < (const Mac48Address &a, const Mac48Address &b); std::ostream& operator<< (std::ostream& os, const Mac48Address & address); +std::istream& operator>> (std::istream& is, const Mac48Address & address); + +class Mac48AddressValue : public Value {}; +class Mac48AddressParamSpec : public ParamSpec {}; + +template +Ptr +MakeMac48AddressParamSpec (T1 a1, Mac48Address address); + +template +Ptr +MakeMac48AddressParamSpec (T1 a1, T2 a2, Mac48Address address); + +} // namespace ns3 + +namespace ns3 { + +template +Ptr +MakeMac48AddressParamSpec (T1 a1, Mac48Address address) +{ + return MakeClassValueHelperParamSpec (a1, address); +} + +template +Ptr +MakeMac48AddressParamSpec (T1 a1, T2 a2, Mac48Address address) +{ + return MakeClassValueHelperParamSpec (a1, a2, address); +} } // namespace ns3 From 7d0e7e3ea80226e91cf067335caa97f159392923 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 14 Feb 2008 00:36:25 +0100 Subject: [PATCH 042/130] add Value support to DataRate --- src/common/data-rate.cc | 30 ++++++++++++++++++++++++++++ src/common/data-rate.h | 43 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/common/data-rate.cc b/src/common/data-rate.cc index 5e8358804..564664a7e 100644 --- a/src/common/data-rate.cc +++ b/src/common/data-rate.cc @@ -185,6 +185,36 @@ uint64_t DataRate::GetBitRate() const return m_bps; } +DataRate::DataRate (PValue value) +{ + *this = ClassValueHelperExtractFrom (value); +} +DataRate::operator PValue () const +{ + return ClassValueHelperConvertTo (this); +} + +std::ostream &operator << (std::ostream &os, const DataRate &rate) +{ + os << rate.GetBitRate () << "bps"; + return os; +} +std::istream &operator >> (std::istream &is, DataRate &rate) +{ + std::string value; + is >> value; + uint64_t v; + bool ok = DoParse (value, &v); + if (!ok) + { + is.setstate (std::ios_base::failbit); + } + rate = DataRate (v); + return is; +} + + + double operator*(const DataRate& lhs, const Time& rhs) { return rhs.GetSeconds()*lhs.GetBitRate(); diff --git a/src/common/data-rate.h b/src/common/data-rate.h index 3607f4916..39ade9d21 100644 --- a/src/common/data-rate.h +++ b/src/common/data-rate.h @@ -27,6 +27,8 @@ #include #include "ns3/nstime.h" #include "ns3/default-value.h" +#include "ns3/value.h" +#include "ns3/class-value-helper.h" namespace ns3 { @@ -94,10 +96,27 @@ class DataRate */ uint64_t GetBitRate() const; - private: + DataRate (PValue value); + operator PValue () const; +private: uint64_t m_bps; static uint64_t Parse(const std::string); }; + +std::ostream &operator << (std::ostream &os, const DataRate &rate); +std::istream &operator >> (std::istream &is, DataRate &rate); + +class DataRateValue : public Value {}; +class DataRateParamSpec : public Value {}; + +template +Ptr +MakeDataRateParamSpec (T1 a1, DataRate initialValue); + +template +Ptr +MakeDataRateParamSpec (T1 a1, T2 a2, DataRate initialValue); + /** * \param lhs * \param rhs @@ -122,6 +141,26 @@ private: DataRate m_value; }; -};//namespace ns3 +} //namespace ns3 + +namespace ns3 { + +template +Ptr +MakeDataRateParamSpec (T1 a1, DataRate initialValue) +{ + return MakeClassValueHelperParamSpec + (a1, initialValue); +} + +template +Ptr +MakeDataRateParamSpec (T1 a1, T2 a2, DataRate initialValue) +{ + return MakeClassValueHelperParamSpec + (a1, a2, initialValue); +} + +} // namespace ns3 #endif /* DATA_RATE_H */ From 4fc37956810f940db4fcd979b45e002be23b5fa3 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 14 Feb 2008 00:36:39 +0100 Subject: [PATCH 043/130] add Value support to Address --- src/node/address.cc | 16 ++++++++++++++++ src/node/address.h | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/node/address.cc b/src/node/address.cc index 0cc37b1a2..5228da710 100644 --- a/src/node/address.cc +++ b/src/node/address.cc @@ -107,6 +107,16 @@ Address::Register (void) return type; } +Address::Address (PValue value) +{ + *this = ClassValueHelperExtractFrom (value); +} +Address::operator PValue () const +{ + return ClassValueHelperConvertTo (this); +} + + bool operator == (const Address &a, const Address &b) { NS_ASSERT (a.m_type == b.m_type || @@ -159,6 +169,12 @@ std::ostream& operator<< (std::ostream& os, const Address & address) return os; } +std::istream& operator>> (std::istream& is, Address & address) +{ + // XXX: need to be able to parse this. + return is; +} + } // namespace ns3 diff --git a/src/node/address.h b/src/node/address.h index aedc940d4..911bda30d 100644 --- a/src/node/address.h +++ b/src/node/address.h @@ -3,6 +3,8 @@ #include #include +#include "ns3/value.h" +#include "ns3/class-value-helper.h" namespace ns3 { @@ -151,6 +153,9 @@ public: * \returns a new type id. */ static uint8_t Register (void); + + Address (PValue value); + operator PValue () const; private: friend bool operator == (const Address &a, const Address &b); friend bool operator < (const Address &a, const Address &b); @@ -165,6 +170,40 @@ bool operator == (const Address &a, const Address &b); bool operator != (const Address &a, const Address &b); bool operator < (const Address &a, const Address &b); std::ostream& operator<< (std::ostream& os, const Address & address); +std::istream& operator>> (std::istream& is, Address & address); + +class AddressValue : public Value {}; +class AddressParamSpec : public ParamSpec {}; + +template +Ptr +MakeAddressParamSpec (T1 a1, Address initialValue); + +template +Ptr +MakeAddressParamSpec (T1 a1, T2 a2, Address initialValue); + +} // namespace ns3 + +namespace ns3 { + +template +Ptr +MakeAddressParamSpec (T1 a1, Address initialValue) +{ + return MakeClassValueHelperParamSpec + (a1, initialValue); +} + +template +Ptr +MakeAddressParamSpec (T1 a1, T2 a2, Address initialValue) +{ + return MakeClassValueHelperParamSpec + (a1, a2, initialValue); +} + + } // namespace ns3 From 5847f6002573842d2498e617904c48334245d3f7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 14 Feb 2008 00:43:46 +0100 Subject: [PATCH 044/130] fix an old typo. --- src/node/mac48-address.h | 6 +++--- src/node/mac64-address.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/node/mac48-address.h b/src/node/mac48-address.h index 91068506d..cac30976d 100644 --- a/src/node/mac48-address.h +++ b/src/node/mac48-address.h @@ -17,8 +17,8 @@ * * Author: Mathieu Lacage */ -#ifndef EUI48_ADDRESS_H -#define EUI48_ADDRESS_H +#ifndef MAC48_ADDRESS_H +#define MAC48_ADDRESS_H #include #include @@ -149,4 +149,4 @@ MakeMac48AddressParamSpec (T1 a1, T2 a2, Mac48Address address) } // namespace ns3 -#endif /* EUI48_ADDRESS_H */ +#endif /* MAC48_ADDRESS_H */ diff --git a/src/node/mac64-address.h b/src/node/mac64-address.h index 028ad23ab..67d3cfc2f 100644 --- a/src/node/mac64-address.h +++ b/src/node/mac64-address.h @@ -17,8 +17,8 @@ * * Author: Mathieu Lacage */ -#ifndef EUI64_ADDRESS_H -#define EUI64_ADDRESS_H +#ifndef MAC64_ADDRESS_H +#define MAC64_ADDRESS_H #include #include @@ -95,4 +95,4 @@ std::ostream& operator<< (std::ostream& os, const Mac64Address & address); } // namespace ns3 -#endif /* EUI64_ADDRESS_H */ +#endif /* MAC64_ADDRESS_H */ From f6b221eda20b7cc329712f901da8c495e98098e7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 14 Feb 2008 01:08:45 +0100 Subject: [PATCH 045/130] replace ambiguous constructor with separate construction function. --- src/common/data-rate.cc | 33 ++++++++++++++++----------------- src/common/data-rate.h | 25 ++++++------------------- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/common/data-rate.cc b/src/common/data-rate.cc index 564664a7e..ff4e7230e 100644 --- a/src/common/data-rate.cc +++ b/src/common/data-rate.cc @@ -125,25 +125,13 @@ DoParse (const std::string s, uint64_t *v) namespace ns3 { +DataRate::DataRate () + : m_bps (0) +{} + DataRate::DataRate(uint64_t bps) :m_bps(bps) -{ -} - -DataRate::DataRate (const std::string s) - : m_bps(DataRate::Parse(s)) -{ -} - -uint64_t DataRate::Parse(const std::string s) -{ - uint64_t v; - if (!DoParse (s, &v)) - { - NS_FATAL_ERROR("Can't Parse data rate "< (this); } +DataRate MakeDataRate (std::string rate) +{ + uint64_t bps; + bool ok = DoParse (rate, &bps); + if (!ok) + { + NS_FATAL_ERROR ("Could not parse rate."); + } + return DataRate (bps); +} + std::ostream &operator << (std::ostream &os, const DataRate &rate) { os << rate.GetBitRate () << "bps"; diff --git a/src/common/data-rate.h b/src/common/data-rate.h index 39ade9d21..af2653d5c 100644 --- a/src/common/data-rate.h +++ b/src/common/data-rate.h @@ -48,7 +48,8 @@ namespace ns3 { */ class DataRate { - public: +public: + DataRate (); /** * \brief Integer constructor * @@ -57,23 +58,7 @@ class DataRate * non-trivial bitrate availiable. */ DataRate (uint64_t bps); - - /** - * \brief String constructor - * - * Construct a DataRate from a string. The supported strings have a - * numerical portion, followed by units in the following format: - * - Prefix: nothing, "k", "M", "G" - * - Data Unit: "b, "B" - * - Time Suffix: "ps", "/s" \n - * The prefixes are SI powers of 10 (10^0,10^3,10^6,10^9 respectively).\n - * The units are the bit, and the (8-bit) byte respectively.\n - * Both time suffixes denote "per second". Some supported examples include - * "20B/s", "56kbps", "4.4MB/s", and "100Gb/s". Any malformed string causes - * a fatal error. - */ - DataRate (const std::string s); - + bool operator < (const DataRate& rhs) const; bool operator <= (const DataRate& rhs) const; bool operator > (const DataRate& rhs) const; @@ -103,11 +88,13 @@ private: static uint64_t Parse(const std::string); }; +DataRate MakeDataRate (std::string rate); + std::ostream &operator << (std::ostream &os, const DataRate &rate); std::istream &operator >> (std::istream &is, DataRate &rate); class DataRateValue : public Value {}; -class DataRateParamSpec : public Value {}; +class DataRateParamSpec : public ParamSpec {}; template Ptr From a01118e6ce161584fcdb3892b90a00e4f1d31c87 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 14 Feb 2008 19:47:19 +0100 Subject: [PATCH 046/130] convert DropTailQueue to parameters --- src/node/drop-tail-queue.cc | 29 +++++++++-------------------- src/node/drop-tail-queue.h | 12 ------------ 2 files changed, 9 insertions(+), 32 deletions(-) diff --git a/src/node/drop-tail-queue.cc b/src/node/drop-tail-queue.cc index c4532d1a9..07be6e8de 100644 --- a/src/node/drop-tail-queue.cc +++ b/src/node/drop-tail-queue.cc @@ -18,6 +18,7 @@ */ #include "ns3/log.h" +#include "ns3/uint-value.h" #include "drop-tail-queue.h" NS_LOG_COMPONENT_DEFINE ("DropTailQueue"); @@ -30,14 +31,18 @@ TypeId DropTailQueue::GetTypeId (void) { static TypeId tid = TypeId ("DropTailQueue") .SetParent () - .AddConstructor (); + .AddConstructor () + .AddParameter ("MaxPackets", "The maximum number of packets accepted by this DropTailQueue.", + MakeUintParamSpec (&DropTailQueue::m_maxPackets, + 100)) + ; + return tid; } DropTailQueue::DropTailQueue () : Queue (), - m_packets (), - m_maxPackets(DTQ_NPACKETS_MAX_DEFAULT) + m_packets () { NS_LOG_FUNCTION; } @@ -47,22 +52,6 @@ DropTailQueue::~DropTailQueue () NS_LOG_FUNCTION; } -void -DropTailQueue::SetMaxPackets (uint32_t npackets) -{ - NS_LOG_FUNCTION; - NS_LOG_PARAMS (this << npackets); - m_maxPackets = npackets; -} - -uint32_t -DropTailQueue::GetMaxPackets (void) -{ - NS_LOG_FUNCTION; - NS_LOG_LOGIC ("returns " << m_maxPackets); - return m_maxPackets; -} - bool DropTailQueue::DoEnqueue (Ptr p) { @@ -143,7 +132,7 @@ DropTailQueueTest::RunTests (void) bool result = true; DropTailQueue queue; - queue.SetMaxPackets (3); + queue.Set ("MaxPackets", UintValue (3)); Ptr p1, p2, p3, p4; p1 = Create (); diff --git a/src/node/drop-tail-queue.h b/src/node/drop-tail-queue.h index b9a1f6570..9fef02e1d 100644 --- a/src/node/drop-tail-queue.h +++ b/src/node/drop-tail-queue.h @@ -28,8 +28,6 @@ namespace ns3 { class TraceContainer; -const int DTQ_NPACKETS_MAX_DEFAULT = 100; - /** * \brief A FIFO packet queue that drops tail-end packets on overflow */ @@ -44,16 +42,6 @@ public: DropTailQueue (); virtual ~DropTailQueue(); - /** - * \param npackets The maximum number of packets this queue will hold before - * dropping packets. - */ - void SetMaxPackets (uint32_t npackets); - /** - * \return The maximum number of packets this queue will hold before dropping - * packets. - */ - uint32_t GetMaxPackets (void); private: virtual bool DoEnqueue (Ptr p); From cdeb6fa7aab28d1f33386c013f6ca9a1c59bc99d Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 14 Feb 2008 23:56:21 +0100 Subject: [PATCH 047/130] add device/node containers for helper API. --- src/node/net-device-container.cc | 40 ++++++++++++++++++++++++++ src/node/net-device-container.h | 31 +++++++++++++++++++++ src/node/node-container.cc | 48 ++++++++++++++++++++++++++++++++ src/node/node-container.h | 31 +++++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 src/node/net-device-container.cc create mode 100644 src/node/net-device-container.h create mode 100644 src/node/node-container.cc create mode 100644 src/node/node-container.h diff --git a/src/node/net-device-container.cc b/src/node/net-device-container.cc new file mode 100644 index 000000000..9bde8226c --- /dev/null +++ b/src/node/net-device-container.cc @@ -0,0 +1,40 @@ +#include "net-device-container.h" + +namespace ns3 { + +NetDeviceContainer::Iterator +NetDeviceContainer::Begin (void) const +{ + return m_nodes.begin (); +} +NetDeviceContainer::Iterator +NetDeviceContainer::End (void) const +{ + return m_nodes.end (); +} + +uint32_t +NetDeviceContainer::GetN (void) const +{ + return m_nodes.size (); +} +Ptr +NetDeviceContainer::Get (uint32_t i) const +{ + return m_nodes[i]; +} +void +NetDeviceContainer::Add (NetDeviceContainer other) +{ + for (Iterator i = other.Begin (); i != other.End (); i++) + { + m_nodes.push_back (*i); + } +} +void +NetDeviceContainer::Add (Ptr node) +{ + m_nodes.push_back (node); +} + +} // namespace ns3 diff --git a/src/node/net-device-container.h b/src/node/net-device-container.h new file mode 100644 index 000000000..e90192067 --- /dev/null +++ b/src/node/net-device-container.h @@ -0,0 +1,31 @@ +#ifndef NET_DEVICE_CONTAINER_H +#define NET_DEVICE_CONTAINER_H + +#include +#include +#include "net-device.h" + +namespace ns3 { + +class NetDeviceContainer +{ +public: + typedef std::vector >::const_iterator Iterator; + + Iterator Begin (void) const; + Iterator End (void) const; + + uint32_t GetN (void) const; + Ptr Get (uint32_t i) const; + + void Create (uint32_t n); + void Add (NetDeviceContainer other); + void Add (Ptr node); + +private: + std::vector > m_nodes; +}; + +} // namespace ns3 + +#endif /* NET_DEVICE_CONTAINER_H */ diff --git a/src/node/node-container.cc b/src/node/node-container.cc new file mode 100644 index 000000000..6952da70f --- /dev/null +++ b/src/node/node-container.cc @@ -0,0 +1,48 @@ +#include "node-container.h" + +namespace ns3 { + +NodeContainer::Iterator +NodeContainer::Begin (void) const +{ + return m_nodes.begin (); +} +NodeContainer::Iterator +NodeContainer::End (void) const +{ + return m_nodes.end (); +} + +uint32_t +NodeContainer::GetN (void) const +{ + return m_nodes.size (); +} +Ptr +NodeContainer::Get (uint32_t i) const +{ + return m_nodes[i]; +} +void +NodeContainer::Create (uint32_t n) +{ + for (uint32_t i = 0; i < n; i++) + { + m_nodes.push_back (CreateObject ()); + } +} +void +NodeContainer::Add (NodeContainer other) +{ + for (Iterator i = other.Begin (); i != other.End (); i++) + { + m_nodes.push_back (*i); + } +} +void +NodeContainer::Add (Ptr node) +{ + m_nodes.push_back (node); +} + +} // namespace ns3 diff --git a/src/node/node-container.h b/src/node/node-container.h new file mode 100644 index 000000000..c7eb31c1b --- /dev/null +++ b/src/node/node-container.h @@ -0,0 +1,31 @@ +#ifndef NODE_CONTAINER_H +#define NODE_CONTAINER_H + +#include +#include +#include "node.h" + +namespace ns3 { + +class NodeContainer +{ +public: + typedef std::vector >::const_iterator Iterator; + + Iterator Begin (void) const; + Iterator End (void) const; + + uint32_t GetN (void) const; + Ptr Get (uint32_t i) const; + + void Create (uint32_t n); + void Add (NodeContainer other); + void Add (Ptr node); + + private: + std::vector > m_nodes; +}; + +} // namespace ns3 + +#endif /* NODE_CONTAINER_H */ From 11b727c06e4402cf4257bb094e7d7f8136113fad Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 14 Feb 2008 23:56:54 +0100 Subject: [PATCH 048/130] an initial draft of helper classes. don't work yet. --- src/devices/csma/csma-helper.cc | 59 +++++++++++++++++++ src/devices/csma/csma-helper.h | 38 ++++++++++++ .../point-to-point/point-to-point-helper.cc | 53 +++++++++++++++++ .../point-to-point/point-to-point-helper.h | 42 +++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 src/devices/csma/csma-helper.cc create mode 100644 src/devices/csma/csma-helper.h create mode 100644 src/devices/point-to-point/point-to-point-helper.cc create mode 100644 src/devices/point-to-point/point-to-point-helper.h diff --git a/src/devices/csma/csma-helper.cc b/src/devices/csma/csma-helper.cc new file mode 100644 index 000000000..85919329e --- /dev/null +++ b/src/devices/csma/csma-helper.cc @@ -0,0 +1,59 @@ +#include "csma-helper.h" + +namespace ns3 { + +CsmaHelper::CsmaHelper () +{ + m_queueFactory.SetTypeId ("DropTailQueue"); + m_deviceFactory.SetTypeId ("CsmaNetDevice"); + m_deviceChannel.SetTypeId ("CsmaChannel"); +} + +void +CsmaHelper::SetQueue (std::string type, + std::string n1, PValue v1, + std::string n2, PValue v2, + std::string n3, PValue v3, + std::string n4, PValue v4) +{ + m_queueFactory.SetTypeId (type); + m_queueFactory.Set (n1, v1); + m_queueFactory.Set (n2, v2); + m_queueFactory.Set (n3, v3); + m_queueFactory.Set (n4, v4); +} + + /** + * Set these parameters on each PointToPointNetDevice created + * by this helper. + */ +void +CsmaHelper::SetDeviceParameter (std::string n1, PValue v1) +{ + m_deviceFactory.Set (n1, v1); +} + +void +CsmaHelper::SetChannelParameter (std::string n1, PValue v1) +{ + m_csmaFactory.Set (n1, v1); +} + +NetDeviceContainer +CsmaHelper::Build (const NodeContainer &c) +{ + Ptr channel = m_channelFactory.Create ()->GetObject (); + return Build (c, channel); +} + +NetDeviceContainer +CsmaHelper::Build (const NodeContainer &c, Ptr channel) +{ + for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++) + { + + } +} + + +} // namespace ns3 diff --git a/src/devices/csma/csma-helper.h b/src/devices/csma/csma-helper.h new file mode 100644 index 000000000..552fd9037 --- /dev/null +++ b/src/devices/csma/csma-helper.h @@ -0,0 +1,38 @@ +#ifndef CSMA_HELPER_H +#define CSMA_HELPER_H + +namespace ns3 { + +class CsmaHelper +{ +public: + CsmaHelper (); + + void SetQueue (std::string type, + std::string n1 = "", PValue v1 = PValue (), + std::string n2 = "", PValue v2 = PValue (), + std::string n3 = "", PValue v3 = PValue (), + std::string n4 = "", PValue v4 = PValue ()); + + /** + * Set these parameters on each PointToPointNetDevice created + * by this helper. + */ + void SetDeviceParameter (std::string n1, PValue v1); + + void SetChannelParameter (std::string n1, PValue v1); + + NetDeviceContainer Build (const NodeContainer &c); + + NetDeviceContainer Build (const NodeContainer &c, Ptr channel); + +private: + ObjectFactory m_queueFactory; + ObjectFactory m_deviceFactory; + ObjectFactory m_channelFactory; +}; + + +} // namespace ns3 + +#endif /* CSMA_HELPER_H */ diff --git a/src/devices/point-to-point/point-to-point-helper.cc b/src/devices/point-to-point/point-to-point-helper.cc new file mode 100644 index 000000000..346aff88a --- /dev/null +++ b/src/devices/point-to-point/point-to-point-helper.cc @@ -0,0 +1,53 @@ +#include "point-to-point-helper.h" +#include "point-to-point-net-device.h" +#include "point-to-point-channel.h" +#include "ns3/queue.h" + +namespace ns3 { + + +PointToPointHelper::PointToPointHelper () +{} + +void +PointToPointHelper::SetQueue (std::string type, + std::string n1, PValue v1, + std::string n2, PValue v2, + std::string n3, PValue v3, + std::string n4, PValue v4) +{ + m_queueFactory.SetTypeId (type); + m_queueFactory.Set (n1, v1); + m_queueFactory.Set (n2, v2); + m_queueFactory.Set (n3, v3); + m_queueFactory.Set (n4, v4); +} + +NetDeviceContainer +PointToPointHelper::Build (NodeContainer c) +{ + NS_ASSERT (c.GetN () == 2); + return Build (c.Get (0), c.Get (1)); +} +NetDeviceContainer +PointToPointHelper::Build (Ptr a, Ptr b) +{ + NetDeviceContainer container; + + Ptr devA = CreateObject (a); + Ptr queueA = m_queueFactory.Create ()->GetObject (); + devA->AddQueue (queueA); + Ptr devB = CreateObject (b); + Ptr queueB = m_queueFactory.Create ()->GetObject (); + devB->AddQueue (queueB); + Ptr channel = CreateObject (); + devA->Attach (channel); + devB->Attach (channel); + container.Add (devA); + container.Add (devB); + + return container; +} + + +} // namespace ns3 diff --git a/src/devices/point-to-point/point-to-point-helper.h b/src/devices/point-to-point/point-to-point-helper.h new file mode 100644 index 000000000..bad42730e --- /dev/null +++ b/src/devices/point-to-point/point-to-point-helper.h @@ -0,0 +1,42 @@ +#ifndef POINT_TO_POINT_HELPER_H +#define POINT_TO_POINT_HELPER_H + +#include "ns3/object-factory.h" +#include "ns3/net-device-container.h" +#include "ns3/node-container.h" +#include + +namespace ns3 { + +class PointToPointHelper +{ +public: + // by default, create queues of type DropTailQueue. + PointToPointHelper (); + + void SetQueue (std::string type, + std::string n1 = "", PValue v1 = PValue (), + std::string n2 = "", PValue v2 = PValue (), + std::string n3 = "", PValue v3 = PValue (), + std::string n4 = "", PValue v4 = PValue ()); + + /** + * Set these parameters on each PointToPointNetDevice created + * by this helper. + */ + void SetDeviceParameters (std::string n1, PValue v1, + std::string n2 = "", PValue v2 = PValue (), + std::string n3 = "", PValue v3 = PValue (), + std::string n4 = "", PValue v4 = PValue ()); + + NetDeviceContainer Build (NodeContainer c); + NetDeviceContainer Build (Ptr a, Ptr b); + +private: + ObjectFactory m_queueFactory; +}; + + +} // namespace ns3 + +#endif /* POINT_TO_POINT_HELPER_H */ From 5a569049a0ecc4655b63df983079b725f7f27d61 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 14 Feb 2008 23:57:18 +0100 Subject: [PATCH 049/130] add node/device containers to build --- src/node/wscript | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/node/wscript b/src/node/wscript index 893659fb9..ea91bd1b6 100644 --- a/src/node/wscript +++ b/src/node/wscript @@ -28,6 +28,8 @@ def build(bld): 'tcp.cc', 'ipv4.cc', 'application.cc', + 'node-container.cc', + 'net-device-container.cc', ] headers = bld.create_obj('ns3header') @@ -56,4 +58,6 @@ def build(bld): 'tcp.h', 'ipv4.h', 'application.h', + 'node-container.h', + 'net-device-container.h', ] From ba7dfe0b0c75694d4f7cf606fdccdc6300eec3f6 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 15 Feb 2008 02:08:55 +0100 Subject: [PATCH 050/130] helper getters and setters for c++ native types. --- src/core/object.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++ src/core/object.h | 11 ++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/core/object.cc b/src/core/object.cc index d85be6e1f..0bf29ae40 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -23,6 +23,10 @@ #include "singleton.h" #include "trace-resolver.h" #include "value.h" +#include "uint-value.h" +#include "int-value.h" +#include "fp-value.h" +#include "enum-value.h" #include "log.h" #include #include @@ -929,6 +933,27 @@ Object::Set (std::string name, PValue value) } return DoSet (info.spec, value); } +bool +Object::SetUint (std::string name, uint64_t value) +{ + return Set (name, UintValue (value)); +} +bool +Object::SetInt (std::string name, int64_t value) +{ + return Set (name, IntValue (value)); +} +bool +Object::SetFp (std::string name, double value) +{ + return Set (name, FpValue (value)); +} +bool +Object::SetEnum (std::string name, int value) +{ + return Set (name, EnumValue (value)); +} + bool Object::Get (std::string name, std::string &value) const { @@ -970,6 +995,31 @@ Object::Get (std::string name) const } return value; } +uint64_t +Object::GetUint (std::string name) const +{ + UintValue value = Get (name); + return value.Get (); +} +int64_t +Object::GetInt (std::string name) const +{ + IntValue value = Get (name); + return value.Get (); +} +double +Object::GetFp (std::string name) const +{ + FpValue value = Get (name); + return value.Get (); +} +int +Object::GetEnum (std::string name) const +{ + EnumValue value = Get (name); + return value.Get (); +} + Ptr Object::DoGetObject (TypeId tid) const diff --git a/src/core/object.h b/src/core/object.h index 79fadb993..8b8c2fb7c 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -350,6 +350,12 @@ public: * Set a single parameter. */ bool Set (std::string name, PValue value); + + bool SetUint (std::string name, uint64_t value); + bool SetInt (std::string name, int64_t value); + bool SetFp (std::string name, double value); + bool SetEnum (std::string name, int value); + /** * \param name the name of the parameter to read * \param value a reference to the string where the value of the @@ -365,6 +371,11 @@ public: */ PValue Get (std::string name) const; + uint64_t GetUint (std::string name) const; + int64_t GetInt (std::string name) const; + double GetFp (std::string name) const; + int GetEnum (std::string name) const; + /** * Increment the reference count. This method should not be called * by user code. Object instances are expected to be used in conjunction From 6124479424b04f371ecab8faee5eadc88a414d8e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 15 Feb 2008 03:52:56 +0100 Subject: [PATCH 051/130] get rid of Value::ConvertFrom method. --- src/core/object.cc | 26 +++++++++++++++++++------- src/core/value.cc | 15 --------------- src/core/value.h | 2 -- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/core/object.cc b/src/core/object.cc index 0bf29ae40..72a126c51 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -691,12 +691,21 @@ Parameters::DoSet (Ptr spec, PValue value) bool ok = spec->Check (value); if (!ok) { + // attempt to convert to string. + std::string str = value.SerializeToString (0); + // attempt to convert back to value. PValue v = spec->CreateValue (); - ok = v.ConvertFrom (value, spec); + ok = v.DeserializeFromString (str, spec); if (!ok) { return false; } + ok = spec->Check (v); + if (!ok) + { + return false; + } + value = v; } DoSetOne (spec, value); return true; @@ -903,18 +912,21 @@ Object::DoSet (Ptr spec, PValue value) bool ok = spec->Check (value); if (!ok) { + // attempt to convert to string + std::string str = value.SerializeToString (0); + // attempt to convert back from string. PValue v = spec->CreateValue (); - ok = v.ConvertFrom (value, spec); + ok = v.DeserializeFromString (str, spec); + if (!ok) + { + return false; + } + ok = spec->Check (v); if (!ok) { return false; } value = v; - ok = spec->Check (value); - if (!ok) - { - return false; - } } ok = spec->Set (this, value); return ok; diff --git a/src/core/value.cc b/src/core/value.cc index 478a29b99..23b147f9c 100644 --- a/src/core/value.cc +++ b/src/core/value.cc @@ -20,11 +20,6 @@ Value::operator = (const Value &o) } Value::~Value () {} -bool -Value::ConvertFrom (PValue value, Ptr spec) -{ - return false; -} /*************************************************************** * Big interesting warning. @@ -117,16 +112,6 @@ PValue::DeserializeFromString (std::string value, Ptr spec) { return m_value->DeserializeFromString (value, spec); } -bool -PValue::ConvertFrom (PValue value, Ptr spec) -{ - const StringValue *str = value.DynCast (); - if (str == 0) - { - return false; - } - return DeserializeFromString (str->Get (), spec); -} PValue::PValue (const char *value) : m_value (new StringValue (value)) diff --git a/src/core/value.h b/src/core/value.h index c01b65cf4..b2a97b7ed 100644 --- a/src/core/value.h +++ b/src/core/value.h @@ -22,7 +22,6 @@ public: virtual PValue Copy (void) const = 0; virtual std::string SerializeToString (Ptr spec) const = 0; virtual bool DeserializeFromString (std::string value, Ptr spec) = 0; - virtual bool ConvertFrom (PValue value, Ptr spec); private: friend class PValue; uint32_t m_count; @@ -39,7 +38,6 @@ public: PValue Copy (void) const; std::string SerializeToString (Ptr spec) const; bool DeserializeFromString (std::string value, Ptr spec); - bool ConvertFrom (PValue value, Ptr spec); template static PValue Create (void); From 203ed97c0d42148816d5481439fdb93c16693b43 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 15 Feb 2008 05:38:29 +0100 Subject: [PATCH 052/130] macro-ify --- src/common/data-rate.cc | 11 ++--------- src/common/data-rate.h | 38 ++++--------------------------------- src/core/value-helper.h | 38 +++++++++++++++++++++++++++++++++++++ src/core/wscript | 1 + src/mobility/rectangle.cc | 9 +-------- src/mobility/rectangle.h | 33 +++----------------------------- src/mobility/vector.cc | 12 ++---------- src/mobility/vector.h | 36 +++-------------------------------- src/node/address.h | 40 ++++----------------------------------- src/node/mac48-address.cc | 12 ++---------- src/node/mac48-address.h | 36 ++++------------------------------- 11 files changed, 64 insertions(+), 202 deletions(-) create mode 100644 src/core/value-helper.h diff --git a/src/common/data-rate.cc b/src/common/data-rate.cc index ff4e7230e..a4b5282de 100644 --- a/src/common/data-rate.cc +++ b/src/common/data-rate.cc @@ -125,6 +125,8 @@ DoParse (const std::string s, uint64_t *v) namespace ns3 { +VALUE_HELPER_CPP (DataRate); + DataRate::DataRate () : m_bps (0) {} @@ -173,15 +175,6 @@ uint64_t DataRate::GetBitRate() const return m_bps; } -DataRate::DataRate (PValue value) -{ - *this = ClassValueHelperExtractFrom (value); -} -DataRate::operator PValue () const -{ - return ClassValueHelperConvertTo (this); -} - DataRate MakeDataRate (std::string rate) { uint64_t bps; diff --git a/src/common/data-rate.h b/src/common/data-rate.h index af2653d5c..fc596431c 100644 --- a/src/common/data-rate.h +++ b/src/common/data-rate.h @@ -28,7 +28,7 @@ #include "ns3/nstime.h" #include "ns3/default-value.h" #include "ns3/value.h" -#include "ns3/class-value-helper.h" +#include "ns3/value-helper.h" namespace ns3 { @@ -80,9 +80,8 @@ public: * \return The underlying bitrate in bits per second */ uint64_t GetBitRate() const; - - DataRate (PValue value); - operator PValue () const; + + VALUE_HELPER_HEADER_1 (DataRate); private: uint64_t m_bps; static uint64_t Parse(const std::string); @@ -93,16 +92,7 @@ DataRate MakeDataRate (std::string rate); std::ostream &operator << (std::ostream &os, const DataRate &rate); std::istream &operator >> (std::istream &is, DataRate &rate); -class DataRateValue : public Value {}; -class DataRateParamSpec : public ParamSpec {}; - -template -Ptr -MakeDataRateParamSpec (T1 a1, DataRate initialValue); - -template -Ptr -MakeDataRateParamSpec (T1 a1, T2 a2, DataRate initialValue); +VALUE_HELPER_HEADER_2 (DataRate); /** * \param lhs @@ -130,24 +120,4 @@ private: } //namespace ns3 -namespace ns3 { - -template -Ptr -MakeDataRateParamSpec (T1 a1, DataRate initialValue) -{ - return MakeClassValueHelperParamSpec - (a1, initialValue); -} - -template -Ptr -MakeDataRateParamSpec (T1 a1, T2 a2, DataRate initialValue) -{ - return MakeClassValueHelperParamSpec - (a1, a2, initialValue); -} - -} // namespace ns3 - #endif /* DATA_RATE_H */ diff --git a/src/core/value-helper.h b/src/core/value-helper.h new file mode 100644 index 000000000..f53fb95ce --- /dev/null +++ b/src/core/value-helper.h @@ -0,0 +1,38 @@ +#ifndef VALUE_HELPER_H +#define VALUE_HELPER_H + +#include "class-value-helper.h" + +#define VALUE_HELPER_HEADER_1(type) \ + type (PValue value); \ + operator PValue () const; + +#define VALUE_HELPER_HEADER_2(type) \ + class type##Value : public Value {}; \ + class type##ParamSpec : public ParamSpec {}; \ + template \ + Ptr Make##type##ParamSpec (T1 a1, \ + type initialValue) \ + { \ + return MakeClassValueHelperParamSpec< type , \ + type##Value, type##ParamSpec> (a1, initialValue); \ + } \ + template \ + Ptr Make##type##ParamSpec (T1 a1, T2 a2, \ + type initialValue) \ + { \ + return MakeClassValueHelperParamSpec (a1, a2, initialValue); \ + } + +#define VALUE_HELPER_CPP(type) \ + type::type (PValue value) \ + { \ + *this = ClassValueHelperExtractFrom (value); \ + } \ + type::operator PValue () const \ + { \ + return ClassValueHelperConvertTo (this); \ + } + +#endif /* VALUE_HELPER_H */ diff --git a/src/core/wscript b/src/core/wscript index e5cad486e..d1c8e55a1 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -114,5 +114,6 @@ def build(bld): 'enum-value.h', 'object-factory.h', 'class-value-helper.h', + 'value-helper.h', ] diff --git a/src/mobility/rectangle.cc b/src/mobility/rectangle.cc index 7fa815661..fcbea44db 100644 --- a/src/mobility/rectangle.cc +++ b/src/mobility/rectangle.cc @@ -119,14 +119,7 @@ Rectangle::CalculateIntersection (const Vector ¤t, const Vector &speed) co } -Rectangle::Rectangle (PValue value) -{ - *this = ClassValueHelperExtractFrom (value); -} -Rectangle::operator PValue () const -{ - return ClassValueHelperConvertTo (this); -} +VALUE_HELPER_CPP (Rectangle); std::ostream & operator << (std::ostream &os, const Rectangle &rectangle) diff --git a/src/mobility/rectangle.h b/src/mobility/rectangle.h index b4f4386af..29926ee84 100644 --- a/src/mobility/rectangle.h +++ b/src/mobility/rectangle.h @@ -21,7 +21,7 @@ #define RECTANGLE_H #include "ns3/value.h" -#include "ns3/class-value-helper.h" +#include "ns3/value-helper.h" namespace ns3 { @@ -62,40 +62,13 @@ public: double yMin; double yMax; - Rectangle (PValue value); - operator PValue () const; + VALUE_HELPER_HEADER_1 (Rectangle); }; std::ostream &operator << (std::ostream &os, const Rectangle &rectangle); std::istream &operator >> (std::istream &is, Rectangle &rectangle); - -class RectangleValue : public Value {}; -class RectangleParamSpec : public ParamSpec {}; - -template -Ptr MakeRectangleParamSpec (T1 a1, - Rectangle initialValue); -template -Ptr MakeRectangleParamSpec (T1 a1, T2 a2, - Rectangle initialValue); - -} // namespace ns3 - -namespace ns3 { - -template -Ptr MakeRectangleParamSpec (T1 a1, - Rectangle initialValue) -{ - return MakeClassValueHelperParamSpec (a1, initialValue); -} -template -Ptr MakeRectangleParamSpec (T1 a1, T2 a2, - Rectangle initialValue) -{ - return MakeClassValueHelperParamSpec (a1, a2, initialValue); -} +VALUE_HELPER_HEADER_2 (Rectangle); } // namespace ns3 diff --git a/src/mobility/vector.cc b/src/mobility/vector.cc index c44d5ca3c..7027aeb82 100644 --- a/src/mobility/vector.cc +++ b/src/mobility/vector.cc @@ -24,6 +24,8 @@ namespace ns3 { +VALUE_HELPER_CPP (Vector); + Vector::Vector (double _x, double _y, double _z) : x (_x), @@ -37,16 +39,6 @@ Vector::Vector () z (0.0) {} -Vector::Vector (PValue value) -{ - *this = ClassValueHelperExtractFrom (value); -} -Vector::operator PValue () const -{ - return ClassValueHelperConvertTo (this); -} - - double CalculateDistance (const Vector &a, const Vector &b) { diff --git a/src/mobility/vector.h b/src/mobility/vector.h index 118448337..b0e50337a 100644 --- a/src/mobility/vector.h +++ b/src/mobility/vector.h @@ -21,7 +21,7 @@ #define VECTOR_H #include "ns3/value.h" -#include "ns3/class-value-helper.h" +#include "ns3/value-helper.h" namespace ns3 { @@ -58,46 +58,16 @@ public: */ double z; - Vector (PValue value); - operator PValue () const; + VALUE_HELPER_HEADER_1 (Vector); }; double CalculateDistance (const Vector &a, const Vector &b); -class VectorValue : public Value {}; -class VectorParamSpec : public ParamSpec {}; +VALUE_HELPER_HEADER_2 (Vector); std::ostream &operator << (std::ostream &os, const Vector &vector); std::istream &operator >> (std::istream &is, Vector &vector); -template -Ptr -MakeVectorParamSpec (T1 a1, const Vector &initialValue); - -template -Ptr -MakeVectorParamSpec (T1 a1, T2 a2, - const Vector &initialValue); - -} // namespace ns3 - -namespace ns3 { - -template -Ptr -MakeVectorParamSpec (T1 a1, const Vector &initialValue) -{ - return MakeClassValueHelperParamSpec (a1, initialValue); -} - -template -Ptr -MakeVectorParamSpec (T1 a1, T2 a2, - const Vector &initialValue) -{ - return MakeClassValueHelperParamSpec (a1, a2, initialValue); -} - } // namespace ns3 #endif /* VECTOR_H */ diff --git a/src/node/address.h b/src/node/address.h index 911bda30d..6d23490ec 100644 --- a/src/node/address.h +++ b/src/node/address.h @@ -4,7 +4,7 @@ #include #include #include "ns3/value.h" -#include "ns3/class-value-helper.h" +#include "ns3/value-helper.h" namespace ns3 { @@ -154,8 +154,7 @@ public: */ static uint8_t Register (void); - Address (PValue value); - operator PValue () const; + VALUE_HELPER_HEADER_1 (Address); private: friend bool operator == (const Address &a, const Address &b); friend bool operator < (const Address &a, const Address &b); @@ -166,46 +165,15 @@ private: uint8_t m_data[MAX_SIZE]; }; +VALUE_HELPER_HEADER_2 (Address); + bool operator == (const Address &a, const Address &b); bool operator != (const Address &a, const Address &b); bool operator < (const Address &a, const Address &b); std::ostream& operator<< (std::ostream& os, const Address & address); std::istream& operator>> (std::istream& is, Address & address); -class AddressValue : public Value {}; -class AddressParamSpec : public ParamSpec {}; - -template -Ptr -MakeAddressParamSpec (T1 a1, Address initialValue); - -template -Ptr -MakeAddressParamSpec (T1 a1, T2 a2, Address initialValue); } // namespace ns3 -namespace ns3 { - -template -Ptr -MakeAddressParamSpec (T1 a1, Address initialValue) -{ - return MakeClassValueHelperParamSpec - (a1, initialValue); -} - -template -Ptr -MakeAddressParamSpec (T1 a1, T2 a2, Address initialValue) -{ - return MakeClassValueHelperParamSpec - (a1, a2, initialValue); -} - - - -} // namespace ns3 - - #endif /* ADDRESS_H */ diff --git a/src/node/mac48-address.cc b/src/node/mac48-address.cc index 0c4013da1..12c664b38 100644 --- a/src/node/mac48-address.cc +++ b/src/node/mac48-address.cc @@ -25,6 +25,8 @@ namespace ns3 { +VALUE_HELPER_CPP (Mac48Address); + #define ASCII_a (0x41) #define ASCII_z (0x5a) #define ASCII_A (0x61) @@ -150,16 +152,6 @@ Mac48Address::GetBroadcast (void) return broadcast; } -Mac48Address::Mac48Address (PValue value) -{ - *this = ClassValueHelperExtractFrom (value); -} -Mac48Address::operator PValue () const -{ - return ClassValueHelperConvertTo (this); -} - - bool operator == (const Mac48Address &a, const Mac48Address &b) { return memcmp (a.m_address, b.m_address, 6) == 0; diff --git a/src/node/mac48-address.h b/src/node/mac48-address.h index cac30976d..a427493ed 100644 --- a/src/node/mac48-address.h +++ b/src/node/mac48-address.h @@ -23,7 +23,7 @@ #include #include #include "ns3/value.h" -#include "ns3/class-value-helper.h" +#include "ns3/value-helper.h" namespace ns3 { @@ -96,8 +96,7 @@ public: */ static Mac48Address GetBroadcast (void); - Mac48Address (PValue value); - operator PValue () const; + VALUE_HELPER_HEADER_1 (Mac48Address); private: /** * \returns a new Address instance @@ -112,41 +111,14 @@ private: uint8_t m_address[6]; }; +VALUE_HELPER_HEADER_2 (Mac48Address); + bool operator == (const Mac48Address &a, const Mac48Address &b); bool operator != (const Mac48Address &a, const Mac48Address &b); bool operator < (const Mac48Address &a, const Mac48Address &b); std::ostream& operator<< (std::ostream& os, const Mac48Address & address); std::istream& operator>> (std::istream& is, const Mac48Address & address); -class Mac48AddressValue : public Value {}; -class Mac48AddressParamSpec : public ParamSpec {}; - -template -Ptr -MakeMac48AddressParamSpec (T1 a1, Mac48Address address); - -template -Ptr -MakeMac48AddressParamSpec (T1 a1, T2 a2, Mac48Address address); - -} // namespace ns3 - -namespace ns3 { - -template -Ptr -MakeMac48AddressParamSpec (T1 a1, Mac48Address address) -{ - return MakeClassValueHelperParamSpec (a1, address); -} - -template -Ptr -MakeMac48AddressParamSpec (T1 a1, T2 a2, Mac48Address address) -{ - return MakeClassValueHelperParamSpec (a1, a2, address); -} - } // namespace ns3 #endif /* MAC48_ADDRESS_H */ From 00d00b52313886df7af01cbc9c4a93da0962f66e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 17 Feb 2008 00:46:24 +0100 Subject: [PATCH 053/130] get rid of ParamSpec::CreateValue. --- src/core/enum-value.cc | 8 -------- src/core/enum-value.h | 1 - src/core/object-vector.cc | 6 ------ src/core/object-vector.h | 1 - src/core/object.cc | 10 +++++----- src/core/param-spec-helper.h | 5 ----- src/core/value.h | 4 ---- 7 files changed, 5 insertions(+), 30 deletions(-) diff --git a/src/core/enum-value.cc b/src/core/enum-value.cc index 8cc1c110a..1701b2d4d 100644 --- a/src/core/enum-value.cc +++ b/src/core/enum-value.cc @@ -134,12 +134,4 @@ EnumParamSpec::GetInitialValue (void) const return PValue::Create (m_valueSet.front ().first); } -PValue -EnumParamSpec::CreateValue (void) const -{ - return PValue::Create (m_valueSet.front ().first); -} - - - } // namespace ns3 diff --git a/src/core/enum-value.h b/src/core/enum-value.h index 65361f819..af3b5ccee 100644 --- a/src/core/enum-value.h +++ b/src/core/enum-value.h @@ -37,7 +37,6 @@ public: virtual bool Check (PValue value) const; virtual PValue GetInitialValue (void) const; - virtual PValue CreateValue (void) const; private: virtual bool DoSet (ObjectBase *object, const EnumValue *value) const = 0; virtual bool DoGet (const ObjectBase *object, EnumValue *value) const = 0; diff --git a/src/core/object-vector.cc b/src/core/object-vector.cc index 3cef5c8bb..9280439a0 100644 --- a/src/core/object-vector.cc +++ b/src/core/object-vector.cc @@ -107,11 +107,5 @@ ObjectVectorParamSpec::GetInitialValue (void) const { return PValue::Create (); } -PValue -ObjectVectorParamSpec::CreateValue (void) const -{ - return PValue::Create (); -} - } // name diff --git a/src/core/object-vector.h b/src/core/object-vector.h index dbd64733c..d7e258f40 100644 --- a/src/core/object-vector.h +++ b/src/core/object-vector.h @@ -69,7 +69,6 @@ public: virtual bool Get (const ObjectBase * object, PValue value) const; virtual bool Check (PValue value) const; virtual PValue GetInitialValue (void) const; - virtual PValue CreateValue (void) const; private: virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const = 0; virtual Ptr DoGet (const ObjectBase *object, uint32_t i) const = 0; diff --git a/src/core/object.cc b/src/core/object.cc index 72a126c51..63e447da4 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -694,7 +694,7 @@ Parameters::DoSet (Ptr spec, PValue value) // attempt to convert to string. std::string str = value.SerializeToString (0); // attempt to convert back to value. - PValue v = spec->CreateValue (); + PValue v = spec->GetInitialValue ().Copy (); ok = v.DeserializeFromString (str, spec); if (!ok) { @@ -792,7 +792,7 @@ Parameters::DeserializeFromString (std::string str) value = str.substr (equal+1, next - (equal+1)); cur++; } - PValue val = info.spec->CreateValue (); + PValue val = info.spec->GetInitialValue ().Copy (); bool ok = val.DeserializeFromString (value, info.spec); if (!ok) { @@ -915,7 +915,7 @@ Object::DoSet (Ptr spec, PValue value) // attempt to convert to string std::string str = value.SerializeToString (0); // attempt to convert back from string. - PValue v = spec->CreateValue (); + PValue v = spec->GetInitialValue ().Copy (); ok = v.DeserializeFromString (str, spec); if (!ok) { @@ -978,7 +978,7 @@ Object::Get (std::string name, std::string &value) const { return false; } - PValue v = info.spec->CreateValue (); + PValue v = info.spec->GetInitialValue ().Copy (); bool ok = info.spec->Get (this, v); if (ok) { @@ -999,7 +999,7 @@ Object::Get (std::string name) const { return PValue (); } - PValue value = info.spec->CreateValue (); + PValue value = info.spec->GetInitialValue ().Copy (); bool ok = info.spec->Get (this, value); if (!ok) { diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index 696db23f1..31730eecf 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -78,11 +78,6 @@ public: return m_initialValue; } - virtual PValue CreateValue (void) const { - return m_initialValue.Copy (); - } - - private: virtual bool DoSet (T *object, const U *v) const = 0; virtual bool DoGet (const T *object, U *v) const = 0; diff --git a/src/core/value.h b/src/core/value.h index b2a97b7ed..38754ff70 100644 --- a/src/core/value.h +++ b/src/core/value.h @@ -77,7 +77,6 @@ public: virtual bool Get (const ObjectBase * object, PValue value) const = 0; virtual bool Check (PValue value) const = 0; virtual PValue GetInitialValue (void) const = 0; - virtual PValue CreateValue (void) const = 0; private: mutable uint32_t m_count; }; @@ -273,9 +272,6 @@ public: return true; } virtual PValue GetInitialValue (void) const { - return CreateValue (); - } - virtual PValue CreateValue (void) const { return PValue::Create > (Ptr (0)); } private: From 97b26adb64e4bf2dbf7cbe50f959cbfd1c92c6fd Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 17 Feb 2008 04:38:34 +0100 Subject: [PATCH 054/130] split initial value from ParamSpec. --- src/applications/onoff/onoff-application.cc | 2 +- src/core/boolean-value.h | 12 +- src/core/class-value-helper.h | 8 +- src/core/enum-value.cc | 7 - src/core/enum-value.h | 1 - src/core/fp-value.h | 20 +-- src/core/int-value.h | 24 ++-- src/core/object-vector.cc | 10 +- src/core/object-vector.h | 2 +- src/core/object.cc | 81 +++++++---- src/core/object.h | 12 +- src/core/param-spec-helper.h | 127 ++++++++---------- src/core/random-variable.h | 16 +-- src/core/uint-value.h | 24 ++-- src/core/value-helper.h | 10 +- src/core/value-test.cc | 33 +++-- src/core/value.h | 4 - .../point-to-point-net-device.cc | 2 +- src/mobility/hierarchical-mobility-model.cc | 2 + src/mobility/mobility-model.cc | 9 +- src/mobility/position-allocator.cc | 38 +++--- .../random-direction-2d-mobility-model.cc | 12 +- src/mobility/random-walk-2d-mobility-model.cc | 21 +-- .../random-waypoint-mobility-model.cc | 9 +- src/node/drop-tail-queue.cc | 40 +++--- src/simulator/nstime.h | 16 +-- 26 files changed, 269 insertions(+), 273 deletions(-) diff --git a/src/applications/onoff/onoff-application.cc b/src/applications/onoff/onoff-application.cc index d6adf0231..04a2ea336 100644 --- a/src/applications/onoff/onoff-application.cc +++ b/src/applications/onoff/onoff-application.cc @@ -45,7 +45,7 @@ namespace ns3 { // Defaults for rate/size static DataRateDefaultValue g_defaultRate ("OnOffApplicationDataRate", "The data rate in on state for OnOffApplication", - DataRate ("500kb/s")); + MakeDataRate ("500kb/s")); static NumericDefaultValue g_defaultSize ("OnOffApplicationPacketSize", "The size of packets sent in on state for OnOffApplication", 512, 1); diff --git a/src/core/boolean-value.h b/src/core/boolean-value.h index d4054fa2d..0c3962d62 100644 --- a/src/core/boolean-value.h +++ b/src/core/boolean-value.h @@ -28,10 +28,10 @@ class BooleanParamSpec : public ParamSpec {}; template -Ptr MakeBooleanParamSpec (T1 a1, bool initialValue); +Ptr MakeBooleanParamSpec (T1 a1); template -Ptr MakeBooleanParamSpec (T1 a1, T2 a2, bool initialValue); +Ptr MakeBooleanParamSpec (T1 a1, T2 a2); } // namespace ns3 @@ -40,15 +40,15 @@ Ptr MakeBooleanParamSpec (T1 a1, T2 a2, bool initialValue); namespace ns3 { template -Ptr MakeBooleanParamSpec (T1 a1, bool initialValue) +Ptr MakeBooleanParamSpec (T1 a1) { - return MakeParamSpecHelper (a1, BooleanValue (initialValue)); + return MakeParamSpecHelper (a1); } template -Ptr MakeBooleanParamSpec (T1 a1, T2 a2, bool initialValue) +Ptr MakeBooleanParamSpec (T1 a1, T2 a2) { - return MakeParamSpecHelper (a1, a2, BooleanValue (initialValue)); + return MakeParamSpecHelper (a1, a2); } diff --git a/src/core/class-value-helper.h b/src/core/class-value-helper.h index 3ad76e81e..bbf9295b4 100644 --- a/src/core/class-value-helper.h +++ b/src/core/class-value-helper.h @@ -78,17 +78,17 @@ ClassValueHelperConvertTo (const T *self) template Ptr -MakeClassValueHelperParamSpec (T1 a1, const T &initialValue) +MakeClassValueHelperParamSpec (T1 a1) { - return MakeParamSpecHelper (a1, ClassValue (initialValue)); + return MakeParamSpecHelper > (a1); } template Ptr -MakeClassValueHelperParamSpec (T1 a1, T2 a2, const T &initialValue) +MakeClassValueHelperParamSpec (T1 a1, T2 a2) { - return MakeParamSpecHelper (a1, a2, ClassValue (initialValue)); + return MakeParamSpecHelper > (a1, a2); } } // namespace ns3 diff --git a/src/core/enum-value.cc b/src/core/enum-value.cc index 1701b2d4d..f4959da38 100644 --- a/src/core/enum-value.cc +++ b/src/core/enum-value.cc @@ -127,11 +127,4 @@ EnumParamSpec::Check (PValue value) const return false; } -PValue -EnumParamSpec::GetInitialValue (void) const -{ - NS_ASSERT (!m_valueSet.empty ()); - return PValue::Create (m_valueSet.front ().first); -} - } // namespace ns3 diff --git a/src/core/enum-value.h b/src/core/enum-value.h index af3b5ccee..98ed30d2d 100644 --- a/src/core/enum-value.h +++ b/src/core/enum-value.h @@ -36,7 +36,6 @@ public: virtual bool Get (const ObjectBase* object, PValue value) const; virtual bool Check (PValue value) const; - virtual PValue GetInitialValue (void) const; private: virtual bool DoSet (ObjectBase *object, const EnumValue *value) const = 0; virtual bool DoGet (const ObjectBase *object, EnumValue *value) const = 0; diff --git a/src/core/fp-value.h b/src/core/fp-value.h index d733b06b9..8bcd89b53 100644 --- a/src/core/fp-value.h +++ b/src/core/fp-value.h @@ -28,11 +28,9 @@ private: class FpParamSpec : public ParamSpec {}; template -Ptr MakeFpParamSpec (T1 a1, - double initialValue); +Ptr MakeFpParamSpec (T1 a1); template Ptr MakeFpParamSpec (T1 a1, T2 a2, - double initialValue, double minValue, double maxValue); @@ -68,36 +66,32 @@ private: template Ptr -MakeFpParamSpec (T1 a1, - double initialValue) +MakeFpParamSpec (T1 a1) { - return MakeParamSpecHelperWithChecker (a1, FpValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, FpValueChecker::Create (a1)); } template Ptr MakeFpParamSpec (T1 a1, - double initialValue, double minValue, double maxValue) { - return MakeParamSpecHelperWithChecker (a1, FpValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, FpValueChecker (minValue, maxValue)); } template -Ptr MakeFpParamSpec (T1 a1, T2 a2, - double initialValue) +Ptr MakeFpParamSpec (T1 a1, T2 a2) { - return MakeParamSpecHelperWithChecker (a1, a2, FpValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, a2, FpValueChecker::Create (a1)); } template Ptr MakeFpParamSpec (T1 a1, T2 a2, - double initialValue, double minValue, double maxValue) { - return MakeParamSpecHelperWithChecker (a1, a2, FpValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, a2, FpValueChecker (minValue, maxValue)); } diff --git a/src/core/int-value.h b/src/core/int-value.h index 2281336e0..d9c3de429 100644 --- a/src/core/int-value.h +++ b/src/core/int-value.h @@ -27,19 +27,15 @@ private: class IntParamSpec : public ParamSpec {}; template -Ptr MakeIntParamSpec (T1 a1, - int64_t initialValue); +Ptr MakeIntParamSpec (T1 a1); template Ptr MakeIntParamSpec (T1 a1, - int64_t initialValue, int64_t minValue, int64_t maxValue); template -Ptr MakeIntParamSpec (T1 a1, T2 a2, - int64_t initialValue); +Ptr MakeIntParamSpec (T1 a1, T2 a2); template Ptr MakeIntParamSpec (T1 a1, T2 a2, - int64_t initialValue, int64_t minValue, int64_t maxValue); @@ -75,36 +71,32 @@ private: template Ptr -MakeIntParamSpec (T1 a1, - int64_t initialValue) +MakeIntParamSpec (T1 a1) { - return MakeParamSpecHelperWithChecker (a1, IntValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, IntValueChecker::Create (a1)); } template Ptr MakeIntParamSpec (T1 a1, - int64_t initialValue, int64_t minValue, int64_t maxValue) { - return MakeParamSpecHelperWithChecker (a1, IntValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, IntValueChecker (minValue, maxValue)); } template -Ptr MakeIntParamSpec (T1 a1, T2 a2, - int64_t initialValue) +Ptr MakeIntParamSpec (T1 a1, T2 a2) { - return MakeParamSpecHelperWithChecker (a1, a2, IntValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, a2, IntValueChecker::Create (a1)); } template Ptr MakeIntParamSpec (T1 a1, T2 a2, - int64_t initialValue, int64_t minValue, int64_t maxValue) { - return MakeParamSpecHelperWithChecker (a1, a2, IntValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, a2, IntValueChecker (minValue, maxValue)); } diff --git a/src/core/object-vector.cc b/src/core/object-vector.cc index 9280439a0..0137136be 100644 --- a/src/core/object-vector.cc +++ b/src/core/object-vector.cc @@ -36,6 +36,11 @@ ObjectVector::ObjectVector (PValue value) *this = v->Get (); } +ObjectVector::operator PValue () const +{ + return PValue::Create (); +} + ObjectVectorValue::ObjectVectorValue () : m_vector () {} @@ -102,10 +107,5 @@ ObjectVectorParamSpec::Check (PValue value) const const ObjectVectorValue *v = value.DynCast (); return v != 0; } -PValue -ObjectVectorParamSpec::GetInitialValue (void) const -{ - return PValue::Create (); -} } // name diff --git a/src/core/object-vector.h b/src/core/object-vector.h index d7e258f40..b204b6fe4 100644 --- a/src/core/object-vector.h +++ b/src/core/object-vector.h @@ -21,6 +21,7 @@ public: Ptr Get (uint32_t i) const; ObjectVector (PValue value); + operator PValue () const; private: friend class ObjectVectorParamSpec; std::vector > m_objects; @@ -68,7 +69,6 @@ public: virtual bool Set (ObjectBase * object, PValue value) const; virtual bool Get (const ObjectBase * object, PValue value) const; virtual bool Check (PValue value) const; - virtual PValue GetInitialValue (void) const; private: virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const = 0; virtual Ptr DoGet (const ObjectBase *object, uint32_t i) const = 0; diff --git a/src/core/object.cc b/src/core/object.cc index 63e447da4..2722c2a4e 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -61,11 +61,12 @@ public: std::string name, std::string help, uint32_t flags, + ns3::PValue initialValue, ns3::Ptr spec); uint32_t GetParametersN (uint16_t uid) const; std::string GetParameterName (uint16_t uid, uint32_t i) const; uint32_t GetParameterFlags (uint16_t uid, uint32_t i) const; - uint32_t GetParameterUid (uint16_t uid, uint32_t i) const; + ns3::PValue GetParameterInitialValue (uint16_t uid, uint32_t i) const; ns3::Ptr GetParameterParamSpec (uint16_t uid, uint32_t i) const; private: struct ConstructorInformation { @@ -76,7 +77,7 @@ private: std::string name; std::string help; uint32_t flags; - uint32_t uid; + ns3::PValue initialValue; ns3::Ptr param; }; struct IidInformation { @@ -246,6 +247,7 @@ IidManager::AddParameter (uint16_t uid, std::string name, std::string help, uint32_t flags, + ns3::PValue initialValue, ns3::Ptr spec) { struct IidInformation *information = LookupInformation (uid); @@ -262,6 +264,7 @@ IidManager::AddParameter (uint16_t uid, param.name = name; param.help = help; param.flags = flags; + param.initialValue = initialValue; param.param = spec; information->parameters.push_back (param); } @@ -287,6 +290,13 @@ IidManager::GetParameterFlags (uint16_t uid, uint32_t i) const NS_ASSERT (i < information->parameters.size ()); return information->parameters[i].flags; } +ns3::PValue +IidManager::GetParameterInitialValue (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->parameters.size ()); + return information->parameters[i].initialValue; +} ns3::Ptr IidManager::GetParameterParamSpec (uint16_t uid, uint32_t i) const { @@ -295,7 +305,6 @@ IidManager::GetParameterParamSpec (uint16_t uid, uint32_t i) const return information->parameters[i].param; } - } // anonymous namespace /********************************************************************* @@ -431,6 +440,7 @@ TypeId::LookupParameterByName (std::string name, struct TypeId::ParameterInfo *i { info->spec = GetParameterParamSpec (i); info->flags = GetParameterFlags (i); + info->initialValue = tid.GetParameterInitialValue (i); return true; } } @@ -452,6 +462,7 @@ TypeId::LookupParameterByPosition (uint32_t i, struct TypeId::ParameterInfo *inf { info->spec = tid.GetParameterParamSpec (j); info->flags = tid.GetParameterFlags (j); + info->initialValue = tid.GetParameterInitialValue (j); return true; } cur++; @@ -522,9 +533,10 @@ TypeId::DoAddConstructor (CallbackBase cb, uint32_t nArguments) TypeId TypeId::AddParameter (std::string name, std::string help, + PValue initialValue, Ptr param) { - Singleton::Get ()->AddParameter (m_tid, name, help, PARAM_SGC, param); + Singleton::Get ()->AddParameter (m_tid, name, help, PARAM_SGC, initialValue, param); return *this; } @@ -532,9 +544,10 @@ TypeId TypeId::AddParameter (std::string name, std::string help, uint32_t flags, + PValue initialValue, Ptr param) { - Singleton::Get ()->AddParameter (m_tid, name, help, flags, param); + Singleton::Get ()->AddParameter (m_tid, name, help, flags, initialValue, param); return *this; } @@ -578,6 +591,12 @@ TypeId::GetParameterFullName (uint32_t i) const { return GetName () + "::" + GetParameterName (i); } +PValue +TypeId::GetParameterInitialValue (uint32_t i) const +{ + PValue value = Singleton::Get ()->GetParameterInitialValue (m_tid, i); + return value; +} Ptr TypeId::GetParameterParamSpec (uint32_t i) const { @@ -644,7 +663,7 @@ Parameters::Set (std::string name, PValue value) { struct TypeId::ParameterInfo info; TypeId::LookupParameterByFullName (name, &info); - bool ok = DoSet (info.spec, value); + bool ok = DoSet (&info, value); return ok; } void @@ -652,14 +671,14 @@ Parameters::SetWithTid (TypeId tid, std::string name, PValue value) { struct TypeId::ParameterInfo info; tid.LookupParameterByName (name, &info); - DoSet (info.spec, value); + DoSet (&info, value); } void Parameters::SetWithTid (TypeId tid, uint32_t position, PValue value) { struct TypeId::ParameterInfo info; tid.LookupParameterByPosition (position, &info); - DoSet (info.spec, value); + DoSet (&info, value); } void @@ -682,32 +701,36 @@ Parameters::DoSetOne (Ptr spec, PValue value) m_parameters.push_back (p); } bool -Parameters::DoSet (Ptr spec, PValue value) +Parameters::DoSet (struct TypeId::ParameterInfo *info, PValue value) { - if (spec == 0) + if (info->spec == 0) { return false; } - bool ok = spec->Check (value); + bool ok = info->spec->Check (value); if (!ok) { // attempt to convert to string. - std::string str = value.SerializeToString (0); + const StringValue *str = value.DynCast (); + if (str == 0) + { + return false; + } // attempt to convert back to value. - PValue v = spec->GetInitialValue ().Copy (); - ok = v.DeserializeFromString (str, spec); + PValue v = info->initialValue.Copy (); + ok = v.DeserializeFromString (str->Get (), info->spec); if (!ok) { return false; } - ok = spec->Check (v); + ok = info->spec->Check (v); if (!ok) { return false; } value = v; } - DoSetOne (spec, value); + DoSetOne (info->spec, value); return true; } void @@ -792,7 +815,7 @@ Parameters::DeserializeFromString (std::string str) value = str.substr (equal+1, next - (equal+1)); cur++; } - PValue val = info.spec->GetInitialValue ().Copy (); + PValue val = info.initialValue.Copy (); bool ok = val.DeserializeFromString (value, info.spec); if (!ok) { @@ -855,6 +878,7 @@ Object::Construct (const Parameters ¶meters) for (uint32_t i = 0; i < tid.GetParametersN (); i++) { Ptr paramSpec = tid.GetParameterParamSpec (i); + PValue initial = tid.GetParameterInitialValue (i); NS_LOG_DEBUG ("try to construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\""); if (!(tid.GetParameterFlags (i) & TypeId::PARAM_CONSTRUCT)) @@ -869,7 +893,7 @@ Object::Construct (const Parameters ¶meters) if (j->spec == paramSpec) { // We have a matching parameter value. - DoSet (paramSpec, j->value); + DoSet (paramSpec, initial, j->value); NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\""); found = true; @@ -885,7 +909,7 @@ Object::Construct (const Parameters ¶meters) if (j->spec == paramSpec) { // We have a matching parameter value. - DoSet (paramSpec, j->value); + DoSet (paramSpec, initial, j->value); NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\" from global"); found = true; @@ -896,7 +920,6 @@ Object::Construct (const Parameters ¶meters) if (!found) { // No matching parameter value so we set the default value. - PValue initial = paramSpec->GetInitialValue (); paramSpec->Set (this, initial); NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\" from local"); @@ -907,16 +930,20 @@ Object::Construct (const Parameters ¶meters) NotifyConstructionCompleted (); } bool -Object::DoSet (Ptr spec, PValue value) +Object::DoSet (Ptr spec, PValue initialValue, PValue value) { bool ok = spec->Check (value); if (!ok) { // attempt to convert to string - std::string str = value.SerializeToString (0); + const StringValue *str = value.DynCast (); + if (str == 0) + { + return false; + } // attempt to convert back from string. - PValue v = spec->GetInitialValue ().Copy (); - ok = v.DeserializeFromString (str, spec); + PValue v = initialValue.Copy (); + ok = v.DeserializeFromString (str->Get (), spec); if (!ok) { return false; @@ -943,7 +970,7 @@ Object::Set (std::string name, PValue value) { return false; } - return DoSet (info.spec, value); + return DoSet (info.spec, info.initialValue, value); } bool Object::SetUint (std::string name, uint64_t value) @@ -978,7 +1005,7 @@ Object::Get (std::string name, std::string &value) const { return false; } - PValue v = info.spec->GetInitialValue ().Copy (); + PValue v = info.initialValue.Copy (); bool ok = info.spec->Get (this, v); if (ok) { @@ -999,7 +1026,7 @@ Object::Get (std::string name) const { return PValue (); } - PValue value = info.spec->GetInitialValue ().Copy (); + PValue value = info.initialValue.Copy (); bool ok = info.spec->Get (this, value); if (!ok) { diff --git a/src/core/object.h b/src/core/object.h index 8b8c2fb7c..af9b58607 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -141,6 +141,8 @@ public: */ std::string GetParameterFullName (uint32_t i) const; + PValue GetParameterInitialValue (uint32_t i) const; + Ptr CreateObject (const Parameters ¶meters) const; @@ -209,7 +211,9 @@ public: * Record in this TypeId the fact that a new parameter exists. */ TypeId AddParameter (std::string name, - std::string help, Ptr spec); + std::string help, + PValue initialValue, + Ptr spec); /** * \param name the name of the new parameter @@ -224,6 +228,7 @@ public: TypeId AddParameter (std::string name, std::string help, uint32_t flags, + PValue initialValue, Ptr spec); // construct an invalid TypeId. @@ -237,6 +242,7 @@ private: struct ParameterInfo { Ptr spec; + PValue initialValue; uint32_t flags; }; @@ -323,7 +329,7 @@ private: - bool DoSet (Ptr spec, PValue param); + bool DoSet (struct TypeId::ParameterInfo *info, PValue param); void DoSetOne (Ptr spec, PValue param); std::string LookupParameterFullNameByParamSpec (Ptr spec) const; @@ -474,7 +480,7 @@ private: friend Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7); - bool DoSet (Ptr spec, PValue value); + bool DoSet (Ptr spec, PValue intialValue, PValue value); Ptr DoGetObject (TypeId tid) const; void DoCollectSources (std::string path, const TraceContext &context, TraceResolver::SourceCollection *collection) const; diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index 31730eecf..edb76856d 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -3,21 +3,21 @@ namespace ns3 { -template +template Ptr -MakeParamSpecHelper (T1 a1, V initialValue); +MakeParamSpecHelper (T1 a1); -template +template Ptr -MakeParamSpecHelper (T1 a1, T2 a2, V initialValue); +MakeParamSpecHelper (T1 a1, T2 a2); -template +template Ptr -MakeParamSpecHelperWithChecker (T1 a1, V initialValue, CHECKER checker); +MakeParamSpecHelperWithChecker (T1 a1, CHECKER checker); -template +template Ptr -MakeParamSpecHelperWithChecker (T1 a1, T2 a2, V initialValue, CHECKER checker); +MakeParamSpecHelperWithChecker (T1 a1, T2 a2, CHECKER checker); } // namespace ns3 @@ -33,9 +33,8 @@ template class ParamSpecHelper : public BASE { public: - ParamSpecHelper (PValue initialValue, CHECKER checker) - : m_initialValue (initialValue), - m_checker (checker) {} + ParamSpecHelper (CHECKER checker) + : m_checker (checker) {} virtual bool Set (ObjectBase * object, PValue val) const { const U *value = val.DynCast (); @@ -74,14 +73,10 @@ public: return m_checker.Check (val->Get ()); } - virtual PValue GetInitialValue (void) const { - return m_initialValue; - } private: virtual bool DoSet (T *object, const U *v) const = 0; virtual bool DoGet (const T *object, U *v) const = 0; - PValue m_initialValue; CHECKER m_checker; }; @@ -92,15 +87,15 @@ public: bool Check (const T &value) const {return true;} }; -template +template Ptr -DoMakeParamSpecHelperOne (U T::*memberVariable, V initialValue, CHECKER checker) +DoMakeParamSpecHelperOne (U T::*memberVariable, CHECKER checker) { class MemberVariable : public ParamSpecHelper { public: - MemberVariable (U T::*memberVariable, V initialValue, CHECKER checker) - : ParamSpecHelper (initialValue, checker), + MemberVariable (U T::*memberVariable, CHECKER checker) + : ParamSpecHelper (checker), m_memberVariable (memberVariable) {} private: @@ -115,27 +110,27 @@ DoMakeParamSpecHelperOne (U T::*memberVariable, V initialValue, CHECKER checker) U T::*m_memberVariable; }; - return Ptr (new MemberVariable (memberVariable, initialValue, checker), false); + return Ptr (new MemberVariable (memberVariable, checker), false); } -template +template Ptr -DoMakeParamSpecHelperOne (U T::*memberVariable, V initialValue) +DoMakeParamSpecHelperOne (U T::*memberVariable) { - return DoMakeParamSpecHelperOne (memberVariable, initialValue, ParamSpecHelperSimpleChecker ()); + return DoMakeParamSpecHelperOne (memberVariable, ParamSpecHelperSimpleChecker ()); } -template +template Ptr DoMakeParamSpecHelperOne (U (T::*getter) (void) const, - V initialValue, CHECKER checker) + CHECKER checker) { class MemberMethod : public ParamSpecHelper { public: MemberMethod (U (T::*getter) (void) const, - V initialValue, CHECKER checker) - : ParamSpecHelper (initialValue, checker), + CHECKER checker) + : ParamSpecHelper (checker), m_getter (getter) {} private: @@ -148,30 +143,29 @@ DoMakeParamSpecHelperOne (U (T::*getter) (void) const, } U (T::*m_getter) (void) const; }; - return Ptr (new MemberMethod (getter, initialValue, checker), false); + return Ptr (new MemberMethod (getter, checker), false); } -template +template Ptr -DoMakeParamSpecHelperOne (U (T::*getter) (void) const, - V initialValue) +DoMakeParamSpecHelperOne (U (T::*getter) (void) const) { - return DoMakeParamSpecHelperOne (getter, initialValue, ParamSpecHelperSimpleChecker ()); + return DoMakeParamSpecHelperOne (getter, ParamSpecHelperSimpleChecker ()); } -template +template Ptr DoMakeParamSpecHelperOne (void (T::*setter) (U), - V initialValue, CHECKER checker) + CHECKER checker) { class MemberMethod : public ParamSpecHelper { public: MemberMethod (void (T::*setter) (U), - V initialValue, CHECKER checker) - : ParamSpecHelper (initialValue, checker), + CHECKER checker) + : ParamSpecHelper (checker), m_setter (setter) {} private: @@ -184,24 +178,22 @@ DoMakeParamSpecHelperOne (void (T::*setter) (U), } void (T::*m_setter) (U); }; - return Ptr (new MemberMethod (setter, initialValue, checker), false); + return Ptr (new MemberMethod (setter, checker), false); } -template +template Ptr -DoMakeParamSpecHelperOne (void (T::*setter) (U), - V initialValue) +DoMakeParamSpecHelperOne (void (T::*setter) (U)) { - return DoMakeParamSpecHelperOne (setter, initialValue, + return DoMakeParamSpecHelperOne (setter, ParamSpecHelperSimpleChecker::ReferencedType> ()); } -template +template Ptr DoMakeParamSpecHelperTwo (void (T::*setter) (U), V (T::*getter) (void) const, - W initialValue, CHECKER checker = ParamSpecHelperSimpleChecker ()) { class MemberMethod : public ParamSpecHelper @@ -209,8 +201,8 @@ DoMakeParamSpecHelperTwo (void (T::*setter) (U), public: MemberMethod (void (T::*setter) (U), V (T::*getter) (void) const, - W initialValue, CHECKER checker) - : ParamSpecHelper (initialValue, checker), + CHECKER checker) + : ParamSpecHelper (checker), m_setter (setter), m_getter (getter) {} @@ -226,63 +218,60 @@ DoMakeParamSpecHelperTwo (void (T::*setter) (U), void (T::*m_setter) (U); V (T::*m_getter) (void) const; }; - return Ptr (new MemberMethod (setter, getter, initialValue, checker), false); + return Ptr (new MemberMethod (setter, getter, checker), false); } -template +template Ptr DoMakeParamSpecHelperTwo (void (T::*setter) (U), - V (T::*getter) (void) const, - W initialValue) + V (T::*getter) (void) const) { - return DoMakeParamSpecHelperTwo (setter, getter, initialValue, ParamSpecHelperSimpleChecker ()); + return DoMakeParamSpecHelperTwo (setter, getter, ParamSpecHelperSimpleChecker ()); } -template +template Ptr DoMakeParamSpecHelperTwo (V (T::*getter) (void) const, void (T::*setter) (U), - W initialValue, CHECKER checker) { - return DoMakeParamSpecHelperTwo (setter, getter, initialValue, checker); + return DoMakeParamSpecHelperTwo (setter, getter, checker); } -template +template Ptr DoMakeParamSpecHelperTwo (V (T::*getter) (void) const, - void (T::*setter) (U), - W initialValue) + void (T::*setter) (U)) { - return DoMakeParamSpecHelperTwo (setter, getter, initialValue); + return DoMakeParamSpecHelperTwo (setter, getter); } -template +template Ptr -MakeParamSpecHelper (T1 a1, V initialValue) +MakeParamSpecHelper (T1 a1) { - return DoMakeParamSpecHelperOne (a1, initialValue); + return DoMakeParamSpecHelperOne (a1); } -template +template Ptr -MakeParamSpecHelper (T1 a1, T2 a2, V initialValue) +MakeParamSpecHelper (T1 a1, T2 a2) { - return DoMakeParamSpecHelperTwo (a1, a2, initialValue); + return DoMakeParamSpecHelperTwo (a1, a2); } -template +template Ptr -MakeParamSpecHelperWithChecker (T1 a1, V initialValue, CHECKER checker) +MakeParamSpecHelperWithChecker (T1 a1, CHECKER checker) { - return DoMakeParamSpecHelperOne (a1, initialValue, checker); + return DoMakeParamSpecHelperOne (a1, checker); } -template +template Ptr -MakeParamSpecHelperWithChecker (T1 a1, T2 a2, V initialValue, CHECKER checker) +MakeParamSpecHelperWithChecker (T1 a1, T2 a2, CHECKER checker) { - return DoMakeParamSpecHelperTwo (a1, a2, initialValue, checker); + return DoMakeParamSpecHelperTwo (a1, a2, checker); } } // namespace ns3 diff --git a/src/core/random-variable.h b/src/core/random-variable.h index e251f0a74..d5bc96002 100644 --- a/src/core/random-variable.h +++ b/src/core/random-variable.h @@ -669,11 +669,9 @@ class RandomVariableValue : public Value {}; class RandomVariableParamSpec : public ParamSpec {}; template -Ptr MakeRandomVariableParamSpec (T1 a1, - RandomVariable initialValue); +Ptr MakeRandomVariableParamSpec (T1 a1); template -Ptr MakeRandomVariableParamSpec (T1 a1, T2 a2, - RandomVariable initialValue); +Ptr MakeRandomVariableParamSpec (T1 a1, T2 a2); }//namespace ns3 @@ -682,19 +680,17 @@ Ptr MakeRandomVariableParamSpec (T1 a1, T2 a2, namespace ns3 { template -Ptr MakeRandomVariableParamSpec (T1 a1, - RandomVariable initialValue) +Ptr MakeRandomVariableParamSpec (T1 a1) { return MakeClassValueHelperParamSpec (a1, initialValue); + RandomVariableValue, RandomVariableParamSpec> (a1); } template -Ptr MakeRandomVariableParamSpec (T1 a1, T2 a2, - RandomVariable initialValue) +Ptr MakeRandomVariableParamSpec (T1 a1, T2 a2) { return MakeClassValueHelperParamSpec (a1, a2, initialValue); + RandomVariableValue,RandomVariableParamSpec> (a1, a2); } diff --git a/src/core/uint-value.h b/src/core/uint-value.h index 21ffad5dc..46426bffa 100644 --- a/src/core/uint-value.h +++ b/src/core/uint-value.h @@ -27,19 +27,15 @@ private: class UintParamSpec : public ParamSpec {}; template -Ptr MakeUintParamSpec (T1 a1, - uint64_t initialValue); +Ptr MakeUintParamSpec (T1 a1); template Ptr MakeUintParamSpec (T1 a1, - uint64_t initialValue, uint64_t minValue, uint64_t maxValue); template -Ptr MakeUintParamSpec (T1 a1, T2 a2, - uint64_t initialValue); +Ptr MakeUintParamSpec (T1 a1, T2 a2); template Ptr MakeUintParamSpec (T1 a1, T2 a2, - uint64_t initialValue, uint64_t minValue, uint64_t maxValue); @@ -75,36 +71,32 @@ private: template Ptr -MakeUintParamSpec (T1 a1, - uint64_t initialValue) +MakeUintParamSpec (T1 a1) { - return MakeParamSpecHelperWithChecker (a1, UintValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, UintValueChecker::Create (a1)); } template Ptr MakeUintParamSpec (T1 a1, - uint64_t initialValue, uint64_t minValue, uint64_t maxValue) { - return MakeParamSpecHelperWithChecker (a1, UintValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, UintValueChecker (minValue, maxValue)); } template -Ptr MakeUintParamSpec (T1 a1, T2 a2, - uint64_t initialValue) +Ptr MakeUintParamSpec (T1 a1, T2 a2) { - return MakeParamSpecHelperWithChecker (a1, a2, UintValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, a2, UintValueChecker::Create (a1)); } template Ptr MakeUintParamSpec (T1 a1, T2 a2, - uint64_t initialValue, uint64_t minValue, uint64_t maxValue) { - return MakeParamSpecHelperWithChecker (a1, a2, UintValue (initialValue), + return MakeParamSpecHelperWithChecker (a1, a2, UintValueChecker (minValue, maxValue)); } diff --git a/src/core/value-helper.h b/src/core/value-helper.h index f53fb95ce..91850c453 100644 --- a/src/core/value-helper.h +++ b/src/core/value-helper.h @@ -11,18 +11,16 @@ class type##Value : public Value {}; \ class type##ParamSpec : public ParamSpec {}; \ template \ - Ptr Make##type##ParamSpec (T1 a1, \ - type initialValue) \ + Ptr Make##type##ParamSpec (T1 a1) \ { \ return MakeClassValueHelperParamSpec< type , \ - type##Value, type##ParamSpec> (a1, initialValue); \ + type##Value, type##ParamSpec> (a1); \ } \ template \ - Ptr Make##type##ParamSpec (T1 a1, T2 a2, \ - type initialValue) \ + Ptr Make##type##ParamSpec (T1 a1, T2 a2) \ { \ return MakeClassValueHelperParamSpec (a1, a2, initialValue); \ + type##Value,type##ParamSpec> (a1, a2); \ } #define VALUE_HELPER_CPP(type) \ diff --git a/src/core/value-test.cc b/src/core/value-test.cc index aab20b899..34f7cabe2 100644 --- a/src/core/value-test.cc +++ b/src/core/value-test.cc @@ -42,39 +42,46 @@ public: static TypeId tid = TypeId ("ParamSpecObjectTest") .SetParent () .AddParameter ("TestBoolName", "help text", - MakeBooleanParamSpec (&ParamSpecObjectTest::m_boolTest, false)) + BooleanValue (false), + MakeBooleanParamSpec (&ParamSpecObjectTest::m_boolTest)) .AddParameter ("TestBoolA", "help text", + BooleanValue (false), MakeBooleanParamSpec (&ParamSpecObjectTest::DoSetTestB, - &ParamSpecObjectTest::DoGetTestB, - false)) + &ParamSpecObjectTest::DoGetTestB)) .AddParameter ("TestPtr", "help text", + Ptr (0), MakePtrParamSpec (&ParamSpecObjectTest::m_derived)) .AddParameter ("TestInt16", "help text", - MakeIntParamSpec (&ParamSpecObjectTest::m_int16, - -2)) + IntValue (-2), + MakeIntParamSpec (&ParamSpecObjectTest::m_int16)) .AddParameter ("TestInt16WithBounds", "help text", + IntValue (-2), MakeIntParamSpec (&ParamSpecObjectTest::m_int16WithBounds, - -2, -5, 10)) + -5, 10)) .AddParameter ("TestInt16SetGet", "help text", + IntValue (6), MakeIntParamSpec (&ParamSpecObjectTest::DoSetInt16, - &ParamSpecObjectTest::DoGetInt16, - 6)) + &ParamSpecObjectTest::DoGetInt16)) .AddParameter ("TestUint8", "help text", - MakeUintParamSpec (&ParamSpecObjectTest::m_uint8, - 1)) + UintValue (1), + MakeUintParamSpec (&ParamSpecObjectTest::m_uint8)) .AddParameter ("TestEnum", "help text", + EnumValue (TEST_A), MakeEnumParamSpec (&ParamSpecObjectTest::m_enum, TEST_A, "TestA", TEST_B, "TestB", TEST_C, "TestC")) .AddParameter ("TestRandom", "help text", - MakeRandomVariableParamSpec (&ParamSpecObjectTest::m_random, - ConstantVariable (1.0))) + ConstantVariable (1.0), + MakeRandomVariableParamSpec (&ParamSpecObjectTest::m_random)) .AddParameter ("TestFloat", "help text", - MakeFpParamSpec (&ParamSpecObjectTest::m_float, -1.1)) + FpValue (-1.1), + MakeFpParamSpec (&ParamSpecObjectTest::m_float)) .AddParameter ("TestVector1", "help text", + ObjectVector (), MakeObjectVectorParamSpec (&ParamSpecObjectTest::m_vector1)) .AddParameter ("TestVector2", "help text", + ObjectVector (), MakeObjectVectorParamSpec (&ParamSpecObjectTest::DoGetVectorN, &ParamSpecObjectTest::DoGetVector)) ; diff --git a/src/core/value.h b/src/core/value.h index 38754ff70..87e33de70 100644 --- a/src/core/value.h +++ b/src/core/value.h @@ -76,7 +76,6 @@ public: virtual bool Set (ObjectBase * object, PValue value) const = 0; virtual bool Get (const ObjectBase * object, PValue value) const = 0; virtual bool Check (PValue value) const = 0; - virtual PValue GetInitialValue (void) const = 0; private: mutable uint32_t m_count; }; @@ -271,9 +270,6 @@ public: } return true; } - virtual PValue GetInitialValue (void) const { - return PValue::Create > (Ptr (0)); - } private: virtual void DoSet (T *object, Ptr value) const = 0; virtual Ptr DoGet (const T *object) const = 0; diff --git a/src/devices/point-to-point/point-to-point-net-device.cc b/src/devices/point-to-point/point-to-point-net-device.cc index 5fafd7eeb..c93f0cd15 100644 --- a/src/devices/point-to-point/point-to-point-net-device.cc +++ b/src/devices/point-to-point/point-to-point-net-device.cc @@ -37,7 +37,7 @@ namespace ns3 { DataRateDefaultValue PointToPointNetDevice::g_defaultRate( "PointToPointLinkDataRate", "The default data rate for point to point links", - DataRate ("10Mb/s")); + MakeDataRate ("10Mb/s")); PointToPointTraceType::PointToPointTraceType (enum Type type) : m_type (type) diff --git a/src/mobility/hierarchical-mobility-model.cc b/src/mobility/hierarchical-mobility-model.cc index bc4cda238..73151c1f4 100644 --- a/src/mobility/hierarchical-mobility-model.cc +++ b/src/mobility/hierarchical-mobility-model.cc @@ -31,8 +31,10 @@ HierarchicalMobilityModel::GetTypeId (void) .SetParent () .AddConstructor () .AddParameter ("child", "The child mobility model.", + Ptr (0), MakePtrParamSpec (&HierarchicalMobilityModel::SetChild)) .AddParameter ("parent", "The parent mobility model.", + Ptr (0), MakePtrParamSpec (&HierarchicalMobilityModel::SetParent)) ; return tid; diff --git a/src/mobility/mobility-model.cc b/src/mobility/mobility-model.cc index 33d82f717..941cfdc63 100644 --- a/src/mobility/mobility-model.cc +++ b/src/mobility/mobility-model.cc @@ -30,14 +30,13 @@ MobilityModel::GetTypeId (void) .SetParent () .AddParameter ("position", "The current position of the mobility model.", TypeId::PARAM_SGC, + Vector (0.0, 0.0, 0.0), MakeVectorParamSpec (&MobilityModel::SetPosition, - &MobilityModel::GetPosition, - Vector (0.0, 0.0, 0.0))) + &MobilityModel::GetPosition)) .AddParameter ("velocity", "The current velocity of the mobility model.", TypeId::PARAM_GET, - MakeVectorParamSpec (&MobilityModel::GetVelocity, - Vector (0.0, 0.0, 0.0) // ignored initial value. - )) + Vector (0.0, 0.0, 0.0), // ignored initial value. + MakeVectorParamSpec (&MobilityModel::GetVelocity)) ; return tid; } diff --git a/src/mobility/position-allocator.cc b/src/mobility/position-allocator.cc index 03ace15ba..d4d21afdd 100644 --- a/src/mobility/position-allocator.cc +++ b/src/mobility/position-allocator.cc @@ -54,16 +54,22 @@ GridPositionAllocator::GetTypeId (void) .SetGroupName ("Mobility") .AddConstructor () .AddParameter ("GridWidth", "The number of objects layed out on a line.", - MakeIntParamSpec (&GridPositionAllocator::m_n, 10)) + IntValue (10), + MakeIntParamSpec (&GridPositionAllocator::m_n)) .AddParameter ("MinX", "The x coordinate where the grid starts.", - MakeFpParamSpec (&GridPositionAllocator::m_xMin, 0.0)) + FpValue (1.0), + MakeFpParamSpec (&GridPositionAllocator::m_xMin)) .AddParameter ("MinY", "The y coordinate where the grid starts.", - MakeFpParamSpec (&GridPositionAllocator::m_yMin, 0.0)) + FpValue (0.0), + MakeFpParamSpec (&GridPositionAllocator::m_yMin)) .AddParameter ("DeltaX", "The x space between objects.", - MakeFpParamSpec (&GridPositionAllocator::m_deltaX, 1.0)) + FpValue (1.0), + MakeFpParamSpec (&GridPositionAllocator::m_deltaX)) .AddParameter ("DeltaY", "The y space between objects.", - MakeFpParamSpec (&GridPositionAllocator::m_deltaY, 1.0)) + FpValue (1.0), + MakeFpParamSpec (&GridPositionAllocator::m_deltaY)) .AddParameter ("LayoutType", "The type of layout.", + EnumValue (ROW_FIRST), MakeEnumParamSpec (&GridPositionAllocator::m_layoutType, ROW_FIRST, "RowFirst", COLUMN_FIRST, "ColumnFirst")) @@ -104,12 +110,12 @@ RandomRectanglePositionAllocator::GetTypeId (void) .AddConstructor () .AddParameter ("X", "A random variable which represents the x coordinate of a position in a random rectangle.", - MakeRandomVariableParamSpec (&RandomRectanglePositionAllocator::m_x, - UniformVariable (0.0, 1.0))) + UniformVariable (0.0, 1.0), + MakeRandomVariableParamSpec (&RandomRectanglePositionAllocator::m_x)) .AddParameter ("Y", "A random variable which represents the y coordinate of a position in a random rectangle.", - MakeRandomVariableParamSpec (&RandomRectanglePositionAllocator::m_y, - UniformVariable (0.0, 1.0))); + UniformVariable (0.0, 1.0), + MakeRandomVariableParamSpec (&RandomRectanglePositionAllocator::m_y)); return tid; } @@ -136,18 +142,20 @@ RandomDiscPositionAllocator::GetTypeId (void) .AddConstructor () .AddParameter ("Theta", "A random variable which represents the angle (gradients) of a position in a random disc.", - MakeRandomVariableParamSpec (&RandomDiscPositionAllocator::m_theta, - UniformVariable (0.0, 6.2830))) + UniformVariable (0.0, 6.2830), + MakeRandomVariableParamSpec (&RandomDiscPositionAllocator::m_theta)) .AddParameter ("Rho", "A random variable which represents the radius of a position in a random disc.", - MakeRandomVariableParamSpec (&RandomDiscPositionAllocator::m_rho, - UniformVariable (0.0, 200.0))) + UniformVariable (0.0, 200.0), + MakeRandomVariableParamSpec (&RandomDiscPositionAllocator::m_rho)) .AddParameter ("X", "The x coordinate of the center of the random position disc.", - MakeFpParamSpec (&RandomDiscPositionAllocator::m_x, 0.0)) + FpValue (0.0), + MakeFpParamSpec (&RandomDiscPositionAllocator::m_x)) .AddParameter ("Y", "The y coordinate of the center of the random position disc.", - MakeFpParamSpec (&RandomDiscPositionAllocator::m_y, 0.0)); + FpValue (0.0), + MakeFpParamSpec (&RandomDiscPositionAllocator::m_y)); return tid; } diff --git a/src/mobility/random-direction-2d-mobility-model.cc b/src/mobility/random-direction-2d-mobility-model.cc index 8e0d090ce..1f1f42272 100644 --- a/src/mobility/random-direction-2d-mobility-model.cc +++ b/src/mobility/random-direction-2d-mobility-model.cc @@ -42,14 +42,14 @@ RandomDirection2dMobilityModel::GetTypeId (void) .SetGroupName ("Mobility") .AddConstructor () .AddParameter ("bounds", "The 2d bounding area", - MakeRectangleParamSpec (&RandomDirection2dMobilityModel::m_bounds, - Rectangle (-100, 100, -100, 100))) + Rectangle (-100, 100, -100, 100), + MakeRectangleParamSpec (&RandomDirection2dMobilityModel::m_bounds)) .AddParameter ("speed", "A random variable to control the speed (m/s).", - MakeRandomVariableParamSpec (&RandomDirection2dMobilityModel::m_speed, - UniformVariable (1.0, 2.0))) + UniformVariable (1.0, 2.0), + MakeRandomVariableParamSpec (&RandomDirection2dMobilityModel::m_speed)) .AddParameter ("pause", "A random variable to control the pause (s).", - MakeRandomVariableParamSpec (&RandomDirection2dMobilityModel::m_pause, - ConstantVariable (2.0))); + ConstantVariable (2.0), + MakeRandomVariableParamSpec (&RandomDirection2dMobilityModel::m_pause)); return tid; } diff --git a/src/mobility/random-walk-2d-mobility-model.cc b/src/mobility/random-walk-2d-mobility-model.cc index 4e11fedf8..1953c0634 100644 --- a/src/mobility/random-walk-2d-mobility-model.cc +++ b/src/mobility/random-walk-2d-mobility-model.cc @@ -39,30 +39,31 @@ RandomWalk2dMobilityModel::GetTypeId (void) .AddConstructor () .AddParameter ("bounds", "Bounds of the area to cruise.", - MakeRectangleParamSpec (&RandomWalk2dMobilityModel::m_bounds, - Rectangle (0.0, 0.0, 100.0, 100.0))) + Rectangle (0.0, 0.0, 100.0, 100.0), + MakeRectangleParamSpec (&RandomWalk2dMobilityModel::m_bounds)) .AddParameter ("time", "Change current direction and speed after moving for this delay.", - MakeTimeParamSpec (&RandomWalk2dMobilityModel::m_modeTime, - Seconds (1.0))) + Seconds (1.0), + MakeTimeParamSpec (&RandomWalk2dMobilityModel::m_modeTime)) .AddParameter ("distance", "Change current direction and speed after moving for this distance.", - MakeTimeParamSpec (&RandomWalk2dMobilityModel::m_modeTime, - Seconds (1.0))) + Seconds (1.0), + MakeTimeParamSpec (&RandomWalk2dMobilityModel::m_modeTime)) .AddParameter ("mode", "The mode indicates the condition used to " "change the current speed and direction", + EnumValue (RandomWalk2dMobilityModel::MODE_DISTANCE), MakeEnumParamSpec (&RandomWalk2dMobilityModel::m_mode, RandomWalk2dMobilityModel::MODE_DISTANCE, "Distance", RandomWalk2dMobilityModel::MODE_TIME, "Time")) .AddParameter ("direction", "A random variable used to pick the direction (gradients).", - MakeRandomVariableParamSpec (&RandomWalk2dMobilityModel::m_direction, - UniformVariable (0.0, 6.283184))) + UniformVariable (0.0, 6.283184), + MakeRandomVariableParamSpec (&RandomWalk2dMobilityModel::m_direction)) .AddParameter ("speed", "A random variable used to pick the speed (m/s).", - MakeRandomVariableParamSpec (&RandomWalk2dMobilityModel::m_speed, - UniformVariable (2.0, 4.0))); + UniformVariable (2.0, 4.0), + MakeRandomVariableParamSpec (&RandomWalk2dMobilityModel::m_speed)); return tid; } diff --git a/src/mobility/random-waypoint-mobility-model.cc b/src/mobility/random-waypoint-mobility-model.cc index bf926a26d..52def3c6a 100644 --- a/src/mobility/random-waypoint-mobility-model.cc +++ b/src/mobility/random-waypoint-mobility-model.cc @@ -38,14 +38,15 @@ RandomWaypointMobilityModel::GetTypeId (void) .AddConstructor () .AddParameter ("speed", "A random variable used to pick the speed of a random waypoint model.", - MakeRandomVariableParamSpec (&RandomWaypointMobilityModel::m_speed, - UniformVariable (0.3, 0.7))) + UniformVariable (0.3, 0.7), + MakeRandomVariableParamSpec (&RandomWaypointMobilityModel::m_speed)) .AddParameter ("pause", "A random variable used to pick the pause of a random waypoint model.", - MakeRandomVariableParamSpec (&RandomWaypointMobilityModel::m_pause, - ConstantVariable (2.0))) + ConstantVariable (2.0), + MakeRandomVariableParamSpec (&RandomWaypointMobilityModel::m_pause)) .AddParameter ("position", "The position model used to pick a destination point.", + Ptr (0), MakePtrParamSpec (&RandomWaypointMobilityModel::m_position)); return tid; diff --git a/src/node/drop-tail-queue.cc b/src/node/drop-tail-queue.cc index 07be6e8de..7e92adda9 100644 --- a/src/node/drop-tail-queue.cc +++ b/src/node/drop-tail-queue.cc @@ -33,8 +33,8 @@ TypeId DropTailQueue::GetTypeId (void) .SetParent () .AddConstructor () .AddParameter ("MaxPackets", "The maximum number of packets accepted by this DropTailQueue.", - MakeUintParamSpec (&DropTailQueue::m_maxPackets, - 100)) + UintValue (100), + MakeUintParamSpec (&DropTailQueue::m_maxPackets)) ; return tid; @@ -131,8 +131,8 @@ DropTailQueueTest::RunTests (void) { bool result = true; - DropTailQueue queue; - queue.Set ("MaxPackets", UintValue (3)); + Ptr queue = CreateObject (); + NS_TEST_ASSERT (queue->Set ("MaxPackets", UintValue (3))); Ptr p1, p2, p3, p4; p1 = Create (); @@ -140,34 +140,34 @@ DropTailQueueTest::RunTests (void) p3 = Create (); p4 = Create (); - NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 0); - queue.Enqueue (p1); - NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 1); - queue.Enqueue (p2); - NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 2); - queue.Enqueue (p3); - NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 3); - queue.Enqueue (p4); // will be dropped - NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 3); + NS_TEST_ASSERT_EQUAL (queue->GetNPackets (), 0); + queue->Enqueue (p1); + NS_TEST_ASSERT_EQUAL (queue->GetNPackets (), 1); + queue->Enqueue (p2); + NS_TEST_ASSERT_EQUAL (queue->GetNPackets (), 2); + queue->Enqueue (p3); + NS_TEST_ASSERT_EQUAL (queue->GetNPackets (), 3); + queue->Enqueue (p4); // will be dropped + NS_TEST_ASSERT_EQUAL (queue->GetNPackets (), 3); Ptr p; - p = queue.Dequeue (); + p = queue->Dequeue (); NS_TEST_ASSERT (p != 0); - NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 2); + NS_TEST_ASSERT_EQUAL (queue->GetNPackets (), 2); NS_TEST_ASSERT_EQUAL (p->GetUid (), p1->GetUid ()); - p = queue.Dequeue (); + p = queue->Dequeue (); NS_TEST_ASSERT (p != 0); - NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 1); + NS_TEST_ASSERT_EQUAL (queue->GetNPackets (), 1); NS_TEST_ASSERT_EQUAL (p->GetUid (), p2->GetUid ()); - p = queue.Dequeue (); + p = queue->Dequeue (); NS_TEST_ASSERT (p != 0); - NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 0); + NS_TEST_ASSERT_EQUAL (queue->GetNPackets (), 0); NS_TEST_ASSERT_EQUAL (p->GetUid (), p3->GetUid ()); - p = queue.Dequeue (); + p = queue->Dequeue (); NS_TEST_ASSERT (p == 0); return result; diff --git a/src/simulator/nstime.h b/src/simulator/nstime.h index 4673100db..c97f5f884 100644 --- a/src/simulator/nstime.h +++ b/src/simulator/nstime.h @@ -674,11 +674,9 @@ class TimeValue : public Value {}; class TimeParamSpec : public ParamSpec {}; template -Ptr MakeTimeParamSpec (T1 a1, - Time initialValue); +Ptr MakeTimeParamSpec (T1 a1); template -Ptr MakeTimeParamSpec (T1 a1, T2 a2, - Time initialValue); +Ptr MakeTimeParamSpec (T1 a1, T2 a2); } // namespace ns3 @@ -686,17 +684,15 @@ Ptr MakeTimeParamSpec (T1 a1, T2 a2, namespace ns3 { template -Ptr MakeTimeParamSpec (T1 a1, - Time initialValue) +Ptr MakeTimeParamSpec (T1 a1) { - return MakeClassValueHelperParamSpec (a1, initialValue); + return MakeClassValueHelperParamSpec (a1); } template -Ptr MakeTimeParamSpec (T1 a1, T2 a2, - Time initialValue) +Ptr MakeTimeParamSpec (T1 a1, T2 a2) { - return MakeClassValueHelperParamSpec (a1, a2, initialValue); + return MakeClassValueHelperParamSpec (a1, a2); } From 46854dde588dab3ebb2c0e52ba72f2be9d434975 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 17 Feb 2008 04:38:41 +0100 Subject: [PATCH 055/130] Backed out changeset 7b145012b2eb --- src/core/object.cc | 50 ---------------------------------------------- src/core/object.h | 11 ---------- 2 files changed, 61 deletions(-) diff --git a/src/core/object.cc b/src/core/object.cc index 0bf29ae40..d85be6e1f 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -23,10 +23,6 @@ #include "singleton.h" #include "trace-resolver.h" #include "value.h" -#include "uint-value.h" -#include "int-value.h" -#include "fp-value.h" -#include "enum-value.h" #include "log.h" #include #include @@ -933,27 +929,6 @@ Object::Set (std::string name, PValue value) } return DoSet (info.spec, value); } -bool -Object::SetUint (std::string name, uint64_t value) -{ - return Set (name, UintValue (value)); -} -bool -Object::SetInt (std::string name, int64_t value) -{ - return Set (name, IntValue (value)); -} -bool -Object::SetFp (std::string name, double value) -{ - return Set (name, FpValue (value)); -} -bool -Object::SetEnum (std::string name, int value) -{ - return Set (name, EnumValue (value)); -} - bool Object::Get (std::string name, std::string &value) const { @@ -995,31 +970,6 @@ Object::Get (std::string name) const } return value; } -uint64_t -Object::GetUint (std::string name) const -{ - UintValue value = Get (name); - return value.Get (); -} -int64_t -Object::GetInt (std::string name) const -{ - IntValue value = Get (name); - return value.Get (); -} -double -Object::GetFp (std::string name) const -{ - FpValue value = Get (name); - return value.Get (); -} -int -Object::GetEnum (std::string name) const -{ - EnumValue value = Get (name); - return value.Get (); -} - Ptr Object::DoGetObject (TypeId tid) const diff --git a/src/core/object.h b/src/core/object.h index 8b8c2fb7c..79fadb993 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -350,12 +350,6 @@ public: * Set a single parameter. */ bool Set (std::string name, PValue value); - - bool SetUint (std::string name, uint64_t value); - bool SetInt (std::string name, int64_t value); - bool SetFp (std::string name, double value); - bool SetEnum (std::string name, int value); - /** * \param name the name of the parameter to read * \param value a reference to the string where the value of the @@ -371,11 +365,6 @@ public: */ PValue Get (std::string name) const; - uint64_t GetUint (std::string name) const; - int64_t GetInt (std::string name) const; - double GetFp (std::string name) const; - int GetEnum (std::string name) const; - /** * Increment the reference count. This method should not be called * by user code. Object instances are expected to be used in conjunction From 1b2ccc38a370b2910c253315dba7896deac75860 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 18 Feb 2008 00:18:45 +0100 Subject: [PATCH 056/130] split checker from ParamSpec. --- src/core/boolean-value.cc | 9 +- src/core/boolean-value.h | 8 +- src/core/class-value-helper.h | 4 +- src/core/enum-value.cc | 118 ++++++++++---- src/core/enum-value.h | 152 ++++-------------- src/core/fp-value.cc | 16 +- src/core/fp-value.h | 92 +++++------ src/core/int-value.cc | 16 +- src/core/int-value.h | 95 +++++------ src/core/object-vector.cc | 11 +- src/core/object-vector.h | 7 +- src/core/object.cc | 89 ++++++---- src/core/object.h | 17 +- src/core/param-spec-helper.h | 141 ++++------------ src/core/random-variable.cc | 6 + src/core/random-variable.h | 1 + src/core/uint-value.cc | 17 +- src/core/uint-value.h | 89 ++++------ src/core/value-helper.h | 5 + src/core/value-test.cc | 42 +++-- src/core/value.cc | 35 +++- src/core/value.h | 89 +++++++--- src/mobility/hierarchical-mobility-model.cc | 6 +- src/mobility/mobility-model.cc | 6 +- src/mobility/position-allocator.cc | 40 +++-- .../random-direction-2d-mobility-model.cc | 10 +- src/mobility/random-walk-2d-mobility-model.cc | 21 ++- .../random-waypoint-mobility-model.cc | 9 +- src/node/drop-tail-queue.cc | 3 +- src/simulator/nstime.h | 4 + src/simulator/time.cc | 5 + 31 files changed, 555 insertions(+), 608 deletions(-) diff --git a/src/core/boolean-value.cc b/src/core/boolean-value.cc index 75e36def2..68d3af6d0 100644 --- a/src/core/boolean-value.cc +++ b/src/core/boolean-value.cc @@ -22,7 +22,7 @@ BooleanValue::Copy (void) const return PValue::Create (*this); } std::string -BooleanValue::SerializeToString (Ptr spec) const +BooleanValue::SerializeToString (Ptr checker) const { std::string value; if (m_value) @@ -36,7 +36,7 @@ BooleanValue::SerializeToString (Ptr spec) const return value; } bool -BooleanValue::DeserializeFromString (std::string value, Ptr spec) +BooleanValue::DeserializeFromString (std::string value, Ptr checker) { if (value == "true" || value == "1" || @@ -71,4 +71,9 @@ BooleanValue::operator PValue () const return PValue::Create (*this); } +Ptr MakeBooleanChecker (void) +{ + return MakeSimpleAttributeChecker (); +} + } // namespace ns3 diff --git a/src/core/boolean-value.h b/src/core/boolean-value.h index 0c3962d62..1684c16b0 100644 --- a/src/core/boolean-value.h +++ b/src/core/boolean-value.h @@ -15,8 +15,8 @@ public: bool Get (void) const; virtual PValue Copy (void) const; - virtual std::string SerializeToString (Ptr spec) const; - virtual bool DeserializeFromString (std::string value, Ptr spec); + virtual std::string SerializeToString (Ptr checker) const; + virtual bool DeserializeFromString (std::string value, Ptr checker); BooleanValue (PValue value); operator PValue () const; @@ -33,6 +33,8 @@ Ptr MakeBooleanParamSpec (T1 a1); template Ptr MakeBooleanParamSpec (T1 a1, T2 a2); +Ptr MakeBooleanChecker (void); + } // namespace ns3 @@ -51,8 +53,6 @@ Ptr MakeBooleanParamSpec (T1 a1, T2 a2) return MakeParamSpecHelper (a1, a2); } - - } // namespace ns3 #endif /* BOOLEAN_PARAMETER_H */ diff --git a/src/core/class-value-helper.h b/src/core/class-value-helper.h index bbf9295b4..f1481a3c7 100644 --- a/src/core/class-value-helper.h +++ b/src/core/class-value-helper.h @@ -26,12 +26,12 @@ public: virtual PValue Copy (void) const { return PValue::Create > (*this); } - virtual std::string SerializeToString (Ptr spec) const { + virtual std::string SerializeToString (Ptr checker) const { std::ostringstream oss; oss << m_value; return oss.str (); } - virtual bool DeserializeFromString (std::string value, Ptr spec) { + virtual bool DeserializeFromString (std::string value, Ptr checker) { std::istringstream iss; iss.str (value); iss >> m_value; diff --git a/src/core/enum-value.cc b/src/core/enum-value.cc index f4959da38..05a29abc8 100644 --- a/src/core/enum-value.cc +++ b/src/core/enum-value.cc @@ -25,11 +25,11 @@ EnumValue::Copy (void) const return PValue::Create (*this); } std::string -EnumValue::SerializeToString (Ptr spec) const +EnumValue::SerializeToString (Ptr checker) const { - const EnumParamSpec *p = dynamic_cast (PeekPointer (spec)); + const EnumChecker *p = dynamic_cast (PeekPointer (checker)); NS_ASSERT (p != 0); - for (EnumParamSpec::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++) + for (EnumChecker::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++) { if (i->first == m_v) { @@ -42,11 +42,11 @@ EnumValue::SerializeToString (Ptr spec) const return ""; } bool -EnumValue::DeserializeFromString (std::string value, Ptr spec) +EnumValue::DeserializeFromString (std::string value, Ptr checker) { - const EnumParamSpec *p = dynamic_cast (PeekPointer (spec)); + const EnumChecker *p = dynamic_cast (PeekPointer (checker)); NS_ASSERT (p != 0); - for (EnumParamSpec::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++) + for (EnumChecker::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++) { if (i->second == value) { @@ -73,44 +73,21 @@ EnumValue::operator PValue () const -EnumParamSpec::EnumParamSpec () +EnumChecker::EnumChecker () {} void -EnumParamSpec::AddDefault (int v, std::string name) +EnumChecker::AddDefault (int v, std::string name) { m_valueSet.push_front (std::make_pair (v, name)); } void -EnumParamSpec::Add (int v, std::string name) +EnumChecker::Add (int v, std::string name) { m_valueSet.push_back (std::make_pair (v, name)); } - bool -EnumParamSpec::Set (ObjectBase * object, PValue value) const -{ - const EnumValue *p = value.DynCast (); - if (p == 0) - { - return false; - } - DoSet (object, p); - return true; -} -bool -EnumParamSpec::Get (const ObjectBase * object, PValue value) const -{ - EnumValue *p = value.DynCast (); - if (p == 0) - { - return false; - } - DoGet (object, p); - return true; -} -bool -EnumParamSpec::Check (PValue value) const +EnumChecker::Check (PValue value) const { const EnumValue *p = value.DynCast (); if (p == 0) @@ -127,4 +104,79 @@ EnumParamSpec::Check (PValue value) const return false; } +Ptr +MakeEnumChecker (int v1, std::string n1, + int v2, std::string n2, + int v3, std::string n3, + int v4, std::string n4, + int v5, std::string n5, + int v6, std::string n6, + int v7, std::string n7, + int v8, std::string n8, + int v9, std::string n9, + int v10, std::string n10, + int v11, std::string n11, + int v12, std::string n12) +{ + Ptr checker = Create (); + checker->AddDefault (v1, n1); + if (n2 == "") + { + return checker; + } + checker->Add (v2, n2); + if (n3 == "") + { + return checker; + } + checker->Add (v3, n3); + if (n4 == "") + { + return checker; + } + checker->Add (v4, n4); + if (n5 == "") + { + return checker; + } + checker->Add (v5, n5); + if (n6 == "") + { + return checker; + } + checker->Add (v6, n6); + if (n7 == "") + { + return checker; + } + checker->Add (v7, n7); + if (n8 == "") + { + return checker; + } + checker->Add (v8, n8); + if (n9 == "") + { + return checker; + } + checker->Add (v9, n9); + if (n10 == "") + { + return checker; + } + checker->Add (v10, n10); + if (n11 == "") + { + return checker; + } + checker->Add (v11, n11); + if (n12 == "") + { + return checker; + } + checker->Add (v12, n12); + return checker; +} + + } // namespace ns3 diff --git a/src/core/enum-value.h b/src/core/enum-value.h index 98ed30d2d..f05014870 100644 --- a/src/core/enum-value.h +++ b/src/core/enum-value.h @@ -15,8 +15,8 @@ public: int Get (void) const; virtual PValue Copy (void) const; - virtual std::string SerializeToString (Ptr spec) const; - virtual bool DeserializeFromString (std::string value, Ptr spec); + virtual std::string SerializeToString (Ptr checker) const; + virtual bool DeserializeFromString (std::string value, Ptr checker); EnumValue (PValue value); operator PValue () const; @@ -24,145 +24,57 @@ private: int m_v; }; -class EnumParamSpec : public ParamSpec +class EnumParamSpec : public ParamSpec {}; +class EnumChecker : public AttributeChecker { public: - EnumParamSpec (); + EnumChecker (); void AddDefault (int v, std::string name); void Add (int v, std::string name); - virtual bool Set (ObjectBase* object, PValue value) const; - virtual bool Get (const ObjectBase* object, PValue value) const; virtual bool Check (PValue value) const; private: - virtual bool DoSet (ObjectBase *object, const EnumValue *value) const = 0; - virtual bool DoGet (const ObjectBase *object, EnumValue *value) const = 0; friend class EnumValue; typedef std::list > ValueSet; ValueSet m_valueSet; }; -template -Ptr MakeEnumParamSpec (U T::*v, - int v1, std::string n1, - int v2 = 0, std::string n2 = "", - int v3 = 0, std::string n3 = "", - int v4 = 0, std::string n4 = "", - int v5 = 0, std::string n5 = "", - int v6 = 0, std::string n6 = "", - int v7 = 0, std::string n7 = "", - int v8 = 0, std::string n8 = "", - int v9 = 0, std::string n9 = "", - int v10 = 0, std::string n10 = "", - int v11 = 0, std::string n11 = "", - int v12 = 0, std::string n12 = ""); +template +Ptr MakeEnumParamSpec (T1 a1); + +template +Ptr MakeEnumParamSpec (T1 a1, T2 a2); + +Ptr MakeEnumChecker (int v1, std::string n1, + int v2 = 0, std::string n2 = "", + int v3 = 0, std::string n3 = "", + int v4 = 0, std::string n4 = "", + int v5 = 0, std::string n5 = "", + int v6 = 0, std::string n6 = "", + int v7 = 0, std::string n7 = "", + int v8 = 0, std::string n8 = "", + int v9 = 0, std::string n9 = "", + int v10 = 0, std::string n10 = "", + int v11 = 0, std::string n11 = "", + int v12 = 0, std::string n12 = ""); } // namespace ns3 namespace ns3 { -template -Ptr MakeEnumParamSpec (U T::*v, - int v1, std::string n1, - int v2 = 0, std::string n2 = "", - int v3 = 0, std::string n3 = "", - int v4 = 0, std::string n4 = "", - int v5 = 0, std::string n5 = "", - int v6 = 0, std::string n6 = "", - int v7 = 0, std::string n7 = "", - int v8 = 0, std::string n8 = "", - int v9 = 0, std::string n9 = "", - int v10 = 0, std::string n10 = "", - int v11 = 0, std::string n11 = "", - int v12 = 0, std::string n12 = "") +template +Ptr MakeEnumParamSpec (T1 a1) { - class MemberVariable : public EnumParamSpec - { - private: - U T::*m_memberVariable; - public: - MemberVariable (U T::*memberVariable) - : m_memberVariable (memberVariable) {} - virtual bool DoSet (ObjectBase *object, const EnumValue *value) const { - T *o = dynamic_cast (object); - if (o == 0) - { - return false; - } - (o->*m_memberVariable) = U (value->Get ()); - return true; - } - virtual bool DoGet (const ObjectBase *object, EnumValue *value) const { - const T *o = dynamic_cast (object); - if (o == 0) - { - return false; - } - value->Set (o->*m_memberVariable); - return true; - } - }; - Ptr spec = Ptr (new MemberVariable (v), false); - spec->AddDefault (v1, n1); - if (n2 == "") - { - return spec; - } - spec->Add (v2, n2); - if (n3 == "") - { - return spec; - } - spec->Add (v3, n3); - if (n4 == "") - { - return spec; - } - spec->Add (v4, n4); - if (n5 == "") - { - return spec; - } - spec->Add (v5, n5); - if (n6 == "") - { - return spec; - } - spec->Add (v6, n6); - if (n7 == "") - { - return spec; - } - spec->Add (v7, n7); - if (n8 == "") - { - return spec; - } - spec->Add (v8, n8); - if (n9 == "") - { - return spec; - } - spec->Add (v9, n9); - if (n10 == "") - { - return spec; - } - spec->Add (v10, n10); - if (n11 == "") - { - return spec; - } - spec->Add (v11, n11); - if (n12 == "") - { - return spec; - } - spec->Add (v12, n12); - return spec; + return MakeParamSpecHelper (a1); +} + +template +Ptr MakeEnumParamSpec (T1 a1, T2 a2) +{ + return MakeParamSpecHelper (a1, a2); } } // namespace ns3 diff --git a/src/core/fp-value.cc b/src/core/fp-value.cc index 71e2e3583..0bf63bc0f 100644 --- a/src/core/fp-value.cc +++ b/src/core/fp-value.cc @@ -24,14 +24,14 @@ FpValue::Get (void) const return m_value; } std::string -FpValue::SerializeToString (Ptr spec) const +FpValue::SerializeToString (Ptr checker) const { std::ostringstream oss; oss << m_value; return oss.str (); } bool -FpValue::DeserializeFromString (std::string value, Ptr spec) +FpValue::DeserializeFromString (std::string value, Ptr checker) { double v; std::istringstream iss; @@ -59,16 +59,4 @@ FpValue::operator PValue () const return PValue::Create (*this); } - - -FpValueChecker::FpValueChecker (double min, double max) - : m_min (min), - m_max (max) -{} -bool -FpValueChecker::Check (double v) const -{ - return v >= m_min && v <= m_max; -} - } // namespace ns3 diff --git a/src/core/fp-value.h b/src/core/fp-value.h index 8bcd89b53..c44fd908d 100644 --- a/src/core/fp-value.h +++ b/src/core/fp-value.h @@ -13,8 +13,8 @@ public: FpValue (double value); virtual PValue Copy (void) const; - virtual std::string SerializeToString (Ptr spec) const; - virtual bool DeserializeFromString (std::string value, Ptr spec); + virtual std::string SerializeToString (Ptr checker) const; + virtual bool DeserializeFromString (std::string value, Ptr checker); void Set (double value); double Get (void) const; @@ -30,70 +30,58 @@ class FpParamSpec : public ParamSpec {}; template Ptr MakeFpParamSpec (T1 a1); template -Ptr MakeFpParamSpec (T1 a1, T2 a2, - double minValue, - double maxValue); +Ptr MakeFpParamSpec (T1 a1, T2 a2); + +template +Ptr MakeFpChecker (void); +template +Ptr MakeFpChecker (T min, T max); + + } // namespace ns3 namespace ns3 { -class FpValueChecker -{ -public: - FpValueChecker (double minValue, double maxValue); - bool Check (double value) const; - - template - static FpValueChecker Create (U T::*) { - return FpValueChecker (-std::numeric_limits::max (), - std::numeric_limits::max ()); - } - template - static FpValueChecker Create (U (T::*) (void) const) { - return FpValueChecker (-std::numeric_limits::max (), - std::numeric_limits::max ()); - } - template - static FpValueChecker Create (void (T::*) (U)) { - return FpValueChecker (-std::numeric_limits::max (), - std::numeric_limits::max ()); - } -private: - double m_min; - double m_max; -}; - template -Ptr -MakeFpParamSpec (T1 a1) +Ptr MakeFpParamSpec (T1 a1) { - return MakeParamSpecHelperWithChecker (a1, - FpValueChecker::Create (a1)); -} - -template -Ptr MakeFpParamSpec (T1 a1, - double minValue, - double maxValue) -{ - return MakeParamSpecHelperWithChecker (a1, - FpValueChecker (minValue, maxValue)); + return MakeParamSpecHelper (a1); } template Ptr MakeFpParamSpec (T1 a1, T2 a2) { - return MakeParamSpecHelperWithChecker (a1, a2, - FpValueChecker::Create (a1)); + return MakeParamSpecHelper (a1, a2); } -template -Ptr MakeFpParamSpec (T1 a1, T2 a2, - double minValue, - double maxValue) + +template +Ptr MakeFpChecker (void) { - return MakeParamSpecHelperWithChecker (a1, a2, - FpValueChecker (minValue, maxValue)); + return MakeFpChecker (-std::numeric_limits::max (), + std::numeric_limits::max ()); } +template +Ptr MakeFpChecker (T min, T max) +{ + struct Checker : public AttributeChecker + { + Checker (double minValue, double maxValue) + : m_minValue (minValue), + m_maxValue (maxValue) {} + virtual bool Check (PValue value) const { + const FpValue *v = value.DynCast (); + if (v == 0) + { + return false; + } + return v->Get () >= m_minValue && v->Get () <= m_maxValue; + } + double m_minValue; + double m_maxValue; + } *checker = new Checker (min, max); + return Ptr (checker, false); +} + } // namespace ns3 diff --git a/src/core/int-value.cc b/src/core/int-value.cc index 2d3ea6dfa..bf8c8f3d9 100644 --- a/src/core/int-value.cc +++ b/src/core/int-value.cc @@ -24,14 +24,14 @@ IntValue::Get (void) const return m_value; } std::string -IntValue::SerializeToString (Ptr spec) const +IntValue::SerializeToString (Ptr checker) const { std::ostringstream oss; oss << m_value; return oss.str (); } bool -IntValue::DeserializeFromString (std::string value, Ptr spec) +IntValue::DeserializeFromString (std::string value, Ptr checker) { int64_t v; std::istringstream iss; @@ -60,16 +60,4 @@ IntValue::operator PValue () const } -IntValueChecker::IntValueChecker (int64_t minValue, int64_t maxValue) - : m_minValue (minValue), - m_maxValue (maxValue) -{} -bool -IntValueChecker::Check (const int64_t &value) const -{ - return value >= m_minValue && value <= m_maxValue; -} - - - } // namespace ns3 diff --git a/src/core/int-value.h b/src/core/int-value.h index d9c3de429..a66aa22bc 100644 --- a/src/core/int-value.h +++ b/src/core/int-value.h @@ -15,8 +15,8 @@ public: int64_t Get (void) const; virtual PValue Copy (void) const; - virtual std::string SerializeToString (Ptr spec) const; - virtual bool DeserializeFromString (std::string value, Ptr spec); + virtual std::string SerializeToString (Ptr checker) const; + virtual bool DeserializeFromString (std::string value, Ptr checker); IntValue (PValue value); operator PValue () const; @@ -28,78 +28,63 @@ class IntParamSpec : public ParamSpec {}; template Ptr MakeIntParamSpec (T1 a1); -template -Ptr MakeIntParamSpec (T1 a1, - int64_t minValue, - int64_t maxValue); template Ptr MakeIntParamSpec (T1 a1, T2 a2); -template -Ptr MakeIntParamSpec (T1 a1, T2 a2, - int64_t minValue, - int64_t maxValue); + +template +Ptr MakeIntChecker (void); + +template +Ptr MakeIntChecker (T min, T max); } // namespace ns3 namespace ns3 { -class IntValueChecker -{ -public: - IntValueChecker (int64_t minValue, int64_t maxValue); - bool Check (const int64_t &value) const; - - template - static IntValueChecker Create (U T::*) { - return IntValueChecker (std::numeric_limits::min (), - std::numeric_limits::max ()); - } - template - static IntValueChecker Create (U (T::*) (void) const) { - return IntValueChecker (std::numeric_limits::min (), - std::numeric_limits::max ()); - } - template - static IntValueChecker Create (void (T::*) (U)) { - return IntValueChecker (std::numeric_limits::min (), - std::numeric_limits::max ()); - } -private: - int64_t m_minValue; - int64_t m_maxValue; -}; - template Ptr MakeIntParamSpec (T1 a1) { - return MakeParamSpecHelperWithChecker (a1, - IntValueChecker::Create (a1)); -} - -template -Ptr MakeIntParamSpec (T1 a1, - int64_t minValue, - int64_t maxValue) -{ - return MakeParamSpecHelperWithChecker (a1, - IntValueChecker (minValue, maxValue)); + return MakeParamSpecHelper (a1); } template Ptr MakeIntParamSpec (T1 a1, T2 a2) { - return MakeParamSpecHelperWithChecker (a1, a2, - IntValueChecker::Create (a1)); + return MakeParamSpecHelper (a1, a2); } -template -Ptr MakeIntParamSpec (T1 a1, T2 a2, - int64_t minValue, - int64_t maxValue) + +template +Ptr +MakeIntChecker (void) { - return MakeParamSpecHelperWithChecker (a1, a2, - IntValueChecker (minValue, maxValue)); + return MakeIntChecker (std::numeric_limits::min (), + std::numeric_limits::max ()); } +template +Ptr +MakeIntChecker (T min, T max) +{ + struct IntChecker : public AttributeChecker + { + IntChecker (int64_t minValue, int64_t maxValue) + : m_minValue (minValue), + m_maxValue (maxValue) {} + virtual bool Check (PValue value) const { + const IntValue *v = value.DynCast (); + if (v == 0) + { + return false; + } + return v->Get () >= m_minValue && v->Get () <= m_maxValue; + } + int64_t m_minValue; + int64_t m_maxValue; + } *checker = new IntChecker (min, max); + return Ptr (checker, false); +} + + } // namespace ns3 #endif /* INT_VALUE_H */ diff --git a/src/core/object-vector.cc b/src/core/object-vector.cc index 0137136be..2e7384681 100644 --- a/src/core/object-vector.cc +++ b/src/core/object-vector.cc @@ -61,13 +61,13 @@ ObjectVectorValue::Copy (void) const return PValue::Create (*this); } std::string -ObjectVectorValue::SerializeToString (Ptr spec) const +ObjectVectorValue::SerializeToString (Ptr checker) const { // XXX return ""; } bool -ObjectVectorValue::DeserializeFromString (std::string value, Ptr spec) +ObjectVectorValue::DeserializeFromString (std::string value, Ptr checker) { // XXX ?? Can we implement this correctly ?? I doubt it very much. return true; @@ -101,11 +101,10 @@ ObjectVectorParamSpec::Get (const ObjectBase * object, PValue value) const } return true; } -bool -ObjectVectorParamSpec::Check (PValue value) const +Ptr +MakeObjectVectorChecker (void) { - const ObjectVectorValue *v = value.DynCast (); - return v != 0; + return MakeSimpleAttributeChecker (); } } // name diff --git a/src/core/object-vector.h b/src/core/object-vector.h index b204b6fe4..4d1b9737c 100644 --- a/src/core/object-vector.h +++ b/src/core/object-vector.h @@ -42,6 +42,8 @@ MakeObjectVectorParamSpec (INDEX (T::*getN) (void) const, Ptr (T::*get) (INDEX) const); +Ptr MakeObjectVectorChecker (void); + } // namespace ns3 namespace ns3 { @@ -55,8 +57,8 @@ public: ObjectVector Get (void) const; virtual PValue Copy (void) const; - virtual std::string SerializeToString (Ptr spec) const; - virtual bool DeserializeFromString (std::string value, Ptr spec); + virtual std::string SerializeToString (Ptr checker) const; + virtual bool DeserializeFromString (std::string value, Ptr checker); private: friend class ObjectVectorParamSpec; @@ -68,7 +70,6 @@ class ObjectVectorParamSpec : public ParamSpec public: virtual bool Set (ObjectBase * object, PValue value) const; virtual bool Get (const ObjectBase * object, PValue value) const; - virtual bool Check (PValue value) const; private: virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const = 0; virtual Ptr DoGet (const ObjectBase *object, uint32_t i) const = 0; diff --git a/src/core/object.cc b/src/core/object.cc index 3224c0d3d..c7f398300 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -58,12 +58,14 @@ public: std::string help, uint32_t flags, ns3::PValue initialValue, - ns3::Ptr spec); + ns3::Ptr spec, + ns3::Ptr checker); uint32_t GetParametersN (uint16_t uid) const; std::string GetParameterName (uint16_t uid, uint32_t i) const; uint32_t GetParameterFlags (uint16_t uid, uint32_t i) const; ns3::PValue GetParameterInitialValue (uint16_t uid, uint32_t i) const; ns3::Ptr GetParameterParamSpec (uint16_t uid, uint32_t i) const; + ns3::Ptr GetParameterChecker (uint16_t uid, uint32_t i) const; private: struct ConstructorInformation { ns3::CallbackBase cb; @@ -75,6 +77,7 @@ private: uint32_t flags; ns3::PValue initialValue; ns3::Ptr param; + ns3::Ptr checker; }; struct IidInformation { std::string name; @@ -244,7 +247,8 @@ IidManager::AddParameter (uint16_t uid, std::string help, uint32_t flags, ns3::PValue initialValue, - ns3::Ptr spec) + ns3::Ptr spec, + ns3::Ptr checker) { struct IidInformation *information = LookupInformation (uid); for (std::vector::const_iterator j = information->parameters.begin (); @@ -262,6 +266,7 @@ IidManager::AddParameter (uint16_t uid, param.flags = flags; param.initialValue = initialValue; param.param = spec; + param.checker = checker; information->parameters.push_back (param); } @@ -300,6 +305,13 @@ IidManager::GetParameterParamSpec (uint16_t uid, uint32_t i) const NS_ASSERT (i < information->parameters.size ()); return information->parameters[i].param; } +ns3::Ptr +IidManager::GetParameterChecker (uint16_t uid, uint32_t i) const +{ + struct IidInformation *information = LookupInformation (uid); + NS_ASSERT (i < information->parameters.size ()); + return information->parameters[i].checker; +} } // anonymous namespace @@ -437,6 +449,7 @@ TypeId::LookupParameterByName (std::string name, struct TypeId::ParameterInfo *i info->spec = GetParameterParamSpec (i); info->flags = GetParameterFlags (i); info->initialValue = tid.GetParameterInitialValue (i); + info->checker = tid.GetParameterChecker (i); return true; } } @@ -459,6 +472,7 @@ TypeId::LookupParameterByPosition (uint32_t i, struct TypeId::ParameterInfo *inf info->spec = tid.GetParameterParamSpec (j); info->flags = tid.GetParameterFlags (j); info->initialValue = tid.GetParameterInitialValue (j); + info->checker = tid.GetParameterChecker (j); return true; } cur++; @@ -530,9 +544,10 @@ TypeId TypeId::AddParameter (std::string name, std::string help, PValue initialValue, - Ptr param) + Ptr param, + Ptr checker) { - Singleton::Get ()->AddParameter (m_tid, name, help, PARAM_SGC, initialValue, param); + Singleton::Get ()->AddParameter (m_tid, name, help, PARAM_SGC, initialValue, param, checker); return *this; } @@ -541,9 +556,10 @@ TypeId::AddParameter (std::string name, std::string help, uint32_t flags, PValue initialValue, - Ptr param) + Ptr param, + Ptr checker) { - Singleton::Get ()->AddParameter (m_tid, name, help, flags, initialValue, param); + Singleton::Get ()->AddParameter (m_tid, name, help, flags, initialValue, param, checker); return *this; } @@ -607,6 +623,13 @@ TypeId::GetParameterFlags (uint32_t i) const uint32_t flags = Singleton::Get ()->GetParameterFlags (m_tid, i); return flags; } +Ptr +TypeId::GetParameterChecker (uint32_t i) const +{ + // Used exclusively by the Object class. + Ptr checker = Singleton::Get ()->GetParameterChecker (m_tid, i); + return checker; +} bool operator == (TypeId a, TypeId b) @@ -631,7 +654,7 @@ Parameters::Parameters (const Parameters &o) for (Params::const_iterator i = o.m_parameters.begin (); i != o.m_parameters.end (); i++) { struct Param param; - param.spec = i->spec; + param.checker = i->checker; param.value = i->value.Copy (); m_parameters.push_back (param); } @@ -643,7 +666,7 @@ Parameters::operator = (const Parameters &o) for (Params::const_iterator i = o.m_parameters.begin (); i != o.m_parameters.end (); i++) { struct Param param; - param.spec = i->spec; + param.checker = i->checker; param.value = i->value.Copy (); m_parameters.push_back (param); } @@ -678,13 +701,13 @@ Parameters::SetWithTid (TypeId tid, uint32_t position, PValue value) } void -Parameters::DoSetOne (Ptr spec, PValue value) +Parameters::DoSetOne (Ptr checker, PValue value) { // get rid of any previous value stored in this // vector of values. for (Params::iterator k = m_parameters.begin (); k != m_parameters.end (); k++) { - if (k->spec == spec) + if (k->checker == checker) { m_parameters.erase (k); break; @@ -692,18 +715,18 @@ Parameters::DoSetOne (Ptr spec, PValue value) } // store the new value. struct Param p; - p.spec = spec; + p.checker = checker; p.value = value.Copy (); m_parameters.push_back (p); } bool Parameters::DoSet (struct TypeId::ParameterInfo *info, PValue value) { - if (info->spec == 0) + if (info->checker == 0) { return false; } - bool ok = info->spec->Check (value); + bool ok = info->checker->Check (value); if (!ok) { // attempt to convert to string. @@ -714,19 +737,19 @@ Parameters::DoSet (struct TypeId::ParameterInfo *info, PValue value) } // attempt to convert back to value. PValue v = info->initialValue.Copy (); - ok = v.DeserializeFromString (str->Get (), info->spec); + ok = v.DeserializeFromString (str->Get (), info->checker); if (!ok) { return false; } - ok = info->spec->Check (v); + ok = info->checker->Check (v); if (!ok) { return false; } value = v; } - DoSetOne (info->spec, value); + DoSetOne (info->checker, value); return true; } void @@ -741,14 +764,14 @@ Parameters::GetGlobal (void) } std::string -Parameters::LookupParameterFullNameByParamSpec (Ptr spec) const +Parameters::LookupParameterFullNameByChecker (Ptr checker) const { for (uint32_t i = 0; i < TypeId::GetRegisteredN (); i++) { TypeId tid = TypeId::GetRegistered (i); for (uint32_t j = 0; j < tid.GetParametersN (); j++) { - if (spec == tid.GetParameterParamSpec (j)) + if (checker == tid.GetParameterChecker (j)) { return tid.GetParameterFullName (j); } @@ -765,8 +788,8 @@ Parameters::SerializeToString (void) const std::ostringstream oss; for (Params::const_iterator i = m_parameters.begin (); i != m_parameters.end (); i++) { - std::string name = LookupParameterFullNameByParamSpec (i->spec); - oss << name << "=" << i->value.SerializeToString (PeekPointer (i->spec)); + std::string name = LookupParameterFullNameByChecker (i->checker); + oss << name << "=" << i->value.SerializeToString (i->checker); if (i != m_parameters.end ()) { oss << "|"; @@ -812,7 +835,7 @@ Parameters::DeserializeFromString (std::string str) cur++; } PValue val = info.initialValue.Copy (); - bool ok = val.DeserializeFromString (value, info.spec); + bool ok = val.DeserializeFromString (value, info.checker); if (!ok) { // XXX invalid value @@ -820,7 +843,7 @@ Parameters::DeserializeFromString (std::string str) } else { - DoSetOne (info.spec, val); + DoSetOne (info.checker, val); } } } @@ -875,6 +898,7 @@ Object::Construct (const Parameters ¶meters) { Ptr paramSpec = tid.GetParameterParamSpec (i); PValue initial = tid.GetParameterInitialValue (i); + Ptr checker = tid.GetParameterChecker (i); NS_LOG_DEBUG ("try to construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\""); if (!(tid.GetParameterFlags (i) & TypeId::PARAM_CONSTRUCT)) @@ -886,10 +910,10 @@ Object::Construct (const Parameters ¶meters) for (Parameters::Params::const_iterator j = parameters.m_parameters.begin (); j != parameters.m_parameters.end (); j++) { - if (j->spec == paramSpec) + if (j->checker == checker) { // We have a matching parameter value. - DoSet (paramSpec, initial, j->value); + DoSet (paramSpec, initial, checker, j->value); NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\""); found = true; @@ -902,10 +926,10 @@ Object::Construct (const Parameters ¶meters) for (Parameters::Params::const_iterator j = Parameters::GetGlobal ()->m_parameters.begin (); j != Parameters::GetGlobal ()->m_parameters.end (); j++) { - if (j->spec == paramSpec) + if (j->checker == checker) { // We have a matching parameter value. - DoSet (paramSpec, initial, j->value); + DoSet (paramSpec, initial, checker, j->value); NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< tid.GetParameterName (i)<<"\" from global"); found = true; @@ -926,9 +950,10 @@ Object::Construct (const Parameters ¶meters) NotifyConstructionCompleted (); } bool -Object::DoSet (Ptr spec, PValue initialValue, PValue value) +Object::DoSet (Ptr spec, PValue initialValue, + Ptr checker, PValue value) { - bool ok = spec->Check (value); + bool ok = checker->Check (value); if (!ok) { // attempt to convert to string @@ -939,12 +964,12 @@ Object::DoSet (Ptr spec, PValue initialValue, PValue value) } // attempt to convert back from string. PValue v = initialValue.Copy (); - ok = v.DeserializeFromString (str->Get (), spec); + ok = v.DeserializeFromString (str->Get (), checker); if (!ok) { return false; } - ok = spec->Check (v); + ok = checker->Check (v); if (!ok) { return false; @@ -966,7 +991,7 @@ Object::Set (std::string name, PValue value) { return false; } - return DoSet (info.spec, info.initialValue, value); + return DoSet (info.spec, info.initialValue, info.checker, value); } bool Object::Get (std::string name, std::string &value) const @@ -984,7 +1009,7 @@ Object::Get (std::string name, std::string &value) const bool ok = info.spec->Get (this, v); if (ok) { - value = v.SerializeToString (info.spec); + value = v.SerializeToString (info.checker); } return ok; } diff --git a/src/core/object.h b/src/core/object.h index 76e6a7263..314c71776 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -213,7 +213,8 @@ public: TypeId AddParameter (std::string name, std::string help, PValue initialValue, - Ptr spec); + Ptr spec, + Ptr checker); /** * \param name the name of the new parameter @@ -229,7 +230,8 @@ public: std::string help, uint32_t flags, PValue initialValue, - Ptr spec); + Ptr spec, + Ptr checker); // construct an invalid TypeId. TypeId (); @@ -244,6 +246,7 @@ private: Ptr spec; PValue initialValue; uint32_t flags; + Ptr checker; }; /** @@ -267,6 +270,7 @@ private: CallbackBase LookupConstructor (uint32_t nArguments) const; Ptr GetParameterParamSpec (uint32_t i) const; uint32_t GetParameterFlags (uint32_t i) const; + Ptr GetParameterChecker (uint32_t i) const; uint16_t m_tid; }; @@ -320,7 +324,7 @@ public: private: friend class Object; struct Param { - Ptr spec; + Ptr checker; PValue value; }; typedef std::vector Params; @@ -330,8 +334,8 @@ private: bool DoSet (struct TypeId::ParameterInfo *info, PValue param); - void DoSetOne (Ptr spec, PValue param); - std::string LookupParameterFullNameByParamSpec (Ptr spec) const; + void DoSetOne (Ptr checker, PValue param); + std::string LookupParameterFullNameByChecker (Ptr checker) const; Params m_parameters; }; @@ -469,7 +473,8 @@ private: friend Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7); - bool DoSet (Ptr spec, PValue intialValue, PValue value); + bool DoSet (Ptr spec, PValue intialValue, + Ptr checker, PValue value); Ptr DoGetObject (TypeId tid) const; void DoCollectSources (std::string path, const TraceContext &context, TraceResolver::SourceCollection *collection) const; diff --git a/src/core/param-spec-helper.h b/src/core/param-spec-helper.h index edb76856d..a646264fc 100644 --- a/src/core/param-spec-helper.h +++ b/src/core/param-spec-helper.h @@ -11,14 +11,6 @@ template Ptr MakeParamSpecHelper (T1 a1, T2 a2); -template -Ptr -MakeParamSpecHelperWithChecker (T1 a1, CHECKER checker); - -template -Ptr -MakeParamSpecHelperWithChecker (T1 a1, T2 a2, CHECKER checker); - } // namespace ns3 /*************************************************************** @@ -29,12 +21,11 @@ MakeParamSpecHelperWithChecker (T1 a1, T2 a2, CHECKER checker); namespace ns3 { -template +template class ParamSpecHelper : public BASE { public: - ParamSpecHelper (CHECKER checker) - : m_checker (checker) {} + ParamSpecHelper () {} virtual bool Set (ObjectBase * object, PValue val) const { const U *value = val.DynCast (); @@ -64,43 +55,26 @@ public: return DoGet (obj, value); } - virtual bool Check (PValue value) const { - const U *val = value.DynCast (); - if (val == 0) - { - return false; - } - return m_checker.Check (val->Get ()); - } - private: virtual bool DoSet (T *object, const U *v) const = 0; virtual bool DoGet (const T *object, U *v) const = 0; - CHECKER m_checker; }; -template -class ParamSpecHelperSimpleChecker -{ -public: - bool Check (const T &value) const {return true;} -}; - -template +template Ptr -DoMakeParamSpecHelperOne (U T::*memberVariable, CHECKER checker) +DoMakeParamSpecHelperOne (U T::*memberVariable) { - class MemberVariable : public ParamSpecHelper + class MemberVariable : public ParamSpecHelper { public: - MemberVariable (U T::*memberVariable, CHECKER checker) - : ParamSpecHelper (checker), + MemberVariable (U T::*memberVariable) + : ParamSpecHelper (), m_memberVariable (memberVariable) {} private: virtual bool DoSet (T *object, const V *v) const { - (object->*m_memberVariable) = v->Get (); + (object->*m_memberVariable) = U (v->Get ()); return true; } virtual bool DoGet (const T *object, V *v) const { @@ -110,27 +84,18 @@ DoMakeParamSpecHelperOne (U T::*memberVariable, CHECKER checker) U T::*m_memberVariable; }; - return Ptr (new MemberVariable (memberVariable, checker), false); + return Ptr (new MemberVariable (memberVariable), false); } + template Ptr -DoMakeParamSpecHelperOne (U T::*memberVariable) +DoMakeParamSpecHelperOne (U (T::*getter) (void) const) { - return DoMakeParamSpecHelperOne (memberVariable, ParamSpecHelperSimpleChecker ()); -} - - -template -Ptr -DoMakeParamSpecHelperOne (U (T::*getter) (void) const, - CHECKER checker) -{ - class MemberMethod : public ParamSpecHelper + class MemberMethod : public ParamSpecHelper { public: - MemberMethod (U (T::*getter) (void) const, - CHECKER checker) - : ParamSpecHelper (checker), + MemberMethod (U (T::*getter) (void) const) + : ParamSpecHelper (), m_getter (getter) {} private: @@ -143,29 +108,19 @@ DoMakeParamSpecHelperOne (U (T::*getter) (void) const, } U (T::*m_getter) (void) const; }; - return Ptr (new MemberMethod (getter, checker), false); + return Ptr (new MemberMethod (getter), false); } + template Ptr -DoMakeParamSpecHelperOne (U (T::*getter) (void) const) +DoMakeParamSpecHelperOne (void (T::*setter) (U)) { - return DoMakeParamSpecHelperOne (getter, ParamSpecHelperSimpleChecker ()); -} - - - -template -Ptr -DoMakeParamSpecHelperOne (void (T::*setter) (U), - CHECKER checker) -{ - class MemberMethod : public ParamSpecHelper + class MemberMethod : public ParamSpecHelper { public: - MemberMethod (void (T::*setter) (U), - CHECKER checker) - : ParamSpecHelper (checker), + MemberMethod (void (T::*setter) (U)) + : ParamSpecHelper (), m_setter (setter) {} private: @@ -178,31 +133,20 @@ DoMakeParamSpecHelperOne (void (T::*setter) (U), } void (T::*m_setter) (U); }; - return Ptr (new MemberMethod (setter, checker), false); + return Ptr (new MemberMethod (setter), false); } -template -Ptr -DoMakeParamSpecHelperOne (void (T::*setter) (U)) -{ - return DoMakeParamSpecHelperOne (setter, - ParamSpecHelperSimpleChecker::ReferencedType> ()); -} - - -template +template Ptr DoMakeParamSpecHelperTwo (void (T::*setter) (U), - V (T::*getter) (void) const, - CHECKER checker = ParamSpecHelperSimpleChecker ()) + V (T::*getter) (void) const) { - class MemberMethod : public ParamSpecHelper + class MemberMethod : public ParamSpecHelper { public: MemberMethod (void (T::*setter) (U), - V (T::*getter) (void) const, - CHECKER checker) - : ParamSpecHelper (checker), + V (T::*getter) (void) const) + : ParamSpecHelper (), m_setter (setter), m_getter (getter) {} @@ -218,28 +162,11 @@ DoMakeParamSpecHelperTwo (void (T::*setter) (U), void (T::*m_setter) (U); V (T::*m_getter) (void) const; }; - return Ptr (new MemberMethod (setter, getter, checker), false); + return Ptr (new MemberMethod (setter, getter), false); } template Ptr -DoMakeParamSpecHelperTwo (void (T::*setter) (U), - V (T::*getter) (void) const) -{ - return DoMakeParamSpecHelperTwo (setter, getter, ParamSpecHelperSimpleChecker ()); -} - -template -Ptr -DoMakeParamSpecHelperTwo (V (T::*getter) (void) const, - void (T::*setter) (U), - CHECKER checker) -{ - return DoMakeParamSpecHelperTwo (setter, getter, checker); -} - -template -Ptr DoMakeParamSpecHelperTwo (V (T::*getter) (void) const, void (T::*setter) (U)) { @@ -260,20 +187,6 @@ MakeParamSpecHelper (T1 a1, T2 a2) return DoMakeParamSpecHelperTwo (a1, a2); } -template -Ptr -MakeParamSpecHelperWithChecker (T1 a1, CHECKER checker) -{ - return DoMakeParamSpecHelperOne (a1, checker); -} - -template -Ptr -MakeParamSpecHelperWithChecker (T1 a1, T2 a2, CHECKER checker) -{ - return DoMakeParamSpecHelperTwo (a1, a2, checker); -} - } // namespace ns3 #endif /* PARAM_SPEC_HELPER_H */ diff --git a/src/core/random-variable.cc b/src/core/random-variable.cc index 95aa4d4f4..ec7793abc 100644 --- a/src/core/random-variable.cc +++ b/src/core/random-variable.cc @@ -309,6 +309,12 @@ RandomVariable::operator PValue () const return ClassValueHelperConvertTo (this); } +Ptr +MakeRandomVariableChecker (void) +{ + return MakeSimpleAttributeChecker (); +} + //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- diff --git a/src/core/random-variable.h b/src/core/random-variable.h index d5bc96002..aa863c60a 100644 --- a/src/core/random-variable.h +++ b/src/core/random-variable.h @@ -673,6 +673,7 @@ Ptr MakeRandomVariableParamSpec (T1 a1); template Ptr MakeRandomVariableParamSpec (T1 a1, T2 a2); +Ptr MakeRandomVariableChecker (void); }//namespace ns3 diff --git a/src/core/uint-value.cc b/src/core/uint-value.cc index 0445e7899..28632e733 100644 --- a/src/core/uint-value.cc +++ b/src/core/uint-value.cc @@ -24,14 +24,14 @@ UintValue::Get (void) const return m_value; } std::string -UintValue::SerializeToString (Ptr spec) const +UintValue::SerializeToString (Ptr checker) const { std::ostringstream oss; oss << m_value; return oss.str (); } bool -UintValue::DeserializeFromString (std::string value, Ptr spec) +UintValue::DeserializeFromString (std::string value, Ptr checker) { uint64_t v; std::istringstream iss; @@ -59,17 +59,4 @@ 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 index 46426bffa..93b7451e3 100644 --- a/src/core/uint-value.h +++ b/src/core/uint-value.h @@ -12,8 +12,8 @@ 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); + virtual std::string SerializeToString (Ptr checker) const; + virtual bool DeserializeFromString (std::string value, Ptr checker); void Set (uint64_t value); uint64_t Get (void) const; @@ -28,76 +28,57 @@ class UintParamSpec : public ParamSpec {}; template Ptr MakeUintParamSpec (T1 a1); -template -Ptr MakeUintParamSpec (T1 a1, - uint64_t minValue, - uint64_t maxValue); template Ptr MakeUintParamSpec (T1 a1, T2 a2); -template -Ptr MakeUintParamSpec (T1 a1, T2 a2, - uint64_t minValue, - uint64_t maxValue); + +template +Ptr MakeUintChecker (void); +template +Ptr MakeUintChecker (T min, T max); } // namespace ns3 namespace ns3 { -class UintValueChecker -{ -public: - UintValueChecker (uint64_t minValue, uint64_t maxValue); - bool Check (const uint64_t &value) const; - - template - static UintValueChecker Create (U T::*) { - return UintValueChecker (std::numeric_limits::min (), - std::numeric_limits::max ()); - } - template - static UintValueChecker Create (U (T::*) (void) const) { - return UintValueChecker (std::numeric_limits::min (), - std::numeric_limits::max ()); - } - template - static UintValueChecker Create (void (T::*) (U)) { - return UintValueChecker (std::numeric_limits::min (), - std::numeric_limits::max ()); - } -private: - uint64_t m_minValue; - uint64_t m_maxValue; -}; - template Ptr MakeUintParamSpec (T1 a1) { - return MakeParamSpecHelperWithChecker (a1, - UintValueChecker::Create (a1)); + return MakeParamSpecHelper (a1); } -template -Ptr MakeUintParamSpec (T1 a1, - uint64_t minValue, - uint64_t maxValue) -{ - return MakeParamSpecHelperWithChecker (a1, - UintValueChecker (minValue, maxValue)); -} template Ptr MakeUintParamSpec (T1 a1, T2 a2) { - return MakeParamSpecHelperWithChecker (a1, a2, - UintValueChecker::Create (a1)); + return MakeParamSpecHelper (a1, a2); } -template -Ptr MakeUintParamSpec (T1 a1, T2 a2, - uint64_t minValue, - uint64_t maxValue) + +template +Ptr MakeUintChecker (void) { - return MakeParamSpecHelperWithChecker (a1, a2, - UintValueChecker (minValue, maxValue)); + return MakeUintChecker (std::numeric_limits::min (), + std::numeric_limits::max ()); +} +template +Ptr MakeUintChecker (T min, T max) +{ + struct Checker : public AttributeChecker + { + Checker (uint64_t minValue, uint64_t maxValue) + : m_minValue (minValue), + m_maxValue (maxValue) {} + virtual bool Check (PValue value) const { + const UintValue *v = value.DynCast (); + if (v == 0) + { + return false; + } + return v->Get () >= m_minValue && v->Get () <= m_maxValue; + } + uint64_t m_minValue; + uint64_t m_maxValue; + } *checker = new Checker (min, max); + return Ptr (checker, false); } } // namespace ns3 diff --git a/src/core/value-helper.h b/src/core/value-helper.h index 91850c453..29040ec80 100644 --- a/src/core/value-helper.h +++ b/src/core/value-helper.h @@ -10,6 +10,7 @@ #define VALUE_HELPER_HEADER_2(type) \ class type##Value : public Value {}; \ class type##ParamSpec : public ParamSpec {}; \ + Ptr Make##type##Checker (void); \ template \ Ptr Make##type##ParamSpec (T1 a1) \ { \ @@ -24,6 +25,10 @@ } #define VALUE_HELPER_CPP(type) \ + Ptr Make##type##Checker (void) \ + { \ + return MakeSimpleAttributeChecker (); \ + } \ type::type (PValue value) \ { \ *this = ClassValueHelperExtractFrom (value); \ diff --git a/src/core/value-test.cc b/src/core/value-test.cc index 34f7cabe2..18e53a62b 100644 --- a/src/core/value-test.cc +++ b/src/core/value-test.cc @@ -43,47 +43,57 @@ public: .SetParent () .AddParameter ("TestBoolName", "help text", BooleanValue (false), - MakeBooleanParamSpec (&ParamSpecObjectTest::m_boolTest)) + MakeBooleanParamSpec (&ParamSpecObjectTest::m_boolTest), + MakeBooleanChecker ()) .AddParameter ("TestBoolA", "help text", BooleanValue (false), MakeBooleanParamSpec (&ParamSpecObjectTest::DoSetTestB, - &ParamSpecObjectTest::DoGetTestB)) + &ParamSpecObjectTest::DoGetTestB), + MakeBooleanChecker ()) .AddParameter ("TestPtr", "help text", Ptr (0), - MakePtrParamSpec (&ParamSpecObjectTest::m_derived)) + MakePtrParamSpec (&ParamSpecObjectTest::m_derived), + MakePtrChecker ()) .AddParameter ("TestInt16", "help text", IntValue (-2), - MakeIntParamSpec (&ParamSpecObjectTest::m_int16)) + MakeIntParamSpec (&ParamSpecObjectTest::m_int16), + MakeIntChecker ()) .AddParameter ("TestInt16WithBounds", "help text", IntValue (-2), - MakeIntParamSpec (&ParamSpecObjectTest::m_int16WithBounds, - -5, 10)) + MakeIntParamSpec (&ParamSpecObjectTest::m_int16WithBounds), + MakeIntChecker (-5, 10)) .AddParameter ("TestInt16SetGet", "help text", IntValue (6), MakeIntParamSpec (&ParamSpecObjectTest::DoSetInt16, - &ParamSpecObjectTest::DoGetInt16)) + &ParamSpecObjectTest::DoGetInt16), + MakeIntChecker ()) .AddParameter ("TestUint8", "help text", UintValue (1), - MakeUintParamSpec (&ParamSpecObjectTest::m_uint8)) + MakeUintParamSpec (&ParamSpecObjectTest::m_uint8), + MakeUintChecker ()) .AddParameter ("TestEnum", "help text", EnumValue (TEST_A), - MakeEnumParamSpec (&ParamSpecObjectTest::m_enum, - TEST_A, "TestA", - TEST_B, "TestB", - TEST_C, "TestC")) + MakeEnumParamSpec (&ParamSpecObjectTest::m_enum), + MakeEnumChecker (TEST_A, "TestA", + TEST_B, "TestB", + TEST_C, "TestC")) .AddParameter ("TestRandom", "help text", ConstantVariable (1.0), - MakeRandomVariableParamSpec (&ParamSpecObjectTest::m_random)) + MakeRandomVariableParamSpec (&ParamSpecObjectTest::m_random), + MakeRandomVariableChecker ()) .AddParameter ("TestFloat", "help text", FpValue (-1.1), - MakeFpParamSpec (&ParamSpecObjectTest::m_float)) + MakeFpParamSpec (&ParamSpecObjectTest::m_float), + MakeFpChecker ()) .AddParameter ("TestVector1", "help text", ObjectVector (), - MakeObjectVectorParamSpec (&ParamSpecObjectTest::m_vector1)) + MakeObjectVectorParamSpec (&ParamSpecObjectTest::m_vector1), + MakeObjectVectorChecker ()) .AddParameter ("TestVector2", "help text", ObjectVector (), MakeObjectVectorParamSpec (&ParamSpecObjectTest::DoGetVectorN, - &ParamSpecObjectTest::DoGetVector)) + &ParamSpecObjectTest::DoGetVector), + MakeObjectVectorChecker ()) ; return tid; diff --git a/src/core/value.cc b/src/core/value.cc index 23b147f9c..0a150946e 100644 --- a/src/core/value.cc +++ b/src/core/value.cc @@ -103,14 +103,14 @@ PValue::Copy (void) const return m_value->Copy (); } std::string -PValue::SerializeToString (Ptr spec) const +PValue::SerializeToString (Ptr checker) const { - return m_value->SerializeToString (spec); + return m_value->SerializeToString (checker); } bool -PValue::DeserializeFromString (std::string value, Ptr spec) +PValue::DeserializeFromString (std::string value, Ptr checker) { - return m_value->DeserializeFromString (value, spec); + return m_value->DeserializeFromString (value, checker); } PValue::PValue (const char *value) @@ -141,6 +141,25 @@ ParamSpec::Unref (void) const ParamSpec::~ParamSpec () {} +AttributeChecker::AttributeChecker () + : m_count (1) +{} +void +AttributeChecker::Ref (void) const +{ + m_count++; +} +void +AttributeChecker::Unref (void) const +{ + m_count--; + if (m_count == 0) + { + delete this; + } +} +AttributeChecker::~AttributeChecker () +{} StringValue::StringValue (const char *value) : m_value (value) @@ -164,12 +183,12 @@ StringValue::Copy (void) const return PValue::Create (*this); } std::string -StringValue::SerializeToString (Ptr spec) const +StringValue::SerializeToString (Ptr checker) const { return m_value; } bool -StringValue::DeserializeFromString (std::string value, Ptr spec) +StringValue::DeserializeFromString (std::string value, Ptr checker) { m_value = value; return true; @@ -190,7 +209,7 @@ StringValue::operator PValue () const std::string -PtrValueBase::SerializeToString (Ptr spec) const +PtrValueBase::SerializeToString (Ptr checker) const { std::ostringstream oss; oss << PeekObjectBase (); @@ -198,7 +217,7 @@ PtrValueBase::SerializeToString (Ptr spec) const } bool -PtrValueBase::DeserializeFromString (std::string value, Ptr spec) +PtrValueBase::DeserializeFromString (std::string value, Ptr checker) { // XXX return false; diff --git a/src/core/value.h b/src/core/value.h index 87e33de70..aaadfe2e9 100644 --- a/src/core/value.h +++ b/src/core/value.h @@ -9,6 +9,7 @@ namespace ns3 { class ParamSpec; +class AttributeChecker; class PValue; class Value @@ -20,8 +21,8 @@ public: virtual ~Value (); virtual PValue Copy (void) const = 0; - virtual std::string SerializeToString (Ptr spec) const = 0; - virtual bool DeserializeFromString (std::string value, Ptr spec) = 0; + virtual std::string SerializeToString (Ptr checker) const = 0; + virtual bool DeserializeFromString (std::string value, Ptr checker) = 0; private: friend class PValue; uint32_t m_count; @@ -36,8 +37,8 @@ public: ~PValue (); PValue Copy (void) const; - std::string SerializeToString (Ptr spec) const; - bool DeserializeFromString (std::string value, Ptr spec); + std::string SerializeToString (Ptr checker) const; + bool DeserializeFromString (std::string value, Ptr checker); template static PValue Create (void); @@ -75,11 +76,25 @@ public: */ virtual bool Set (ObjectBase * object, PValue value) const = 0; virtual bool Get (const ObjectBase * object, PValue value) const = 0; +private: + mutable uint32_t m_count; +}; + +class AttributeChecker : public ObjectBase +{ +public: + AttributeChecker (); + void Ref (void) const; + void Unref (void) const; + virtual ~AttributeChecker (); virtual bool Check (PValue value) const = 0; private: mutable uint32_t m_count; }; +template +Ptr +MakeSimpleAttributeChecker (void); template Ptr @@ -110,8 +125,8 @@ public: std::string Get (void) const; virtual PValue Copy (void) const; - virtual std::string SerializeToString (Ptr spec) const; - virtual bool DeserializeFromString (std::string value, Ptr spec); + virtual std::string SerializeToString (Ptr checker) const; + virtual bool DeserializeFromString (std::string value, Ptr checker); StringValue (PValue value); operator PValue () const; @@ -130,8 +145,8 @@ class PtrValueBase : public Value public: virtual ObjectBase *PeekObjectBase (void) const = 0; virtual bool SetObjectBase (ObjectBase *object) = 0; - virtual std::string SerializeToString (Ptr spec) const; - virtual bool DeserializeFromString (std::string value, Ptr spec); + virtual std::string SerializeToString (Ptr checker) const; + virtual bool DeserializeFromString (std::string value, Ptr checker); }; /******************************************************** @@ -253,23 +268,6 @@ public: } return value->SetObjectBase (PeekPointer (DoGet (obj))); } - virtual bool Check (PValue val) const { - const PtrValueBase *value = val.DynCast (); - if (value == 0) - { - return false; - } - if (value->PeekObjectBase () == 0) - { - return true; - } - U *ptr = dynamic_cast (value->PeekObjectBase ()); - if (ptr == 0) - { - return false; - } - return true; - } private: virtual void DoSet (T *object, Ptr value) const = 0; virtual Ptr DoGet (const T *object) const = 0; @@ -340,6 +338,47 @@ MakePtrParamSpec (Ptr (T::*getter) (void) const) return Ptr (spec, false); } +template +Ptr +MakePtrChecker (void) +{ + struct PtrChecker : public AttributeChecker + { + virtual bool Check (PValue val) const { + const PtrValueBase *value = val.DynCast (); + if (value == 0) + { + return false; + } + if (value->PeekObjectBase () == 0) + { + return true; + } + T *ptr = dynamic_cast (value->PeekObjectBase ()); + if (ptr == 0) + { + return false; + } + return true; + } + } *checker = new PtrChecker (); + return Ptr (checker, false); +} + +template +Ptr +MakeSimpleAttributeChecker (void) +{ + struct SimpleAttributeChecker : public AttributeChecker + { + virtual bool Check (PValue value) const { + return value.DynCast () != 0; + } + } *checker = new SimpleAttributeChecker (); + return Ptr (checker, false); +} + + } // namespace ns3 #endif /* VALUE_H */ diff --git a/src/mobility/hierarchical-mobility-model.cc b/src/mobility/hierarchical-mobility-model.cc index 73151c1f4..959c15957 100644 --- a/src/mobility/hierarchical-mobility-model.cc +++ b/src/mobility/hierarchical-mobility-model.cc @@ -32,10 +32,12 @@ HierarchicalMobilityModel::GetTypeId (void) .AddConstructor () .AddParameter ("child", "The child mobility model.", Ptr (0), - MakePtrParamSpec (&HierarchicalMobilityModel::SetChild)) + MakePtrParamSpec (&HierarchicalMobilityModel::SetChild), + MakePtrChecker ()) .AddParameter ("parent", "The parent mobility model.", Ptr (0), - MakePtrParamSpec (&HierarchicalMobilityModel::SetParent)) + MakePtrParamSpec (&HierarchicalMobilityModel::SetParent), + MakePtrChecker ()) ; return tid; } diff --git a/src/mobility/mobility-model.cc b/src/mobility/mobility-model.cc index 941cfdc63..86f067fdf 100644 --- a/src/mobility/mobility-model.cc +++ b/src/mobility/mobility-model.cc @@ -32,11 +32,13 @@ MobilityModel::GetTypeId (void) TypeId::PARAM_SGC, Vector (0.0, 0.0, 0.0), MakeVectorParamSpec (&MobilityModel::SetPosition, - &MobilityModel::GetPosition)) + &MobilityModel::GetPosition), + MakeVectorChecker ()) .AddParameter ("velocity", "The current velocity of the mobility model.", TypeId::PARAM_GET, Vector (0.0, 0.0, 0.0), // ignored initial value. - MakeVectorParamSpec (&MobilityModel::GetVelocity)) + MakeVectorParamSpec (&MobilityModel::GetVelocity), + MakeVectorChecker ()) ; return tid; } diff --git a/src/mobility/position-allocator.cc b/src/mobility/position-allocator.cc index d4d21afdd..ac23e85d2 100644 --- a/src/mobility/position-allocator.cc +++ b/src/mobility/position-allocator.cc @@ -55,24 +55,29 @@ GridPositionAllocator::GetTypeId (void) .AddConstructor () .AddParameter ("GridWidth", "The number of objects layed out on a line.", IntValue (10), - MakeIntParamSpec (&GridPositionAllocator::m_n)) + MakeIntParamSpec (&GridPositionAllocator::m_n), + MakeIntChecker ()) .AddParameter ("MinX", "The x coordinate where the grid starts.", FpValue (1.0), - MakeFpParamSpec (&GridPositionAllocator::m_xMin)) + MakeFpParamSpec (&GridPositionAllocator::m_xMin), + MakeFpChecker ()) .AddParameter ("MinY", "The y coordinate where the grid starts.", FpValue (0.0), - MakeFpParamSpec (&GridPositionAllocator::m_yMin)) + MakeFpParamSpec (&GridPositionAllocator::m_yMin), + MakeFpChecker ()) .AddParameter ("DeltaX", "The x space between objects.", FpValue (1.0), - MakeFpParamSpec (&GridPositionAllocator::m_deltaX)) + MakeFpParamSpec (&GridPositionAllocator::m_deltaX), + MakeFpChecker ()) .AddParameter ("DeltaY", "The y space between objects.", FpValue (1.0), - MakeFpParamSpec (&GridPositionAllocator::m_deltaY)) + MakeFpParamSpec (&GridPositionAllocator::m_deltaY), + MakeFpChecker ()) .AddParameter ("LayoutType", "The type of layout.", EnumValue (ROW_FIRST), - MakeEnumParamSpec (&GridPositionAllocator::m_layoutType, - ROW_FIRST, "RowFirst", - COLUMN_FIRST, "ColumnFirst")) + MakeEnumParamSpec (&GridPositionAllocator::m_layoutType), + MakeEnumChecker (ROW_FIRST, "RowFirst", + COLUMN_FIRST, "ColumnFirst")) ; return tid; } @@ -111,11 +116,13 @@ RandomRectanglePositionAllocator::GetTypeId (void) .AddParameter ("X", "A random variable which represents the x coordinate of a position in a random rectangle.", UniformVariable (0.0, 1.0), - MakeRandomVariableParamSpec (&RandomRectanglePositionAllocator::m_x)) + MakeRandomVariableParamSpec (&RandomRectanglePositionAllocator::m_x), + MakeRandomVariableChecker ()) .AddParameter ("Y", "A random variable which represents the y coordinate of a position in a random rectangle.", UniformVariable (0.0, 1.0), - MakeRandomVariableParamSpec (&RandomRectanglePositionAllocator::m_y)); + MakeRandomVariableParamSpec (&RandomRectanglePositionAllocator::m_y), + MakeRandomVariableChecker ()); return tid; } @@ -143,19 +150,24 @@ RandomDiscPositionAllocator::GetTypeId (void) .AddParameter ("Theta", "A random variable which represents the angle (gradients) of a position in a random disc.", UniformVariable (0.0, 6.2830), - MakeRandomVariableParamSpec (&RandomDiscPositionAllocator::m_theta)) + MakeRandomVariableParamSpec (&RandomDiscPositionAllocator::m_theta), + MakeRandomVariableChecker ()) .AddParameter ("Rho", "A random variable which represents the radius of a position in a random disc.", UniformVariable (0.0, 200.0), - MakeRandomVariableParamSpec (&RandomDiscPositionAllocator::m_rho)) + MakeRandomVariableParamSpec (&RandomDiscPositionAllocator::m_rho), + MakeRandomVariableChecker ()) .AddParameter ("X", "The x coordinate of the center of the random position disc.", FpValue (0.0), - MakeFpParamSpec (&RandomDiscPositionAllocator::m_x)) + MakeFpParamSpec (&RandomDiscPositionAllocator::m_x), + MakeFpChecker ()) .AddParameter ("Y", "The y coordinate of the center of the random position disc.", FpValue (0.0), - MakeFpParamSpec (&RandomDiscPositionAllocator::m_y)); + MakeFpParamSpec (&RandomDiscPositionAllocator::m_y), + MakeFpChecker ()) + ; return tid; } diff --git a/src/mobility/random-direction-2d-mobility-model.cc b/src/mobility/random-direction-2d-mobility-model.cc index 1f1f42272..94c197afe 100644 --- a/src/mobility/random-direction-2d-mobility-model.cc +++ b/src/mobility/random-direction-2d-mobility-model.cc @@ -43,13 +43,17 @@ RandomDirection2dMobilityModel::GetTypeId (void) .AddConstructor () .AddParameter ("bounds", "The 2d bounding area", Rectangle (-100, 100, -100, 100), - MakeRectangleParamSpec (&RandomDirection2dMobilityModel::m_bounds)) + MakeRectangleParamSpec (&RandomDirection2dMobilityModel::m_bounds), + MakeRectangleChecker ()) .AddParameter ("speed", "A random variable to control the speed (m/s).", UniformVariable (1.0, 2.0), - MakeRandomVariableParamSpec (&RandomDirection2dMobilityModel::m_speed)) + MakeRandomVariableParamSpec (&RandomDirection2dMobilityModel::m_speed), + MakeRandomVariableChecker ()) .AddParameter ("pause", "A random variable to control the pause (s).", ConstantVariable (2.0), - MakeRandomVariableParamSpec (&RandomDirection2dMobilityModel::m_pause)); + MakeRandomVariableParamSpec (&RandomDirection2dMobilityModel::m_pause), + MakeRandomVariableChecker ()) + ; return tid; } diff --git a/src/mobility/random-walk-2d-mobility-model.cc b/src/mobility/random-walk-2d-mobility-model.cc index 1953c0634..6022f1674 100644 --- a/src/mobility/random-walk-2d-mobility-model.cc +++ b/src/mobility/random-walk-2d-mobility-model.cc @@ -40,30 +40,35 @@ RandomWalk2dMobilityModel::GetTypeId (void) .AddParameter ("bounds", "Bounds of the area to cruise.", Rectangle (0.0, 0.0, 100.0, 100.0), - MakeRectangleParamSpec (&RandomWalk2dMobilityModel::m_bounds)) + MakeRectangleParamSpec (&RandomWalk2dMobilityModel::m_bounds), + MakeRectangleChecker ()) .AddParameter ("time", "Change current direction and speed after moving for this delay.", Seconds (1.0), - MakeTimeParamSpec (&RandomWalk2dMobilityModel::m_modeTime)) + MakeTimeParamSpec (&RandomWalk2dMobilityModel::m_modeTime), + MakeTimeChecker ()) .AddParameter ("distance", "Change current direction and speed after moving for this distance.", Seconds (1.0), - MakeTimeParamSpec (&RandomWalk2dMobilityModel::m_modeTime)) + MakeTimeParamSpec (&RandomWalk2dMobilityModel::m_modeTime), + MakeTimeChecker ()) .AddParameter ("mode", "The mode indicates the condition used to " "change the current speed and direction", EnumValue (RandomWalk2dMobilityModel::MODE_DISTANCE), - MakeEnumParamSpec (&RandomWalk2dMobilityModel::m_mode, - RandomWalk2dMobilityModel::MODE_DISTANCE, "Distance", - RandomWalk2dMobilityModel::MODE_TIME, "Time")) + MakeEnumParamSpec (&RandomWalk2dMobilityModel::m_mode), + MakeEnumChecker (RandomWalk2dMobilityModel::MODE_DISTANCE, "Distance", + RandomWalk2dMobilityModel::MODE_TIME, "Time")) .AddParameter ("direction", "A random variable used to pick the direction (gradients).", UniformVariable (0.0, 6.283184), - MakeRandomVariableParamSpec (&RandomWalk2dMobilityModel::m_direction)) + MakeRandomVariableParamSpec (&RandomWalk2dMobilityModel::m_direction), + MakeRandomVariableChecker ()) .AddParameter ("speed", "A random variable used to pick the speed (m/s).", UniformVariable (2.0, 4.0), - MakeRandomVariableParamSpec (&RandomWalk2dMobilityModel::m_speed)); + MakeRandomVariableParamSpec (&RandomWalk2dMobilityModel::m_speed), + MakeRandomVariableChecker ()); return tid; } diff --git a/src/mobility/random-waypoint-mobility-model.cc b/src/mobility/random-waypoint-mobility-model.cc index 52def3c6a..26b3520f8 100644 --- a/src/mobility/random-waypoint-mobility-model.cc +++ b/src/mobility/random-waypoint-mobility-model.cc @@ -39,15 +39,18 @@ RandomWaypointMobilityModel::GetTypeId (void) .AddParameter ("speed", "A random variable used to pick the speed of a random waypoint model.", UniformVariable (0.3, 0.7), - MakeRandomVariableParamSpec (&RandomWaypointMobilityModel::m_speed)) + MakeRandomVariableParamSpec (&RandomWaypointMobilityModel::m_speed), + MakeRandomVariableChecker ()) .AddParameter ("pause", "A random variable used to pick the pause of a random waypoint model.", ConstantVariable (2.0), - MakeRandomVariableParamSpec (&RandomWaypointMobilityModel::m_pause)) + MakeRandomVariableParamSpec (&RandomWaypointMobilityModel::m_pause), + MakeRandomVariableChecker ()) .AddParameter ("position", "The position model used to pick a destination point.", Ptr (0), - MakePtrParamSpec (&RandomWaypointMobilityModel::m_position)); + MakePtrParamSpec (&RandomWaypointMobilityModel::m_position), + MakePtrChecker ()); return tid; } diff --git a/src/node/drop-tail-queue.cc b/src/node/drop-tail-queue.cc index 7e92adda9..703fed3a9 100644 --- a/src/node/drop-tail-queue.cc +++ b/src/node/drop-tail-queue.cc @@ -34,7 +34,8 @@ TypeId DropTailQueue::GetTypeId (void) .AddConstructor () .AddParameter ("MaxPackets", "The maximum number of packets accepted by this DropTailQueue.", UintValue (100), - MakeUintParamSpec (&DropTailQueue::m_maxPackets)) + MakeUintParamSpec (&DropTailQueue::m_maxPackets), + MakeUintChecker ()) ; return tid; diff --git a/src/simulator/nstime.h b/src/simulator/nstime.h index c97f5f884..5e8becbd1 100644 --- a/src/simulator/nstime.h +++ b/src/simulator/nstime.h @@ -678,6 +678,9 @@ Ptr MakeTimeParamSpec (T1 a1); template Ptr MakeTimeParamSpec (T1 a1, T2 a2); +Ptr MakeTimeChecker (void); + + } // namespace ns3 @@ -696,6 +699,7 @@ Ptr MakeTimeParamSpec (T1 a1, T2 a2) } + } // namespace ns3 #endif /* TIME_H */ diff --git a/src/simulator/time.cc b/src/simulator/time.cc index a98072e00..c4d7040c7 100644 --- a/src/simulator/time.cc +++ b/src/simulator/time.cc @@ -305,6 +305,11 @@ TimeUnit<1>::operator PValue () const return ClassValueHelperConvertTo (this); } +Ptr MakeTimeChecker (void) +{ + return MakeSimpleAttributeChecker