Convert units. More...
#include <convert_units.h>
Allow the user to convert between two different units after specifying a conversion factor. This class will also automatically combine two conversion factors to create a new unit conversion (but it cannot combine more than two).
Conversions are performed by the convert() function. The run-time unit cache is initially filled with hard-coded conversions, and convert() searches this cache is searched for the requested conversion first. If the conversion is not found and if use_gnu_units is true, then convert() tries to open a pipe to open a shell to get the conversion factor from 'units'
. If this is successful, then the conversion factor is returned and the conversion is added to the cache.
If the GNU units command is not in the local path, the user may modify units_cmd_string to specify the full pathname. One can also modify units_cmd_string to specify a different units.dat
file.
Example:
An object of this type is created by o2scl_settings (of type lib_settings_class) for several unit conversions used internally in O2scl .
Definition at line 105 of file convert_units.h.
Classes | |
struct | unit_t |
The type for caching unit conversions. More... | |
Public Member Functions | |
convert_units () | |
Create a unit-conversion object. | |
Basic usage | |
virtual double | convert (std::string from, std::string to, double val) |
Return the value val after converting using units from and to . | |
virtual double | convert_const (std::string from, std::string to, double val) const |
Return the value val after converting using units from and to (const version) | |
Conversions which don't throw exceptions | |
virtual int | convert_ret (std::string from, std::string to, double val, double &converted) |
Return the value val after converting using units from and to , returning a non-zero value on failure. | |
virtual int | convert_ret_const (std::string from, std::string to, double val, double &converted) const |
Return the value val after converting using units from and to , returning a non-zero value on failure (const version) | |
Manipulate cache and create units.dat files | |
void | insert_cache (std::string from, std::string to, double conv) |
Manually insert a unit conversion into the cache. | |
void | remove_cache (std::string from, std::string to) |
Manually remove a unit conversion into the cache. | |
void | print_cache () const |
Print the present unit cache to std::cout. | |
void | make_units_dat (std::string fname, bool c_1=false, bool hbar_1=false, bool K_1=false) const |
Make a GNU units.dat file from the GSL constants. More... | |
int | test_cache () |
Exhaustive test the cache against GNU units. | |
Public Attributes | |
User settings | |
int | verbose |
Verbosity (default 0) | |
bool | use_gnu_units |
If true, use a system call to units to derive new conversions (default true) More... | |
bool | err_on_fail |
If true, throw an exception when a conversion fails (default true) | |
bool | combine_two_conv |
If true, allow combinations of two conversions (default true) | |
std::string | units_cmd_string |
Command string to call units (default "units") | |
Protected Types | |
typedef std::map< std::string, unit_t, std::greater< std::string > >::iterator | miter |
The iterator type. | |
typedef std::map< std::string, unit_t, std::greater< std::string > >::const_iterator | mciter |
The const iterator type. | |
Protected Member Functions | |
int | convert_internal (std::string from, std::string to, double val, double &converted, double &factor, bool &new_conv) const |
The internal conversion function which tries the cache first and, if that failed, tries GNU units. More... | |
int | convert_gnu_units (std::string from, std::string to, double val, double &converted, double &factor, bool &new_conv) const |
Attempt to use GNU units to perform a conversion. More... | |
int | convert_cache (std::string from, std::string to, double val, double &converted, double &factor) const |
Attempt to construct a conversion from the internal unit cache. More... | |
Protected Attributes | |
std::map< std::string, unit_t, std::greater< std::string > > | mcache |
The cache where unit conversions are stored. | |
|
protected |
This function returns 0 if the conversion was successful and o2scl::exc_efailed otherwise. This function does not call the error handler.
|
protected |
This function attempts to open a pipe to GNU units independent of the value of use_gnu_units. However, it will always return a non-zero valeu if HAVE_POPEN
is not defined signaling that the popen()
function is not available (but does not call the error handler in this case). The function returns 0 if the conversion was successful. If HAVE_POPEN is defined but the call to GNU units fails for some reason, then the error handler is called if err_on_fail is true.
|
protected |
This function returns 0 if the conversion was successful. If the conversion fails and err_on_fail is true
, then the error handler is called. If the conversion fails and err_on_fail is false
, then the value o2scl::exc_enotfound is returned.
The public conversion functions in this class are basically just wrappers around this internal function.
void o2scl::convert_units::make_units_dat | ( | std::string | fname, |
bool | c_1 = false , |
||
bool | hbar_1 = false , |
||
bool | K_1 = false |
||
) | const |
If c_1
is true, then the second is defined in terms of meters so that the speed of light is unitless. If hbar_1
is true, then the kilogram is defined in terms of s/m^2
so that is unitless.
bool o2scl::convert_units::use_gnu_units |
This also requires popen()
.
Definition at line 208 of file convert_units.h.
Documentation generated with Doxygen. Provided under the
GNU Free Documentation License (see License Information).