stlab
2.4.0
Modern, modular C++ algorithms, data structures, and concurrency primitives
Toggle main menu visibility
Loading...
Searching...
No Matches
scope.hpp
Go to the documentation of this file.
1
/*
2
Copyright 2015 Adobe
3
Distributed under the Boost Software License, Version 1.0.
4
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
*/
6
7
/**************************************************************************************************/
8
9
#ifndef STLAB_SCOPE_HPP
10
#define STLAB_SCOPE_HPP
11
30
31
/**************************************************************************************************/
32
33
#include <stlab/config.hpp>
34
35
#include <mutex>
36
#include <tuple>
37
#include <utility>
38
39
/**************************************************************************************************/
40
41
namespace
stlab
{
42
STLAB_VERSION_NAMESPACE_BEGIN()
43
44
48
49
/**************************************************************************************************/
50
51
namespace detail {
52
53
template
<
typename
T,
typename
Tuple,
size_t
... S>
54
auto
scope_call(Tuple&& t, std::index_sequence<S...>) {
55
T scoped(std::forward<std::tuple_element_t<S, Tuple>>(std::get<S>(t))...);
56
(void)scoped;
57
58
// call the function
59
constexpr
size_t
last_index = std::tuple_size_v<Tuple> - 1;
60
return
std::forward<std::tuple_element_t<last_index, Tuple>>(std::get<last_index>(t))();
61
}
62
63
}
// namespace detail
64
65
/**************************************************************************************************/
66
70
template
<
typename
T,
typename
... Args>
71
inline
auto
scope
(Args&&... args) {
72
return
detail::scope_call<T>(std::forward_as_tuple(std::forward<Args>(args)...),
73
std::make_index_sequence<
sizeof
...(args) - 1>());
74
}
75
78
template
<
typename
T,
typename
F>
79
inline
auto
scope
(std::mutex& m, F&& f) {
80
T scoped(m);
81
return
std::forward<F>(f)();
82
}
83
84
/**************************************************************************************************/
85
87
88
STLAB_VERSION_NAMESPACE_END()
89
}
// namespace stlab
90
91
/**************************************************************************************************/
92
93
#endif
94
95
/**************************************************************************************************/
stlab::scope
auto scope(Args &&... args)
Scopes the lifetime of an instance of T. All but the last arguments construct T; the last argument is...
Definition
scope.hpp:71
stlab
Definition
reverse.hpp:28
stlab
scope.hpp
Generated by
1.18.0