TimeDefaultValue
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 23 Jul 2007 14:41:53 +0200
changeset 961 fee9facb6ab2
parent 960 f75042c35fc6
child 962 473a781ec774
TimeDefaultValue
src/simulator/time-default-value.cc
src/simulator/time-default-value.h
src/simulator/wscript
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/simulator/time-default-value.cc	Mon Jul 23 14:41:53 2007 +0200
@@ -0,0 +1,106 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2007 INRIA
+ * All rights reserved.
+ *
+ * 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 <mathieu.lacage@sophia.inria.fr>
+ */
+
+#include "time-default-value.h"
+
+namespace ns3 {
+
+TimeDefaultValue::TimeDefaultValue (const std::string name,
+				    const std::string help,
+				    Time defaultValue)
+  : DefaultValueBase (name, help),
+    m_defaultValue (defaultValue),
+    m_value (defaultValue)
+{
+  DefaultValueList::Add (this);
+}
+Time 
+TimeDefaultValue::GetValue (void) const
+{
+  return m_value;
+}
+bool 
+TimeDefaultValue::DoParseValue (const std::string &value)
+{
+  std::string::size_type n = value.find_first_not_of("0123456789.");
+  if (n == std::string::npos)
+    {
+      return false;
+    }
+  std::string trailer = value.substr(n, std::string::npos);
+  std::istringstream iss;
+  iss.str (value.substr(0, n));
+
+  if (trailer == std::string("s"))
+    {
+      double v;
+      iss >> v;
+      m_value = 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_value = MilliSeconds (integer);
+      return true;
+    }
+  if (trailer == std::string("us"))
+    {
+      m_value = MicroSeconds (integer);
+      return true;
+    }
+  if (trailer == std::string("ns"))
+    {
+      m_value = NanoSeconds (integer);
+      return true;
+    }
+  if (trailer == std::string("ps"))
+    {
+      m_value = PicoSeconds (integer);
+      return true;
+    }
+  if (trailer == std::string("fs"))
+    {
+      m_value = FemtoSeconds (integer);
+      return true;
+    }
+  return false;
+}
+std::string 
+TimeDefaultValue::DoGetType (void) const
+{
+  return "(s|ms|us|ns|ps|fs)";
+}
+std::string 
+TimeDefaultValue::DoGetDefaultValue (void) const
+{
+  std::ostringstream oss;
+  oss << m_value.GetSeconds () << "s";
+  return oss.str ();
+}
+
+
+} // namespace ns3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/simulator/time-default-value.h	Mon Jul 23 14:41:53 2007 +0200
@@ -0,0 +1,70 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2007 INRIA
+ * All rights reserved.
+ *
+ * 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 <mathieu.lacage@sophia.inria.fr>
+ */
+#ifndef TIME_DEFAULT_VALUE_H
+#define TIME_DEFAULT_VALUE_H
+
+#include "ns3/default-value.h"
+#include "ns3/nstime.h"
+
+namespace ns3 {
+
+/**
+ * \ingroup config
+ * \brief a ns3::Time variable for ns3::Bind
+ *
+ * Every instance of this type is automatically 
+ * registered in the variable pool which is used
+ * by ns3::Bind. 
+ */
+class TimeDefaultValue : public DefaultValueBase
+{
+public:
+  /**
+   * \param name name of variable
+   * \param help help text which explains the purpose
+   *        and the semantics of this variable
+   * \param defaultValue the default value to assign
+   *        to this variable.
+   *
+   * Unless the user invokes ns3::Bind with the right arguments,
+   * the GetValue method will return the default value. Otherwise,
+   * it will return the user-specified value.
+   */
+  TimeDefaultValue (const std::string name,
+		    const std::string help,
+		    Time defaultValue);
+  /**
+   * \returns the default value for this variable or a
+   *          user-provided overriden variable.
+   */
+  Time GetValue (void) const;
+private:
+  virtual bool DoParseValue (const std::string &value);
+  virtual std::string DoGetType (void) const;
+  virtual std::string DoGetDefaultValue (void) const;
+
+  Time m_defaultValue;
+  Time m_value;
+};
+
+} // namespace ns3
+
+#endif /* TIME_DEFAULT_VALUE_H */
--- a/src/simulator/wscript	Mon Jul 23 14:41:34 2007 +0200
+++ b/src/simulator/wscript	Mon Jul 23 14:41:53 2007 +0200
@@ -63,6 +63,7 @@
         'scheduler-map.cc',
         'event-impl.cc',
         'simulator.cc',
+        'time-default-value.cc',
         ]
     sim.includes = '.'
 
@@ -76,6 +77,7 @@
         'scheduler.h',
         'scheduler-factory.h',
         'simulation-singleton.h',
+        'time-default-value.h',
         ]
 
     env = bld.env_of_name('default')