From ba25f019012b632f6af9f5addee5058ba6a4e077 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 6 Mar 2018 21:37:39 +0000 Subject: Wrap support for making unique ptrs from functions --- service/uptr.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 service/uptr.h (limited to 'service/uptr.h') 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 +#include + +namespace MirrorSearch { + typedef std::function OnError; + + std::string + failingFunction(void * const func); + + void + defaultErrorHandler(const std::string &); + + template using UPtr = std::unique_ptr; + + template + 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 + UPtr + make_unique(R*(*get)(P...), void(*release)(R*), OnError onError, A ... p) + { + return std::unique_ptr(make_unique(get, onError, p...), release); + } +} + +#endif + -- cgit v1.2.3