add StringDefaultValue
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 15 Oct 2007 12:58:53 +0200
changeset 1962 600b310a3218
parent 1961 c61eab0e89c4
child 1963 007214146da7
add StringDefaultValue
src/core/default-value.cc
src/core/default-value.h
--- a/src/core/default-value.cc	Thu Oct 11 16:53:42 2007 +0200
+++ b/src/core/default-value.cc	Mon Oct 15 12:58:53 2007 +0200
@@ -317,6 +317,86 @@
 }
 
 
+StringDefaultValue::StringDefaultValue (const std::string &name,
+                                        const std::string &help,
+                                        const std::string defaultValue)
+  : DefaultValueBase (name, help),
+    m_defaultValue (defaultValue),
+    m_value (defaultValue),
+    m_minSize (0),
+    m_maxSize (-1)
+{
+  DefaultValueList::Add (this);
+}
+StringDefaultValue::StringDefaultValue (const std::string &name,
+                                        const std::string &help,
+                                        const std::string defaultValue,
+                                        int maxSize)
+  : DefaultValueBase (name, help),
+    m_defaultValue (defaultValue),
+    m_value (defaultValue),
+    m_minSize (0),
+    m_maxSize (maxSize)
+{
+  DefaultValueList::Add (this);
+}
+StringDefaultValue::StringDefaultValue (const std::string &name,
+                                        const std::string &help,
+                                        const std::string defaultValue,
+                                        int minSize,
+                                        int maxSize)
+  : DefaultValueBase (name, help),
+    m_defaultValue (defaultValue),
+    m_value (defaultValue),
+    m_minSize (minSize),
+    m_maxSize (maxSize)
+{
+  DefaultValueList::Add (this);
+}
+
+
+std::string 
+StringDefaultValue::GetValue (void) const
+{
+  return m_value;
+}
+
+bool 
+StringDefaultValue::DoParseValue (const std::string &value)
+{
+  if ((int)value.size () < m_minSize)
+    {
+      return false;
+    }
+  if (m_maxSize != -1 && (int)value.size () > m_maxSize)
+    {
+      return false;
+    }
+  m_value = value;
+  return true;
+}
+std::string 
+StringDefaultValue::DoGetType (void) const
+{
+  if (m_maxSize == -1)
+    {
+      return "string:0";
+    }
+  else 
+    {
+      std::ostringstream oss;
+      oss << "string:0:" << m_maxSize;
+      return oss.str ();
+    }
+}
+std::string 
+StringDefaultValue::DoGetDefaultValue (void) const
+{
+  return m_defaultValue;
+}
+
+
+
 }//namespace ns3
 
 #ifdef RUN_SELF_TESTS
--- a/src/core/default-value.h	Thu Oct 11 16:53:42 2007 +0200
+++ b/src/core/default-value.h	Mon Oct 15 12:58:53 2007 +0200
@@ -340,6 +340,42 @@
 };
 
 /**
+ * \brief A string variable for ns3::Bind
+ * \ingroup config
+ *
+ * Every instance of this type is automatically 
+ * registered in the variable pool which is used
+ * by ns3::Bind. 
+ */
+class StringDefaultValue : public DefaultValueBase
+{
+public:
+  StringDefaultValue (const std::string &name,
+                      const std::string &help,
+                      const std::string defaultValue);
+  StringDefaultValue (const std::string &name,
+                      const std::string &help,
+                      const std::string defaultValue, 
+                      int maxSize);
+  StringDefaultValue (const std::string &name,
+                      const std::string &help,
+                      const std::string defaultValue, 
+                      int minSize,
+                      int maxSize);
+
+  std::string GetValue (void) const;
+private:
+  virtual bool DoParseValue (const std::string &value);
+  virtual std::string DoGetType (void) const;
+  virtual std::string DoGetDefaultValue (void) const;
+
+  std::string m_defaultValue;
+  std::string m_value;
+  int m_minSize;
+  int m_maxSize;
+};
+
+/**
  * \brief Class used to call a certain function during the configuration of the
  * simulation
  * \ingroup config