diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-03-06 21:37:39 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-03-06 21:37:39 +0000 |
commit | ba25f019012b632f6af9f5addee5058ba6a4e077 (patch) | |
tree | f9e5bc70a9fb075e43f11ca4a02493147e4b3d50 /service/uptr.h | |
parent | Add basic site setup (diff) | |
download | mirrorsearch-ba25f019012b632f6af9f5addee5058ba6a4e077.tar.bz2 mirrorsearch-ba25f019012b632f6af9f5addee5058ba6a4e077.tar.xz mirrorsearch-ba25f019012b632f6af9f5addee5058ba6a4e077.zip |
Wrap support for making unique ptrs from functions
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 + |