[4838] | 1 | /*! |
---|
[7193] | 2 | * @file resource.h |
---|
[9847] | 3 | * @brief Definition of a Resource. |
---|
[3245] | 4 | */ |
---|
[1853] | 5 | |
---|
[7193] | 6 | #ifndef _RESOURCE_H |
---|
| 7 | #define _RESOURCE_H |
---|
[1853] | 8 | |
---|
[3543] | 9 | #include "base_object.h" |
---|
[7195] | 10 | #include <string> |
---|
[9714] | 11 | #include <vector> |
---|
| 12 | #include <set> |
---|
[1853] | 13 | |
---|
[9785] | 14 | #include "filesys/directory.h" |
---|
| 15 | |
---|
[9791] | 16 | namespace Resources |
---|
[7195] | 17 | { |
---|
[9801] | 18 | //! The KeepLevel handles the unloading of Resources. |
---|
| 19 | /** |
---|
| 20 | * Allocating a Resource also appends a KeepLevel to the Resource. |
---|
| 21 | * When the Resource is not used anymore it is decided on the grounds of the KeepLevel, |
---|
| 22 | * if the Resource should be deleted. (e.g. at the end of a Level, Campaign, or something like this). |
---|
| 23 | */ |
---|
[9718] | 24 | class KeepLevel |
---|
[9714] | 25 | { |
---|
| 26 | public: |
---|
[9801] | 27 | /** @param keepLevel the level to set. */ |
---|
[9846] | 28 | inline KeepLevel(unsigned int keepLevel) { _keepLevel = keepLevel; }; |
---|
[9789] | 29 | KeepLevel(const std::string& keepLevelName); |
---|
| 30 | |
---|
[9846] | 31 | inline bool operator==(const KeepLevel& keepLevel) const { return this->_keepLevel == keepLevel._keepLevel; }; |
---|
| 32 | inline bool operator!=(const KeepLevel& keepLevel) const { return this->_keepLevel != keepLevel._keepLevel; }; |
---|
| 33 | inline bool operator<=(const KeepLevel& keepLevel) const { return this->_keepLevel <= keepLevel._keepLevel; }; |
---|
| 34 | inline bool operator<(const KeepLevel& keepLevel) const { return this->_keepLevel < keepLevel._keepLevel; }; |
---|
[9845] | 35 | |
---|
[9801] | 36 | /** @returns the KeepLevel */ |
---|
[9846] | 37 | inline unsigned int keepLevel() const { return _keepLevel; }; |
---|
[9799] | 38 | const std::string& name() const; |
---|
[9714] | 39 | private: |
---|
[9801] | 40 | unsigned int _keepLevel; //!< The KeepLevel a Resource is in. |
---|
[9714] | 41 | }; |
---|
[7195] | 42 | |
---|
[9846] | 43 | |
---|
| 44 | |
---|
[9801] | 45 | //! Stores a Resource-Pointer, the LoadString and it's keepLevel. |
---|
[9789] | 46 | class StorePointer |
---|
[9786] | 47 | { |
---|
[9800] | 48 | public: |
---|
[9845] | 49 | //! Virtual Destructor, that removes the Stored information-pointer. |
---|
| 50 | virtual ~StorePointer() {}; |
---|
| 51 | |
---|
[9801] | 52 | /** @returns the LoadString this resource was loaded with */ |
---|
[9800] | 53 | const std::string& loadString() const { return _loadString; }; |
---|
[9801] | 54 | /** @returns the KeepLevel of this resource */ |
---|
[9800] | 55 | const Resources::KeepLevel& keepLevel() const { return _keepLevel; }; |
---|
[9786] | 56 | |
---|
[9846] | 57 | virtual bool last() const = 0; |
---|
| 58 | |
---|
| 59 | protected: |
---|
| 60 | StorePointer(const std::string& loadString, const Resources::KeepLevel& keeplevel); |
---|
| 61 | |
---|
| 62 | private: |
---|
| 63 | StorePointer(const StorePointer&) : _keepLevel(0) {}; |
---|
| 64 | |
---|
[9800] | 65 | private: |
---|
| 66 | std::string _loadString; //!< An identifier, to match when loading a File. |
---|
| 67 | Resources::KeepLevel _keepLevel; //!< The Priority of this resource. (can only be increased, so none else will delete this) |
---|
[9786] | 68 | }; |
---|
| 69 | |
---|
[9846] | 70 | |
---|
| 71 | |
---|
| 72 | |
---|
[9801] | 73 | //! A Type of Resources. |
---|
| 74 | /** |
---|
| 75 | * The Type is used to store the Pointers to already loaded Resources, |
---|
| 76 | * and also to store type-specific properties. |
---|
| 77 | * These are the Loading Paths, the subpaths and so on. |
---|
| 78 | */ |
---|
[9714] | 79 | class Type |
---|
| 80 | { |
---|
| 81 | public: |
---|
[9823] | 82 | virtual ~Type(); |
---|
[9801] | 83 | /** @returns true if the names match @param resourceName the Name to compare. @brief compare the Type with a Name */ |
---|
[9792] | 84 | bool operator==(const std::string& resourceName) const { return this->_typeName == resourceName; }; |
---|
[7195] | 85 | |
---|
[9714] | 86 | void addExtension(const std::string& extension); |
---|
[1853] | 87 | |
---|
[9790] | 88 | bool addResourcePath(const std::string& path); |
---|
| 89 | bool addResourceSubPath(const std::string& subPath); |
---|
[9784] | 90 | |
---|
| 91 | /// Retrieve Functions |
---|
[9801] | 92 | /** @returns the name of the stored Class this Type loads Resources for */ |
---|
[9792] | 93 | const std::string& storedClassName() const { return _typeName; }; |
---|
[9801] | 94 | /** @returns the ID of the Type != ClassID */ |
---|
| 95 | /** @returns the type-specific paths this Resource searches in. */ |
---|
[9790] | 96 | const std::vector<Directory>& resourcePaths() const { return _resourcePaths; }; |
---|
[9801] | 97 | /** @returns the Type specific SubPaths this Resource Searches in @see std::vector<std::string> _resourceSubPaths */ |
---|
[9790] | 98 | const std::vector<Directory>& resourceSubPaths() const { return _resourceSubPaths; }; |
---|
[9801] | 99 | /** @returns the Pointers to the Stored resources. @note do not use this, for more than some lookup */ |
---|
[9791] | 100 | const std::vector<Resources::StorePointer*>& storedResources() const { return _storedResources; }; |
---|
[9783] | 101 | |
---|
[9823] | 102 | virtual void createFromString(const std::string& loadString) = 0; |
---|
| 103 | |
---|
[9845] | 104 | void unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel); |
---|
| 105 | |
---|
[9791] | 106 | void addResource(Resources::StorePointer* resource); |
---|
[9783] | 107 | |
---|
[9793] | 108 | void debug() const; |
---|
| 109 | |
---|
[9844] | 110 | protected: |
---|
| 111 | Type(const std::string& typeName); |
---|
| 112 | |
---|
[9714] | 113 | private: |
---|
[9844] | 114 | Type(const Type& type) {}; |
---|
| 115 | private: |
---|
[9801] | 116 | const std::string _typeName; //!< Name of the Type. (Name of the Resource this loads.) |
---|
| 117 | std::vector<Directory> _resourcePaths; //!< The Paths to search for files in this type |
---|
| 118 | std::vector<Directory> _resourceSubPaths; //!< The subpaths that will be searched under all the _resourcePaths. |
---|
| 119 | std::vector<std::string> _fileExtensions; //!< File Extensions, this Resource supports. |
---|
[9784] | 120 | |
---|
[9801] | 121 | std::vector<Resources::StorePointer*> _storedResources; //!< An array of all the stored Resources. |
---|
[9714] | 122 | }; |
---|
| 123 | |
---|
[9823] | 124 | template<class T> class tType : public Type |
---|
| 125 | { |
---|
| 126 | public: |
---|
| 127 | tType(const std::string& typeName) : Type(typeName) {}; |
---|
| 128 | virtual void createFromString(const std::string& loadString) { T::createFromString(loadString); } |
---|
| 129 | }; |
---|
[9758] | 130 | |
---|
[9823] | 131 | |
---|
[9847] | 132 | |
---|
| 133 | |
---|
| 134 | //! A Resource is an Object, that can be loaded from Disk |
---|
[9800] | 135 | /** |
---|
[9847] | 136 | * The Resource Hanldes the location and stores pointers to data that can be retrieved. |
---|
[9800] | 137 | */ |
---|
[9847] | 138 | class Resource : virtual public BaseObject |
---|
[9800] | 139 | { |
---|
[9847] | 140 | ObjectListDeclaration(Resource); |
---|
[1853] | 141 | |
---|
[9800] | 142 | public: |
---|
[9847] | 143 | Resource(Resources::Type* type); |
---|
| 144 | virtual ~Resource(); |
---|
[3245] | 145 | |
---|
[9801] | 146 | /** @brief reloads the underlying resource */ |
---|
[9800] | 147 | virtual bool reload() { return false; }; |
---|
[9801] | 148 | /** @brief unloads the underlying Resource */ |
---|
[9800] | 149 | virtual bool unload() { return false; }; |
---|
[9785] | 150 | |
---|
[9800] | 151 | std::string locateFile(const std::string& fileName) const; |
---|
[9785] | 152 | |
---|
[9800] | 153 | protected: |
---|
| 154 | Resources::StorePointer* acquireResource(const std::string& loadString); |
---|
| 155 | void addResource(Resources::StorePointer* pointer); |
---|
[9790] | 156 | |
---|
[9800] | 157 | private: |
---|
| 158 | std::string locateFileInSubDir(const Directory& directory, const std::string& fileName) const; |
---|
[1853] | 159 | |
---|
[9800] | 160 | private: |
---|
| 161 | Resources::StorePointer* _pointer; //!< Virtual Pointer to the ResourceData. |
---|
[9847] | 162 | Resources::Type* _type; //!< Type of the Resource. |
---|
[9800] | 163 | }; |
---|
| 164 | } |
---|
| 165 | |
---|
[7193] | 166 | #endif /* _RESOURCE_H */ |
---|