File: Root.hh

package info (click to toggle)
notcurses 3.0.17%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,780 kB
  • sloc: ansic: 50,375; cpp: 17,808; python: 1,123; sh: 230; makefile: 35
file content (104 lines) | stat: -rw-r--r-- 2,451 bytes parent folder | download | duplicates (2)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef __NCPP_ROOT_HH
#define __NCPP_ROOT_HH

#include <type_traits>
#include <string>
#include <notcurses/notcurses.h>

#include "_helpers.hh"
#include "_exceptions.hh"

namespace ncpp {
#if defined (NCPP_EXCEPTIONS_PLEASE)
#define NOEXCEPT_MAYBE
#else
#define NOEXCEPT_MAYBE noexcept
#endif

	class NotCurses;

	class NCPP_API_EXPORT Root
	{
	protected:
		static constexpr char ncpp_invalid_state_message[] = "notcurses++ is in an invalid state (already stopped?)";

	public:
		notcurses* get_notcurses () const;

		NotCurses* get_notcurses_cpp () const
		{
			return nc;
		}

	protected:
		explicit Root (NotCurses *ncinst)
			: nc (ncinst)
		{}

		template<typename TRet = bool, typename TValue = int>
		static TRet error_guard (TValue ret, TValue error_value)
		{
			static constexpr bool ret_is_bool = std::is_same_v<TRet, bool>;

			if constexpr (!ret_is_bool) {
				static_assert (std::is_same_v<TRet, TValue>, "Both TRet and TValue must be the same type unless TValue is 'bool'");
			}

			if (ret != error_value) {
				if constexpr (ret_is_bool) {
					return true;
				} else {
					return ret;
				}
			}

#if defined (NCPP_EXCEPTIONS_PLEASE)
			throw call_error ("Call returned an error value");
#else
			if constexpr (ret_is_bool) {
				return false;
			} else {
				return ret;
			}
#endif
		}

		template<typename TRet = bool, typename TValue = int>
		static TRet error_guard_cond ([[maybe_unused]] TValue ret, bool error_value)
		{
			static constexpr bool ret_is_bool = std::is_same_v<TRet, bool>;

			if constexpr (!ret_is_bool) {
				static_assert (std::is_same_v<TRet, TValue>, "Both TRet and TValue must be the same type unless TValue is 'bool'");
			}

			if (!error_value) {
				if constexpr (ret_is_bool) {
					return true;
				} else {
					return ret;
				}
			}

#if defined (NCPP_EXCEPTIONS_PLEASE)
			throw call_error ("Call returned an error value");
#else
			if constexpr (ret_is_bool) {
				return false;
			} else {
				return ret;
			}
#endif
		}

		// All the objects which need to destroy notcurses entities (planes, panelreel etc etc) **have to** call this
		// function before calling to notcurses from their destructor. This is to prevent a segfault when
		// NotCurses::stop has been called and the app uses smart pointers holding NotCurses objects which may be
		// destructed **after** notcurses is stopped.
		bool is_notcurses_stopped () const noexcept;

	private:
		NotCurses *nc = nullptr;
	};
}
#endif