| 1 | /*! |
|---|
| 2 | * @file resource.h |
|---|
| 3 | * @brief Definition of a Resource. |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | #ifndef _RESOURCE_H |
|---|
| 7 | #define _RESOURCE_H |
|---|
| 8 | |
|---|
| 9 | #include "base_object.h" |
|---|
| 10 | #include <string> |
|---|
| 11 | #include <vector> |
|---|
| 12 | #include <set> |
|---|
| 13 | |
|---|
| 14 | #include "filesys/directory.h" |
|---|
| 15 | |
|---|
| 16 | //! A Namespace Resources and ResourceHandling is defined in. |
|---|
| 17 | namespace Resources |
|---|
| 18 | { |
|---|
| 19 | //! The KeepLevel handles the unloading of Resources. |
|---|
| 20 | /** |
|---|
| 21 | * Allocating a Resource also appends a KeepLevel to the Resource. |
|---|
| 22 | * When the Resource is not used anymore it is decided on the grounds of the KeepLevel, |
|---|
| 23 | * if the Resource should be deleted. (e.g. at the end of a Level, Campaign, or something like this). |
|---|
| 24 | */ |
|---|
| 25 | class KeepLevel |
|---|
| 26 | { |
|---|
| 27 | public: |
|---|
| 28 | KeepLevel(); |
|---|
| 29 | KeepLevel(unsigned int keepLevel); |
|---|
| 30 | KeepLevel(const std::string& keepLevelName); |
|---|
| 31 | |
|---|
| 32 | //! Compare equality |
|---|
| 33 | inline bool operator==(const KeepLevel& keepLevel) const { return this->_keepLevel == keepLevel._keepLevel; }; |
|---|
| 34 | //! Compares inequality |
|---|
| 35 | inline bool operator!=(const KeepLevel& keepLevel) const { return this->_keepLevel != keepLevel._keepLevel; }; |
|---|
| 36 | //! Compares less/equal than |
|---|
| 37 | inline bool operator<=(const KeepLevel& keepLevel) const { return this->_keepLevel <= keepLevel._keepLevel; }; |
|---|
| 38 | //! Compares less than |
|---|
| 39 | inline bool operator<(const KeepLevel& keepLevel) const { return this->_keepLevel < keepLevel._keepLevel; }; |
|---|
| 40 | |
|---|
| 41 | /** @returns the KeepLevel as a number */ |
|---|
| 42 | inline unsigned int keepLevel() const { return _keepLevel; }; |
|---|
| 43 | const std::string& name() const; |
|---|
| 44 | private: |
|---|
| 45 | unsigned int _keepLevel; //!< The KeepLevel a Resource is in. |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | /////////////////// |
|---|
| 50 | // STORE POINTER // |
|---|
| 51 | /////////////////// |
|---|
| 52 | //! Stores a Resource-Pointer, the LoadString and it's keepLevel. |
|---|
| 53 | class StorePointer |
|---|
| 54 | { |
|---|
| 55 | public: |
|---|
| 56 | //! Virtual Destructor, that removes the Stored information-pointer. |
|---|
| 57 | virtual ~StorePointer(); |
|---|
| 58 | |
|---|
| 59 | /** @returns the LoadString this resource was loaded with */ |
|---|
| 60 | const std::string& loadString() const { return _loadString; }; |
|---|
| 61 | /** @returns the KeepLevel of this resource */ |
|---|
| 62 | const Resources::KeepLevel& keepLevel() const { return _keepLevel; }; |
|---|
| 63 | |
|---|
| 64 | virtual bool last() const = 0; |
|---|
| 65 | |
|---|
| 66 | protected: |
|---|
| 67 | StorePointer(const std::string& loadString, const Resources::KeepLevel& keeplevel); |
|---|
| 68 | |
|---|
| 69 | private: |
|---|
| 70 | StorePointer(const StorePointer&) : _keepLevel(0) {}; |
|---|
| 71 | |
|---|
| 72 | private: |
|---|
| 73 | std::string _loadString; //!< An identifier, to match when loading a File. |
|---|
| 74 | Resources::KeepLevel _keepLevel; //!< The Priority of this resource. (can only be increased, so none else will delete this) |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | /////////////////// |
|---|
| 80 | // RESOURCE TYPE // |
|---|
| 81 | /////////////////// |
|---|
| 82 | //! A Type of Resources. |
|---|
| 83 | /** |
|---|
| 84 | * The Type is used to store the Pointers to already loaded Resources, |
|---|
| 85 | * and also to store type-specific properties. |
|---|
| 86 | * These are the Loading Paths, the subpaths and so on. |
|---|
| 87 | */ |
|---|
| 88 | class Type |
|---|
| 89 | { |
|---|
| 90 | public: |
|---|
| 91 | virtual ~Type(); |
|---|
| 92 | /** @returns true if the names match @param typeName the Name to compare. @brief compare the Type with a Name */ |
|---|
| 93 | bool operator==(const std::string& typeName) const { return this->_typeName == typeName; }; |
|---|
| 94 | |
|---|
| 95 | //////////////////// |
|---|
| 96 | //// EXTENSIONS //// |
|---|
| 97 | void addExtension(const std::string& extension); |
|---|
| 98 | |
|---|
| 99 | /////////////// |
|---|
| 100 | //// PATHS //// |
|---|
| 101 | bool addResourcePath(const std::string& path); |
|---|
| 102 | bool addResourceSubPath(const std::string& subPath); |
|---|
| 103 | |
|---|
| 104 | /// Retrieve Functions |
|---|
| 105 | /** @returns the name of the stored Class this Type loads Resources for */ |
|---|
| 106 | const std::string& storedClassName() const { return _typeName; }; |
|---|
| 107 | /** @returns the ID of the Type != ClassID */ |
|---|
| 108 | /** @returns the type-specific paths this Resource searches in. */ |
|---|
| 109 | const std::vector<Directory>& resourcePaths() const { return _resourcePaths; }; |
|---|
| 110 | /** @returns the Type specific SubPaths this Resource Searches in @see std::vector<std::string> _resourceSubPaths */ |
|---|
| 111 | const std::vector<Directory>& resourceSubPaths() const { return _resourceSubPaths; }; |
|---|
| 112 | /** @returns the Pointers to the Stored resources. @note do not use this, for more than some lookup */ |
|---|
| 113 | const std::vector<Resources::StorePointer*>& storedResources() const { return _storedResources; }; |
|---|
| 114 | |
|---|
| 115 | /////////////////////////////// |
|---|
| 116 | //// LOADING AND UNLOADING //// |
|---|
| 117 | virtual void createFromString(const std::string& loadString, const KeepLevel& keepLevel = KeepLevel()) = 0; |
|---|
| 118 | void unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel); |
|---|
| 119 | |
|---|
| 120 | /////////////////// |
|---|
| 121 | //// INTERNALS //// |
|---|
| 122 | void addResource(Resources::StorePointer* resource); |
|---|
| 123 | |
|---|
| 124 | /////////////// |
|---|
| 125 | //// DEBUG //// |
|---|
| 126 | void debug() const; |
|---|
| 127 | |
|---|
| 128 | protected: |
|---|
| 129 | Type(const std::string& typeName); |
|---|
| 130 | |
|---|
| 131 | private: |
|---|
| 132 | Type(const Type& type) {}; |
|---|
| 133 | private: |
|---|
| 134 | const std::string _typeName; //!< Name of the Type. (Name of the Resource this loads.) |
|---|
| 135 | std::vector<Directory> _resourcePaths; //!< The Paths to search for files in this type |
|---|
| 136 | std::vector<Directory> _resourceSubPaths; //!< The subpaths that will be searched under all the _resourcePaths. |
|---|
| 137 | std::vector<std::string> _fileExtensions; //!< File Extensions, this Resource supports. |
|---|
| 138 | |
|---|
| 139 | std::vector<Resources::StorePointer*> _storedResources; //!< An array of all the stored Resources. |
|---|
| 140 | }; |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * @brief A Type Definition Class for any Object that is resourceable. |
|---|
| 144 | * |
|---|
| 145 | * This Class's main reason of Existence is, that resources can be dynamically |
|---|
| 146 | * created over a loadString. For this the Type of Resource is required, and the Resource must |
|---|
| 147 | * itself support the 'void createFromString(const std::string&)' function. |
|---|
| 148 | */ |
|---|
| 149 | template<class T> class tType : public Type |
|---|
| 150 | { |
|---|
| 151 | public: |
|---|
| 152 | /** Create the ResourceType @see Type(const std::string&) */ |
|---|
| 153 | tType(const std::string& typeName) : Type(typeName) {}; |
|---|
| 154 | /** @param loadString the String to load a Resource with @brief tries to create a Resource of Type T with a loadString */ |
|---|
| 155 | virtual void createFromString(const std::string& loadString, const KeepLevel& keepLevel = KeepLevel()) { T::createFromString(loadString, keepLevel); } |
|---|
| 156 | }; |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | ///////////////////// |
|---|
| 161 | // RESOURCE ITSELF // |
|---|
| 162 | ///////////////////// |
|---|
| 163 | //! A Resource is an Object, that can be loaded from Disk |
|---|
| 164 | /** |
|---|
| 165 | * The Resource Hanldes the location and stores pointers to data that can be retrieved. |
|---|
| 166 | */ |
|---|
| 167 | class Resource : virtual public BaseObject |
|---|
| 168 | { |
|---|
| 169 | ObjectListDeclaration(Resource); |
|---|
| 170 | |
|---|
| 171 | public: |
|---|
| 172 | Resource(Resources::Type* type); |
|---|
| 173 | virtual ~Resource(); |
|---|
| 174 | |
|---|
| 175 | /** @brief reloads the underlying resource */ |
|---|
| 176 | virtual bool reload() { return false; }; |
|---|
| 177 | /** @brief unloads the underlying Resource */ |
|---|
| 178 | virtual bool unload() { return false; }; |
|---|
| 179 | |
|---|
| 180 | std::string locateFile(const std::string& fileName) const; |
|---|
| 181 | |
|---|
| 182 | protected: |
|---|
| 183 | Resources::StorePointer* acquireResource(const std::string& loadString); |
|---|
| 184 | void addResource(Resources::StorePointer* pointer); |
|---|
| 185 | |
|---|
| 186 | private: |
|---|
| 187 | std::string locateFileInSubDir(const Directory& directory, const std::string& fileName) const; |
|---|
| 188 | |
|---|
| 189 | private: |
|---|
| 190 | Resources::StorePointer* _pointer; //!< Virtual Pointer to the ResourceData. |
|---|
| 191 | Resources::Type* _type; //!< Type of the Resource. |
|---|
| 192 | }; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | #endif /* _RESOURCE_H */ |
|---|