TBTK
Need a break? Support the development by playing Polarity Puzzles
Quantity.h
Go to the documentation of this file.
1 /* Copyright 2019 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_QUANTITY_QUANTITY
24 #define COM_DAFER45_TBTK_QUANTITY_QUANTITY
25 
26 #include "TBTK/Real.h"
27 #include "TBTK/TBTKMacros.h"
28 
29 #include <map>
30 #include <string>
31 
32 namespace TBTK{
33 namespace Quantity{
34 
167 template<typename Units, typename Exponents>
168 class Quantity : public Real{
169 public:
170  using Unit = Units;
171  using Exponent = Exponents;
172 
174  Quantity(){};
175 
179  Quantity(double value) : Real(value){};
180 
186  static std::string getUnitString(Unit unit);
187 
193  static Unit getUnit(const std::string &unit);
194 
202  static double getConversionFactor(Unit unit);
203 protected:
204  static class ConversionTable{
205  public:
206  std::map<Unit, std::string> unitToString;
207  std::map<std::string, Unit> stringToUnit;
208  std::map<Unit, double> conversionFactors;
209 
210  ConversionTable(
211  const std::map<
212  Unit,
213  std::pair<std::string, double>
214  > &conversionTable
215  ){
216  for(auto entry : conversionTable){
217  unitToString[entry.first] = entry.second.first;
218  stringToUnit[entry.second.first] = entry.first;
219  conversionFactors[entry.first]
220  = entry.second.second;
221  }
222  }
223  } conversionTable;
224 };
225 
226 template<typename Units, typename Exponents>
228  try{
229  return conversionTable.unitToString.at(unit);
230  }
231  catch(const std::out_of_range &e){
232  TBTKExit(
233  "Quantity::getUnitString()",
234  "Unknown unit '" << static_cast<int>(unit) << "'.",
235  ""
236  );
237  return "";
238  }
239 }
240 
241 template<typename Units, typename Exponents>
242 typename Quantity<Units, Exponents>::Unit Quantity<Units, Exponents>::getUnit(
243  const std::string &unit
244 ){
245  try{
246  return conversionTable.stringToUnit.at(unit);
247  }
248  catch(const std::out_of_range &e){
249  TBTKExit(
250  "Quantity::getUnit()",
251  "Unknown unit '" << unit << "'.",
252  ""
253  );
254  }
255 }
256 
257 template<typename Units, typename Exponents>
259  try{
260  return conversionTable.conversionFactors.at(unit);
261  }
262  catch(const std::out_of_range &e){
263  TBTKExit(
264  "Quantity::getConversionFactor()",
265  "Unknown unit '" << static_cast<int>(unit) << "'.",
266  ""
267  );
268  }
269 }
270 
271 }; //End of namesapce Quantity
272 }; //End of namesapce TBTK
273 
274 #endif
275 
TBTK::Quantity::Quantity::Quantity
Quantity(double value)
Definition: Quantity.h:179
TBTK::Quantity::Quantity::getConversionFactor
static double getConversionFactor(Unit unit)
Definition: Quantity.h:258
TBTK::Real
Real number.
Definition: Real.h:33
TBTK::Quantity::Quantity::getUnitString
static std::string getUnitString(Unit unit)
Definition: Quantity.h:227
TBTK::Quantity::Quantity::Quantity
Quantity()
Definition: Quantity.h:174
TBTK::Quantity::Quantity::getUnit
static Unit getUnit(const std::string &unit)
Definition: Quantity.h:242
TBTK::Quantity::Quantity
Base class for Quantitis.
Definition: Quantity.h:168
TBTKMacros.h
Precompiler macros.
Real.h
Real number.