diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-20 00:53:49 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-20 00:53:49 +0100 |
commit | 47311cc8b211a7fef033d744d9eba9f308726ea8 (patch) | |
tree | 348726aef17297729233b93b6d7eef86f25c7a78 /src/core/resource/Resource.hpp | |
parent | d836d70ea2352dcf277c6fce91ba1ded3f074b44 (diff) |
Refactored stuff surrounding the ResourceLocator class, implemented StaticResourceLocator which can be used for registering static resources (mainly for testing or if certain resources need to be available from the executable)
Diffstat (limited to 'src/core/resource/Resource.hpp')
-rw-r--r-- | src/core/resource/Resource.hpp | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/src/core/resource/Resource.hpp b/src/core/resource/Resource.hpp index 9df851d..1934029 100644 --- a/src/core/resource/Resource.hpp +++ b/src/core/resource/Resource.hpp @@ -22,15 +22,159 @@ * Contains the Resource class, representing an external resource as well as * further types used for describing resources. * + * @author Benjamin Paaßen (bpassen@techfak.uni-bielefeld.de) */ #ifndef _OUSIA_RESOURCE_HPP_ #define _OUSIA_RESOURCE_HPP_ +#include <map> +#include <memory> +#include <string> + namespace ousia { +// Forward declaration +class ResourceLocator; + +/** + * This enum contains all possible types of includable resources in Ousía. + */ +enum class ResourceType { + /** + * Unknown type. + */ + UNKNOWN, + + /** + * The resource contains a domain description. + */ + DOMAIN_DESC, + + /** + * The resource contains a typesystem description. + */ + TYPESYSTEM, + + /** + * The resource contains a simple document. + */ + DOCUMENT, + /** + * The resource contains style attributes. + */ + ATTRIBUTES, + + /** + * The resource is a stylesheet. + */ + STYLESHEET, + + /** + * The resource contains script (note that the actual scripting language is + * not specified by the resource type). + */ + SCRIPT, + + /** + * Generic data, such as e.g. images. + */ + DATA +}; + +/** + * 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. + */ +class Resource { +private: + /** + * Specifies whether the resource points at a valid location or not. + */ + bool valid; + /** + * Reference pointing at the location + */ + ResourceLocator const *locator; + + /** + * Requested type of the resource. + */ + ResourceType type; + + /** + * This is a fully qualified/canonical path to the resource found or + * in an undefined state (possibly empty) if the 'valid' flag is set + * to 'false'. + */ + std::string location; + +public: + /** + * Default constructor of the Resource class, represents an invalid + * resource. + */ + Resource(); + + /** + * Constructor of the Resource class. + * + * @param valid specifies whether the Resource is valid or not. + * @param locator specifies the resource locator that was used to locate the + * resource. + */ + Resource(bool valid, const ResourceLocator &locator, + ResourceType type, const std::string &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; + + /** + * Returns whether this resource is valid or not. + * + * @return true if the resource is valid, false otherwise. + */ + bool isValid() const { return valid; } + + /** + * Returns a reference pointing at the locator that was used to locate this + * resource. + * + * @return the locator used for locating this resource. + */ + const ResourceLocator &getLocator() const { return *locator; } + + /** + * Returns the type of the resource that was requested when the resource was + * located. + * + * @return the type of the resource. + */ + ResourceType getType() const { return type; } + + /** + * Returns a canonical location that can be used in a hash map to identify + * a resource. + */ + const std::string &getLocation() const { return location; } +}; + +/** + * Invalid resource instance. + */ +extern const Resource NullResource; } #endif /* _OUSIA_RESOURCE_HPP_ */ |