summaryrefslogtreecommitdiff
path: root/src/core/common/VariantConverter.hpp
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-11 23:59:46 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-11 23:59:46 +0100
commit57346af125d4274187bf4af424d13fde072155de (patch)
tree0b5ee3fd53b433ff92653239c453a59056910ae8 /src/core/common/VariantConverter.hpp
parent68f693eedc1c2674d1535553bb519dfac07fdc5e (diff)
Implemented conversion to more Variant types, implemented VariantConverter::convert method
Diffstat (limited to 'src/core/common/VariantConverter.hpp')
-rw-r--r--src/core/common/VariantConverter.hpp137
1 files changed, 128 insertions, 9 deletions
diff --git a/src/core/common/VariantConverter.hpp b/src/core/common/VariantConverter.hpp
index 22ead7a..6becbc4 100644
--- a/src/core/common/VariantConverter.hpp
+++ b/src/core/common/VariantConverter.hpp
@@ -16,6 +16,16 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+/**
+ * @file VariantConverter.hpp
+ *
+ * Contains the VariantConverter class which contains code commonly used by
+ * the Variant, Typesystem and Argument classes to cast Variants to other
+ * Variant types.
+ *
+ * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
+ */
+
#ifndef _OUSIA_VARIANT_CONVERTER_HPP_
#define _OUSIA_VARIANT_CONVERTER_HPP_
@@ -25,7 +35,6 @@ namespace ousia {
class Logger;
class RttiType;
class Variant;
-enum class VariantType : int16_t;
/**
* The VariantConverter class is used to convert a variant to a certain
@@ -39,14 +48,25 @@ public:
* conversions (without any data loss) are performed, or all possible
* conversions are tried (with possible data loss).
*/
- enum class Mode { SAFE, ALL };
+ enum class Mode {
+ /**
+ * Performs only lossless and sane conversions.
+ */
+ SAFE,
+
+ /**
+ * Performs possibly lossy and probably unintuitive conversions.
+ */
+ ALL
+ };
/**
* Converts the given variant to a boolean. If the "mode" parameter is set
* to Mode::SAFE, only booleans can be converted to booleans. For all other
* types the conversion fails. If "mode" is set to Mode::ALL, nullptr
* values and zero numeric values are treated as "false", all other values
- * are treated as "true".
+ * are treated as "true". If the conversion fails, false is returned as
+ * default value.
*
* @param var is instance of the Variant class that should be converted to
* the requested type.
@@ -54,6 +74,8 @@ public:
* should be logged.
* @param mode is the conversion mode. See method description for the exact
* effect.
+ * @return true if the operation was successful, false otherwise. In any
+ * case the input/output parameter "var" will have the requested type.
*/
static bool toBool(Variant &var, Logger &logger, Mode mode = Mode::SAFE);
@@ -64,7 +86,8 @@ public:
* converted to 0, 1, nullptr is converted to 0, doubles are truncated,
* strings are parsed and truncated, arrays with one element are converted
* to an integer. Conversion fails for objects, functions, maps and arrays
- * with zero or more than one entry.
+ * with zero or more than one entry. If the conversion fails, 0 is returned
+ * as default value.
*
* @param var is instance of the Variant class that should be converted to
* the requested type.
@@ -72,6 +95,8 @@ public:
* should be logged.
* @param mode is the conversion mode. See method description for the exact
* effect.
+ * @return true if the operation was successful, false otherwise. In any
+ * case the input/output parameter "var" will have the requested type.
*/
static bool toInt(Variant &var, Logger &logger, Mode mode = Mode::SAFE);
@@ -82,7 +107,8 @@ public:
* booleans are converted to 0.0, 1.0, nullptr is converted to 0.0, strings
* are parsed, arrays with one element are converted to a double.
* Conversion fails for objects, functions, maps and arrays with zero or
- * more than one entry.
+ * more than one entry. If the conversion fails, 0.0 is returned as default
+ * value.
*
* @param var is instance of the Variant class that should be converted to
* the requested type.
@@ -90,6 +116,8 @@ public:
* should be logged.
* @param mode is the conversion mode. See method description for the exact
* effect.
+ * @return true if the operation was successful, false otherwise. In any
+ * case the input/output parameter "var" will have the requested type.
*/
static bool toDouble(Variant &var, Logger &logger, Mode mode = Mode::SAFE);
@@ -99,7 +127,8 @@ public:
* all other types the conversion fails. If "mode" is set to Mode::ALL,
* maps and arrays are converted to a JSON representation, objects and
* functions are converted to an informative string containing their pointer
- * and type.
+ * and type. If the conversion fails, an empty string is returned as default
+ * value.
*
* @param var is instance of the Variant class that should be converted to
* the requested type.
@@ -107,14 +136,104 @@ public:
* should be logged.
* @param mode is the conversion mode. See method description for the exact
* effect.
+ * @return true if the operation was successful, false otherwise. In any
+ * case the input/output parameter "var" will have the requested type.
*/
static bool toString(Variant &var, Logger &logger, Mode mode = Mode::SAFE);
- static bool convert(Variant &var, VariantType requestedType,
- const RttiType &rttiType, Logger &logger,
+ /**
+ * Converts the given variant to an array with the given inner type. If the
+ * "mode" parameter is set to Mode::SAFE, the given variant must be an
+ * array, If mode is set to Mode::ALL, other variant values are encapsulated
+ * in array with one entry. In both cases, if "innerType" points at a
+ * primitive Rtti type, conversion to that type is tried (using the
+ * specified conversion mode).
+ *
+ * @param var is instance of the Variant class that should be converted to
+ * the requested type.
+ * @param innerType is the inner type of the array entries. Should be set to
+ * RttiTypes::None in case the inner type of the array does not matter.
+ * @param logger is a reference to the logger instance into which messages
+ * should be logged.
+ * @param mode is the conversion mode. See method description for the exact
+ * effect.
+ * @return true if the operation was successful, false otherwise. In any
+ * case the input/output parameter "var" will have the requested type.
+ */
+ static bool toArray(Variant &var, const RttiType &innerType, Logger &logger,
Mode mode = Mode::SAFE);
- static bool convert(Variant &var, VariantType requestedType,
+
+ /**
+ * Converts the given variant to an map with the given inner type. The given
+ * variant must be a map. If "innerType" points at a primitive Rtti type,
+ * conversion to that type is tried with the specified conversion mode.
+ *
+ * @param var is instance of the Variant class that should be converted to
+ * the requested type.
+ * @param innerType is the inner type of the map entries. Should be set to
+ * RttiTypes::None in case the inner type of the map does not matter.
+ * @param logger is a reference to the logger instance into which messages
+ * should be logged.
+ * @param mode is the conversion mode used for converting the entries of the
+ * map to the inner type.
+ * @return true if the operation was successful, false otherwise. In any
+ * case the input/output parameter "var" will have the requested type.
+ */
+ static bool toMap(Variant &var, const RttiType &innerType, Logger &logger,
+ Mode mode = Mode::SAFE);
+
+ /**
+ * Makes sure the given variant is a function. If it is not, it is replaced
+ * by a dummy function which takes no arguments and returns nullptr.
+ *
+ * @param var is the variant that should be converted to a function.
+ * @param logger is the logger to which error messages should be written.
+ * @return true if the operation was successful, false otherwise. In any
+ * case the input/output parameter "var" will have the requested type.
+ */
+ static bool toFunction(Variant &var, Logger &logger);
+
+ /**
+ * Tries conversion to the given RttiType with the given optional inner
+ * type.
+ *
+ * @param type describes the type to which the variant should be converted.
+ * This might either be a variant type such as RttiType::Bool,
+ * RttiType::Int, RttiType::Double, RttiType::String, RttiType::Array,
+ * RttiType::Map or RttiType::Function. All other types are regarded as
+ * managed object of this type. If RttiType::None is given, all types are
+ * accepted.
+ * @param innerType is used in case of maps or arrays to check the type of
+ * the elements of these containers. If RttiType::None is given, no special
+ * type is required.
+ * @param logger is a reference at the logger instance to which error
+ * messages are forwarded.
+ * @param mode is the conversion mode that is being enforced.
+ * @return true if the operation was successful, false otherwise. In any
+ * case the input/output parameter "var" will have the requested type.
+ */
+ static bool convert(Variant &var, const RttiType &type,
+ const RttiType &innerType, Logger &logger,
+ Mode mode = Mode::SAFE);
+
+ /**
+ * Tries conversion to the given RttiType without any enforcement regarding
+ * the inner type of container types.
+ *
+ * @param type describes the type to which the variant should be converted.
+ * This might either be a variant type such as RttiType::Bool,
+ * RttiType::Int, RttiType::Double, RttiType::String, RttiType::Array,
+ * RttiType::Map or RttiType::Function. All other types are regarded as
+ * managed object of this type. If RttiType::None is given, all types are
+ * accepted.
+ * @param logger is a reference at the logger instance to which error
+ * messages are forwarded.
+ * @param mode is the conversion mode that is being enforced.
+ * @return true if the operation was successful, false otherwise. In any
+ * case the input/output parameter "var" will have the requested type.
+ */
+ static bool convert(Variant &var, const RttiType &type,
Logger &logger, Mode mode = Mode::SAFE);
};
}