diff options
Diffstat (limited to 'src/core/model')
| -rw-r--r-- | src/core/model/Typesystem.cpp | 49 | ||||
| -rw-r--r-- | src/core/model/Typesystem.hpp | 27 | 
2 files changed, 76 insertions, 0 deletions
diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp index fb99f87..dad2ec3 100644 --- a/src/core/model/Typesystem.cpp +++ b/src/core/model/Typesystem.cpp @@ -21,6 +21,7 @@  #include <core/common/RttiBuilder.hpp>  #include <core/common/Utils.hpp>  #include <core/common/VariantConverter.hpp> +#include <core/common/VariantReader.hpp>  namespace ousia { @@ -67,6 +68,54 @@ bool Type::build(Variant &data, Logger &logger) const  	return build(data, logger, NullMagicCallback);  } +std::pair<bool, Variant> Type::read(CharReader &reader, Logger &logger, +                                    const std::unordered_set<char> &delims) +{ +	// try all variant types of this type and use the first successful one. +	Variant v; +	bool success = false; +	for (auto t : getVariantTypes()) { +		auto res = VariantReader::parseTyped(t, reader, logger, delims); +		if (res.first) { +			v = res.second; +			success = true; +			break; +		} +	} + +	if (!success) { +		return std::make_pair(false, Variant{}); +	} +	if (!build(v, logger)) { +		return std::make_pair(false, Variant{}); +	} +	return std::make_pair(true, v); +} + +std::pair<bool, Variant> Type::read(const std::string &str, Logger &logger, +                                    SourceId sourceId, size_t offs) +{ +	// try all variant types of this type and use the first successful one. +	Variant v; +	bool success = false; +	for (auto t : getVariantTypes()) { +		auto res = VariantReader::parseTyped(t, str, logger, sourceId, offs); +		if (res.first) { +			v = res.second; +			success = true; +			break; +		} +	} + +	if (!success) { +		return std::make_pair(false, Variant{}); +	} +	if (!build(v, logger)) { +		return std::make_pair(false, Variant{}); +	} +	return std::make_pair(true, v); +} +  bool Type::doCheckIsa(Handle<const Type> type) const { return false; }  bool Type::checkIsa(Handle<const Type> type) const diff --git a/src/core/model/Typesystem.hpp b/src/core/model/Typesystem.hpp index c0a073a..d58a98f 100644 --- a/src/core/model/Typesystem.hpp +++ b/src/core/model/Typesystem.hpp @@ -43,6 +43,7 @@  namespace ousia {  // Forward declarations +class CharReader;  class Rtti;  class Typesystem;  class SystemTypesystem; @@ -168,6 +169,32 @@ public:  	bool build(Variant &data, Logger &logger) const;  	/** +	 * Tries to parse an instance of this type from the given stream. +	 * +	 * @param reader is a reference to the CharReader instance which is +	 * the source for the character data. The reader will be positioned +	 * at the end of the type instance (or the delimiting character). +	 * @param delims is a set of characters which will terminate the typed +	 * instance if the according parser uses delimiting characters. +	 * These characters are not included in the result. May not be nullptr. +	 */ +	std::pair<bool, Variant> read(CharReader &reader, Logger &logger, +	                              const std::unordered_set<char> &delims = {}); + +	/** +	 * Tries to parse an instance of this type from the given string. +	 * +	 * @param str is the string from which the value should be read. +	 * @param sourceId is an optional descriptor of the source file from which +	 * the element is being read. +	 * @param offs is the by offset in the source file at which the string +	 * starts. +	 */ +	std::pair<bool, Variant> read(const std::string &str, Logger &logger, +	                              SourceId sourceId = InvalidSourceId, +	                              size_t offs = 0); + +	/**  	 * Returns true if and only if the given Variant adheres to this Type. In  	 * essence this just calls the build method on a copy of the input Variant.  	 *  | 
