Diffstat (limited to 'include/konforka/responsible_wrapper.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | include/konforka/responsible_wrapper.h | 99 |
1 files changed, 99 insertions, 0 deletions
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 @@ | |||
1 | #ifndef __KONFORKA_RESPONSIBLE_WRAPPER_H | ||
2 | #define __KONFORKA_RESPONSIBLE_WRAPPER_H | ||
3 | |||
4 | /** | ||
5 | * @file | ||
6 | * @brief The konforka::responsible_wrapper class declaration. | ||
7 | */ | ||
8 | |||
9 | #include <konforka/basic_wrapper.h> | ||
10 | |||
11 | namespace konforka { | ||
12 | |||
13 | /** | ||
14 | * @brief The auto-cleanup wrapper class. | ||
15 | * | ||
16 | * The wrapper class that may feel responsible for releasing the resources | ||
17 | * associated with the content attached. | ||
18 | * | ||
19 | */ | ||
20 | template<typename T> | ||
21 | class responsible_wrapper : public basic_wrapper<T> { | ||
22 | public: | ||
23 | /** | ||
24 | * The type of wrapped content. | ||
25 | */ | ||
26 | typedef typename basic_wrapper<T>::content_type content_type; | ||
27 | /** | ||
28 | * Flag indicating whether the object feels responsible for | ||
29 | * releasing resources associated with the content. | ||
30 | */ | ||
31 | bool bresponsible; | ||
32 | |||
33 | /** | ||
34 | * Default constructor creates the object with no content | ||
35 | * attached. | ||
36 | */ | ||
37 | responsible_wrapper() | ||
38 | : basic_wrapper<content_type>() { } | ||
39 | /** | ||
40 | * Constructor, associating the content with the instance. | ||
41 | * @param o the content. | ||
42 | * @param br indicates whether resources associated with the | ||
43 | * content should be released. | ||
44 | */ | ||
45 | responsible_wrapper(content_type o,bool br=true) | ||
46 | : basic_wrapper<content_type>(o), bresponsible(br) { } | ||
47 | /** | ||
48 | * Destructor releases resources associated with the content | ||
49 | * attached (if any), if the instance feels responsible for the | ||
50 | * content. | ||
51 | */ | ||
52 | virtual ~responsible_wrapper() { drop(); } | ||
53 | |||
54 | /** | ||
55 | * Attaches the given content to the object. | ||
56 | * @param o the content. | ||
57 | * @param br indicates whether the object should feel | ||
58 | * responsible for releasing the content. | ||
59 | */ | ||
60 | void attach(content_type o,bool br=true) { | ||
61 | drop(); | ||
62 | basic_wrapper<content_type>::attach(o); | ||
63 | bresponsible = true; | ||
64 | } | ||
65 | |||
66 | /** | ||
67 | * 'empties' object, releasing resources associated with the | ||
68 | * content if it feels responsible. | ||
69 | */ | ||
70 | virtual void drop() { | ||
71 | if(!this->is()) | ||
72 | return; | ||
73 | if(bresponsible) | ||
74 | release(); | ||
75 | this->bopkele = false; | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Detaches the content from the object without releasing | ||
80 | * resources even if feels responsible for it. | ||
81 | * @return the content attached. | ||
82 | */ | ||
83 | virtual content_type detach() { | ||
84 | this->ensure(); | ||
85 | this->bopkele = false; | ||
86 | return this->opkele; | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Pure virtual provided for derived classes to override for | ||
91 | * doing whatever it takes to release resources associated with | ||
92 | * the content. | ||
93 | */ | ||
94 | virtual void release() = 0; | ||
95 | }; | ||
96 | } | ||
97 | |||
98 | #endif /* __KONFORKA_RESPONSIBLE_WRAPPER_H */ | ||
99 | /* vim:set ft=cpp: */ | ||