diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-12-10 02:41:23 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-12-10 02:41:23 +0100 |
commit | a8f78252f05327d37aec3b853109b6d1359af452 (patch) | |
tree | 1eb136057bc59c6cf19ac52d2830c5a186149ce8 /src/core | |
parent | d84efdc312a9c5d0b6757c826810809a8e78f1e2 (diff) | |
parent | 325d78169620094178513303d65c71d933182ae4 (diff) |
Merge branch 'master' of somweyr.de:ousia
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/CSS.hpp | 12 | ||||
-rw-r--r-- | src/core/ResourceLocator.hpp | 120 | ||||
-rw-r--r-- | src/core/variant/Variant.hpp | 73 |
3 files changed, 197 insertions, 8 deletions
diff --git a/src/core/CSS.hpp b/src/core/CSS.hpp index aa701b5..1510f3a 100644 --- a/src/core/CSS.hpp +++ b/src/core/CSS.hpp @@ -70,7 +70,7 @@ struct Specificity { */ class RuleSet : public Managed { private: - std::map<std::string, variant::Variant> rules; + std::map<std::string, Variant> rules; public: /** @@ -78,9 +78,9 @@ public: */ RuleSet(Manager &mgr) : Managed(mgr), rules() {} - std::map<std::string, variant::Variant> &getRules() { return rules; } + std::map<std::string, Variant> &getRules() { return rules; } - const std::map<std::string, variant::Variant> &getRules() const + const std::map<std::string, Variant> &getRules() const { return rules; } @@ -127,11 +127,11 @@ public: class PseudoSelector { private: const std::string name; - const std::vector<std::string> args; + const Variant::arrayType args; const bool generative; public: - PseudoSelector(std::string name, std::vector<std::string> args, + PseudoSelector(std::string name, Variant::arrayType args, bool generative) : name(std::move(name)), args(std::move(args)), generative(generative) { @@ -144,7 +144,7 @@ public: const std::string &getName() const { return name; } - const std::vector<std::string> &getArgs() const { return args; } + const Variant::arrayType &getArgs() const { return args; } const bool &isGenerative() const { return generative; } }; diff --git a/src/core/ResourceLocator.hpp b/src/core/ResourceLocator.hpp new file mode 100644 index 0000000..aaea8a5 --- /dev/null +++ b/src/core/ResourceLocator.hpp @@ -0,0 +1,120 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef _OUSIA_RESOURCE_LOCATOR_HPP_ +#define _OUSIA_RESOURCE_LOCATOR_HPP_ + +#include <istream> +#include <memory> + +namespace ousia { + +/** + * A ResourceLocator is a class able to locate resources in some way, usually + * on the hard drive. + * + * We specify this as an abstract superclass to have an interface layer between + * the program core and possible future extensions in terms of resource + * locations (e.g. online resources, .zip files, etc.). + */ +class ResourceLocator { +public: + /** + * This enum contains all possible types of includable resources in Ousía. + */ + enum class Type { + // A Domain description + DOMAIN, + // An ECMA/JavaScript + SCRIPT, + // A Type System + TYPESYSTEM, + // TODO: Aren't documents and attribute descriptors missing? + // TODO: What is the purpose of these two? + GENERIC_MODULE, + GENERIC_INCLUDE + }; + + /** + * A Location contains the location of a Resource, e.g. a file path + * on a hard drive. Note that the 'found' flag might be set to false + * indicating that a resource was not found. + */ + struct Location { + const bool found; + const ResourceLocator &locator; + const Type type; + const std::string location; + + Location(const bool found, const ResourceLocator &locator, + const Type type, const std::string location) + : found(found), locator(locator), type(type), location(location) + { + } + + /** + * This calls the 'stream' method of the underlying ResourceLocator that + * found this location and returns a stream containing the data of the + * Resource at this location. + * + * @return a stream containing the data of the Resource at this + * location. This has to be a unique_pointer because the current + * C++11 compiler does not yet support move semantics for + * streams. + */ + std::unique_ptr<std::istream> stream() const + { + return std::move(locator.stream(location)); + } + }; + + /** + * The locate function uses this ResourceLocator to search for a given + * Resource name (path parameter). It returns a Location with the + * 'found' flag set accordingly. + * + * @param path is the resource name. + * @param relativeTo TODO: What is the meaning of this parameter? + * @param type is the type of this resource. + * + * @return A Location containing either the found location of the + * Resource and the found flag set to 'true' or an empty location + * and the found flag set to 'false'. + */ + virtual Location locate(const std::string &path, + const std::string &relativeTo, + const Type type) const = 0; + + /** + * This method returns a strem containing the data of the resource at the + * given location. + * + * @param location is a found location, most likely from a Location. + * + * @return a stream containing the data of the Resource at this + * location. This has to be a unique_pointer because the current + * C++11 compiler does not yet support move semantics for + * streams. + */ + virtual std::unique_ptr<std::istream> stream( + const std::string &location) const = 0; +}; +} + +#endif /* _RESOURCE_LOCATOR_HPP_ */ + diff --git a/src/core/variant/Variant.hpp b/src/core/variant/Variant.hpp index 6476780..1e62644 100644 --- a/src/core/variant/Variant.hpp +++ b/src/core/variant/Variant.hpp @@ -65,8 +65,7 @@ public: /** * Exception thrown whenever a variant is accessed via a getter function - * that - * is not supported for the current variant type. + * that is not supported for the current variant type. */ class TypeException : public OusiaException { private: @@ -686,6 +685,76 @@ public: // TODO: Use proper serialization function return os << "\"" << v.first << "\": " << v.second.toString(true); } + + /* + * Comprison operators. + */ + + friend bool operator<(const Variant &lhs, const Variant &rhs) + { + // If the types do not match, we can not do a meaningful comparison. + if (lhs.getType() != rhs.getType()) { + throw TypeException(lhs.getType(), rhs.getType()); + } + switch (lhs.getType()) { + case Type::NULLPTR: + return false; + case Type::BOOL: + return lhs.boolVal < rhs.boolVal; + case Type::INT: + return lhs.intVal < rhs.intVal; + case Type::DOUBLE: + return lhs.doubleVal < rhs.doubleVal; + case Type::STRING: + return lhs.asString() < rhs.asString(); + case Type::ARRAY: + return lhs.asArray() < rhs.asArray(); + case Type::MAP: + return lhs.asMap() < rhs.asMap(); + } + throw OusiaException("Internal Error! Unknown type!"); + } + friend bool operator>(const Variant &lhs, const Variant &rhs) + { + return rhs < lhs; + } + friend bool operator<=(const Variant &lhs, const Variant &rhs) + { + return !(lhs > rhs); + } + friend bool operator>=(const Variant &lhs, const Variant &rhs) + { + return !(lhs < rhs); + } + + friend bool operator==(const Variant &lhs, const Variant &rhs) + { + if (lhs.getType() != rhs.getType()) { + return false; + } + switch (lhs.getType()) { + case Type::NULLPTR: + return true; + case Type::BOOL: + return lhs.boolVal == rhs.boolVal; + case Type::INT: + return lhs.intVal == rhs.intVal; + case Type::DOUBLE: + return lhs.doubleVal == rhs.doubleVal; + case Type::STRING: + return lhs.asString() == rhs.asString(); + case Type::ARRAY: + return lhs.asArray() == rhs.asArray(); + case Type::MAP: + return lhs.asMap() == rhs.asMap(); + } + throw OusiaException("Internal Error! Unknown type!"); + } + + friend bool operator!=(const Variant &lhs, const Variant &rhs) + { + return !(lhs == rhs); + } }; } |