/*! * @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. */ inline KeepLevel(unsigned int keepLevel) { _keepLevel = keepLevel; }; KeepLevel(const std::string& keepLevelName); inline bool operator==(const KeepLevel& keepLevel) const { return this->_keepLevel == keepLevel._keepLevel; }; inline bool operator!=(const KeepLevel& keepLevel) const { return this->_keepLevel != keepLevel._keepLevel; }; inline bool operator<=(const KeepLevel& keepLevel) const { return this->_keepLevel <= keepLevel._keepLevel; }; inline bool operator<(const KeepLevel& keepLevel) const { return this->_keepLevel < keepLevel._keepLevel; }; /** @returns the KeepLevel */ inline 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: //! Virtual Destructor, that removes the Stored information-pointer. virtual ~StorePointer() {}; /** @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; }; virtual bool last() const = 0; protected: StorePointer(const std::string& loadString, const Resources::KeepLevel& keeplevel); private: StorePointer(const StorePointer&) : _keepLevel(0) {}; 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: virtual ~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 */ /** @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; }; virtual void createFromString(const std::string& loadString) = 0; void unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel); void addResource(Resources::StorePointer* resource); void debug() const; protected: Type(const std::string& typeName); private: Type(const Type& type) {}; private: 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. }; template class tType : public Type { public: tType(const std::string& typeName) : Type(typeName) {}; virtual void createFromString(const std::string& loadString) { T::createFromString(loadString); } }; //! 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 */