add automatic conversion from string to PValue.
--- a/src/core/object.cc Fri Feb 08 02:22:36 2008 +0100
+++ b/src/core/object.cc Fri Feb 08 04:11:08 2008 +0100
@@ -643,21 +643,6 @@
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 @@
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 @@
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<const ParamSpec> 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 @@
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 @@
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 @@
NotifyConstructionCompleted ();
}
bool
+Object::DoSet (Ptr<const ParamSpec> 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 @@
{
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
--- a/src/core/object.h Fri Feb 08 02:22:36 2008 +0100
+++ b/src/core/object.h Fri Feb 08 04:11:08 2008 +0100
@@ -294,12 +294,9 @@
* 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 @@
bool DoSet (Ptr<const ParamSpec> spec, PValue param);
- bool DoSet (Ptr<const ParamSpec> spec, std::string value);
void DoSetOne (Ptr<const ParamSpec> spec, PValue param);
std::string LookupParameterFullNameByParamSpec (Ptr<const ParamSpec> spec) const;
@@ -360,7 +356,6 @@
*
* 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 @@
friend Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7);
+ bool DoSet (Ptr<const ParamSpec> spec, PValue value);
Ptr<Object> DoQueryInterface (TypeId tid) const;
void DoCollectSources (std::string path, const TraceContext &context,
TraceResolver::SourceCollection *collection) const;
@@ -746,18 +742,6 @@
return CreateObject<T> (parameters);
}
-template <typename T>
-Ptr<T>
-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<T> (parameters);
-}
-
} // namespace ns3
#endif /* OBJECT_H */
--- a/src/core/value.cc Fri Feb 08 02:22:36 2008 +0100
+++ b/src/core/value.cc Fri Feb 08 04:11:08 2008 +0100
@@ -1,5 +1,6 @@
#include "value.h"
#include "log.h"
+#include "fatal-error.h"
#include <sstream>
NS_LOG_COMPONENT_DEFINE ("Value");
@@ -19,6 +20,11 @@
}
Value::~Value ()
{}
+bool
+Value::ConvertFrom (PValue value, Ptr<const ParamSpec> spec)
+{
+ return false;
+}
PValue::PValue ()
: m_value (0)
@@ -90,6 +96,24 @@
{
return m_value->DeserializeFromString (value, spec);
}
+bool
+PValue::ConvertFrom (PValue value, Ptr<const ParamSpec> spec)
+{
+ const StringValue *str = value.DynCast<const StringValue *> ();
+ 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::~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<StringValue> (*this);
+}
+std::string
+StringValue::SerializeToString (Ptr<const ParamSpec> spec) const
+{
+ return m_value;
+}
+bool
+StringValue::DeserializeFromString (std::string value, Ptr<const ParamSpec> spec)
+{
+ m_value = value;
+ return true;
+}
+StringValue::StringValue (PValue value)
+{
+ const StringValue *v = value.DynCast<const StringValue *> ();
+ if (v == 0)
+ {
+ NS_FATAL_ERROR ("Expected value of type String.");
+ }
+ m_value = v->Get ();
+}
+StringValue::operator PValue () const
+{
+ return PValue::Create<StringValue> (*this);
+}
+
+
std::string
PtrValueBase::SerializeToString (Ptr<const ParamSpec> spec) const
{
--- a/src/core/value.h Fri Feb 08 02:22:36 2008 +0100
+++ b/src/core/value.h Fri Feb 08 04:11:08 2008 +0100
@@ -22,6 +22,7 @@
virtual PValue Copy (void) const = 0;
virtual std::string SerializeToString (Ptr<const ParamSpec> spec) const = 0;
virtual bool DeserializeFromString (std::string value, Ptr<const ParamSpec> spec) = 0;
+ virtual bool ConvertFrom (PValue value, Ptr<const ParamSpec> spec);
private:
friend class PValue;
uint32_t m_count;
@@ -38,6 +39,7 @@
PValue Copy (void) const;
std::string SerializeToString (Ptr<const ParamSpec> spec) const;
bool DeserializeFromString (std::string value, Ptr<const ParamSpec> spec);
+ bool ConvertFrom (PValue value, Ptr<const ParamSpec> spec);
template <typename T>
static PValue Create (void);
@@ -52,6 +54,8 @@
template <typename T>
operator Ptr<T> ();
+ PValue (const char *value);
+ PValue (std::string value);
private:
PValue (Value *value);
Value *m_value;
@@ -98,6 +102,29 @@
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<const ParamSpec> spec) const;
+ virtual bool DeserializeFromString (std::string value, Ptr<const ParamSpec> spec);
+
+ StringValue (PValue value);
+ operator PValue () const;
+private:
+ std::string m_value;
+};
+
+
+/********************************************************
* The class used to access the pointer stored in a
* PtrValue<T> Value instance.
********************************************************/