db_conn  v0.2.1-alpha
Database Connection API
utilities.hpp
1 /*
2  * File: utilities.hpp
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef UTILITIES_HPP
20 #define UTILITIES_HPP
21 
22 #include <functional>
23 #include <iostream>
24 #include <type_traits>
25 #include <mutex>
26 #include <atomic>
27 
28 namespace std {
29  template<typename T>
30  ostream& operator<<(typename enable_if<is_enum<T>::value, ostream>::type& stream, const T& e)
31  {
32  return stream << static_cast<typename underlying_type<T>::type>(e);
33  }
34 } // namespace std
35 
36 namespace vgi { namespace dbconn { namespace utils {
37 
38  template<typename T>
39  constexpr typename std::underlying_type<T>::type base_type(T t) { return typename std::underlying_type<T>::type(t); }
40 
41  class spin_lock
42  {
43  public:
44  void lock()
45  {
46  while(lck.test_and_set(std::memory_order_acquire))
47  {}
48  }
49 
50  void unlock()
51  {
52  lck.clear(std::memory_order_release);
53  }
54 
55  private:
56  std::atomic_flag lck = ATOMIC_FLAG_INIT;
57  };
58 
59 } } } // namepsace vgi::dbconn::utils
60 
61 #endif // UTILITIES_HPP
62 
STL namespace.