diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-02-01 21:29:51 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-02-01 21:29:51 +0100 |
commit | ba5439849b72ac341344d55f2bf05ccf11e37410 (patch) | |
tree | 7d60d6eb3bb2b98a2f75d7ec5eb6fe215e75c302 /src/core/common | |
parent | 126547c380b32a71178a8dca47c7f0a5849bc26c (diff) |
Made Argument and Arguments copyable
Diffstat (limited to 'src/core/common')
-rw-r--r-- | src/core/common/Argument.cpp | 51 | ||||
-rw-r--r-- | src/core/common/Argument.hpp | 65 |
2 files changed, 74 insertions, 42 deletions
diff --git a/src/core/common/Argument.cpp b/src/core/common/Argument.cpp index 05c9761..385bae1 100644 --- a/src/core/common/Argument.cpp +++ b/src/core/common/Argument.cpp @@ -28,12 +28,12 @@ namespace ousia { /* Class Argument */ Argument::Argument(std::string name, const Rtti &type, const Rtti &innerType, - Variant defaultValue, bool hasDefault) - : type(type), - innerType(innerType), - name(std::move(name)), + Variant defaultValue, bool hasDefaultValue) + : name(std::move(name)), + type(&type), + innerType(&innerType), defaultValue(std::move(defaultValue)), - hasDefault(hasDefault) + hasDefaultValue(hasDefaultValue) { } @@ -181,9 +181,9 @@ Argument Argument::Cardinality(std::string name, bool Argument::validate(Variant &var, Logger &logger) const { - if (!VariantConverter::convert(var, type, innerType, logger, + if (!VariantConverter::convert(var, *type, *innerType, logger, VariantConverter::Mode::SAFE)) { - if (hasDefault) { + if (hasDefaultValue) { var = defaultValue; } return false; @@ -191,6 +191,12 @@ bool Argument::validate(Variant &var, Logger &logger) const return true; } +const std::string &Argument::getName() const { return name; } + +const Variant &Argument::getDefaultValue() const { return defaultValue; } + +bool Argument::hasDefault() const { return hasDefaultValue; } + /* Class Arguments */ // Instantiations of the "None" arguments @@ -203,14 +209,14 @@ static std::unordered_map<std::string, size_t> buildArgumentNames( std::unordered_map<std::string, size_t> res; size_t i = 0; for (const Argument &arg : arguments) { - if (!Utils::isIdentifier(arg.name)) { - throw OusiaException{std::string("Argument name ") + arg.name + + const std::string &name = arg.getName(); + if (!Utils::isIdentifier(name)) { + throw OusiaException{std::string("Argument name ") + name + std::string(" is not a valid identifier")}; } - if (!res.emplace(arg.name, i++).second) { - throw OusiaException{ - std::string("Argument names must be unique (") + arg.name + - std::string(")")}; + if (!res.emplace(name, i++).second) { + throw OusiaException{std::string("Argument name \"") + name + + std::string("\" is not unique")}; } } return res; @@ -252,15 +258,15 @@ bool Arguments::validateArray(Variant::arrayType &arr, Logger &logger) const if (a < n) { ok = ok && arguments[a].validate(arr[a], logger); } else { - if (arguments[a].hasDefault) { - arr[a] = arguments[a].defaultValue; + if (arguments[a].hasDefault()) { + arr[a] = arguments[a].getDefaultValue(); } else { // Call "validate" to inject a standard value arr[a] = Variant::fromObject(nullptr); arguments[a].validate(arr[a], nullLogger); logger.error(std::string("Missing argument ") + std::to_string(a + 1) + std::string(" \"") + - arguments[a].name + std::string("\"")); + arguments[a].getName() + std::string("\"")); ok = false; } } @@ -308,14 +314,15 @@ bool Arguments::validateMap(Variant::mapType &map, Logger &logger, // Insert all unset arguments for (size_t a = 0; a < N; a++) { if (!set[a]) { - if (arguments[a].hasDefault) { - map[arguments[a].name] = arguments[a].defaultValue; + const std::string &name = arguments[a].getName(); + if (arguments[a].hasDefault()) { + map[name] = arguments[a].getDefaultValue(); } else { // Call "validate" to inject a standard value - map[arguments[a].name] = Variant::fromObject(nullptr); - arguments[a].validate(map[arguments[a].name], nullLogger); - logger.error(std::string("Missing argument \"") + - arguments[a].name + std::string("\"")); + map[name] = Variant::fromObject(nullptr); + arguments[a].validate(map[name], nullLogger); + logger.error(std::string("Missing argument \"") + name + + std::string("\"")); ok = false; } } diff --git a/src/core/common/Argument.hpp b/src/core/common/Argument.hpp index 42b1722..ea68e3c 100644 --- a/src/core/common/Argument.hpp +++ b/src/core/common/Argument.hpp @@ -53,15 +53,33 @@ class Rtti; class Argument { private: /** + * Contains the name of the argument. Used for logging and in case the + * arguments are presented as map. + */ + std::string name; + + /** * Type that should be returned by the Variant rttiType function. */ - const Rtti &type; + Rtti const* type; /** * Describes the inner type of the variant -- e.g. the type of the elements * inside an array. Normally set to RttiTypes::None. */ - const Rtti &innerType; + Rtti const* innerType; + + /** + * Default value. Note that a value of nullptr does not indicate that no + * default value has been set. Use the "hasDefault" flag for this purpose. + * Nullptr is a valid value for objects. + */ + Variant defaultValue; + + /** + * True if a default value is set, false otherwise. + */ + bool hasDefaultValue; /** * Private constructor used for manually setting all internal data fields. @@ -101,24 +119,6 @@ private: public: /** - * Contains the name of the argument. Used for logging and in case the - * arguments are presented as map. - */ - const std::string name; - - /** - * Default value. Note that a value of nullptr does not indicate that no - * default value has been set. Use the "hasDefault" flag for this purpose. - * Nullptr is a valid value for objects. - */ - const Variant defaultValue; - - /** - * True if a default value is set, false otherwise. - */ - const bool hasDefault; - - /** * Named constructor for an argument with any type. * * @param name is the name of the argument as used for error messages and in @@ -404,6 +404,31 @@ public: * @return true if the given variant was valid, false otherwise. */ bool validate(Variant &var, Logger &logger) const; + + /** + * Returns the name of the argument. The name is used for logging and in + * case a map is presented as arguments. + * + * @return the name of the argument given in the constructor. + */ + const std::string &getName() const; + + /** + * Returns the default value. Note that a value of nullptr does not indicate + * that no default value has been set. Use the "hasDefault" flag for this + * purpose. Nullptr is a valid value for objects. + * + * @return the default value that was given in the constructor (may be + * nullptr) and nullptr if no default value was given. + */ + const Variant& getDefaultValue() const; + + /** + * Returns true if a default value was set in the constructor. + * + * @return true if a default value is set, false otherwise. + */ + bool hasDefault() const; }; /** |