summaryrefslogtreecommitdiffabout
path: root/include/konforka/pointer_map.h
blob: d706e71d0d2bda39ae437314ed9a95d0c3c88d3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef __KONFORKA_POINTER_MAP_H
#define __KONFORKA_POINTER_MAP_H

#include <typeinfo>

/**
 * @file
 * @brief mapping of pointers.
 *
 * The support for global mapping of pointers. Useful when using third-party
 * libraries callbacks when the library does not provide mechanism for passing
 * along custom context-dependent data.
 */

namespace konforka {

    /**
     * @brief internally used actual implementation of mapping pointer.
     *
     * @param tf the typeid of the key pointer.
     * @param pf the key pointer.
     * @param tt the typeid of the value pointer.
     * @param pt the value pointer.
     */
    void _map_pointer(const type_info& tf,void *pf,const type_info& tt,void *pt);
    /**
     * @brief internally used actual implementation of destroying mapped
     * pointer.
     *
     * @param tf the typeid of the key pointer.
     * @param pf the key pointer.
     * @param tt the typeid of the value pointer.
     * @param pt the value pointer.
     */
    void _unmap_pointer(const type_info& tf,void *pf,const type_info& tt,void *pt);
    /**
     * @brief internally used actual implementation of retrieving mapped
     * pointer.
     *
     * @param tf the typeid of the key pointer.
     * @param pf the key pointer.
     * @param tt the typeid of the value pointer.
     * @return the value.
     */
    void *_mapped_pointer(const type_info& tf,void *pf,const type_info& tt);

    /**
     * @brief the object, maintaining mapped pointer.
     * 
     * @param from_t the key type.
     * @param to_t the value type.
     */
    template<typename from_t,typename to_t>
	class map_pointer {
	    public:
		/**
		 * stored key.
		 */
		from_t _from;
		/**
		 * stored value.
		 */
		to_t _to;
		/**
		 * flag, specifying that the object is currently mapped.
		 */
		bool bmapped;

		/**
		 * @brief constructs the object, mapping the key/value pair.
		 * 
		 * @param f the key.
		 * @param t the value.
		 */
		map_pointer(from_t f,to_t t)
		    : _from(f), _to(t), bmapped(false) {
			_map_pointer(typeid(from_t),_from,typeid(to_t),_to);
			bmapped=true;
		    }
		/**
		 * @brief destructor unmaps the key/value pair stored.
		 */
		~map_pointer() {
		    if(bmapped)
			_unmap_pointer(typeid(from_t),_from,typeid(to_t),_to);
		}
	};

    /**
     * The template function for pointer retrieval.
     *
     * @param from_t the key type.
     * @param to_t the value type.
     * @param f the key.
     * @return the value.
     */
    template<typename from_t,typename to_t>
	to_t mapped_pointer(from_t f) {
	    return (to_t)_mapped_pointer(typeid(from_t),f,typeid(to_t));
	}

}

#endif /* __KONFORKA_POINTER_MAP_H */
/* vim:set ft=cpp: */