From 4c7ba3345999c2837a861a9556bdbe025cbfc51e Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Fri, 13 Feb 2015 16:16:34 +0100 Subject: added Type::read methods --- src/core/model/Typesystem.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++ src/core/model/Typesystem.hpp | 27 ++++++++++++++++++++++++ 2 files changed, 76 insertions(+) (limited to 'src') 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 #include #include +#include namespace ousia { @@ -67,6 +68,54 @@ bool Type::build(Variant &data, Logger &logger) const return build(data, logger, NullMagicCallback); } +std::pair Type::read(CharReader &reader, Logger &logger, + const std::unordered_set &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 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 type) const { return false; } bool Type::checkIsa(Handle 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; @@ -167,6 +168,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 read(CharReader &reader, Logger &logger, + const std::unordered_set &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 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. -- cgit v1.2.3