Notcurses 3.0.13
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
Root.hh
Go to the documentation of this file.
1#ifndef __NCPP_ROOT_HH
2#define __NCPP_ROOT_HH
3
4#include <type_traits>
5#include <string>
7
8#include "_helpers.hh"
9#include "_exceptions.hh"
10
11namespace ncpp {
12#if defined (NCPP_EXCEPTIONS_PLEASE)
13#define NOEXCEPT_MAYBE
14#else
15#define NOEXCEPT_MAYBE noexcept
16#endif
17
18 class NotCurses;
19
21 {
22 protected:
23 static constexpr char ncpp_invalid_state_message[] = "notcurses++ is in an invalid state (already stopped?)";
24
25 public:
27
29 {
30 return nc;
31 }
32
33 protected:
34 explicit Root (NotCurses *ncinst)
35 : nc (ncinst)
36 {}
37
38 template<typename TRet = bool, typename TValue = int>
39 static TRet error_guard (TValue ret, TValue error_value)
40 {
41 static constexpr bool ret_is_bool = std::is_same_v<TRet, bool>;
42
43 if constexpr (!ret_is_bool) {
44 static_assert (std::is_same_v<TRet, TValue>, "Both TRet and TValue must be the same type unless TValue is 'bool'");
45 }
46
47 if (ret != error_value) {
48 if constexpr (ret_is_bool) {
49 return true;
50 } else {
51 return ret;
52 }
53 }
54
55#if defined (NCPP_EXCEPTIONS_PLEASE)
56 throw call_error ("Call returned an error value");
57#else
58 if constexpr (ret_is_bool) {
59 return false;
60 } else {
61 return ret;
62 }
63#endif
64 }
65
66 template<typename TRet = bool, typename TValue = int>
67 static TRet error_guard_cond ([[maybe_unused]] TValue ret, bool error_value)
68 {
69 static constexpr bool ret_is_bool = std::is_same_v<TRet, bool>;
70
71 if constexpr (!ret_is_bool) {
72 static_assert (std::is_same_v<TRet, TValue>, "Both TRet and TValue must be the same type unless TValue is 'bool'");
73 }
74
75 if (!error_value) {
76 if constexpr (ret_is_bool) {
77 return true;
78 } else {
79 return ret;
80 }
81 }
82
83#if defined (NCPP_EXCEPTIONS_PLEASE)
84 throw call_error ("Call returned an error value");
85#else
86 if constexpr (ret_is_bool) {
87 return false;
88 } else {
89 return ret;
90 }
91#endif
92 }
93
94 // All the objects which need to destroy notcurses entities (planes, panelreel etc etc) **have to** call this
95 // function before calling to notcurses from their destructor. This is to prevent a segfault when
96 // NotCurses::stop has been called and the app uses smart pointers holding NotCurses objects which may be
97 // destructed **after** notcurses is stopped.
98 bool is_notcurses_stopped () const noexcept;
99
100 private:
101 NotCurses *nc = nullptr;
102 };
103}
104#endif
#define NCPP_API_EXPORT
Definition _helpers.hh:9
static TRet error_guard_cond(TValue ret, bool error_value)
Definition Root.hh:67
NotCurses * get_notcurses_cpp() const
Definition Root.hh:28
notcurses * get_notcurses() const
Root(NotCurses *ncinst)
Definition Root.hh:34
bool is_notcurses_stopped() const noexcept
static TRet error_guard(TValue ret, TValue error_value)
Definition Root.hh:39