|
| 1 | +#include "errors.h" |
| 2 | + |
| 3 | +#include <sqlite3.h> |
| 4 | + |
| 5 | +#include <utility> |
| 6 | +#include <tuple> |
| 7 | +#include <type_traits> |
| 8 | + |
| 9 | +namespace sqlite { |
| 10 | + namespace detail { |
| 11 | + template<class> |
| 12 | + using void_t = void; |
| 13 | + template<class T, class = void> |
| 14 | + struct is_callable : std::false_type {}; |
| 15 | + template<class Functor, class ...Arguments> |
| 16 | + struct is_callable<Functor(Arguments...), void_t<decltype(std::declval<Functor>()(std::declval<Arguments>()...))>> : std::true_type {}; |
| 17 | + template<class Functor, class ...Functors> |
| 18 | + class FunctorOverload: public Functor, public FunctorOverload<Functors...> { |
| 19 | + public: |
| 20 | + template<class Functor1, class ...Remaining> |
| 21 | + FunctorOverload(Functor1 &&functor, Remaining &&... remaining): |
| 22 | + Functor(std::forward<Functor1>(functor)), |
| 23 | + FunctorOverload<Functors...>(std::forward<Remaining>(remaining)...) {} |
| 24 | + using Functor::operator(); |
| 25 | + using FunctorOverload<Functors...>::operator(); |
| 26 | + }; |
| 27 | + template<class Functor> |
| 28 | + class FunctorOverload<Functor>: public Functor { |
| 29 | + public: |
| 30 | + template<class Functor1> |
| 31 | + FunctorOverload(Functor1 &&functor): |
| 32 | + Functor(std::forward<Functor1>(functor)) {} |
| 33 | + using Functor::operator(); |
| 34 | + }; |
| 35 | + template<class Functor> |
| 36 | + class WrapIntoFunctor: public Functor { |
| 37 | + public: |
| 38 | + template<class Functor1> |
| 39 | + WrapIntoFunctor(Functor1 &&functor): |
| 40 | + Functor(std::forward<Functor1>(functor)) {} |
| 41 | + using Functor::operator(); |
| 42 | + }; |
| 43 | + template<class ReturnType, class ...Arguments> |
| 44 | + class WrapIntoFunctor<ReturnType(*)(Arguments...)> { |
| 45 | + ReturnType(*ptr)(Arguments...); |
| 46 | + public: |
| 47 | + WrapIntoFunctor(ReturnType(*ptr)(Arguments...)): ptr(ptr) {} |
| 48 | + ReturnType operator()(Arguments... arguments) { return (*ptr)(std::forward<Arguments>(arguments)...); } |
| 49 | + }; |
| 50 | + inline void store_error_log_data_pointer(std::shared_ptr<void> ptr) { |
| 51 | + static std::shared_ptr<void> stored; |
| 52 | + stored = std::move(ptr); |
| 53 | + } |
| 54 | + template<class T> |
| 55 | + std::shared_ptr<typename std::decay<T>::type> make_shared_inferred(T &&t) { |
| 56 | + return std::make_shared<typename std::decay<T>::type>(std::forward<T>(t)); |
| 57 | + } |
| 58 | + } |
| 59 | + template<class Handler> |
| 60 | + typename std::enable_if<!detail::is_callable<Handler(const sqlite_exception&)>::value>::type |
| 61 | + error_log(Handler &&handler); |
| 62 | + template<class Handler> |
| 63 | + typename std::enable_if<detail::is_callable<Handler(const sqlite_exception&)>::value>::type |
| 64 | + error_log(Handler &&handler); |
| 65 | + template<class ...Handler> |
| 66 | + typename std::enable_if<sizeof...(Handler)>=2>::type |
| 67 | + error_log(Handler &&...handler) { |
| 68 | + return error_log(detail::FunctorOverload<detail::WrapIntoFunctor<typename std::decay<Handler>::type>...>(std::forward<Handler>(handler)...)); |
| 69 | + } |
| 70 | + template<class Handler> |
| 71 | + typename std::enable_if<!detail::is_callable<Handler(const sqlite_exception&)>::value>::type |
| 72 | + error_log(Handler &&handler) { |
| 73 | + return error_log(std::forward<Handler>(handler), [](const sqlite_exception&) {}); |
| 74 | + } |
| 75 | + template<class Handler> |
| 76 | + typename std::enable_if<detail::is_callable<Handler(const sqlite_exception&)>::value>::type |
| 77 | + error_log(Handler &&handler) { |
| 78 | + auto ptr = detail::make_shared_inferred([handler = std::forward<Handler>(handler)](int error_code, const char *errstr) mutable { |
| 79 | + switch(error_code & 0xFF) { |
| 80 | +#define SQLITE_MODERN_CPP_ERROR_CODE(NAME,name,derived) \ |
| 81 | + case SQLITE_ ## NAME: switch(error_code) { \ |
| 82 | + derived \ |
| 83 | + default: handler(errors::name(errstr, "", error_code)); \ |
| 84 | + };break; |
| 85 | +#define SQLITE_MODERN_CPP_ERROR_CODE_EXTENDED(BASE,SUB,base,sub) \ |
| 86 | + case SQLITE_ ## BASE ## _ ## SUB: \ |
| 87 | + handler(errors::base ## _ ## sub(errstr, "", error_code)); \ |
| 88 | + break; |
| 89 | +#include "lists/error_codes.h" |
| 90 | +#undef SQLITE_MODERN_CPP_ERROR_CODE_EXTENDED |
| 91 | +#undef SQLITE_MODERN_CPP_ERROR_CODE |
| 92 | + default: handler(sqlite_exception(errstr, "", error_code)); \ |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + sqlite3_config(SQLITE_CONFIG_LOG, (void(*)(void*,int,const char*))[](void *functor, int error_code, const char *errstr) { |
| 97 | + (*static_cast<decltype(ptr.get())>(functor))(error_code, errstr); |
| 98 | + }, ptr.get()); |
| 99 | + detail::store_error_log_data_pointer(std::move(ptr)); |
| 100 | + } |
| 101 | +} |
0 commit comments