00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2008, 2009, Andrew W. Steiner 00005 00006 This file is part of O2scl. 00007 00008 O2scl is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 O2scl is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with O2scl. If not, see <http://www.gnu.org/licenses/>. 00020 00021 ------------------------------------------------------------------- 00022 */ 00023 #ifndef O2SCL_CONVERT_UNITS_H 00024 #define O2SCL_CONVERT_UNITS_H 00025 00026 #include <cstdio> 00027 #include <cstdlib> 00028 #include <iostream> 00029 #include <map> 00030 00031 #include <o2scl/err_hnd.h> 00032 #include <o2scl/file_format.h> 00033 #include <o2scl/misc.h> 00034 #include <o2scl/constants.h> 00035 #include <o2scl/string_conv.h> 00036 00037 #ifndef DOXYGENP 00038 namespace o2scl { 00039 #endif 00040 00041 /** 00042 \brief Convert units 00043 00044 Allow the user to convert between two different units after 00045 specifying a conversion factor. This class will also 00046 automatically combine two conversion factors to create a new 00047 unit conversion. 00048 00049 Conversions are performed by the \ref convert() function and 00050 the conversion factors must be specified beforehand using the 00051 \ref insert_cache() function. 00052 00053 Example: 00054 \code 00055 convert_units cu; 00056 cu.insert_cache("in","cm",2.54); 00057 cout << "12 in is " << cu.convert("in","cm",12.0) << " cm. " << endl; 00058 \endcode 00059 00060 \future Ideally, a real C++ API for the GNU units command 00061 would probably be better. 00062 */ 00063 class convert_units { 00064 00065 #ifndef DOXYGEN_INTERNAL 00066 00067 protected: 00068 00069 /// The type for caching unit conversions 00070 typedef struct { 00071 /// The input unit 00072 std::string f; 00073 /// The output unit 00074 std::string t; 00075 /// The conversion factor 00076 double c; 00077 } unit_t; 00078 00079 /// The cache where unit conversions are stored 00080 std::map<std::string,unit_t,string_comp> mcache; 00081 00082 /// The iterator type 00083 typedef std::map<std::string,unit_t,string_comp>::iterator miter; 00084 00085 #endif 00086 00087 public: 00088 00089 /// Verbosity (default 0) 00090 int verbose; 00091 00092 /// (default false) 00093 bool use_gnu_units; 00094 00095 /// (default true) 00096 bool err_on_fail; 00097 00098 /// Default 'units' 00099 std::string units_cmd_string; 00100 00101 convert_units() { 00102 verbose=0; 00103 use_gnu_units=false; 00104 units_cmd_string="units"; 00105 err_on_fail=true; 00106 } 00107 00108 virtual ~convert_units() {} 00109 00110 /** \brief Return the value \c val after converting using units \c 00111 from and \c to 00112 */ 00113 virtual double convert(std::string from, std::string to, double val); 00114 00115 /// Manually insert a unit conversion into the cache 00116 int insert_cache(std::string from, std::string to, double conv); 00117 00118 /// Manually remove a unit conversion into the cache 00119 int remove_cache(std::string from, std::string to); 00120 00121 /// Print the present unit cache to std::cout 00122 int print_cache(); 00123 00124 /// Add conversion factors for energy equivalents 00125 int energy_conv(); 00126 00127 }; 00128 00129 #ifndef DOXYGENP 00130 } 00131 #endif 00132 00133 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page