src/utilities/units/QuantityFactory.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "QuantityFactory.hpp" | ||
| 7 | #include "QuantityRegex.hpp" | ||
| 8 | |||
| 9 | #include "../core/Exception.hpp" | ||
| 10 | #include "../core/Containers.hpp" | ||
| 11 | |||
| 12 | #include <boost/pointer_cast.hpp> | ||
| 13 | #include <boost/lexical_cast.hpp> | ||
| 14 | |||
| 15 | using std::shared_ptr; | ||
| 16 | using std::dynamic_pointer_cast; | ||
| 17 | |||
| 18 | namespace openstudio { | ||
| 19 | |||
| 20 | 11 | QuantityFactory& QuantityFactory::instance() { | |
| 21 | static QuantityFactory instance; | ||
| 22 | 11 | return instance; | |
| 23 | } | ||
| 24 | |||
| 25 | ✗ | boost::optional<Quantity> QuantityFactory::createQuantity(const std::string& quantityString) const { | |
| 26 | ✗ | OptionalQuantity result; | |
| 27 | ✗ | StringPair strings = decomposeQuantityString(quantityString); | |
| 28 | try { | ||
| 29 | ✗ | auto value = boost::lexical_cast<double>(strings.first); | |
| 30 | ✗ | result = createQuantity(value, strings.second); | |
| 31 | ✗ | } catch (...) { | |
| 32 | ✗ | LOG(Error, "Unable to create quantity from string '" << quantityString << "'. Returning 0.0."); | |
| 33 | ✗ | } | |
| 34 | ✗ | return result; | |
| 35 | ✗ | } | |
| 36 | |||
| 37 | ✗ | boost::optional<Quantity> QuantityFactory::createQuantity(const std::string& quantityString, UnitSystem system) const { | |
| 38 | ✗ | OptionalQuantity result; | |
| 39 | ✗ | StringPair strings = decomposeQuantityString(quantityString); | |
| 40 | try { | ||
| 41 | ✗ | auto value = boost::lexical_cast<double>(strings.first); | |
| 42 | ✗ | result = createQuantity(value, strings.second, system); | |
| 43 | ✗ | } catch (...) { | |
| 44 | ✗ | LOG(Error, "Unable to create quantity from string '" << quantityString << "'. Returning 0.0."); | |
| 45 | ✗ | } | |
| 46 | ✗ | return result; | |
| 47 | ✗ | } | |
| 48 | |||
| 49 | 11 | boost::optional<Quantity> QuantityFactory::createQuantity(double value, const std::string& unitString) const { | |
| 50 | // create return variable, and try to create unit | ||
| 51 | 11 | OptionalQuantity result; | |
| 52 |
1/1✓ Branch 1 taken 11 times.
|
11 | UnitSystem system = getSystem(unitString); |
| 53 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
|
11 | OptionalUnit unit = UnitFactory::instance().createUnit(unitString, system); |
| 54 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if (unit) { |
| 55 |
3/3✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
|
11 | result = Quantity(value, *unit); |
| 56 | } | ||
| 57 | 22 | return result; | |
| 58 | 11 | } | |
| 59 | |||
| 60 | ✗ | boost::optional<Quantity> QuantityFactory::createQuantity(double value, const std::string& unitString, UnitSystem system) const { | |
| 61 | // create return variable, and try to create unit | ||
| 62 | ✗ | OptionalQuantity result; | |
| 63 | ✗ | OptionalUnit unit = UnitFactory::instance().createUnit(unitString, system); | |
| 64 | ✗ | if (unit) { | |
| 65 | ✗ | result = Quantity(value, *unit); | |
| 66 | } | ||
| 67 | ✗ | return result; | |
| 68 | ✗ | } | |
| 69 | |||
| 70 | ✗ | boost::optional<Quantity> createQuantity(const std::string& quantityString) { | |
| 71 | ✗ | return QuantityFactory::instance().createQuantity(quantityString); | |
| 72 | } | ||
| 73 | |||
| 74 | ✗ | boost::optional<Quantity> createQuantity(const std::string& quantityString, UnitSystem system) { | |
| 75 | ✗ | return QuantityFactory::instance().createQuantity(quantityString, system); | |
| 76 | } | ||
| 77 | |||
| 78 | 11 | boost::optional<Quantity> createQuantity(double value, const std::string& unitString) { | |
| 79 | 11 | return QuantityFactory::instance().createQuantity(value, unitString); | |
| 80 | } | ||
| 81 | |||
| 82 | ✗ | boost::optional<Quantity> createQuantity(double value, const std::string& unitString, UnitSystem system) { | |
| 83 | ✗ | return QuantityFactory::instance().createQuantity(value, unitString, system); | |
| 84 | } | ||
| 85 | |||
| 86 | } // namespace openstudio | ||
| 87 |