Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

more nice comments, and also updated the KeepLevel loading (if you want to load a Resource to a KeepLevel just append it at loadtime:
eg.:
Texture tex = ResourceTexture(orxonox.png, GL_TEXTURE_2D, GameEnd);
where GameEnd is the KeepLevel as defined in orxonox.cc→initResources()

File size: 7.1 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:
[9854]28    KeepLevel();
29    KeepLevel(unsigned int 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
[9854]95    ////////////////////
96    //// EXTENSIONS ////
[9714]97    void addExtension(const std::string& extension);
[1853]98
[9854]99    ///////////////
100    //// PATHS ////
[9790]101    bool addResourcePath(const std::string& path);
102    bool addResourceSubPath(const std::string& subPath);
[9784]103
104    /// Retrieve Functions
[9801]105    /** @returns the name of the stored Class this Type loads Resources for */
[9792]106    const std::string& storedClassName() const { return _typeName; };
[9801]107    /** @returns the ID of the Type != ClassID */
108    /** @returns the type-specific paths this Resource searches in. */
[9790]109    const std::vector<Directory>& resourcePaths() const { return _resourcePaths; };
[9801]110    /** @returns the Type specific SubPaths this Resource Searches in @see std::vector<std::string>  _resourceSubPaths */
[9790]111    const std::vector<Directory>& resourceSubPaths() const { return _resourceSubPaths; };
[9801]112    /** @returns the Pointers to the Stored resources. @note do not use this, for more than some lookup */
[9791]113    const std::vector<Resources::StorePointer*>& storedResources() const { return _storedResources; };
[9783]114
[9854]115    ///////////////////////////////
116    //// LOADING AND UNLOADING ////
117    virtual void createFromString(const std::string& loadString, const KeepLevel& keepLevel = KeepLevel()) = 0;
[9845]118    void unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel);
119
[9854]120    ///////////////////
121    //// INTERNALS ////
[9791]122    void addResource(Resources::StorePointer* resource);
[9783]123
[9854]124    ///////////////
125    //// DEBUG ////
[9793]126    void debug() const;
127
[9844]128  protected:
129    Type(const std::string& typeName);
130
[9714]131  private:
[9844]132    Type(const Type& type) {};
133  private:
[9801]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.
[9784]138
[9801]139    std::vector<Resources::StorePointer*> _storedResources;   //!< An array of all the stored Resources.
[9714]140  };
141
[9848]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   */
[9823]149  template<class T> class tType : public Type
150  {
151  public:
[9848]152    /** Create the ResourceType @see Type(const std::string&) */
[9823]153    tType(const std::string& typeName) : Type(typeName) {};
[9848]154    /** @param loadString the String to load a Resource with @brief tries to create a Resource of Type T with a loadString */
[9854]155    virtual void createFromString(const std::string& loadString, const KeepLevel& keepLevel = KeepLevel()) { T::createFromString(loadString, keepLevel); }
[9823]156  };
[9758]157
[9823]158
[9847]159
[9848]160  /////////////////////
161  // RESOURCE ITSELF //
162  /////////////////////
[9847]163  //! A Resource is an Object, that can be loaded from Disk
[9800]164  /**
[9847]165   * The Resource Hanldes the location and stores pointers to data that can be retrieved.
[9800]166   */
[9847]167  class Resource : virtual public BaseObject
[9800]168  {
[9847]169    ObjectListDeclaration(Resource);
[1853]170
[9800]171  public:
[9847]172    Resource(Resources::Type* type);
173    virtual ~Resource();
[3245]174
[9801]175    /** @brief reloads the underlying resource */
[9800]176    virtual bool reload() { return false; };
[9801]177    /** @brief unloads the underlying Resource */
[9800]178    virtual bool unload() { return false; };
[9785]179
[9800]180    std::string locateFile(const std::string& fileName) const;
[9785]181
[9800]182  protected:
183    Resources::StorePointer* acquireResource(const std::string& loadString);
184    void addResource(Resources::StorePointer* pointer);
[9790]185
[9800]186  private:
187    std::string locateFileInSubDir(const Directory& directory, const std::string& fileName) const;
[1853]188
[9800]189  private:
190    Resources::StorePointer*       _pointer;                         //!< Virtual Pointer to the ResourceData.
[9847]191    Resources::Type*               _type;                            //!< Type of the Resource.
[9800]192  };
193}
194
[7193]195#endif /* _RESOURCE_H */
Note: See TracBrowser for help on using the repository browser.