summaryrefslogtreecommitdiff
path: root/service/uptr.h
blob: 17eb28db1530561a1b6d860f63c33dd05ac1a8bb (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
37
38
39
#ifndef MIRRORSEARCH_UPTR_H
#define MIRRORSEARCH_UPTR_H

#include <memory>
#include <functional>
#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