summaryrefslogtreecommitdiff
path: root/shared/c_dll.h
blob: 15597a9c8ad25740ec9e30347577fd8210e36333 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************

#ifndef C_DLL_HEADER
#define C_DLL_HEADER

#include <string>
#include <map>
#include <algorithm>


namespace c_dll
{
void writeString(const std::wstring& input, wchar_t* output, size_t outputLen);


//Convert handles to objects and vice versa
template <class S, class T> //T: prefer managed object to ensure cleanup if remove() is not called
class HandleProvider
{
public:
    static HandleProvider& instance();
    S insert(T object);
    void remove(S handle);
    T& retrieve(S handle); //return default-constructed object if not found

private:
    HandleProvider() {}
    HandleProvider(const HandleProvider&);
    HandleProvider& operator=(const HandleProvider&);
    S generate();

    std::map<S, T> handleMap;
};
/*
Example:
		typedef HandleProvider<TBHandle, ComPtr<ITaskbarList3> > HandleTaskbarMap;
		HandleTaskbarMap::instance().insert(xyz);
*/




























//########################## inline implementation #############################
inline
void writeString(const std::wstring& input, wchar_t* output, size_t outputLen)
{
    if (outputLen > 0)
    {
        const size_t maxSize = std::min(input.length(), outputLen - 1);
        std::copy(input.begin(), input.begin() + maxSize, output);
        output[maxSize] = 0;
    }
}


template <class S, class T>
inline
HandleProvider<S, T>& HandleProvider<S, T>::instance()
{
    static HandleProvider inst; //external linkage!!! :)
    return inst;
}


//convert handles to objects and vice versa
template <class S, class T>
inline
S HandleProvider<S, T>::insert(T object)
{
    S newHandle = generate();
    handleMap.insert(std::make_pair(newHandle, object));
    return newHandle;
}


template <class S, class T>
inline
void HandleProvider<S, T>::remove(S handle)
{
    handleMap.erase(handle);
}


template <class S, class T>
inline
T& HandleProvider<S, T>::retrieve(S handle) //return default-constructed object if not found
{
    return handleMap[handle];
}


template <class S, class T>
inline
S HandleProvider<S, T>::generate()
{
    static S handle = 0;
    return ++handle; //don't return 0! 0 is reserved for indicating failure
}


}

#endif //C_DLL_HEADER
bgstack15