diff options
Diffstat (limited to 'service/uptr.h')
-rw-r--r-- | service/uptr.h | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/service/uptr.h b/service/uptr.h new file mode 100644 index 0000000..1de1f99 --- /dev/null +++ b/service/uptr.h @@ -0,0 +1,38 @@ +#ifndef MIRRORSEARCH_UPTR_H +#define MIRRORSEARCH_UPTR_H + +#include <memory> +#include <functional> + +namespace MirrorSearch { + typedef std::function<void(const std::string &)> OnError; + + std::string + failingFunction(void * const func); + + void + defaultErrorHandler(const std::string &); + + template<typename O> using UPtr = std::unique_ptr<O, void(*)(O*)>; + + template<typename R, typename ... P, typename ... A> + R * + make_unique(R * (*func)(P...), OnError onError, A ... p) + { + if (auto obj = func(p...)) { + return obj; + } + onError(failingFunction((void * const)func)); + throw std::runtime_error("Error handler did not throw"); + } + + template<typename R, typename ... P, typename ... A> + UPtr<R> + make_unique(R*(*get)(P...), void(*release)(R*), OnError onError, A ... p) + { + return std::unique_ptr<R, void(*)(R*)>(make_unique(get, onError, p...), release); + } +} + +#endif + |