Diffstat (limited to 'include/konforka/pointer_map.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | include/konforka/pointer_map.h | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/include/konforka/pointer_map.h b/include/konforka/pointer_map.h new file mode 100644 index 0000000..d706e71 --- a/dev/null +++ b/include/konforka/pointer_map.h | |||
@@ -0,0 +1,105 @@ | |||
1 | #ifndef __KONFORKA_POINTER_MAP_H | ||
2 | #define __KONFORKA_POINTER_MAP_H | ||
3 | |||
4 | #include <typeinfo> | ||
5 | |||
6 | /** | ||
7 | * @file | ||
8 | * @brief mapping of pointers. | ||
9 | * | ||
10 | * The support for global mapping of pointers. Useful when using third-party | ||
11 | * libraries callbacks when the library does not provide mechanism for passing | ||
12 | * along custom context-dependent data. | ||
13 | */ | ||
14 | |||
15 | namespace konforka { | ||
16 | |||
17 | /** | ||
18 | * @brief internally used actual implementation of mapping pointer. | ||
19 | * | ||
20 | * @param tf the typeid of the key pointer. | ||
21 | * @param pf the key pointer. | ||
22 | * @param tt the typeid of the value pointer. | ||
23 | * @param pt the value pointer. | ||
24 | */ | ||
25 | void _map_pointer(const type_info& tf,void *pf,const type_info& tt,void *pt); | ||
26 | /** | ||
27 | * @brief internally used actual implementation of destroying mapped | ||
28 | * pointer. | ||
29 | * | ||
30 | * @param tf the typeid of the key pointer. | ||
31 | * @param pf the key pointer. | ||
32 | * @param tt the typeid of the value pointer. | ||
33 | * @param pt the value pointer. | ||
34 | */ | ||
35 | void _unmap_pointer(const type_info& tf,void *pf,const type_info& tt,void *pt); | ||
36 | /** | ||
37 | * @brief internally used actual implementation of retrieving mapped | ||
38 | * pointer. | ||
39 | * | ||
40 | * @param tf the typeid of the key pointer. | ||
41 | * @param pf the key pointer. | ||
42 | * @param tt the typeid of the value pointer. | ||
43 | * @return the value. | ||
44 | */ | ||
45 | void *_mapped_pointer(const type_info& tf,void *pf,const type_info& tt); | ||
46 | |||
47 | /** | ||
48 | * @brief the object, maintaining mapped pointer. | ||
49 | * | ||
50 | * @param from_t the key type. | ||
51 | * @param to_t the value type. | ||
52 | */ | ||
53 | template<typename from_t,typename to_t> | ||
54 | class map_pointer { | ||
55 | public: | ||
56 | /** | ||
57 | * stored key. | ||
58 | */ | ||
59 | from_t _from; | ||
60 | /** | ||
61 | * stored value. | ||
62 | */ | ||
63 | to_t _to; | ||
64 | /** | ||
65 | * flag, specifying that the object is currently mapped. | ||
66 | */ | ||
67 | bool bmapped; | ||
68 | |||
69 | /** | ||
70 | * @brief constructs the object, mapping the key/value pair. | ||
71 | * | ||
72 | * @param f the key. | ||
73 | * @param t the value. | ||
74 | */ | ||
75 | map_pointer(from_t f,to_t t) | ||
76 | : _from(f), _to(t), bmapped(false) { | ||
77 | _map_pointer(typeid(from_t),_from,typeid(to_t),_to); | ||
78 | bmapped=true; | ||
79 | } | ||
80 | /** | ||
81 | * @brief destructor unmaps the key/value pair stored. | ||
82 | */ | ||
83 | ~map_pointer() { | ||
84 | if(bmapped) | ||
85 | _unmap_pointer(typeid(from_t),_from,typeid(to_t),_to); | ||
86 | } | ||
87 | }; | ||
88 | |||
89 | /** | ||
90 | * The template function for pointer retrieval. | ||
91 | * | ||
92 | * @param from_t the key type. | ||
93 | * @param to_t the value type. | ||
94 | * @param f the key. | ||
95 | * @return the value. | ||
96 | */ | ||
97 | template<typename from_t,typename to_t> | ||
98 | to_t mapped_pointer(from_t f) { | ||
99 | return (to_t)_mapped_pointer(typeid(from_t),f,typeid(to_t)); | ||
100 | } | ||
101 | |||
102 | } | ||
103 | |||
104 | #endif /* __KONFORKA_POINTER_MAP_H */ | ||
105 | /* vim:set ft=cpp: */ | ||