/*! * @file resource.h * @brief Definition of a NewResource. */ #ifndef _RESOURCE_H #define _RESOURCE_H #include "base_object.h" #include #include #include #include "filesys/directory.h" namespace Resources { //! The KeepLevel handles the unloading of Resources. /** * Allocating a Resource also appends a KeepLevel to the Resource. * When the Resource is not used anymore it is decided on the grounds of the KeepLevel, * if the Resource should be deleted. (e.g. at the end of a Level, Campaign, or something like this). */ class KeepLevel { public: /** @param keepLevel the level to set. */ KeepLevel(unsigned int keepLevel) { _keepLevel = keepLevel; }; KeepLevel(const std::string& keepLevelName); /** @returns the KeepLevel */ unsigned int keepLevel() const { return _keepLevel; }; const std::string& name() const; private: unsigned int _keepLevel; //!< The KeepLevel a Resource is in. }; //! Stores a Resource-Pointer, the LoadString and it's keepLevel. class StorePointer { public: StorePointer(const std::string& loadString, const Resources::KeepLevel& keeplevel); /** @returns the LoadString this resource was loaded with */ const std::string& loadString() const { return _loadString; }; /** @returns the KeepLevel of this resource */ const Resources::KeepLevel& keepLevel() const { return _keepLevel; }; private: std::string _loadString; //!< An identifier, to match when loading a File. Resources::KeepLevel _keepLevel; //!< The Priority of this resource. (can only be increased, so none else will delete this) }; //! A Type of Resources. /** * The Type is used to store the Pointers to already loaded Resources, * and also to store type-specific properties. * These are the Loading Paths, the subpaths and so on. */ class Type { public: Type(const std::string& typeName); ~Type(); /** @returns true if the names match @param resourceName the Name to compare. @brief compare the Type with a Name */ bool operator==(const std::string& resourceName) const { return this->_typeName == resourceName; }; void addExtension(const std::string& extension); bool addResourcePath(const std::string& path); bool addResourceSubPath(const std::string& subPath); /// Retrieve Functions /** @returns the name of the stored Class this Type loads Resources for */ const std::string& storedClassName() const { return _typeName; }; /** @returns the ID of the Type != ClassID */ int id() const { return _id; }; /** @returns the type-specific paths this Resource searches in. */ const std::vector& resourcePaths() const { return _resourcePaths; }; /** @returns the Type specific SubPaths this Resource Searches in @see std::vector _resourceSubPaths */ const std::vector& resourceSubPaths() const { return _resourceSubPaths; }; /** @returns the Pointers to the Stored resources. @note do not use this, for more than some lookup */ const std::vector& storedResources() const { return _storedResources; }; void setID(int id); void addResource(Resources::StorePointer* resource); void debug() const; private: int _id; //!< ID of the Type in over all of the Types. const std::string _typeName; //!< Name of the Type. (Name of the Resource this loads.) std::vector _resourcePaths; //!< The Paths to search for files in this type std::vector _resourceSubPaths; //!< The subpaths that will be searched under all the _resourcePaths. std::vector _fileExtensions; //!< File Extensions, this Resource supports. std::vector _storedResources; //!< An array of all the stored Resources. }; //! A NewResource is an Object, that can be loaded from Disk /** * The NewResource Hanldes the location and stores pointers to data that can be retrieved. */ class NewResource : virtual public BaseObject { ObjectListDeclaration(NewResource); public: NewResource(Resources::Type* type); virtual ~NewResource(); /** @brief reloads the underlying resource */ virtual bool reload() { return false; }; /** @brief unloads the underlying Resource */ virtual bool unload() { return false; }; std::string locateFile(const std::string& fileName) const; protected: Resources::StorePointer* acquireResource(const std::string& loadString); void addResource(Resources::StorePointer* pointer); private: std::string locateFileInSubDir(const Directory& directory, const std::string& fileName) const; private: Resources::StorePointer* _pointer; //!< Virtual Pointer to the ResourceData. Resources::Type* _type; //!< Type of the NewResource. }; } #endif /* _RESOURCE_H */