Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9869 in orxonox.OLD for trunk/src/lib/util/loading/resource.h


Ignore:
Timestamp:
Oct 3, 2006, 12:19:30 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/util/loading/resource.h

    r7195 r9869  
    88
    99#include "base_object.h"
    10 #include "multi_type.h"
    1110#include <string>
     11#include <vector>
     12#include <set>
    1213
    13 // FORWARD DECLARATION
     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    KeepLevel();
     29    KeepLevel(unsigned int 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  };
    1476
    1577
    1678
    17 //! An enumerator for different (UN)LOAD-types.
    18 /**
    19  * RP_NO:        will be unloaded on request
    20  * RP_LEVEL:     will be unloaded at the end of a Level
    21  * RP_CAMPAIGN:  will be unloaded at the end of a Campaign
    22  * RP_GAME:      will be unloaded at the end of the whole Game (when closing orxonox)
    23  */
    24 typedef enum ResourcePriority
    25 {
    26   RP_NO        =   0,
    27   RP_LEVEL     =   1,
    28   RP_CAMPAIGN  =   2,
    29   RP_GAME      =   3
    30 };
     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    ////////////////////
     96    //// EXTENSIONS ////
     97    void addExtension(const std::string& extension);
     98
     99    ///////////////
     100    //// PATHS ////
     101    bool addResourcePath(const std::string& path);
     102    bool addResourceSubPath(const std::string& subPath);
     103
     104    /// Retrieve Functions
     105    /** @returns the name of the stored Class this Type loads Resources for */
     106    const std::string& storedClassName() const { return _typeName; };
     107    /** @returns the ID of the Type != ClassID */
     108    /** @returns the type-specific paths this Resource searches in. */
     109    const std::vector<Directory>& resourcePaths() const { return _resourcePaths; };
     110    /** @returns the Type specific SubPaths this Resource Searches in @see std::vector<std::string>  _resourceSubPaths */
     111    const std::vector<Directory>& resourceSubPaths() const { return _resourceSubPaths; };
     112    /** @returns the Pointers to the Stored resources. @note do not use this, for more than some lookup */
     113    const std::vector<Resources::StorePointer*>& storedResources() const { return _storedResources; };
     114
     115    ///////////////////////////////
     116    //// LOADING AND UNLOADING ////
     117    virtual void createFromString(const std::string& loadString, const KeepLevel& keepLevel = KeepLevel()) = 0;
     118    void unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel);
     119
     120    ///////////////////
     121    //// INTERNALS ////
     122    void addResource(Resources::StorePointer* resource);
     123
     124    ///////////////
     125    //// DEBUG ////
     126    void debug() const;
     127
     128  protected:
     129    Type(const std::string& typeName);
     130
     131  private:
     132    Type(const Type& type) {};
     133  private:
     134    const std::string                     _typeName;          //!< Name of the Type. (Name of the Resource this loads.)
     135    std::vector<Directory>                _resourcePaths;     //!< The Paths to search for files in this type
     136    std::vector<Directory>                _resourceSubPaths;  //!< The subpaths that will be searched under all the _resourcePaths.
     137    std::vector<std::string>              _fileExtensions;    //!< File Extensions, this Resource supports.
     138
     139    std::vector<Resources::StorePointer*> _storedResources;   //!< An array of all the stored Resources.
     140  };
     141
     142  /**
     143   * @brief A Type Definition Class for any Object that is resourceable.
     144   *
     145   * This Class's main reason of Existence is, that resources can be dynamically
     146   * created over a loadString. For this the Type of Resource is required, and the Resource must
     147   * itself support the 'void createFromString(const std::string&)' function.
     148   */
     149  template<class T> class tType : public Type
     150  {
     151  public:
     152    /** Create the ResourceType @see Type(const std::string&) */
     153    tType(const std::string& typeName) : Type(typeName) {};
     154    /** @param loadString the String to load a Resource with @brief tries to create a Resource of Type T with a loadString */
     155    virtual void createFromString(const std::string& loadString, const KeepLevel& keepLevel = KeepLevel()) { T::createFromString(loadString, keepLevel); }
     156  };
    31157
    32158
    33159
    34 //! A Resource is an Object, that can be loaded from Disk
    35 /**
    36  *
    37  */
    38 class Resource : virtual public BaseObject {
     160  /////////////////////
     161  // RESOURCE ITSELF //
     162  /////////////////////
     163  //! A Resource is an Object, that can be loaded from Disk
     164  /**
     165   * The Resource Hanldes the location and stores pointers to data that can be retrieved.
     166   */
     167  class Resource : virtual public BaseObject
     168  {
     169    ObjectListDeclaration(Resource);
    39170
    40  public:
    41    Resource(const std::string& fileName);
    42   virtual ~Resource();
     171  public:
     172    Resource(Resources::Type* type);
     173    virtual ~Resource();
    43174
    44   virtual bool load(std::string& fileName, const MultiType& param1, const MultiType& param2);
    45   virtual bool reload();
    46   virtual bool unload();
     175    /** @brief reloads the underlying resource */
     176    virtual bool reload() { return false; };
     177    /** @brief unloads the underlying Resource */
     178    virtual bool unload() { return false; };
    47179
    48  private:
    49    std::string       fileName;
     180    std::string locateFile(const std::string& fileName) const;
    50181
    51    unsigned int      referenceCount;    //!< How many times this Resource has been loaded.
    52 /// TODO REMOVE THIS:   ResourceType      type;              //!< ResourceType of this Resource.
    53    ResourcePriority  prio;              //!< The Priority of this resource. (can only be increased, so noone else will delete this)
     182  protected:
     183    Resources::StorePointer* acquireResource(const std::string& loadString);
     184    void addResource(Resources::StorePointer* pointer);
    54185
    55    MultiType         param[3];          //!< The Parameters given to this Resource.
    56 };
     186  private:
     187    std::string locateFileInSubDir(const Directory& directory, const std::string& fileName) const;
     188
     189  private:
     190    Resources::StorePointer*       _pointer;                         //!< Virtual Pointer to the ResourceData.
     191    Resources::Type*               _type;                            //!< Type of the Resource.
     192  };
     193}
    57194
    58195#endif /* _RESOURCE_H */
Note: See TracChangeset for help on using the changeset viewer.