-
Notifications
You must be signed in to change notification settings - Fork 3
/
utilities.hpp
62 lines (51 loc) · 1.69 KB
/
utilities.hpp
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
/*
* File: utilities.hpp
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef UTILITIES_HPP
#define UTILITIES_HPP
#include <functional>
#include <iostream>
#include <type_traits>
#include <mutex>
#include <atomic>
namespace std {
template<typename T>
ostream& operator<<(typename enable_if<is_enum<T>::value, ostream>::type& stream, const T& e)
{
return stream << static_cast<typename underlying_type<T>::type>(e);
}
} // namespace std
namespace vgi { namespace dbconn { namespace utils {
template<typename T>
constexpr typename std::underlying_type<T>::type base_type(T t) { return typename std::underlying_type<T>::type(t); }
class spin_lock
{
public:
void lock()
{
while(lck.test_and_set(std::memory_order_acquire))
{}
}
void unlock()
{
lck.clear(std::memory_order_release);
}
private:
std::atomic_flag lck = ATOMIC_FLAG_INIT;
};
} } } // namepsace vgi::dbconn::utils
#endif // UTILITIES_HPP