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
17namespace 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.
26{
27protected:
30
31public:
32 /** Reference counted handle to binary resource data
33 */
35 {
36 protected:
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 */
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).
75 };
76
77 /// \details Used to hold a resource file open for loading time performance.
78 /// \see CacheResourceFile
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>
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
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
Definition: gls_resource_file_mgr.h:93
T * get()
Definition: gls_resource_file_mgr.h:123
T * _ptr
Pointer to an object with a refernce counting interface.
Definition: gls_resource_file_mgr.h:95
AutoRelease(const AutoRelease &other)
Definition: gls_resource_file_mgr.h:105
AutoRelease(T *obj)
Definition: gls_resource_file_mgr.h:100
Definition: gls_resource_file_mgr.h:35
virtual void Release()=0
Should be called to release this BinaryResource when it is longer needed.
virtual unsigned int GetDataSize()=0
Definition: gls_resource_file_mgr.h:80
virtual void Release()=0
Decrement the reference count.
virtual void AddRef()=0
Increment the reference count.
Definition: gls_resource_file_mgr.h:57
virtual BinaryResource * LoadBinaryData()=0
virtual void Release()=0
Should be called to release this ResourceRef when it is longer needed.
virtual const char * GetName()=0
virtual void AddRef()=0
Increment the ResourceRef reference count.
Definition: gls_resource_file_mgr.h:26
ResourceRef * GetResourceRef(const char *resource_name)
static GlsResourceFileMgr & Instance()
AutoRelease< ResourceCache > CacheResourcesForComponent(const char *componentClassName)
bool AddResourceFolder(const char *url, bool silent=false)
Force inclusion of the DirectShow library.
Definition: bmpimage.h:47