Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

new_class_id: dynamic loading works completely

try the shell with:
ResourceManger load Texture some-pic-from-data

File size: 6.7 KB
Line 
1/*!
2 * @file resource.h
3 * @brief Definition of a Resource.
4*/
5
6#ifndef _RESOURCE_H
7#define _RESOURCE_H
8
9#include "base_object.h"
10#include <string>
11#include <vector>
12#include <set>
13
14#include "filesys/directory.h"
15
16namespace Resources
17{
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   */
24  class KeepLevel
25  {
26  public:
27    /** @param keepLevel the level to set. */
28    inline KeepLevel(unsigned int keepLevel) { _keepLevel = keepLevel; };
29    KeepLevel(const std::string& keepLevelName);
30
31    inline bool operator==(const KeepLevel& keepLevel) const { return this->_keepLevel == keepLevel._keepLevel; };
32    inline bool operator!=(const KeepLevel& keepLevel) const { return this->_keepLevel != keepLevel._keepLevel; };
33    inline bool operator<=(const KeepLevel& keepLevel) const { return this->_keepLevel <= keepLevel._keepLevel; };
34    inline bool operator<(const KeepLevel& keepLevel) const { return this->_keepLevel < keepLevel._keepLevel; };
35
36    /** @returns the KeepLevel */
37    inline unsigned int keepLevel() const { return _keepLevel; };
38    const std::string& name() const;
39  private:
40    unsigned int                _keepLevel;              //!< The KeepLevel a Resource is in.
41  };
42
43
44  ///////////////////
45  // STORE POINTER //
46  ///////////////////
47  //! Stores a Resource-Pointer, the LoadString and it's keepLevel.
48  class StorePointer
49  {
50  public:
51    //! Virtual Destructor, that removes the Stored information-pointer.
52    virtual ~StorePointer() {};
53
54    /** @returns the LoadString this resource was loaded with */
55    const std::string& loadString() const { return _loadString; };
56    /** @returns the KeepLevel of this resource */
57    const Resources::KeepLevel& keepLevel() const { return _keepLevel; };
58
59    virtual bool last() const = 0;
60
61    protected:
62      StorePointer(const std::string& loadString, const Resources::KeepLevel& keeplevel);
63
64    private:
65      StorePointer(const StorePointer&) : _keepLevel(0) {};
66
67  private:
68    std::string                 _loadString;             //!< An identifier, to match when loading a File.
69    Resources::KeepLevel        _keepLevel;              //!< The Priority of this resource. (can only be increased, so none else will delete this)
70  };
71
72
73
74  ///////////////////
75  // RESOURCE TYPE //
76  ///////////////////
77  //! A Type of Resources.
78  /**
79   * The Type is used to store the Pointers to already loaded Resources,
80   * and also to store type-specific properties.
81   * These are the Loading Paths, the subpaths and so on.
82   */
83  class Type
84  {
85  public:
86    virtual ~Type();
87    /** @returns true if the names match @param resourceName the Name to compare. @brief compare the Type with a Name */
88    bool operator==(const std::string& resourceName) const { return this->_typeName == resourceName; };
89
90    void addExtension(const std::string& extension);
91
92    bool addResourcePath(const std::string& path);
93    bool addResourceSubPath(const std::string& subPath);
94
95    /// Retrieve Functions
96    /** @returns the name of the stored Class this Type loads Resources for */
97    const std::string& storedClassName() const { return _typeName; };
98    /** @returns the ID of the Type != ClassID */
99    /** @returns the type-specific paths this Resource searches in. */
100    const std::vector<Directory>& resourcePaths() const { return _resourcePaths; };
101    /** @returns the Type specific SubPaths this Resource Searches in @see std::vector<std::string>  _resourceSubPaths */
102    const std::vector<Directory>& resourceSubPaths() const { return _resourceSubPaths; };
103    /** @returns the Pointers to the Stored resources. @note do not use this, for more than some lookup */
104    const std::vector<Resources::StorePointer*>& storedResources() const { return _storedResources; };
105
106    virtual void createFromString(const std::string& loadString) = 0;
107
108    void unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel);
109
110    void addResource(Resources::StorePointer* resource);
111
112    void debug() const;
113
114  protected:
115    Type(const std::string& typeName);
116
117  private:
118    Type(const Type& type) {};
119  private:
120    const std::string                     _typeName;          //!< Name of the Type. (Name of the Resource this loads.)
121    std::vector<Directory>                _resourcePaths;     //!< The Paths to search for files in this type
122    std::vector<Directory>                _resourceSubPaths;  //!< The subpaths that will be searched under all the _resourcePaths.
123    std::vector<std::string>              _fileExtensions;    //!< File Extensions, this Resource supports.
124
125    std::vector<Resources::StorePointer*> _storedResources;   //!< An array of all the stored Resources.
126  };
127
128  /**
129   * @brief A Type Definition Class for any Object that is resourceable.
130   *
131   * This Class's main reason of Existence is, that resources can be dynamically
132   * created over a loadString. For this the Type of Resource is required, and the Resource must
133   * itself support the 'void createFromString(const std::string&)' function.
134   */
135  template<class T> class tType : public Type
136  {
137  public:
138    /** Create the ResourceType @see Type(const std::string&) */
139    tType(const std::string& typeName) : Type(typeName) {};
140    /** @param loadString the String to load a Resource with @brief tries to create a Resource of Type T with a loadString */
141    virtual void createFromString(const std::string& loadString) { T::createFromString(loadString); }
142  };
143
144
145
146  /////////////////////
147  // RESOURCE ITSELF //
148  /////////////////////
149
150  //! A Resource is an Object, that can be loaded from Disk
151  /**
152   * The Resource Hanldes the location and stores pointers to data that can be retrieved.
153   */
154  class Resource : virtual public BaseObject
155  {
156    ObjectListDeclaration(Resource);
157
158  public:
159    Resource(Resources::Type* type);
160    virtual ~Resource();
161
162    /** @brief reloads the underlying resource */
163    virtual bool reload() { return false; };
164    /** @brief unloads the underlying Resource */
165    virtual bool unload() { return false; };
166
167    std::string locateFile(const std::string& fileName) const;
168
169  protected:
170    Resources::StorePointer* acquireResource(const std::string& loadString);
171    void addResource(Resources::StorePointer* pointer);
172
173  private:
174    std::string locateFileInSubDir(const Directory& directory, const std::string& fileName) const;
175
176  private:
177    Resources::StorePointer*       _pointer;                         //!< Virtual Pointer to the ResourceData.
178    Resources::Type*               _type;                            //!< Type of the Resource.
179  };
180}
181
182#endif /* _RESOURCE_H */
Note: See TracBrowser for help on using the repository browser.