src/utilities/data/Variant.hpp
| 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 | #ifndef UTILITIES_DATA_VARIANT_HPP | ||
| 7 | #define UTILITIES_DATA_VARIANT_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | |||
| 11 | #include "../core/Enum.hpp" | ||
| 12 | #include "../core/Logger.hpp" | ||
| 13 | |||
| 14 | #include <optional> | ||
| 15 | #include <variant> | ||
| 16 | |||
| 17 | namespace Json { | ||
| 18 | class Value; | ||
| 19 | } | ||
| 20 | |||
| 21 | namespace openstudio { | ||
| 22 | |||
| 23 | // clang-format off | ||
| 24 | |||
| 25 | /** \class VariantType | ||
| 26 | * \brief A listing of data types that can be held in a Variant. | ||
| 27 | * \details See the OPENSTUDIO_ENUM documentation in utilities/core/Enum.hpp. The actual macro | ||
| 28 | * call is: | ||
| 29 | * \code | ||
| 30 | OPENSTUDIO_ENUM(VariantType, | ||
| 31 | ((Boolean)(Boolean)(0)) | ||
| 32 | ((Double)(Double)(1)) | ||
| 33 | ((Integer)(Integer)(2)) | ||
| 34 | ((String)(String)(3)) | ||
| 35 | ); | ||
| 36 | * \endcode */ | ||
| 37 |
13/22openstudio::VariantType::buildStringVec[abi:cxx11](bool):
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 228 times.
✓ Branch 3 taken 36 times.
✓ Branch 6 taken 36 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 36 times.
✓ Branch 12 taken 36 times.
✓ Branch 17 taken 228 times.
✓ Branch 20 taken 228 times.
✓ Branch 23 taken 198 times.
✓ Branch 24 taken 132 times.
✓ Branch 25 taken 66 times.
✓ Branch 26 taken 66 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 66 times.
openstudio::VariantType::enumName[abi:cxx11]():
✗ Branch 1 not taken.
openstudio::operator<<(std::ostream&, openstudio::VariantType const&):
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
|
702967 | OPENSTUDIO_ENUM(VariantType, |
| 38 | ((Boolean)(Boolean)(0)) | ||
| 39 | ((Double)(Double)(1)) | ||
| 40 | ((Integer)(Integer)(2)) | ||
| 41 | ((String)(String)(3)) | ||
| 42 | ); | ||
| 43 | |||
| 44 | // clang-format on | ||
| 45 | |||
| 46 | /** A Variant is a flexible type that can hold several types of values. | ||
| 47 | */ | ||
| 48 | class UTILITIES_API Variant | ||
| 49 | { | ||
| 50 | public: | ||
| 51 | /// constructors | ||
| 52 | explicit Variant(); | ||
| 53 | explicit Variant(bool value); | ||
| 54 | explicit Variant(double value); | ||
| 55 | explicit Variant(int value); | ||
| 56 | explicit Variant(const char* value); | ||
| 57 | explicit Variant(const std::string& value); | ||
| 58 | |||
| 59 | /// get the variant value type | ||
| 60 | VariantType variantType() const; | ||
| 61 | |||
| 62 | /// get value as a bool | ||
| 63 | bool valueAsBoolean() const; | ||
| 64 | |||
| 65 | /// get value as int | ||
| 66 | int valueAsInteger() const; | ||
| 67 | |||
| 68 | /// get value as double | ||
| 69 | double valueAsDouble() const; | ||
| 70 | |||
| 71 | /// get value as string | ||
| 72 | std::string valueAsString() const; | ||
| 73 | |||
| 74 | Json::Value valueAsJSON() const; | ||
| 75 | |||
| 76 | // No matter the underlying type, will check if it's true-ish | ||
| 77 | // Used for skipping a measure. eg true, "true", 1, 1.0 are all trueish | ||
| 78 | bool isTrueish() const; | ||
| 79 | |||
| 80 | private: | ||
| 81 | − | REGISTER_LOGGER("openstudio.Variant"); | |
| 82 | |||
| 83 | VariantType m_type; | ||
| 84 | std::variant<bool, int, double, std::string> m_value; | ||
| 85 | }; | ||
| 86 | |||
| 87 | /** \relates Variant */ | ||
| 88 | using OptionalVariant = std::optional<Variant>; | ||
| 89 | |||
| 90 | /** \relates Variant */ | ||
| 91 | using VariantVector = std::vector<Variant>; | ||
| 92 | |||
| 93 | UTILITIES_API std::ostream& operator<<(std::ostream& os, const Variant& variant); | ||
| 94 | |||
| 95 | } // namespace openstudio | ||
| 96 | |||
| 97 | #endif // UTILITIES_DATA_VARIANT_HPP | ||
| 98 |