mirror of
https://github.com/dankamongmen/notcurses.git
synced 2024-11-02 09:40:15 +00:00
b5d8549bb3
This is to make it possible, in the future, to create multiple instances of `NotCurses` for multiple terminals. The first instance of `NotCurses` becomes the default one, so that any instances of other classes that aren't explicitly created with a pointer to another `NotCurses` instance still work as expected. Note that currently trying to call `notcurses_init` twice results in the following error for me: 0x55555559bfc0 is already registered for signals Couldn't drop signals: 0x55555559bfc0 != 0x5555555b6720 terminate called after throwing an instance of 'ncpp::init_error*' Program received signal SIGABRT, Aborted. The error is signalled by `setup_signals` and the pointer shown in the message points to the first `struct notcurses` instance created.
105 lines
2.4 KiB
C++
105 lines
2.4 KiB
C++
#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>
|
|
TRet error_guard (TValue ret, TValue error_value) const
|
|
{
|
|
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>
|
|
TRet error_guard_cond ([[maybe_unused]] TValue ret, bool error_value) const
|
|
{
|
|
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
|