Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

MarkScope.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_MarkScope_h_
00003 #define INCLUDED_MarkScope_h_
00004 
00005 #include "Resource.h"
00006 
00007 //! Provides a way to mark a resource as used for the duration of the instance's scope
00008 /*! This is handy because we don't have to worry about releasing the resource
00009  *  if there are multiple return points, exception handling, or other such issues
00010  *  which might otherwise cause you to forget to release it -- let C++ do it for you! */
00011 class MarkScope {
00012 public:
00013   //! constructor, for marking resources which require no data
00014   MarkScope(Resource& r) : rsrc(r), data(&Resource::emptyData) {
00015     rsrc.useResource(*data);
00016   }
00017   //! constructor, accepts data parameter to pass to Resource::useResource()
00018   MarkScope(Resource& r, Resource::Data& d) : rsrc(r), data(&d) {
00019     rsrc.useResource(*data);
00020   }
00021   //! copy constructor, marks resource used, copying ms's data reference (better make sure the Resource support recursive usage...)
00022   MarkScope(const MarkScope& ms) : rsrc(ms.rsrc), data(ms.data) {
00023     rsrc.useResource(*data);
00024   }
00025   //! copy constructor, accepts additional data parameter to pass to Resource::useResource()
00026   MarkScope(const MarkScope& ms, Resource::Data& d) : rsrc(ms.rsrc), data(&d) {
00027     rsrc.useResource(*data);
00028   }
00029   //! destructor, releases resource
00030   ~MarkScope() {
00031     rsrc.releaseResource(*data);
00032   }
00033   
00034   //! accessor to return the resource being marked
00035   Resource& getResource() const { return rsrc; }
00036   //! accessor to return the data used to access the resource
00037   Resource::Data& getData() const { return *data; }
00038   //! renew the resource usage -- call release and use again, with the same data
00039   void reset() { rsrc.releaseResource(*data); rsrc.useResource(*data); }
00040   //! renew the resource usage -- call release and use again with the new data
00041   void reset(Resource::Data& d) { rsrc.releaseResource(*data); data=&d; rsrc.useResource(*data); }
00042   
00043 protected:
00044   Resource& rsrc; //!<the resource we're using
00045   Resource::Data * data; //!< data passed to resource when using it and releasing it
00046   
00047 private:
00048   MarkScope& operator=(const MarkScope&); //!< assignment prohibited (can't reassign the reference we already hold)
00049 };
00050 
00051 /*! @file
00052 * @brief Defines MarkScope, which provides a way to mark a resource as used for the duration of the instance's scope
00053 * @author Ethan Tira-Thompson (ejt) (Creator)
00054 */
00055 
00056 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:44 2016 by Doxygen 1.6.3