From ee7ac58694c16cdd922a6eeddbe1a2eba0da7b4e Mon Sep 17 00:00:00 2001 From: Michael Krelin Date: Sat, 29 Jan 2005 19:41:34 +0000 Subject: initial commit. --- (limited to 'include/konforka/responsible_wrapper.h') diff --git a/include/konforka/responsible_wrapper.h b/include/konforka/responsible_wrapper.h new file mode 100644 index 0000000..374ad45 --- a/dev/null +++ b/include/konforka/responsible_wrapper.h @@ -0,0 +1,99 @@ +#ifndef __KONFORKA_RESPONSIBLE_WRAPPER_H +#define __KONFORKA_RESPONSIBLE_WRAPPER_H + +/** + * @file + * @brief The konforka::responsible_wrapper class declaration. + */ + +#include + +namespace konforka { + + /** + * @brief The auto-cleanup wrapper class. + * + * The wrapper class that may feel responsible for releasing the resources + * associated with the content attached. + * + */ + template + class responsible_wrapper : public basic_wrapper { + public: + /** + * The type of wrapped content. + */ + typedef typename basic_wrapper::content_type content_type; + /** + * Flag indicating whether the object feels responsible for + * releasing resources associated with the content. + */ + bool bresponsible; + + /** + * Default constructor creates the object with no content + * attached. + */ + responsible_wrapper() + : basic_wrapper() { } + /** + * Constructor, associating the content with the instance. + * @param o the content. + * @param br indicates whether resources associated with the + * content should be released. + */ + responsible_wrapper(content_type o,bool br=true) + : basic_wrapper(o), bresponsible(br) { } + /** + * Destructor releases resources associated with the content + * attached (if any), if the instance feels responsible for the + * content. + */ + virtual ~responsible_wrapper() { drop(); } + + /** + * Attaches the given content to the object. + * @param o the content. + * @param br indicates whether the object should feel + * responsible for releasing the content. + */ + void attach(content_type o,bool br=true) { + drop(); + basic_wrapper::attach(o); + bresponsible = true; + } + + /** + * 'empties' object, releasing resources associated with the + * content if it feels responsible. + */ + virtual void drop() { + if(!this->is()) + return; + if(bresponsible) + release(); + this->bopkele = false; + } + + /** + * Detaches the content from the object without releasing + * resources even if feels responsible for it. + * @return the content attached. + */ + virtual content_type detach() { + this->ensure(); + this->bopkele = false; + return this->opkele; + } + + /** + * Pure virtual provided for derived classes to override for + * doing whatever it takes to release resources associated with + * the content. + */ + virtual void release() = 0; + }; +} + +#endif /* __KONFORKA_RESPONSIBLE_WRAPPER_H */ +/* vim:set ft=cpp: */ -- cgit v0.9.0.2