Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

new_class_id: doxy-tags

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