Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/loading/resource.h @ 9851

Last change on this file since 9851 was 9851, checked in by bensch, 18 years ago

orxonox/new_class_id: Resources now get Loaded and Unloaded correctly by will alone

File size: 6.9 KB
RevLine 
[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
[9850]16//! A Namespace Resources and ResourceHandling is defined in.
[9791]17namespace Resources
[7195]18{
[9801]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   */
[9718]25  class KeepLevel
[9714]26  {
27  public:
[9801]28    /** @param keepLevel the level to set. */
[9846]29    inline KeepLevel(unsigned int keepLevel) { _keepLevel = keepLevel; };
[9789]30    KeepLevel(const std::string& keepLevelName);
31
[9850]32    //! Compare equality
[9846]33    inline bool operator==(const KeepLevel& keepLevel) const { return this->_keepLevel == keepLevel._keepLevel; };
[9850]34    //! Compares inequality
[9846]35    inline bool operator!=(const KeepLevel& keepLevel) const { return this->_keepLevel != keepLevel._keepLevel; };
[9850]36    //! Compares less/equal than
[9846]37    inline bool operator<=(const KeepLevel& keepLevel) const { return this->_keepLevel <= keepLevel._keepLevel; };
[9850]38    //! Compares less than
[9846]39    inline bool operator<(const KeepLevel& keepLevel) const { return this->_keepLevel < keepLevel._keepLevel; };
[9845]40
[9850]41    /** @returns the KeepLevel as a number */
[9846]42    inline unsigned int keepLevel() const { return _keepLevel; };
[9799]43    const std::string& name() const;
[9714]44  private:
[9801]45    unsigned int                _keepLevel;              //!< The KeepLevel a Resource is in.
[9714]46  };
[7195]47
[9846]48
[9848]49  ///////////////////
50  // STORE POINTER //
51  ///////////////////
[9801]52  //! Stores a Resource-Pointer, the LoadString and it's keepLevel.
[9789]53  class StorePointer
[9786]54  {
[9800]55  public:
[9845]56    //! Virtual Destructor, that removes the Stored information-pointer.
[9851]57    virtual ~StorePointer();
[9845]58
[9801]59    /** @returns the LoadString this resource was loaded with */
[9800]60    const std::string& loadString() const { return _loadString; };
[9801]61    /** @returns the KeepLevel of this resource */
[9800]62    const Resources::KeepLevel& keepLevel() const { return _keepLevel; };
[9786]63
[9846]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
[9800]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)
[9786]75  };
76
[9846]77
78
[9848]79  ///////////////////
80  // RESOURCE TYPE //
81  ///////////////////
[9801]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   */
[9714]88  class Type
89  {
90  public:
[9823]91    virtual ~Type();
[9850]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; };
[7195]94
[9714]95    void addExtension(const std::string& extension);
[1853]96
[9790]97    bool addResourcePath(const std::string& path);
98    bool addResourceSubPath(const std::string& subPath);
[9784]99
100    /// Retrieve Functions
[9801]101    /** @returns the name of the stored Class this Type loads Resources for */
[9792]102    const std::string& storedClassName() const { return _typeName; };
[9801]103    /** @returns the ID of the Type != ClassID */
104    /** @returns the type-specific paths this Resource searches in. */
[9790]105    const std::vector<Directory>& resourcePaths() const { return _resourcePaths; };
[9801]106    /** @returns the Type specific SubPaths this Resource Searches in @see std::vector<std::string>  _resourceSubPaths */
[9790]107    const std::vector<Directory>& resourceSubPaths() const { return _resourceSubPaths; };
[9801]108    /** @returns the Pointers to the Stored resources. @note do not use this, for more than some lookup */
[9791]109    const std::vector<Resources::StorePointer*>& storedResources() const { return _storedResources; };
[9783]110
[9823]111    virtual void createFromString(const std::string& loadString) = 0;
112
[9845]113    void unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel);
114
[9791]115    void addResource(Resources::StorePointer* resource);
[9783]116
[9793]117    void debug() const;
118
[9844]119  protected:
120    Type(const std::string& typeName);
121
[9714]122  private:
[9844]123    Type(const Type& type) {};
124  private:
[9801]125    const std::string                     _typeName;          //!< Name of the Type. (Name of the Resource this loads.)
126    std::vector<Directory>                _resourcePaths;     //!< The Paths to search for files in this type
127    std::vector<Directory>                _resourceSubPaths;  //!< The subpaths that will be searched under all the _resourcePaths.
128    std::vector<std::string>              _fileExtensions;    //!< File Extensions, this Resource supports.
[9784]129
[9801]130    std::vector<Resources::StorePointer*> _storedResources;   //!< An array of all the stored Resources.
[9714]131  };
132
[9848]133  /**
134   * @brief A Type Definition Class for any Object that is resourceable.
135   *
136   * This Class's main reason of Existence is, that resources can be dynamically
137   * created over a loadString. For this the Type of Resource is required, and the Resource must
138   * itself support the 'void createFromString(const std::string&)' function.
139   */
[9823]140  template<class T> class tType : public Type
141  {
142  public:
[9848]143    /** Create the ResourceType @see Type(const std::string&) */
[9823]144    tType(const std::string& typeName) : Type(typeName) {};
[9848]145    /** @param loadString the String to load a Resource with @brief tries to create a Resource of Type T with a loadString */
[9823]146    virtual void createFromString(const std::string& loadString) { T::createFromString(loadString); }
147  };
[9758]148
[9823]149
[9847]150
[9848]151  /////////////////////
152  // RESOURCE ITSELF //
153  /////////////////////
[9847]154
155  //! A Resource is an Object, that can be loaded from Disk
[9800]156  /**
[9847]157   * The Resource Hanldes the location and stores pointers to data that can be retrieved.
[9800]158   */
[9847]159  class Resource : virtual public BaseObject
[9800]160  {
[9847]161    ObjectListDeclaration(Resource);
[1853]162
[9800]163  public:
[9847]164    Resource(Resources::Type* type);
165    virtual ~Resource();
[3245]166
[9801]167    /** @brief reloads the underlying resource */
[9800]168    virtual bool reload() { return false; };
[9801]169    /** @brief unloads the underlying Resource */
[9800]170    virtual bool unload() { return false; };
[9785]171
[9800]172    std::string locateFile(const std::string& fileName) const;
[9785]173
[9800]174  protected:
175    Resources::StorePointer* acquireResource(const std::string& loadString);
176    void addResource(Resources::StorePointer* pointer);
[9790]177
[9800]178  private:
179    std::string locateFileInSubDir(const Directory& directory, const std::string& fileName) const;
[1853]180
[9800]181  private:
182    Resources::StorePointer*       _pointer;                         //!< Virtual Pointer to the ResourceData.
[9847]183    Resources::Type*               _type;                            //!< Type of the Resource.
[9800]184  };
185}
186
[7193]187#endif /* _RESOURCE_H */
Note: See TracBrowser for help on using the repository browser.