Diffstat (limited to 'include/konforka/resource_pile.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | include/konforka/resource_pile.h | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/include/konforka/resource_pile.h b/include/konforka/resource_pile.h new file mode 100644 index 0000000..d76a07b --- a/dev/null +++ b/include/konforka/resource_pile.h | |||
@@ -0,0 +1,118 @@ | |||
1 | #ifndef __KONFORKA_RESOURCE_PILE_H | ||
2 | #define __KONFORKA_RESOURCE_PILE_H | ||
3 | |||
4 | #include <map> | ||
5 | #include <konforka/responsible_wrapper.h> | ||
6 | |||
7 | /** | ||
8 | * @file | ||
9 | * @brief resource-piling base support. | ||
10 | * | ||
11 | * Base for implementing resource-piling. | ||
12 | */ | ||
13 | |||
14 | namespace konforka { | ||
15 | using std::multimap; | ||
16 | using std::pair; | ||
17 | |||
18 | /** | ||
19 | * @brief template base class for resource piling. | ||
20 | * | ||
21 | * @param key_t the type used for keying resources. | ||
22 | * @param value_t the type of resource itself. | ||
23 | * @param factory_t the factory class, providing value_t allocate(key_t) | ||
24 | * static member. | ||
25 | */ | ||
26 | template<typename key_t,typename value_t,typename factory_t> | ||
27 | class resource_pile_base : public responsible_wrapper<value_t> { | ||
28 | /** | ||
29 | * @brief the type of the pile container itself. | ||
30 | */ | ||
31 | typedef multimap<key_t,value_t> pile_t; | ||
32 | /* | ||
33 | * @brief the pile of resources. | ||
34 | */ | ||
35 | static pile_t pile; | ||
36 | public: | ||
37 | /** | ||
38 | * @brief stored value for the key associated with the resource | ||
39 | * contained. | ||
40 | */ | ||
41 | key_t _key; | ||
42 | |||
43 | /** | ||
44 | * @brief default constructor fetches or allocates resource. | ||
45 | * | ||
46 | * @param k the key for resource. | ||
47 | * @see allocate | ||
48 | */ | ||
49 | resource_pile_base(const key_t& k) { | ||
50 | allocate(k); | ||
51 | } | ||
52 | /** | ||
53 | * @brief destructor releases the resource back to pile. | ||
54 | */ | ||
55 | virtual ~resource_pile_base() { this->drop(); } | ||
56 | |||
57 | /** | ||
58 | * @brief this is where the resource is handed back to pile. | ||
59 | */ | ||
60 | void release() { | ||
61 | pile.insert(pair<key_t,value_t>(_key,this->opkele)); | ||
62 | } | ||
63 | |||
64 | /** | ||
65 | * @brief fetch from pile or allocate the resource. | ||
66 | * | ||
67 | * Try to see if we have a piled resource keyed to the argument. If | ||
68 | * we do -- fetch it from the pile, otherwise allocate anew. | ||
69 | * | ||
70 | * @param k the key for resource. | ||
71 | */ | ||
72 | void allocate(const key_t& k) { | ||
73 | this->drop(); | ||
74 | typename pile_t::iterator i = pile.find(k); | ||
75 | if(i==pile.end()) { | ||
76 | this->attach(factory_t::allocate(k)); | ||
77 | }else{ | ||
78 | this->attach(i->second); | ||
79 | try{ pile.erase(i); }catch(...){ _key = k; throw; } | ||
80 | } | ||
81 | _key = k; | ||
82 | } | ||
83 | |||
84 | }; | ||
85 | |||
86 | template<typename key_t,typename value_t,typename factory_t> | ||
87 | typename resource_pile_base<key_t,value_t,factory_t>::pile_t | ||
88 | resource_pile_base<key_t,value_t,factory_t>::pile; | ||
89 | |||
90 | /** | ||
91 | * @brief the generic single parameter new-based resource factory. | ||
92 | * | ||
93 | * The generic resource factory using new as a way to allocate resource | ||
94 | * using the single-parameter constructor. | ||
95 | * | ||
96 | * @param key_t the key type. | ||
97 | * @param value_t the resource type. | ||
98 | * | ||
99 | * @see resource_pile_base | ||
100 | */ | ||
101 | template<typename key_t,typename value_t> | ||
102 | struct resource_pile_generic_ptr_factory { | ||
103 | public: | ||
104 | /** | ||
105 | * @brief allocate the resource using new. | ||
106 | * | ||
107 | * @param k the key. | ||
108 | * @return pointer to the newly allocated object. | ||
109 | */ | ||
110 | static value_t *allocate(const key_t& k) { | ||
111 | return new value_t(k); | ||
112 | } | ||
113 | }; | ||
114 | |||
115 | } | ||
116 | |||
117 | #endif /* __KONFORKA_RESOURCE_PILE_H */ | ||
118 | /* vim:set ft=cpp: */ | ||