Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/new_class_id: now it should also be possible, to cache the resources, by suppling a LoadString.
This is vital to loading Resources, when you only know the TypeName and a LoadString, but not the c++-type and the LoadParameters as is the case when loading over the internet.

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