source: nrEngine/include/ResourcePtr.h @ 30

Revision 30, 8.2 KB checked in by art, 12 years ago (diff)

::std wurde durch std ueberall ersetzt!

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