OpenStudio:utilities
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Public Types | Public Member Functions | Related Functions | List of all members
openstudio::UnitFactorySingleton Class Reference

#include <UnitFactory.hpp>

Public Types

using CreateUnitCallback = boost::function< Unit()>
 Units are created by functions that accept no arguments and return something that can be converted into a Unit::Ptr. More...
 

Public Member Functions

boost::optional< UnitcreateUnit (const std::string &unitString, UnitSystem system=UnitSystem::Mixed) const
 Parses unitStr to create a unit based on the internal factory maps. More...
 
std::string lookupPrettyString (const std::string &standardString) const
 Returns the prettyString registered for standardString. More...
 
bool registerEquivalentString (const std::string &equivalentString, const std::string &standardString)
 Register alternative symbols for standardString. More...
 
bool registerUnit (CreateUnitCallback createFn, UnitSystem system=UnitSystem::Mixed)
 Associate a unit creation function with a standard string. More...
 

Related Functions

(Note that these are not member functions.)

bool containsRegisteredBaseUnit (const std::string &unitString)
 Returns true if any of unitString's baseUnits are registered in the factory. More...
 
std::string convertToStandardForm (const std::string &unitString)
 Converts unitString to standard form. More...
 
Unit createDimensionlessUnit (UnitSystem system)
 Returns a Unit of the type that corresponds to system. More...
 
Unit createGPMVolumetricFlowrate ()
 Make sure crgal^3/min shows up as gal/min. More...
 
Unit createIPPowerDensity ()
 In IP units, power density is often expressed as W/ft^2, which is actually a mixed unit. More...
 
Unit createIPPressure ()
 In IP units, pressure is often expressed as lb_f/in^2, which is actually a mixed unit. More...
 
boost::optional< UnitcreateUnit (const std::string &unitString, UnitSystem system)
 Creates a Unit corresponding to unitString (using UnitFactory). More...
 
boost::optional< UnitcreateUnit (const std::string &unitString)
 Creates a Unit corresponding to unitString (using UnitFactory). More...
 
std::string extractUnitString (const std::string &text)
 Extracts a unitString from text, if possible. More...
 
UnitSystem getSystem (const std::string &unitString)
 Returns the best match UnitSystem for unitString, in priority order SI, IP, ..., Mixed. More...
 
bool isInSystem (const std::string &unitString, UnitSystem system)
 Returns true if unitString can be created as a unit in system. More...
 
bool isUnitString (const std::string &unitString)
 Returns true if unitString can be turned into a Unit. More...
 
std::string replaceUnitString (const std::string &text, const std::string &newUnitString)
 Replaces unitString in text, using same finder algorithm as extractUnitString. More...
 
using UnitFactory = openstudio::Singleton< UnitFactorySingleton >
 Typedef for accessing the UnitFactorySingleton as UnitFactory::instance(). More...
 
bool unitStringsEqual (const std::string &uStr1, const std::string &uStr2)
 Returns true if uStr1 and uStr2 evaluate to the same unit. More...
 

Detailed Description

Singleton that creates units based on std::string representation.

Access using UnitFactory::instance(). The class keeps up with standard unit strings, pseudonyms for those strings, and pretty strings.

Standard unit strings use base units like kg, m, s, ft, etc. and follow LaTeX formatting. For example, the standard unit std::string for SI energy is

kg*m^2/s^2

.

Also provides a function for looking up pretty units without having to create a unit.

Member Typedef Documentation

Units are created by functions that accept no arguments and return something that can be converted into a Unit::Ptr.

Member Function Documentation

boost::optional<Unit> openstudio::UnitFactorySingleton::createUnit ( const std::string &  unitString,
UnitSystem  system = UnitSystem::Mixed 
) const

Parses unitStr to create a unit based on the internal factory maps.

The maps are populated using the functions registerUnit and registerEquivalentString. Specify system to alert the factory of your preferred unit system. The returned pointer is not guaranteed to be from that system, but if there is a conflict, it will be preferred.

std::string openstudio::UnitFactorySingleton::lookupPrettyString ( const std::string &  standardString) const

Returns the prettyString registered for standardString.

Returns the empty std::string for unregistered standardStrings.

bool openstudio::UnitFactorySingleton::registerEquivalentString ( const std::string &  equivalentString,
const std::string &  standardString 
)

Register alternative symbols for standardString.

For instance, J is a pseudonym for kg*m^2/s^2. Returns false if equivalentString is already used as a key in the internal map.

bool openstudio::UnitFactorySingleton::registerUnit ( CreateUnitCallback  createFn,
UnitSystem  system = UnitSystem::Mixed 
)

Associate a unit creation function with a standard string.

Also registers the pretty string if one exists. Returns false if standardString or prettyString is already used as a key in one or more internal maps.

Option to specify a unit system is for conflict resolution. For instance, seconds is a base unit in SI and IP. For efficiency, only specify system if you expect a conflict.

Friends And Related Function Documentation

bool containsRegisteredBaseUnit ( const std::string &  unitString)
related

Returns true if any of unitString's baseUnits are registered in the factory.

std::string convertToStandardForm ( const std::string &  unitString)
related

Converts unitString to standard form.

Right now, does conversion like people/100 m^2 –> c(people/m^2).

Unit createDimensionlessUnit ( UnitSystem  system)
related

Returns a Unit of the type that corresponds to system.

Unit createGPMVolumetricFlowrate ( )
related

Make sure crgal^3/min shows up as gal/min.

Unit createIPPowerDensity ( )
related

In IP units, power density is often expressed as W/ft^2, which is actually a mixed unit.

Unit createIPPressure ( )
related

In IP units, pressure is often expressed as lb_f/in^2, which is actually a mixed unit.

boost::optional< Unit > createUnit ( const std::string &  unitString,
UnitSystem  system 
)
related

Creates a Unit corresponding to unitString (using UnitFactory).

User may specify preferred UnitSystem, and can cast the result to the appropriate class, as desired.

boost::optional< Unit > createUnit ( const std::string &  unitString)
related

Creates a Unit corresponding to unitString (using UnitFactory).

Attempts to find the best UnitSystem for unitString, in priority order SI, IP, ..., Mixed.

std::string extractUnitString ( const std::string &  text)
related

Extracts a unitString from text, if possible.

Only returns strings that contain at least one registered base unit.

UnitSystem getSystem ( const std::string &  unitString)
related

Returns the best match UnitSystem for unitString, in priority order SI, IP, ..., Mixed.

bool isInSystem ( const std::string &  unitString,
UnitSystem  system 
)
related

Returns true if unitString can be created as a unit in system.

bool isUnitString ( const std::string &  unitString)
related

Returns true if unitString can be turned into a Unit.

std::string replaceUnitString ( const std::string &  text,
const std::string &  newUnitString 
)
related

Replaces unitString in text, using same finder algorithm as extractUnitString.

Typedef for accessing the UnitFactorySingleton as UnitFactory::instance().

bool unitStringsEqual ( const std::string &  uStr1,
const std::string &  uStr2 
)
related

Returns true if uStr1 and uStr2 evaluate to the same unit.