diff --git a/src/simulator/time-default-value.cc b/src/simulator/time-default-value.cc new file mode 100644 index 000000000..46449f4c2 --- /dev/null +++ b/src/simulator/time-default-value.cc @@ -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 + */ + +#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 diff --git a/src/simulator/time-default-value.h b/src/simulator/time-default-value.h new file mode 100644 index 000000000..87d0c7fcb --- /dev/null +++ b/src/simulator/time-default-value.h @@ -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 + */ +#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 */ diff --git a/src/simulator/wscript b/src/simulator/wscript index b77ded840..c9f14bd67 100644 --- a/src/simulator/wscript +++ b/src/simulator/wscript @@ -63,6 +63,7 @@ def build(bld): 'scheduler-map.cc', 'event-impl.cc', 'simulator.cc', + 'time-default-value.cc', ] sim.includes = '.' @@ -76,6 +77,7 @@ def build(bld): 'scheduler.h', 'scheduler-factory.h', 'simulation-singleton.h', + 'time-default-value.h', ] env = bld.env_of_name('default')