GL Studio API
gls_resource_file_mgr.h
1 #ifndef GLS_RESOURCE_FILE_MGR_H_INCLUDED
2 #define GLS_RESOURCE_FILE_MGR_H_INCLUDED
3 
4 //////////////////// Provides support for creating DLLs ////////////////////////
5 #if (defined(GLSGEN_EXPORT_GLSGEOMETRYRESOURCE) || \
6  defined(GLSGEN_IMPORT_GLSGEOMETRYRESOURCE) || \
7  defined(GLS_EXPORT_GENERATED) || \
8  defined(GLS_IMPORT_GENERATED)) \
9  && defined(_MSC_VER)
10 # if defined(GLSGEN_EXPORT_GLSGEOMETRYRESOURCE) || defined(GLS_EXPORT_GENERATED)
11 # define GLSGEN_GlsGeometryResource_EXPORT __declspec(dllexport)
12 # else
13 # define GLSGEN_GlsGeometryResource_EXPORT __declspec(dllimport)
14 # endif
15 #else
16 # define GLSGEN_GlsGeometryResource_EXPORT
17 #endif
18 ///////////////////////////////////////////////////////////////////////////////
19 
20 namespace disti {
21 
22 /* class GlsResourceFileMgr
23  *
24  * This class manages all known resource files and provides an
25  * API for objects to access the data for a named resource.
26  *
27  * This allows the runtime code to access required resources without
28  * knowing their location. Resources may be stored in individual files,
29  * in an archive file, inline data in the source code, RCDATA, etc.
30  *
31  */
33 {
34 protected:
37 public:
38 
39  /** Reference counted handle to binary resource data
40  */
42  {
43  protected:
45  virtual ~BinaryResource();
46  public:
47  /// Should be called to release this BinaryResource when it is longer needed
48  virtual void Release() = 0;
49 
50  /// \returns the resource string name
51  virtual const char* GetName() = 0;
52 
53  /// \returns size of the data in bytes
54  virtual unsigned int GetDataSize() = 0;
55 
56  /// \returns raw pointer to the binary resource data (Read-only)
57  virtual const char* GetDataPtr() = 0;
58  };
59 
60  /** Lightweight reference to a resource
61  */
63  {
64  protected:
65  // Need virtual destructor
66  virtual ~ResourceRef() {}
67  public:
68  /// Increment the ResourceRef reference count
69  virtual void AddRef() = 0;
70 
71  /// Should be called to release this ResourceRef when it is longer needed
72  virtual void Release() = 0;
73 
74  /// \returns the resource string name
75  virtual const char* GetName() = 0;
76 
77  // Obtain a pointer to the resource data
78  // Make sure to Release() the BinaryResource when you are done with it (a SmartPtr can be used for this purpose)
79  virtual BinaryResource* LoadBinaryData() = 0;
80  };
81 
82  /* Used to hold a resource file open for loading time performance
83  * \see CacheResourceFile
84  */
86  {
87  public:
88  virtual ~ResourceCache() {}
89  /// Increment the reference count
90  virtual void AddRef() = 0;
91  /// Decrement the reference count
92  virtual void Release() = 0;
93  };
94 
95  // Automatically release a resource
96  // when no more AutoRelease instances reference it
97  // \see CacheResourceFile
98  template <class T>
100  {
101  protected:
102  T* _ptr;
103  public:
104  AutoRelease(T* obj) { _ptr = obj; }
105  AutoRelease(const AutoRelease& other) { _ptr = other._ptr; if (_ptr) { _ptr->AddRef(); } }
106  ~AutoRelease() { if (_ptr) { _ptr->Release(); } }
107  T* get() { return _ptr; }
108  };
109 
110  /**
111  * \returns Reference to the GlsResourceFileMgr
112  */
113  GLSGEN_GlsGeometryResource_EXPORT static GlsResourceFileMgr& Instance();
114 
115  /** Get a reference to a given resource
116  * \param resource_name The unique string name of the resource (relative path to the file if using resource folders)
117  * \returns reference to the resource or NULL if the resource was not found.
118  */
119  GLSGEN_GlsGeometryResource_EXPORT ResourceRef* GetResourceRef(const char* resource_name);
120 
121  /** Tell the resource manager to load resources from the given location
122  * This method may be called multiple times to specify multiple locations
123  *
124  * \param url path to the resources. e.g. "./resources/". This may be a directory or zip file.
125  * \param silent if false, then a popup is displayed to the user if the location is not found
126  * \returns true if resources were successfully opened, false otherwise
127  */
128  GLSGEN_GlsGeometryResource_EXPORT bool AddResourceFolder(const char* url, bool silent=false);
129 
130  /** Loads resources for a given component into RAM.
131  * The resources remain cached until the ResourceCache is Released.
132  */
133  GLSGEN_GlsGeometryResource_EXPORT AutoRelease<ResourceCache> CacheResourcesForComponent(const char* componentClassName);
134 };
135 
136 } // end namespace disti
137 
138 #endif
virtual unsigned int GetDataSize()=0
virtual void Release()=0
Decrement the reference count.
Definition: gls_resource_file_mgr.h:99
Definition: gls_resource_file_mgr.h:32
ResourceRef * GetResourceRef(const char *resource_name)
virtual void Release()=0
Should be called to release this BinaryResource when it is longer needed.
virtual void AddRef()=0
Increment the ResourceRef reference count.
AutoRelease< ResourceCache > CacheResourcesForComponent(const char *componentClassName)
bool AddResourceFolder(const char *url, bool silent=false)
static GlsResourceFileMgr & Instance()
Definition: gls_resource_file_mgr.h:85
virtual void AddRef()=0
Increment the reference count.
virtual const char * GetName()=0
virtual void Release()=0
Should be called to release this ResourceRef when it is longer needed.
Definition: gls_resource_file_mgr.h:41
Definition: gls_resource_file_mgr.h:62
Definition: bmpimage.h:46