notcurses/include/ncpp/Palette256.hh
Marek Habersack b5d8549bb3 [C++] Allow multiple instances of NotCurses
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.
2020-05-26 04:34:31 -04:00

69 lines
1.4 KiB
C++

#ifndef __NCPP_PALETTE256_HH
#define __NCPP_PALETTE256_HH
#include "Root.hh"
#include "_helpers.hh"
namespace ncpp
{
class NotCurses;
class NCPP_API_EXPORT Palette256 : public Root
{
public:
Palette256 (NotCurses *ncinst = nullptr)
: Root (ncinst)
{
palette = palette256_new (get_notcurses ());
if (palette == nullptr)
throw init_error ("Notcurses failed to create a new palette");
}
~Palette256 ()
{
palette256_free (palette);
}
operator palette256* () noexcept
{
return palette;
}
operator palette256 const* () const noexcept
{
return palette;
}
bool set (int idx, int r, int g, int b) const NOEXCEPT_MAYBE
{
return error_guard (palette256_set_rgb (palette, idx, r, g, b), -1);
}
bool set (int idx, unsigned rgb) const NOEXCEPT_MAYBE
{
return error_guard (palette256_set (palette, idx, rgb), -1);
}
bool get (int idx, unsigned *r, unsigned *g, unsigned *b) const
{
if (r == nullptr)
throw invalid_argument ("'r' must be a valid pointer");
if (g == nullptr)
throw invalid_argument ("'g' must be a valid pointer");
if (b == nullptr)
throw invalid_argument ("'b' must be a valid pointer");
return get (idx, *r, *g, *b);
}
bool get (int idx, unsigned &r, unsigned &g, unsigned &b) const NOEXCEPT_MAYBE
{
return error_guard (palette256_get_rgb (palette, idx, &r, &g, &b), -1);
}
private:
palette256 *palette;
};
}
#endif