nngn
Loading...
Searching...
No Matches
scoped.h
Go to the documentation of this file.
1#ifndef NNGN_UTILS_SCOPED_H
2#define NNGN_UTILS_SCOPED_H
3
4#include <type_traits>
5#include <utility>
6
7#include "delegate.h"
8#include "utils.h"
9
10#define NNGN_SCOPED(...) NNGN_ANON_DECL(nngn::make_scoped(__VA_ARGS__))
11
12namespace nngn {
13
14namespace detail {
15
16template<typename T, typename F, typename ...Args>
17using scoped_base = std::conditional_t<
18 std::is_same_v<T, nngn::empty>,
19 delegate<F, Args...>,
20 delegate<F, T, Args...>>;
21
22}
23
24template<typename T, typename F, typename ...Args>
25class scoped : private detail::scoped_base<T, F, Args...> {
26 using Base = detail::scoped_base<T, F, Args...>;
27public:
29 scoped(void) = default;
30 explicit scoped(T t, Args ...a) : Base(std::move(t), std::move(a)...) {}
31 explicit scoped(F f, Args ...a) : Base(std::move(f), std::move(a)...) {}
32 scoped(T t, F f, Args ...a) :
33 Base(std::move(f), std::move(t), std::move(a)...) {}
34 ~scoped(void) { std::move(*this)(); }
35 constexpr const T &operator*() const { return this->template arg<0>(); }
36 constexpr T &operator*() { return this->template arg<0>(); }
37 constexpr const T *operator->() const { return &**this; }
38 constexpr T *operator->() { return &**this; }
39 constexpr const T *get() const { return &**this; }
40 constexpr T *get() { return &**this; }
41};
42
43template<typename F, typename ...Ts>
44auto make_scoped(F &&f, Ts &&...ts) {
45 return scoped<empty, std::decay_t<F>, std::decay_t<Ts>...>(
46 std::forward<F>(f), std::forward<Ts>(ts)...);
47}
48
49template<typename T, typename F, typename ...Ts>
50auto make_scoped_obj(T &&t, F &&f, Ts &&...ts) {
51 return scoped<std::decay_t<T>, std::decay_t<F>, std::decay_t<Ts>...>(
52 std::forward<T>(t), std::forward<F>(f), std::forward<Ts>(ts)...);
53}
54
55}
56
57#endif
Definition: scoped.h:25
constexpr T * operator->()
Definition: scoped.h:38
constexpr T * get()
Definition: scoped.h:40
constexpr const T & operator*() const
Definition: scoped.h:35
constexpr const T * operator->() const
Definition: scoped.h:37
detail::scoped_base< T, F, Args... > Base
Definition: scoped.h:26
constexpr T & operator*()
Definition: scoped.h:36
constexpr const T * get() const
Definition: scoped.h:39
scoped(T t, F f, Args ...a)
Definition: scoped.h:32
scoped(F f, Args ...a)
Definition: scoped.h:31
~scoped(void)
Definition: scoped.h:34
move
Definition: camera.lua:48
a
Definition: input.lua:31
function f()) end
#define F(...)
Definition: pp.cpp:12
#define NNGN_MOVE_ONLY(x)
Definition: utils.h:39
#define T(f0, f1, f2)
std::conditional_t< std::is_same_v< T, nngn::empty >, delegate< F, Args... >, delegate< F, T, Args... > > scoped_base
Definition: scoped.h:20
Definition: audio.cpp:7
auto make_scoped_obj(T &&t, F &&f, Ts &&...ts)
Definition: scoped.h:50
auto make_scoped(F &&f, Ts &&...ts)
Definition: scoped.h:44
Definition: debug.h:13