Line | Branch | Exec | Source |
---|---|---|---|
1 | #ifndef LYTHON_UTILITIES_GUARD_HEADER | ||
2 | #define LYTHON_UTILITIES_GUARD_HEADER | ||
3 | |||
4 | namespace lython { | ||
5 | // Execute function upon destruction | ||
6 | template <typename Exit> | ||
7 | struct Guard { | ||
8 | Guard(Exit fun): on_exit(fun) {} | ||
9 | |||
10 | ~Guard() { on_exit(); } | ||
11 | |||
12 | Exit on_exit; | ||
13 | }; | ||
14 | |||
15 | template <typename Exit> | ||
16 | Guard<Exit> guard(Exit fun) { | ||
17 | return Guard(fun); | ||
18 | } | ||
19 | |||
20 | template <typename T, typename U> | ||
21 | struct PopGuard { | ||
22 | PopGuard(T& array, U const& v): array(array), oldsize(array.size()) { array.push_back(v); } | ||
23 | |||
24 | PopGuard(T& array): array(array), oldsize(array.size()) {} | ||
25 | |||
26 | ~PopGuard() { array.pop_back(); } | ||
27 | |||
28 | U const& last(int offset, U const& default_value) const { | ||
29 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 119 times.
|
127 | if (oldsize >= offset) { |
30 | 8 | return array[oldsize - offset]; | |
31 | } | ||
32 | 119 | return default_value; | |
33 | } | ||
34 | |||
35 | T& array; | ||
36 | std::size_t oldsize; | ||
37 | }; | ||
38 | } // namespace lython | ||
39 | |||
40 | #endif | ||
41 |