Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

print out the key each time

File size: 5.4 KB
RevLine 
[4838]1/*!
[7193]2 * @file resource.h
[9788]3 * @brief Definition of a NewResource.
[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]16namespace 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. */
[9789]28    KeepLevel(unsigned int keepLevel) { _keepLevel = keepLevel; };
29    KeepLevel(const std::string& keepLevelName);
30
[9801]31    /** @returns the KeepLevel */
[9798]32    unsigned int keepLevel() const { return _keepLevel; };
[9799]33    const std::string& name() const;
[9714]34  private:
[9801]35    unsigned int                _keepLevel;              //!< The KeepLevel a Resource is in.
[9714]36  };
[7195]37
[9801]38  //! Stores a Resource-Pointer, the LoadString and it's keepLevel.
[9789]39  class StorePointer
[9786]40  {
[9800]41  public:
42    StorePointer(const std::string& loadString, const Resources::KeepLevel& keeplevel);
[9801]43    /** @returns the LoadString this resource was loaded with */
[9800]44    const std::string& loadString() const { return _loadString; };
[9801]45    /** @returns the KeepLevel of this resource */
[9800]46    const Resources::KeepLevel& keepLevel() const { return _keepLevel; };
[9786]47
[9800]48  private:
49    std::string                 _loadString;             //!< An identifier, to match when loading a File.
50    Resources::KeepLevel        _keepLevel;              //!< The Priority of this resource. (can only be increased, so none else will delete this)
[9786]51  };
52
[9801]53  //! A Type of Resources.
54  /**
55   * The Type is used to store the Pointers to already loaded Resources,
56   * and also to store type-specific properties.
57   * These are the Loading Paths, the subpaths and so on.
58   */
[9714]59  class Type
60  {
61  public:
[9823]62    virtual ~Type();
[9801]63    /** @returns true if the names match @param resourceName the Name to compare. @brief compare the Type with a Name */
[9792]64    bool operator==(const std::string& resourceName) const { return this->_typeName == resourceName; };
[7195]65
[9714]66    void addExtension(const std::string& extension);
[1853]67
[9790]68    bool addResourcePath(const std::string& path);
69    bool addResourceSubPath(const std::string& subPath);
[9784]70
71    /// Retrieve Functions
[9801]72    /** @returns the name of the stored Class this Type loads Resources for */
[9792]73    const std::string& storedClassName() const { return _typeName; };
[9801]74    /** @returns the ID of the Type != ClassID */
[9783]75    int id() const { return _id; };
[9801]76    /** @returns the type-specific paths this Resource searches in. */
[9790]77    const std::vector<Directory>& resourcePaths() const { return _resourcePaths; };
[9801]78    /** @returns the Type specific SubPaths this Resource Searches in @see std::vector<std::string>  _resourceSubPaths */
[9790]79    const std::vector<Directory>& resourceSubPaths() const { return _resourceSubPaths; };
[9801]80    /** @returns the Pointers to the Stored resources. @note do not use this, for more than some lookup */
[9791]81    const std::vector<Resources::StorePointer*>& storedResources() const { return _storedResources; };
[9783]82
[9823]83    virtual void createFromString(const std::string& loadString) = 0;
84
[9783]85    void setID(int id);
[9791]86    void addResource(Resources::StorePointer* resource);
[9783]87
[9793]88    void debug() const;
89
[9844]90  protected:
91    Type(const std::string& typeName);
92
[9714]93  private:
[9844]94    Type(const Type& type) {};
95  private:
[9801]96    int                                   _id;                //!< ID of the Type in over all of the Types.
97    const std::string                     _typeName;          //!< Name of the Type. (Name of the Resource this loads.)
98    std::vector<Directory>                _resourcePaths;     //!< The Paths to search for files in this type
99    std::vector<Directory>                _resourceSubPaths;  //!< The subpaths that will be searched under all the _resourcePaths.
100    std::vector<std::string>              _fileExtensions;    //!< File Extensions, this Resource supports.
[9784]101
[9801]102    std::vector<Resources::StorePointer*> _storedResources;   //!< An array of all the stored Resources.
[9714]103  };
104
[9823]105  template<class T> class tType : public Type
106  {
107  public:
108    tType(const std::string& typeName) : Type(typeName) {};
109    virtual void createFromString(const std::string& loadString) { T::createFromString(loadString); }
110  };
[9758]111
[9823]112
[9800]113  //! A NewResource is an Object, that can be loaded from Disk
114  /**
[9801]115   * The NewResource Hanldes the location and stores pointers to data that can be retrieved.
[9800]116   */
117  class NewResource : virtual public BaseObject
118  {
119    ObjectListDeclaration(NewResource);
[1853]120
[9800]121  public:
122    NewResource(Resources::Type* type);
123    virtual ~NewResource();
[3245]124
[9801]125    /** @brief reloads the underlying resource */
[9800]126    virtual bool reload() { return false; };
[9801]127    /** @brief unloads the underlying Resource */
[9800]128    virtual bool unload() { return false; };
[9785]129
[9800]130    std::string locateFile(const std::string& fileName) const;
[9785]131
[9800]132  protected:
133    Resources::StorePointer* acquireResource(const std::string& loadString);
134    void addResource(Resources::StorePointer* pointer);
[9790]135
[9800]136  private:
137    std::string locateFileInSubDir(const Directory& directory, const std::string& fileName) const;
[1853]138
[9800]139  private:
140    Resources::StorePointer*       _pointer;                         //!< Virtual Pointer to the ResourceData.
141    Resources::Type*               _type;                            //!< Type of the NewResource.
142  };
143}
144
[7193]145#endif /* _RESOURCE_H */
Note: See TracBrowser for help on using the repository browser.