Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

renamed NewResource to Resource

File size: 6.0 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  //! Stores a Resource-Pointer, the LoadString and it's keepLevel.
46  class StorePointer
47  {
48  public:
49    //! Virtual Destructor, that removes the Stored information-pointer.
50    virtual ~StorePointer() {};
51
52    /** @returns the LoadString this resource was loaded with */
53    const std::string& loadString() const { return _loadString; };
54    /** @returns the KeepLevel of this resource */
55    const Resources::KeepLevel& keepLevel() const { return _keepLevel; };
56
57    virtual bool last() const = 0;
58
59    protected:
60      StorePointer(const std::string& loadString, const Resources::KeepLevel& keeplevel);
61
62    private:
63      StorePointer(const StorePointer&) : _keepLevel(0) {};
64
65  private:
66    std::string                 _loadString;             //!< An identifier, to match when loading a File.
67    Resources::KeepLevel        _keepLevel;              //!< The Priority of this resource. (can only be increased, so none else will delete this)
68  };
69
70
71
72
73  //! A Type of Resources.
74  /**
75   * The Type is used to store the Pointers to already loaded Resources,
76   * and also to store type-specific properties.
77   * These are the Loading Paths, the subpaths and so on.
78   */
79  class Type
80  {
81  public:
82    virtual ~Type();
83    /** @returns true if the names match @param resourceName the Name to compare. @brief compare the Type with a Name */
84    bool operator==(const std::string& resourceName) const { return this->_typeName == resourceName; };
85
86    void addExtension(const std::string& extension);
87
88    bool addResourcePath(const std::string& path);
89    bool addResourceSubPath(const std::string& subPath);
90
91    /// Retrieve Functions
92    /** @returns the name of the stored Class this Type loads Resources for */
93    const std::string& storedClassName() const { return _typeName; };
94    /** @returns the ID of the Type != ClassID */
95    /** @returns the type-specific paths this Resource searches in. */
96    const std::vector<Directory>& resourcePaths() const { return _resourcePaths; };
97    /** @returns the Type specific SubPaths this Resource Searches in @see std::vector<std::string>  _resourceSubPaths */
98    const std::vector<Directory>& resourceSubPaths() const { return _resourceSubPaths; };
99    /** @returns the Pointers to the Stored resources. @note do not use this, for more than some lookup */
100    const std::vector<Resources::StorePointer*>& storedResources() const { return _storedResources; };
101
102    virtual void createFromString(const std::string& loadString) = 0;
103
104    void unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel);
105
106    void addResource(Resources::StorePointer* resource);
107
108    void debug() const;
109
110  protected:
111    Type(const std::string& typeName);
112
113  private:
114    Type(const Type& type) {};
115  private:
116    const std::string                     _typeName;          //!< Name of the Type. (Name of the Resource this loads.)
117    std::vector<Directory>                _resourcePaths;     //!< The Paths to search for files in this type
118    std::vector<Directory>                _resourceSubPaths;  //!< The subpaths that will be searched under all the _resourcePaths.
119    std::vector<std::string>              _fileExtensions;    //!< File Extensions, this Resource supports.
120
121    std::vector<Resources::StorePointer*> _storedResources;   //!< An array of all the stored Resources.
122  };
123
124  template<class T> class tType : public Type
125  {
126  public:
127    tType(const std::string& typeName) : Type(typeName) {};
128    virtual void createFromString(const std::string& loadString) { T::createFromString(loadString); }
129  };
130
131
132
133
134  //! A Resource is an Object, that can be loaded from Disk
135  /**
136   * The Resource Hanldes the location and stores pointers to data that can be retrieved.
137   */
138  class Resource : virtual public BaseObject
139  {
140    ObjectListDeclaration(Resource);
141
142  public:
143    Resource(Resources::Type* type);
144    virtual ~Resource();
145
146    /** @brief reloads the underlying resource */
147    virtual bool reload() { return false; };
148    /** @brief unloads the underlying Resource */
149    virtual bool unload() { return false; };
150
151    std::string locateFile(const std::string& fileName) const;
152
153  protected:
154    Resources::StorePointer* acquireResource(const std::string& loadString);
155    void addResource(Resources::StorePointer* pointer);
156
157  private:
158    std::string locateFileInSubDir(const Directory& directory, const std::string& fileName) const;
159
160  private:
161    Resources::StorePointer*       _pointer;                         //!< Virtual Pointer to the ResourceData.
162    Resources::Type*               _type;                            //!< Type of the Resource.
163  };
164}
165
166#endif /* _RESOURCE_H */
Note: See TracBrowser for help on using the repository browser.