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