utils.h
7.59 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* ****************************************************************************
* Copyright 2019 Open Systems Development BV *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* ***************************************************************************/
#ifndef OSDEV_COMPONENTS_MQTT_UTILS_H
#define OSDEV_COMPONENTS_MQTT_UTILS_H
// std
#include <algorithm>
#include <functional>
#include <memory>
#include <vector>
namespace osdev {
namespace components {
namespace mqtt {
/**
* @brief Does nothing.
* Utility template function to explicitly use parameters, that would otherwise be unused.
* This helps to prevent the -Wunused-parameter warning.
*/
template <typename... Args>
void apply_unused_parameters(const Args&...)
{
}
/**
* @brief Converts (dynamic_cast) a std::unique_ptr from TFrom to TTo.
* @param from The std::unique_ptr to convert from TFrom to TTo.
* @return The converted std::unique_ptr.
*/
template <typename TTo, typename TFrom>
std::unique_ptr<TTo> dynamic_unique_ptr_cast(std::unique_ptr<TFrom>&& from)
{
auto to = dynamic_cast<TTo*>(from.get());
if (nullptr == to) {
return std::unique_ptr<TTo>(nullptr);
}
from.release();
return std::unique_ptr<TTo>(to);
}
/**
* @brief Converts (dynamic_cast) a std::unique_ptr from TFrom to TTo.
* @param from The std::unique_ptr to convert from TFrom to TTo.
* @return The converted std::unique_ptr.
*/
template <typename TTo, typename TFrom, typename TDeleter>
std::unique_ptr<TTo, TDeleter> dynamic_unique_ptr_cast(std::unique_ptr<TFrom, TDeleter>&& from)
{
auto to = dynamic_cast<TTo*>(from.get());
if (nullptr == to) {
return std::unique_ptr<TTo, TDeleter>(nullptr, from.get_deleter());
}
from.release();
return std::unique_ptr<TTo, TDeleter>(to, std::move(from.get_deleter()));
}
/**
* @brief Helper class for iteration of keys of a map.
*/
template <typename TMap>
class KeyIterator : public TMap::iterator
{
public:
typedef typename TMap::iterator MapIterator;
typedef typename MapIterator::value_type::first_type KeyType;
KeyIterator(const MapIterator& other)
: TMap::iterator(other)
{
}
KeyType& operator*()
{
return TMap::iterator::operator*().first;
}
};
/**
* @brief Helper function to get the begin KeyIterator from a map.
*/
template <typename MapType>
KeyIterator<MapType> KeyBegin(MapType& map)
{
return KeyIterator<MapType>(map.begin());
}
/**
* @brief Helper function to get the end KeyIterator from a map.
*/
template <typename MapType>
KeyIterator<MapType> KeyEnd(MapType& map)
{
return KeyIterator<MapType>(map.end());
}
/**
* @brief Helper class for iteration of keys of a const map.
*/
template <typename TMap>
class KeyConstIterator : public TMap::const_iterator
{
typedef typename TMap::const_iterator TMapIterator;
typedef typename TMapIterator::value_type::first_type TKeyType;
public:
KeyConstIterator(const TMapIterator& other)
: TMapIterator(other)
{
}
const TKeyType& operator*()
{
return TMapIterator::operator*().first;
}
};
/**
* @brief Helper function to get the cbegin KeyConstIterator from a const map.
*/
template <typename TMap>
KeyConstIterator<TMap> KeyBegin(const TMap& map)
{
return KeyConstIterator<TMap>(map.cbegin());
}
/**
* @brief Helper function to get the cend KeyConstIterator from a const map.
*/
template <typename TMap>
KeyConstIterator<TMap> KeyEnd(const TMap& map)
{
return KeyConstIterator<TMap>(map.cend());
}
/**
* @brief Helper function to get the difference of the keys in two maps.
* @param map1 The first map for which to examine the keys.
* @param map2 The second map for which to examine the keys.
* @return Collection of keys present in map1, but not in map2.
* @note The types of the keys in the maps must be identical.
*/
template <typename TMap1, typename TMap2>
std::vector<typename TMap1::key_type> keyDifference(
const TMap1& map1,
const TMap2& map2,
const std::function<bool(const typename TMap1::key_type& key1, const typename TMap1::key_type& key2)>& keyCompare = std::less<typename TMap1::key_type>())
{
static_assert(std::is_same<
typename TMap1::key_type,
typename TMap2::key_type>::value,
"Inconsistent key types.");
typedef typename TMap1::key_type Key;
std::vector<Key> onlyInMap1;
std::set_difference(
KeyBegin(map1),
KeyEnd(map1),
KeyBegin(map2),
KeyEnd(map2),
std::inserter(onlyInMap1, onlyInMap1.begin()),
keyCompare);
return onlyInMap1;
}
/**
* @brief Helper function to get the intersection of the keys in two maps.
* @param map1 The first map for which to examine the keys.
* @param map2 The second map for which to examine the keys.
* @return Collection of keys present in both maps.
* @note The types of the keys in the maps must be identical.
*/
template <typename TMap1, typename TMap2>
std::vector<typename TMap1::key_type> keyIntersection(
const TMap1& map1,
const TMap2& map2,
const std::function<bool(const typename TMap1::key_type& key1, const typename TMap1::key_type& key2)>& keyCompare = std::less<typename TMap1::key_type>())
{
static_assert(std::is_same<
typename TMap1::key_type,
typename TMap2::key_type>::value,
"Inconsistent key types.");
typedef typename TMap1::key_type Key;
std::vector<Key> inBothMaps;
std::set_intersection(
KeyBegin(map1),
KeyEnd(map1),
KeyBegin(map2),
KeyEnd(map2),
std::inserter(inBothMaps, inBothMaps.begin()),
keyCompare);
return inBothMaps;
}
/**
* @brief Determine the absolute path of the binary that belongs to a process.
* @param pid Process Id ifor which to determine the path to the binary. If pid equals -1 then the pid of the current process is used.
* @return Absolute path to the binary.
* @throw SystemException if call to readlink fails.
* @throw PathException if path is to long.
* @note Works only for processes owned by the user that is calling this function.
*/
std::string getPathToBinary(int pid = -1);
} // End namespace mqtt
} // End namespace components
} // End namespace osdev
#endif // OSDEV_COMPONENTS_MQTT_UTILS_H