31#ifndef MYSQLX_COMMON_H
32#define MYSQLX_COMMON_H
42#include <forward_list>
48#define CATCH_AND_WRAP \
49 catch (const ::mysqlx::Error&) { throw; } \
50 catch (const std::out_of_range&) { throw; } \
51 catch (const std::exception &e) \
52 { throw ::mysqlx::Error(e.what()); } \
53 catch (const char *e) \
54 { throw ::mysqlx::Error(e); } \
56 { throw ::mysqlx::Error("Unknown exception"); } \
62using std::out_of_range;
90 Error(
const char *msg)
97void throw_error(
const char *msg)
118 PUBLIC_API
static std::string to_utf8(
const string&);
119 PUBLIC_API
static void from_utf8(
string&,
const std::string&);
121 PUBLIC_API
static std::u32string to_ucs4(
const string&);
122 PUBLIC_API
static void from_ucs4(
string&,
const std::u32string&);
124 PUBLIC_API
static std::wstring to_wide(
const string&);
125 PUBLIC_API
static void from_wide(
string&,
const std::wstring&);
128 template <
typename T>
136 string(
const string&) =
default;
137 string(
string&&) =
default;
139 string& operator=(
const string&) =
default;
140 string& operator=(
string&&) =
default;
142 using std::u16string::basic_string;
144 string(
const std::u16string &other) : std::u16string(other) {}
145 string(std::u16string &&other) : std::u16string(std::move(other)) {}
147 template <
typename C>
153 std::basic_string<C> str(other);
154 traits<C>::from_str(*
this, str);
159 template <
typename C>
160 string(
const std::basic_string<C> &other)
163 traits<C>::from_str(*
this, other);
168 template <
typename C>
169 operator std::basic_string<C>()
const
172 return traits<C>::to_str(*
this);
178 friend bool operator==(
const string&lhs,
const string&rhs)
180 return operator==((
const std::u16string&)lhs, (
const std::u16string&)rhs);
183 friend bool operator!=(
const string&lhs,
const string&rhs)
185 return !(lhs == rhs);
190 friend bool operator==(
const string &lhs,
const char16_t *rhs)
192 return lhs ==
string(rhs);
195 friend bool operator==(
const char16_t *lhs,
const string &rhs)
197 return string(lhs) == rhs;
200 friend bool operator!=(
const string &lhs,
const char16_t *rhs)
202 return !(lhs == rhs);
205 friend bool operator!=(
const char16_t *lhs,
const string &rhs)
207 return !(lhs == rhs);
214struct string::traits<char>
216 using string = std::string;
220 Impl::from_utf8(to, from);
225 return Impl::to_utf8(from);
230struct string::traits<wchar_t>
232 using string = std::wstring;
236 Impl::from_wide(to, from);
241 return Impl::to_wide(from);
246struct string::traits<char32_t>
248 using string = std::u32string;
252 Impl::from_ucs4(to, from);
257 return Impl::to_ucs4(from);
263std::ostream& operator<<(std::ostream &out,
const string &str)
265 const std::string utf8(str);
271typedef unsigned long col_count_t;
272typedef unsigned long row_count_t;
297class bytes :
public std::pair<const byte*, size_t>
302 bytes(
const byte *beg_,
const byte *end_)
303 : pair(beg_, end_ - beg_)
306 bytes(
const byte *beg,
size_t len) : pair(beg, len)
309 bytes(
const char *str) : pair((
const byte*)str, 0)
312 second = strlen(str);
315 bytes(std::pair<const byte*, size_t> buf) : pair(buf)
318 bytes() : pair(
nullptr, 0)
323 virtual const byte* begin()
const {
return first; }
324 virtual const byte* end()
const {
return first + second; }
326 size_t length()
const {
return second; }
327 size_t size()
const {
return length(); }
385 typename T =
typename std::iterator_traits<Impl>::value_type,
386 typename Distance =
typename std::iterator_traits<T*>::difference_type,
387 typename Pointer =
typename std::iterator_traits<T*>::pointer,
388 typename Reference =
typename std::iterator_traits<T*>::reference
393 using iterator_category = std::input_iterator_tag;
394 using value_type = T;
395 using difference_type = Distance;
396 using pointer = Pointer;
397 using reference = Reference;
400 typename std::remove_reference<Impl>::type *m_impl = NULL;
401 bool m_at_end =
false;
404 Iterator(Impl &impl) : m_impl(&impl) {
405 m_impl->iterator_start();
406 m_at_end = !m_impl->iterator_next();
413 bool operator==(
const Iterator &other)
const
415 return (m_at_end && other.m_at_end);
418 bool operator !=(
const Iterator &other)
const
424 return !(m_at_end && other.m_at_end);
427 Iterator& operator++()
430 if (m_impl && !m_at_end)
431 m_at_end = !m_impl->iterator_next();
439 if (!m_impl || m_at_end)
440 THROW(
"Attempt to dereference null iterator");
443 return m_impl->iterator_get();
465template <
class Source>
466class List_initializer
482 template <
typename... Ty>
483 List_initializer(Ty&&... args)
484 : m_src(std::forward<Ty>(args)...)
496 ,
typename std::enable_if<
497 !std::is_same< U, std::initializer_list<typename U::value_type> >::value
503 return U(std::begin(m_src), std::end(m_src));
508 auto begin() ->
decltype(std::begin(m_src))
511 return std::begin(m_src);
516 auto end() const -> decltype(std::end(m_src))
519 return std::end(m_src);
527struct iterator_traits
529 using value_type =
typename std::remove_reference<T>::type;
530 using difference_type
531 =
typename std::iterator_traits<value_type*>::difference_type;
533 =
typename std::iterator_traits<value_type*>::pointer;
535 =
typename std::iterator_traits<value_type*>::reference;
550 typename Value_type =
typename Impl::Value,
551 typename Distance =
typename iterator_traits<Value_type>::difference_type,
552 typename Pointer =
typename iterator_traits<Value_type>::pointer,
553 typename Reference =
typename iterator_traits<Value_type>::reference
563 template <
typename... Ty>
564 List_source(Ty&&... args)
565 : m_impl(std::forward<Ty>(args)...)
568 using iterator = Iterator<Impl, Value_type, Distance, Pointer, Reference>;
572 return iterator(m_impl);
589template <
typename Impl,
typename Value_type =
typename Impl::Value>
596 bool m_at_begin =
true;
600 template <
typename... Ty>
601 Array_src_impl(Ty&&... args)
602 : m_impl(std::forward<Ty>(args)...)
605 void iterator_start()
617 return m_pos < size();
620 Value_type iterator_get()
622 return operator[](m_pos);
625 Value_type operator[](
size_t pos)
632 return m_impl.size();
645 typename Value_type =
typename Impl::Value,
646 typename Distance =
typename iterator_traits<Value_type>::difference_type,
647 typename Pointer =
typename iterator_traits<Value_type>::pointer,
648 typename Reference =
typename iterator_traits<Value_type>::reference
651 :
public List_source<
652 Array_src_impl<Impl, Value_type>,
659 using Base = List_source<
660 Array_src_impl<Impl, Value_type>,
673 Array_src_impl<Impl, Value_type>,
680 Value_type operator[](
size_t pos)
687 return m_impl.size();
722 static std::true_type
724 decltype(std::begin(*((X*)
nullptr)))*,
725 decltype(std::end(*((X*)
nullptr)))*
729 static std::false_type test(...);
733 static const bool value = std::is_same<
735 decltype(test<C>(
nullptr,
nullptr))
778template <
class Base,
class D =
typename Base::Impl*>
787 template <
typename T>
790 template <
typename X>
791 static std::true_type
792 test(
decltype(Base::process_one(*(D*)
nullptr, *(X*)
nullptr))*);
794 template <
typename X>
795 static std::false_type test(...);
799 static const bool value
800 = std::is_same< std::true_type, decltype(test<T>(
nullptr)) >::value;
811 typename std::enable_if<is_range<C>::value>::type* =
nullptr,
812 typename std::enable_if<!can_process<C>::value>::type* =
nullptr
814 static void process_args(D data, C container)
817 for (
auto el : container)
819 Base::process_one(data, el);
831 typename std::enable_if<!can_process<It>::value>::type* =
nullptr
833 static void process_args(D data,
const It &begin,
const It &end)
835 for (It it = begin; it != end; ++it)
837 Base::process_one(data, *it);
848 typename std::enable_if<can_process<T>::value>::type* =
nullptr
850 static void process_args(D data, T first, R&&... rest)
852 process_args1(data, first, std::forward<R>(rest)...);
860 typename std::enable_if<can_process<T>::value>::type* =
nullptr
862 static void process_args1(D data, T first, R&&... rest)
864 Base::process_one(data, first);
865 process_args1(data, std::forward<R>(rest)...);
868 static void process_args1(D)
Base class for connector errors.
Definition: common.h:84
Class representing a region of memory holding raw bytes.
Definition: common.h:298
A wrapper around std::wstring that can perform conversions from/to different character encodings used...
Definition: common.h:114