2020-01-07 22:51:34 +00:00
|
|
|
#ifndef __NCPP_ROOT_HH
|
|
|
|
#define __NCPP_ROOT_HH
|
|
|
|
|
2020-04-12 21:09:03 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <string>
|
2020-02-18 17:36:16 +00:00
|
|
|
#include <notcurses/notcurses.h>
|
2020-01-07 22:51:34 +00:00
|
|
|
|
|
|
|
#include "_helpers.hh"
|
|
|
|
#include "_exceptions.hh"
|
|
|
|
|
|
|
|
namespace ncpp {
|
2020-04-12 21:09:03 +00:00
|
|
|
#if defined (NCPP_EXCEPTIONS_PLEASE)
|
|
|
|
#define NOEXCEPT_MAYBE
|
|
|
|
#else
|
|
|
|
#define NOEXCEPT_MAYBE noexcept
|
|
|
|
#endif
|
|
|
|
|
2020-05-24 12:41:18 +00:00
|
|
|
class NotCurses;
|
|
|
|
|
2020-01-07 22:51:34 +00:00
|
|
|
class NCPP_API_EXPORT Root
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
static constexpr char ncpp_invalid_state_message[] = "notcurses++ is in an invalid state (already stopped?)";
|
|
|
|
|
2020-05-24 12:41:18 +00:00
|
|
|
public:
|
|
|
|
notcurses* get_notcurses () const;
|
|
|
|
|
|
|
|
NotCurses* get_notcurses_cpp () const
|
|
|
|
{
|
|
|
|
return nc;
|
|
|
|
}
|
|
|
|
|
2020-01-07 22:51:34 +00:00
|
|
|
protected:
|
2020-05-24 12:41:18 +00:00
|
|
|
explicit Root (NotCurses *ncinst)
|
|
|
|
: nc (ncinst)
|
|
|
|
{}
|
|
|
|
|
2020-04-12 21:09:03 +00:00
|
|
|
template<typename TRet = bool, typename TValue = int>
|
2020-07-31 20:59:59 +00:00
|
|
|
static TRet error_guard (TValue ret, TValue error_value)
|
2020-04-12 21:09:03 +00:00
|
|
|
{
|
|
|
|
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>
|
2020-07-31 20:59:59 +00:00
|
|
|
static TRet error_guard_cond ([[maybe_unused]] TValue ret, bool error_value)
|
2020-04-12 21:09:03 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-07 22:51:34 +00:00
|
|
|
// 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;
|
2020-05-24 12:41:18 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
NotCurses *nc = nullptr;
|
2020-01-07 22:51:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|