summaryrefslogtreecommitdiff
path: root/service/uptr.h
blob: e73e098a4966570ce15cdf2ffb9cdd54e8e2af64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef MIRRORSEARCH_UPTR_H
#define MIRRORSEARCH_UPTR_H

#include <functional>
#include <memory>
#include <stdexcept>

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