mirror of
https://github.com/dankamongmen/notcurses.git
synced 2024-11-09 19:10:48 +00:00
64eeb95f1e
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.
66 lines
1.3 KiB
C++
66 lines
1.3 KiB
C++
#ifndef __NCPP_PALETTE256_HH
|
|
#define __NCPP_PALETTE256_HH
|
|
|
|
#include "Root.hh"
|
|
#include "_helpers.hh"
|
|
|
|
namespace ncpp
|
|
{
|
|
class NCPP_API_EXPORT Palette256 : public Root
|
|
{
|
|
public:
|
|
Palette256 ()
|
|
{
|
|
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
|