OpenMPCD
ImplementationDetails/OnTheFlyStatistics.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Implements functionality of `OpenMPCD::OnTheFlyStatistics`.
4  */
5 
6 #ifndef OPENMPCD_IMPLEMENTATIONDETAILS_ONTHEFLYSTATISTICS_HPP
7 #define OPENMPCD_IMPLEMENTATIONDETAILS_ONTHEFLYSTATISTICS_HPP
8 
10 
11 #include <boost/algorithm/string.hpp>
12 #include <boost/lexical_cast.hpp>
13 #include <boost/static_assert.hpp>
14 #include <boost/type_traits/is_arithmetic.hpp>
15 
16 #include <sstream>
17 #include <vector>
18 
19 namespace OpenMPCD
20 {
21 
22 template<typename T>
24 {
25  BOOST_STATIC_ASSERT(boost::is_arithmetic<T>::value);
26 
27  std::stringstream ss;
28 
29  ss.precision(std::numeric_limits<T>::digits10 + 2);
30 
31  ss << "1;"; //format version
32  ss << sampleSize << ";";
33  ss << mean << ";";
34  ss << varianceHelper;
35 
36  return ss.str();
37 }
38 
39 template<typename T>
40 void OnTheFlyStatistics<T>::unserializeFromString(const std::string& state)
41 {
42  BOOST_STATIC_ASSERT(boost::is_arithmetic<T>::value);
43 
44  std::vector<std::string> parts;
45  boost::algorithm::split(parts, state, boost::algorithm::is_any_of(";"));
46 
47  try
48  {
49  if(parts[0][0] == '-')
51  const unsigned int version =
52  boost::lexical_cast<unsigned int>(parts[0]);
53 
54  if(version != 1)
55  OPENMPCD_THROW(InvalidArgumentException, "Unknown version");
56 
57  if(parts.size() != 4)
59 
60  if(parts[1][0] == '-')
62 
63  const std::size_t _sampleSize =
64  boost::lexical_cast<std::size_t>(parts[1]);
65  const T _mean = boost::lexical_cast<T>(parts[2]);
66  const T _varianceHelper = boost::lexical_cast<T>(parts[3]);
67 
68  if(_sampleSize == 0)
69  {
70  if(_mean != 0 || _varianceHelper != 0)
72  }
73  if(_varianceHelper < 0)
75 
76  sampleSize = _sampleSize;
77  mean = _mean;
78  varianceHelper = _varianceHelper;
79  }
80  catch(const boost::bad_lexical_cast&)
81  {
83  }
84 }
85 
86 } //namespace OpenMPCD
87 
88 #endif //OPENMPCD_IMPLEMENTATIONDETAILS_ONTHEFLYSTATISTICS_HPP
OPENMPCD_THROW
#define OPENMPCD_THROW(ExceptionType, message)
Throws the given ExceptionType, passing the given message along with file and line number information...
Definition: Exceptions.hpp:22
OpenMPCD::OnTheFlyStatistics::serializeToString
const std::string serializeToString() const
Returns a string that contains the state of this instance.
Definition: ImplementationDetails/OnTheFlyStatistics.hpp:23
OpenMPCD::OnTheFlyStatistics::unserializeFromString
void unserializeFromString(const std::string &state)
Discards the current state, and loads the state specified in the given string instead.
Definition: ImplementationDetails/OnTheFlyStatistics.hpp:40
OnTheFlyStatistics.hpp
OpenMPCD::InvalidArgumentException
Invalid argument exception.
Definition: Exceptions.hpp:128