Added back corrected version of TimeUnit(string)

This commit is contained in:
Unknown
2007-03-25 13:11:20 -04:00
parent 8a2cff0607
commit be28cb56ff
3 changed files with 50 additions and 1 deletions

View File

@@ -60,7 +60,7 @@ uint32_t OnOffApplication::g_defaultSize = 512;
m_cbrRate(rate),
m_pktSize(size),
m_residualBits(0),
m_lastStartTime(0),
m_lastStartTime((HighPrecision)0),
m_maxBytes(0xffffffff),
m_totBytes(0),
m_startStopScheduled(false),

View File

@@ -308,6 +308,21 @@ class TimeUnit<1>
// -*- New methods -*-
public:
/**
* \brief String constructor
* Construct TimeUnit<1> object from common time expressions like "
* 1ms" or "10s". Supported units include:
* - s (seconds)
* - ms (milliseconds)
* - us (microseconds)
* - ns (nanoseconds)
*
* There can be no white space between the numerical portion
* and the units. Any otherwise malformed string causes a fatal error to
* occur.
* \param s The string to parse into a TimeUnit<1>
*/
TimeUnit<1>(const std::string& s);
/**
* \returns an approximation in seconds of the time stored in this
* instance.

View File

@@ -20,9 +20,43 @@
*/
#include "time.h"
#include "simulator.h"
#include "ns3/fatal-error.h"
namespace ns3 {
TimeUnit<1>::TimeUnit(const std::string& s)
{
std::string::size_type n = s.find_first_not_of("0123456789.");
if (n != std::string::npos)
{ // Found non-numeric
double r = atof(s.substr(0, n).c_str());
std::string trailer = s.substr(n, std::string::npos);
if (trailer == std::string("s"))
{
m_data = HighPrecision (r * 1000000000.0);
return;
}
if (trailer == std::string("ms"))
{
m_data = HighPrecision ((int64_t)(r * 1000000), false);
return;
}
if (trailer == std::string("us"))
{
m_data = HighPrecision ((int64_t)(r * 1000), false);
return;
}
if (trailer == std::string("ns"))
{
m_data = HighPrecision ((int64_t)r, false);
return;
}
NS_FATAL_ERROR("Can't Parse Time "<<s);
}
//else
//they didn't provide units, assume seconds
m_data = HighPrecision (atof(s.c_str()) * 1000000000.0);
}
double
TimeUnit<1>::GetSeconds (void) const
{