Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

InverseMarkScope.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_InverseMarkScope_h_
00003 #define INCLUDED_InverseMarkScope_h_
00004 
00005 #include "Resource.h"
00006 
00007 //! Provides a way to mark a resource as @e unused for the duration of the instance's scope, thus negating MarkScope for a portion
00008 /*! This is handy because we don't have to worry about re-securing 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 InverseMarkScope {
00012 public:
00013   //! constructor, for marking resources which require no data
00014   InverseMarkScope(Resource& r) : rsrc(r), data(&Resource::emptyData) {
00015     rsrc.releaseResource(*data);
00016   }
00017   //! constructor, accepts data parameter to pass to Resource::releaseResource()
00018   InverseMarkScope(Resource& r, Resource::Data& d) : rsrc(r), data(&d) {
00019     rsrc.releaseResource(*data);
00020   }
00021   //! copy constructor, marks resource used, copying ms's data reference (better make sure the Resource support recursive usage...)
00022   InverseMarkScope(const InverseMarkScope& ms) : rsrc(ms.rsrc), data(ms.data) {
00023     rsrc.releaseResource(*data);
00024   }
00025   //! copy constructor, accepts additional data parameter to pass to Resource::releaseResource()
00026   InverseMarkScope(const InverseMarkScope& ms, Resource::Data& d) : rsrc(ms.rsrc), data(&d) {
00027     rsrc.releaseResource(*data);
00028   }
00029   //! destructor, re-acquires resource
00030   ~InverseMarkScope() {
00031     rsrc.useResource(*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 use and release again, with the same data
00039   void reset() { rsrc.useResource(*data); rsrc.releaseResource(*data); }
00040   //! renew the resource usage -- call use and release again with the new data
00041   void reset(Resource::Data& d) { rsrc.useResource(*data); data=&d; rsrc.releaseResource(*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   InverseMarkScope& operator=(const InverseMarkScope&); //!< assignment prohibited (can't reassign the reference we already hold)
00049 };
00050 
00051 /*! @file
00052 * @brief Defines InverseMarkScope, which provides a way to mark a resource as @e unused for the duration of the instance's scope (cancels a MarkScope for a portion)
00053 * @author Ethan Tira-Thompson (ejt) (Creator)
00054 */
00055 
00056 #endif

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