source: nrEngine/include/ResourcePtr.h @ 28

Revision 28, 8.1 KB checked in by art, 13 years ago (diff)
  • nrEngines scripting language support now subscripts
  • start_at parameter to scripts added (define starting time of a script/subscript)
  • subscripts inherit some paraemters from their parent scripts
Line 
1/***************************************************************************
2 *                                                                         *
3 *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
4 *       mailto: <tevs@mpi-sb.mpg.de>                                      *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 ***************************************************************************/
12
13
14#ifndef _NR_RESOURCE_POINTER_H_
15#define _NR_RESOURCE_POINTER_H_
16
17
18
19//----------------------------------------------------------------------------------
20// Includes
21//----------------------------------------------------------------------------------
22#include "Prerequisities.h"
23#include "Exception.h"
24
25namespace nrEngine{
26       
27        //! Base untemplated class for resource pointers
28        /**
29        * This is a base class for resource pointers. You can use this pointers as
30        * normal pointers, except you can not use new,delete functions.
31        * We need this base class which is not templated to implement converting
32        * functions in derived classes
33        * @note We do not have any public constructors or destructors. This will prevent
34        *                you by using the resource management system in the wrong way. You have
35        *                to use manager before you can use the pointers to resources. This will
36        *                give the nrEngine the safety of using its resource management system.
37        * \ingroup resource
38        **/
39        class _NRExport IResourcePtr{
40                public:
41               
42                        /**
43                        * Copy constructor
44                        **/
45                        IResourcePtr(const IResourcePtr& resPtr);
46                       
47                        /**
48                        * Virtual destructor
49                        **/
50                        virtual ~IResourcePtr()
51                        {
52                                mHolder.reset();
53                        };
54               
55                        /**
56                        * Check if the given pointer is the same as this one.
57                        * @note Two pointers are the same, if they showing to the same resource, and
58                        *                if their holders are the same or if
59                        *                both pointers points to NULL. If one of the pointers is an empty pointer,
60                        *                so also false will be returned.
61                        **/
62                        virtual bool operator==(IResourcePtr& res) const;
63                       
64                       
65                        /**
66                        * Check equality to normal pointers.
67                        **/
68                        virtual bool operator==(const IResource* p) const;
69                       
70                       
71                        /**
72                        * Check whenver two pointers are not the same.
73                        * @see operator==
74                        **/
75                        virtual bool operator!=(IResourcePtr& res) const;
76                       
77                       
78                        /**
79                        * Check whenver two pointers are not the same.
80                        * @see operator==
81                        **/
82                        virtual bool operator!=(const IResource* res) const;
83                       
84                       
85                        /**
86                        * Check whenever this pointer is NULL - does not contain data
87                        **/
88                        inline bool isNull()const {
89                                return (mHolder.get() == NULL);
90                        }
91               
92                        inline bool valid() const {
93                                return (mHolder.get() != NULL);
94                        }
95                       
96                        /**
97                        * Lock the resource to which one this pointer points, to prevent using
98                        * of empty resource. See more information about locking of real resources
99                        * in the documentation about ResourceManager::lockPure() or
100                        * ResourceHolder::lockPure()
101                        * @return either OK or:
102                        *       - RES_PTR_IS_NULL if this pointer is null
103                        *       - RES_LOCK_STATE_STACK_IS_FULL if we are not able to lock anymore
104                        **/
105                        virtual Result lockPure();
106               
107                       
108                        /**
109                        * Unlock the resource to which one this pointer points, to prevent using
110                        * of empty resource. See more information about locking of real resources
111                        * in the documentation about ResourceManager::unlockpure()
112                        * @return either OK or RES_PTR_IS_NULL if this pointer is null
113                        **/
114                        virtual Result unlockPure();
115                       
116                       
117                        /**
118                        * Access to the resource to which one this pointer points. This access need
119                        * 1 static_cast, 3 function calls until it returns the resource.
120                        * So this function is running in O(1) and is pretty efficient
121                        **/
122                        virtual IResource* getBase() const;
123               
124                       
125                        /**
126                        * Access to the resource to which one this pointer points. This access need
127                        * 1 static_cast, 3 function calls until it returns the resource.
128                        * So this function is running in O(1) and is pretty efficient
129                        **/
130                        //virtual IResource& operator*() const;
131                               
132                protected:
133               
134                        //! Resource Manager is a friend, so it can freely work with this pointers
135                        friend class IResourceLoader;
136
137                        //! Also resource manager is a friend
138                        friend class ResourceManager;
139                       
140                        //! Shared pointer holding the holder of the resource
141                        SharedPtr<ResourceHolder> mHolder;
142               
143                        /**
144                        * Get the holder to which one this pointer shows
145                        **/
146                        NR_FORCEINLINE SharedPtr<ResourceHolder> getResourceHolder() const
147                        {
148                                return mHolder;
149                        }
150                       
151                        /**
152                        * Create an instance of the resource pointer. This pointer is pointing
153                        * to the resource stored by the holder. The constructor is private, so you
154                        * can not use the new function to create the pointer.
155                        **/
156                        IResourcePtr(SharedPtr<ResourceHolder> holder)
157                        {
158                                mHolder = holder;
159                        }
160               
161                        /**
162                        * Simple constructor that is declared as protected/private, so that this
163                        * pointer con not be created outside as from the resource management system
164                        **/
165                        IResourcePtr()
166                        {
167                                mHolder.reset();
168                        }
169                       
170        };
171       
172       
173       
174        //! Resource pointer is a smart pointer pointing to the resource
175        /**
176        * This is a smart pointer that can be used to access resources manages by the
177        * resource manager. This pointer will automaticly be pointing to empty resource
178        * if the resource was unloaded by the manager.
179        *
180        * You can create more than one resource pointer to a resource. Each access to such
181        * a resource through the manager will create a new one for you. However the pointers
182        * are pointing to one resource holder. Each resource holder is controlled/managed
183        * by the manager. So if you for example unload a resource to which one you has pointers,
184        * the manager will replace the resource holding by the holder with empty one. So if
185        * you try to access the resource you can still access them, but they are empty.
186        * Our systems also allows you to delete the manager from the memory, but the pointers
187        * will stay valid. So you can also remove the manager after you loaded all resources.
188        * However you can then do not manage the resources, so this is a way you should <b>not</b>
189        * do it !!!
190        *
191        * @note We do not have any public constructors or destructors. This will prevent
192        *                you by using the resource management system in the wrong way. You have
193        *                to use manager before you can use the pointers to resources. This will
194        *                give the nrEngine the safety of using its resource management system.
195        *
196        * \ingroup resource
197        **/
198        template<typename ResType>
199        class _NRExport ResourcePtr: public IResourcePtr{
200                public:
201               
202                        /**
203                         * Create an empty resource pointer. Such a pointer
204                         * does not point to anything. So using of not initialized
205                         * pointers will give you an exception
206                         **/
207                        ResourcePtr() : IResourcePtr() {}
208                                       
209                        /**
210                        * Copy constructor to allow copying from base class
211                        **/
212                        ResourcePtr(const IResourcePtr& res) : IResourcePtr(res){}
213                       
214                        /**
215                        * Access to the resource to which one this pointer points. This access need
216                        * 1 static_cast, 3 function calls until it returns the resource.
217                        * So this function is running in O(1) and is pretty efficient
218                        **/
219                        NR_FORCEINLINE ResType* operator->() const
220                        {
221                                ResType* ptr = dynamic_cast<ResType*>(IResourcePtr::getBase());
222                                NR_ASSERT(ptr != NULL && "The resource has wrong type");
223                                return ptr;
224                        }
225               
226                        /**
227                        * Get the object stored by this pointer.
228                        * NOTE: The instance is controlled by the pointer, so do not delete it
229                        **/
230                        NR_FORCEINLINE ResType* get() 
231                        {
232                                return operator->();
233                        }                       
234                       
235                        /**
236                        * Access to the resource to which one this pointer points. This access need
237                        * 1 static_cast, 3 function calls until it returns the resource.
238                        * So this function is running in O(1) and is pretty efficient
239                        **/
240                        NR_FORCEINLINE ResType& operator*() const
241                        {
242                                ResType* ptr = dynamic_cast<ResType*>(IResourcePtr::getBase());
243                                NR_ASSERT(ptr != NULL && "The resource has wrong type");
244                                return *ptr;
245                        }
246        };
247               
248};
249#endif
Note: See TracBrowser for help on using the repository browser.