Object-oriented Scientific Computing Library: Version 0.910
Data Structures | Public Member Functions | Data Fields | Protected Types | Protected Attributes
convert_units Class Reference

Convert units. More...

#include <convert_units.h>

Inheritance diagram for convert_units:
convert_units_gnu

Detailed Description

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 and the conversion factors must be specified beforehand using the insert_cache() function.

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:

      convert_units cu;
      cu.insert_cache("in","cm",2.54);
      cout << "12 in is " << cu.convert("in","cm",12.0) << " cm. " << endl;
Note:
Combining two conversions allows for some surprising apparent contradictions from numerical precision errors. If there are two matching unit conversion pairs which give the same requested conversion factor, then one can arrange a situation where the same conversion factor is reported with slightly different values after adding a related conversion to the table. One way to fix this is to force the class not to combine two conversions by setting combine_two_conv to false. Alternatively, one can ensure that no combination is necessary by manually adding the desired combination conversion to the cache after it is first computed.
Idea for Future:
Ideally, a real C++ API for the GNU units command would be better.

Definition at line 78 of file convert_units.h.

Data Structures

struct  unit_t
 The type for caching unit conversions. More...

Public Member Functions

virtual double convert (std::string from, std::string to, double val)
 Return the value val after converting using units from and to.
int insert_cache (std::string from, std::string to, double conv)
 Manually insert a unit conversion into the cache.
int remove_cache (std::string from, std::string to)
 Manually remove a unit conversion into the cache.
int print_cache ()
 Print the present unit cache to std::cout.
int energy_conv ()
 Add conversion factors for energy equivalents.

Data Fields

int verbose
 Verbosity (default 0)
bool use_gnu_units
 (default true)
bool err_on_fail
 (default true)
bool combine_two_conv
 If true, allow combinations of two conversions (default true)
std::string units_cmd_string
 Default 'units'.

Protected Types

typedef std::map< std::string,
unit_t, string_comp >
::iterator 
miter
 The iterator type.

Protected Attributes

std::map< std::string, unit_t,
string_comp
mcache
 The cache where unit conversions are stored.

The documentation for this class was generated from the following file:
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).

Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads.