mdds
global.hpp
1 /*************************************************************************
2  *
3  * Copyright (c) 2008-2020 Kohei Yoshida
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use,
9  * copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following
12  * conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_MDDS_GLOBAL_HPP
29 #define INCLUDED_MDDS_GLOBAL_HPP
30 
31 #include <exception>
32 #include <string>
33 #include <memory>
34 #include <utility>
35 #include <type_traits>
36 
47 #define MDDS_ASCII(literal) literal, sizeof(literal)-1
48 
55 #define MDDS_N_ELEMENTS(name) sizeof(name)/sizeof(name[0])
56 
57 #ifdef __GNUC__
58  #define MDDS_DEPRECATED __attribute__ ((deprecated))
59 #elif defined(_MSC_VER)
60  #define MDDS_DEPRECATED __declspec(deprecated)
61 #else
62  #define MDDS_DEPRECATED
63 #endif
64 
65 #ifndef MDDS_LOOP_UNROLLING
66 #define MDDS_LOOP_UNROLLING 1
67 #endif
68 
69 #ifndef MDDS_USE_OPENMP
70 #define MDDS_USE_OPENMP 0
71 #endif
72 
73 namespace mdds {
74 
75 class general_error : public ::std::exception
76 {
77 public:
78  general_error(const ::std::string& msg) : m_msg(msg) {}
79  virtual ~general_error() throw() {}
80 
81  virtual const char* what() const throw()
82  {
83  return m_msg.c_str();
84  }
85 private:
86  ::std::string m_msg;
87 };
88 
90 {
91 public:
92  invalid_arg_error(const ::std::string& msg) : general_error(msg) {}
93 };
94 
95 class size_error : public general_error
96 {
97 public:
98  size_error(const std::string& msg) : general_error(msg) {}
99 };
100 
101 class type_error : public general_error
102 {
103 public:
104  type_error(const std::string& msg) : general_error(msg) {}
105 };
106 
108 {
109 public:
110  integrity_error(const std::string& msg) : general_error(msg) {}
111 };
112 
113 template<typename T, typename ...Args>
114 std::unique_ptr<T> make_unique(Args&& ...args)
115 {
116  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
117 }
118 
119 template<bool B>
120 using bool_constant = std::integral_constant<bool, B>;
121 
122 template <typename T>
124 {
125  using y_type = char;
126  using n_type = long;
127 
128  template <typename U> static y_type test(typename U::value_type);
129  template <typename U> static n_type test(...);
130 
131 public:
132  static constexpr bool value = sizeof(test<T>(0)) == sizeof(y_type);
133 };
134 
135 template<typename _T, typename _IsConst>
137 
138 template<typename _T>
139 struct const_or_not<_T, std::true_type>
140 {
141  using type = typename std::add_const<_T>::type;
142 };
143 
144 template<typename _T>
145 struct const_or_not<_T, std::false_type>
146 {
147  using type = _T;
148 };
149 
150 template<typename _T, bool _Const>
151 using const_t = typename const_or_not<_T, bool_constant<_Const>>::type;
152 
153 template<typename _T, typename _IsConst>
155 
156 template<typename _T>
157 struct get_iterator_type<_T, std::true_type>
158 {
159  using type = typename _T::const_iterator;
160 };
161 
162 template<typename _T>
163 struct get_iterator_type<_T, std::false_type>
164 {
165  using type = typename _T::iterator;
166 };
167 
168 }
169 
170 #endif
Definition: global.hpp:95
Definition: global.hpp:101
Definition: global.hpp:89
Definition: global.hpp:136
Definition: global.hpp:123
Definition: global.hpp:75
Definition: global.hpp:154
Definition: flat_segment_tree.hpp:46
Definition: global.hpp:107