FNSS C++ library
C++ API for the Fast Network Simulation Setup (FNSS) toolchain
 All Classes Functions Typedefs Pages
measurement-unit.h
1 #ifndef MEASUREMENT_UNIT_H
2 #define MEASUREMENT_UNIT_H
3 
4 #include <map>
5 #include <set>
6 #include <string>
7 
8 namespace fnss {
9 
11 
12 public:
17  typedef std::map <std::string, double> conversionsMapType;
18 
24  MeasurementUnit(const std::string &base_);
25 
32  MeasurementUnit(const std::string &base_,
33  const conversionsMapType &conversions_);
34 
41  double convert(const std::string &unit) const;
42 
49  double convert(const std::string &unit1, const std::string &unit2) const;
50 
56  std::string getBaseUnit() const;
57 
63  std::set <std::string> getAllUnits() const;
64 
71  void addConversion(std::string unit, double multiplier);
72 
78  void addConversions(const conversionsMapType &conversions_);
79 
89 
96 
104  static MeasurementUnit prefixDerivation(const MeasurementUnit &prefix,
105  const MeasurementUnit &target);
106 
117  static MeasurementUnit fractionalDerivation(const MeasurementUnit &numerator,
118  const MeasurementUnit &denominator,
119  const std::string &separator);
120 
121  class UnknownConversionException : public std::exception {
122  public:
123  UnknownConversionException(const std::string &unit1,
124  const std::string &unit2) throw() {
125  this->exceptionStr = "Unknown conversion from unit " + unit1
126  + " to unit " + unit2 + ".";
127  }
128 
129  ~UnknownConversionException() throw() {
130  }
131 
132  const char* what() const throw() {
133  return this->exceptionStr.c_str();
134  }
135 
136  private:
137  std::string exceptionStr;
138  };
139 
140  class BaseMismatchException : public std::exception {
141  public:
142  BaseMismatchException(const std::string &base1,
143  const std::string &base2) throw() {
144  this->exceptionStr = "Base unit mismatch: " + base1
145  + " and " + base2 + ".";
146  }
147 
148  ~BaseMismatchException() throw() {
149  }
150 
151  const char* what() const throw() {
152  return this->exceptionStr.c_str();
153  }
154 
155  private:
156  std::string exceptionStr;
157  };
158 
159 private:
160  //Append the base unit to the \c conversions map, with a multiplier of 1.
161  static conversionsMapType appendBase(const conversionsMapType &conversions,
162  const std::string &base);
163 
164 
165  const std::string base; //Base unit this object operates with.
166 
167  //Map of all possible conversions to the base unit.
168  conversionsMapType conversions;
169 };
170 
171 } //namespace
172 
173 #endif //MEASUREMENT_UNIT_H
static MeasurementUnit prefixDerivation(const MeasurementUnit &prefix, const MeasurementUnit &target)
Definition: measurement-unit.cpp:71
double convert(const std::string &unit) const
Definition: measurement-unit.cpp:13
Definition: measurement-unit.h:10
static MeasurementUnit fractionalDerivation(const MeasurementUnit &numerator, const MeasurementUnit &denominator, const std::string &separator)
Definition: measurement-unit.cpp:88
void addConversions(const conversionsMapType &conversions_)
Definition: measurement-unit.cpp:51
void addConversion(std::string unit, double multiplier)
Definition: measurement-unit.cpp:47
Definition: measurement-unit.h:140
MeasurementUnit(const std::string &base_)
Definition: measurement-unit.cpp:5
std::string getBaseUnit() const
Definition: measurement-unit.cpp:33
MeasurementUnit & operator=(const MeasurementUnit &other)
Definition: measurement-unit.cpp:61
MeasurementUnit & combine(const MeasurementUnit &other)
Definition: measurement-unit.cpp:55
Definition: measurement-unit.h:121
std::map< std::string, double > conversionsMapType
Definition: measurement-unit.h:17
std::set< std::string > getAllUnits() const
Definition: measurement-unit.cpp:37