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