notcurses/include/ncpp/Direct.hh
Marek Habersack 64eeb95f1e [C++] Optionally enable throwing exceptions on errors
Nick prefers error handling based on exceptions in all cases, while I
prefer to save exception handling for truly exceptional situations -
function parameter validation and class constructor. However, there's no
need to not support both approaches, to be chosen at the discretion of
the developer.

NCPP follows RAII and all classes throw exceptions from their
constructors in case they cannot initialize properly. Likewise,
functions taking pointers that are required validate them and throw
exceptions whenever the requirement isn't met.

This commit goes one step further in that it enables optional validation
of notcurses function return values and throwing an
exception (`ncpp::call_error`) should the function signal an error. This
is disabled by default but it can be enabled by defining the
`NCPP_EXCEPTIONS_PLEASE` macro (preferably on the command line or
before *each* inclusion of any NCPP headers).

Out of necessity, this breaks the ABI (plus I found a handful of minor
issues in the code), but I think it's worth having this support in
place.
2020-04-15 12:10:14 -04:00

127 lines
2.8 KiB
C++

#ifndef __NCPP_DIRECT_HH
#define __NCPP_DIRECT_HH
#include <cstdio>
#include <notcurses/notcurses.h>
#include "Root.hh"
#include "Cell.hh"
namespace ncpp
{
class NCPP_API_EXPORT Direct : public Root
{
public:
explicit Direct (const char *termtype = nullptr, FILE *fp = nullptr)
{
direct = ncdirect_init (termtype, fp == nullptr ? stdout : fp);
if (direct == nullptr)
throw init_error ("notcurses failed to initialize direct mode");
}
~Direct ()
{
ncdirect_stop (direct);
}
bool clear () const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_clear (direct), -1);
}
bool set_fg_default () const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_fg_default (direct), -1);
}
bool set_fg (unsigned rgb) const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_fg (direct, rgb), -1);
}
bool set_fg (unsigned r, unsigned g, unsigned b) const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_fg_rgb8 (direct, r, g, b), -1);
}
bool set_bg_default () const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_bg_default (direct), -1);
}
bool set_bg (unsigned rgb) const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_bg (direct, rgb), -1);
}
bool set_bg (unsigned r, unsigned g, unsigned b) const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_bg_rgb8 (direct, r, g, b), -1);
}
int get_dim_x () const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_dim_x (direct), -1);
}
int get_dim_y () const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_dim_y (direct), -1);
}
void styles_set (CellStyle stylebits) const noexcept
{
ncdirect_styles_set (direct, static_cast<unsigned>(stylebits));
}
void styles_on (CellStyle stylebits) const noexcept
{
ncdirect_styles_on (direct, static_cast<unsigned>(stylebits));
}
void styles_off (CellStyle stylebits) const noexcept
{
ncdirect_styles_off (direct, static_cast<unsigned>(stylebits));
}
bool cursor_move_yx (int y, int x) const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_cursor_move_yx (direct, y, x), -1);
}
bool cursor_up (int num) const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_cursor_up (direct, num), -1);
}
bool cursor_left (int num) const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_cursor_left (direct, num), -1);
}
bool cursor_right (int num) const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_cursor_right (direct, num), -1);
}
bool cursor_down (int num) const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_cursor_down (direct, num), -1);
}
bool cursor_enable () const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_cursor_enable (direct), -1);
}
bool cursor_disable () const NOEXCEPT_MAYBE
{
return error_guard (ncdirect_cursor_disable (direct), -1);
}
private:
ncdirect *direct;
};
}
#endif