source: nrEngine/include/ResourcePtr.h @ 16

Revision 16, 8.1 KB checked in by art, 13 years ago (diff)
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               
93                        /**
94                        * Lock the resource to which one this pointer points, to prevent using
95                        * of empty resource. See more information about locking of real resources
96                        * in the documentation about ResourceManager::lockPure() or
97                        * ResourceHolder::lockPure()
98                        * @return either OK or:
99                        *       - RES_PTR_IS_NULL if this pointer is null
100                        *       - RES_LOCK_STATE_STACK_IS_FULL if we are not able to lock anymore
101                        **/
102                        virtual Result lockPure();
103               
104                       
105                        /**
106                        * Unlock the resource to which one this pointer points, to prevent using
107                        * of empty resource. See more information about locking of real resources
108                        * in the documentation about ResourceManager::unlockpure()
109                        * @return either OK or RES_PTR_IS_NULL if this pointer is null
110                        **/
111                        virtual Result unlockPure();
112                       
113                       
114                        /**
115                        * Access to the resource to which one this pointer points. This access need
116                        * 1 static_cast, 3 function calls until it returns the resource.
117                        * So this function is running in O(1) and is pretty efficient
118                        **/
119                        virtual IResource* getBase() const;
120               
121                       
122                        /**
123                        * Access to the resource to which one this pointer points. This access need
124                        * 1 static_cast, 3 function calls until it returns the resource.
125                        * So this function is running in O(1) and is pretty efficient
126                        **/
127                        //virtual IResource& operator*() const;
128                               
129                protected:
130               
131                        //! Resource Manager is a friend, so it can freely work with this pointers
132                        friend class IResourceLoader;
133
134                        //! Also resource manager is a friend
135                        friend class ResourceManager;
136                       
137                        //! Shared pointer holding the holder of the resource
138                        SharedPtr<ResourceHolder> mHolder;
139               
140                        /**
141                        * Get the holder to which one this pointer shows
142                        **/
143                        NR_FORCEINLINE const SharedPtr<ResourceHolder>& getResourceHolder() const
144                        {
145                                return mHolder;
146                        }
147                       
148                        /**
149                        * Create an instance of the resource pointer. This pointer is pointing
150                        * to the resource stored by the holder. The constructor is private, so you
151                        * can not use the new function to create the pointer.
152                        **/
153                        IResourcePtr(SharedPtr<ResourceHolder> holder)
154                        {
155                                mHolder = holder;
156                        }
157               
158                        /**
159                        * Simple constructor that is declared as protected/private, so that this
160                        * pointer con not be created outside as from the resource management system
161                        **/
162                        IResourcePtr()
163                        {
164                                mHolder.reset();
165                        }
166                       
167        };
168       
169       
170       
171        //! Resource pointer is a smart pointer pointing to the resource
172        /**
173        * This is a smart pointer that can be used to access resources manages by the
174        * resource manager. This pointer will automaticly be pointing to empty resource
175        * if the resource was unloaded by the manager.
176        *
177        * You can create more than one resource pointer to a resource. Each access to such
178        * a resource through the manager will create a new one for you. However the pointers
179        * are pointing to one resource holder. Each resource holder is controlled/managed
180        * by the manager. So if you for example unload a resource to which one you has pointers,
181        * the manager will replace the resource holding by the holder with empty one. So if
182        * you try to access the resource you can still access them, but they are empty.
183        * Our systems also allows you to delete the manager from the memory, but the pointers
184        * will stay valid. So you can also remove the manager after you loaded all resources.
185        * However you can then do not manage the resources, so this is a way you should <b>not</b>
186        * do it !!!
187        *
188        * @note We do not have any public constructors or destructors. This will prevent
189        *                you by using the resource management system in the wrong way. You have
190        *                to use manager before you can use the pointers to resources. This will
191        *                give the nrEngine the safety of using its resource management system.
192        *
193        * \ingroup resource
194        **/
195        template<typename ResType>
196        class _NRExport ResourcePtr: public IResourcePtr{
197                public:
198               
199                        /**
200                         * Create an empty resource pointer. Such a pointer
201                         * does not point to anything. So using of not initialized
202                         * pointers will give you an exception
203                         **/
204                        ResourcePtr() : IResourcePtr() {}
205                                       
206                        /**
207                        * Copy constructor to allow copying from base class
208                        **/
209                        ResourcePtr(const IResourcePtr& res) : IResourcePtr(res){}
210                       
211                                       
212                        /**
213                        * Access to the resource to which one this pointer points. This access need
214                        * 1 static_cast, 3 function calls until it returns the resource.
215                        * So this function is running in O(1) and is pretty efficient
216                        **/
217                        NR_FORCEINLINE ResType* operator->() const
218                        {
219                                ResType* ptr = dynamic_cast<ResType*>(IResourcePtr::getBase());
220                                NR_ASSERT(ptr != NULL && "The resource has wrong type");
221                                return ptr;
222                        }
223               
224                        /**
225                        * Get the object stored by this pointer.
226                        * NOTE: The instance is controlled by the pointer, so do not delete it
227                        **/
228                        NR_FORCEINLINE ResType* get() 
229                        {
230                                return operator->();
231                        }                       
232                       
233                        /**
234                        * Access to the resource to which one this pointer points. This access need
235                        * 1 static_cast, 3 function calls until it returns the resource.
236                        * So this function is running in O(1) and is pretty efficient
237                        **/
238                        NR_FORCEINLINE ResType& operator*() const
239                        {
240                                ResType* ptr = dynamic_cast<ResType*>(IResourcePtr::getBase());
241                                NR_ASSERT(ptr != NULL && "The resource has wrong type");
242                                return *ptr;
243                        }
244        };
245               
246};
247#endif
Note: See TracBrowser for help on using the repository browser.