forked from stlab/libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.hpp
68 lines (46 loc) · 2.2 KB
/
scope.hpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Copyright 2015 Adobe
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
/**************************************************************************************************/
#ifndef STLAB_SCOPE_HPP
#define STLAB_SCOPE_HPP
/**************************************************************************************************/
#include <mutex>
#include <tuple>
#include <utility>
/**************************************************************************************************/
namespace stlab {
/**************************************************************************************************/
inline namespace v1 {
/**************************************************************************************************/
namespace detail {
template <typename T, typename Tuple, size_t... S>
auto scope_call(Tuple&& t, std::index_sequence<S...>) {
T scoped(std::forward<std::tuple_element_t<S, Tuple>>(std::get<S>(t))...);
(void)scoped;
// call the function
constexpr size_t last_index = std::tuple_size<Tuple>::value - 1;
return std::forward<std::tuple_element_t<last_index, Tuple>>(std::get<last_index>(t))();
}
} // namespace detail
/**************************************************************************************************/
template <typename T, typename... Args>
inline auto scope(Args&&... args) {
return detail::scope_call<T>(std::forward_as_tuple(std::forward<Args>(args)...),
std::make_index_sequence<sizeof...(args) - 1>());
}
/* Workaround until VS2017 bug is fixed */
template <typename T, typename F>
inline auto scope(std::mutex& m, F&& f) {
T scoped(m);
return std::forward<F>(f)();
}
/**************************************************************************************************/
} // namespace v1
/**************************************************************************************************/
} // namespace stlab
/**************************************************************************************************/
#endif
/**************************************************************************************************/