mirror of
https://github.com/dankamongmen/notcurses.git
synced 2024-11-08 01:10:23 +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.
88 lines
2.1 KiB
C++
88 lines
2.1 KiB
C++
#ifndef __NCPP_SELECTOR_HH
|
|
#define __NCPP_SELECTOR_HH
|
|
|
|
#include <notcurses/notcurses.h>
|
|
|
|
#include "Root.hh"
|
|
#include "NCAlign.hh"
|
|
|
|
namespace ncpp
|
|
{
|
|
class Plane;
|
|
|
|
class NCPP_API_EXPORT Selector : public Root
|
|
{
|
|
public:
|
|
static selector_options default_options;
|
|
|
|
public:
|
|
explicit Selector (Plane *plane, int y, int x, const selector_options *opts = nullptr)
|
|
: Selector (reinterpret_cast<ncplane*>(plane), y, x, opts)
|
|
{}
|
|
|
|
explicit Selector (Plane const* plane, int y, int x, const selector_options *opts = nullptr)
|
|
: Selector (const_cast<Plane*>(plane), y, x, opts)
|
|
{}
|
|
|
|
explicit Selector (Plane &plane, int y, int x, const selector_options *opts = nullptr)
|
|
: Selector (reinterpret_cast<ncplane*>(&plane), y, x, opts)
|
|
{}
|
|
|
|
explicit Selector (Plane const& plane, int y, int x, const selector_options *opts = nullptr)
|
|
: Selector (const_cast<Plane*>(&plane), y, x, opts)
|
|
{}
|
|
|
|
explicit Selector (ncplane *plane, int y, int x, const selector_options *opts = nullptr)
|
|
{
|
|
if (plane == nullptr)
|
|
throw invalid_argument ("'plane' must be a valid pointer");
|
|
|
|
selector = ncselector_create (plane, y, x, opts == nullptr ? &default_options : opts);
|
|
if (selector == nullptr)
|
|
throw init_error ("notcurses failed to create a new selector");
|
|
}
|
|
|
|
~Selector ()
|
|
{
|
|
if (!is_notcurses_stopped ())
|
|
ncselector_destroy (selector, nullptr);
|
|
}
|
|
|
|
int additem (const selector_item *item) const NOEXCEPT_MAYBE
|
|
{
|
|
return error_guard<int> (ncselector_additem (selector, item), -1);
|
|
}
|
|
|
|
int delitem (const char *item) const NOEXCEPT_MAYBE
|
|
{
|
|
return error_guard<int> (ncselector_delitem (selector, item), -1);
|
|
}
|
|
|
|
const char* previtem () const noexcept
|
|
{
|
|
return ncselector_previtem (selector);
|
|
}
|
|
|
|
const char* nextitem () const noexcept
|
|
{
|
|
return ncselector_nextitem (selector);
|
|
}
|
|
|
|
const char* get_selected () const noexcept
|
|
{
|
|
return ncselector_selected (selector);
|
|
}
|
|
|
|
bool offer_input (const struct ncinput* nc) const noexcept
|
|
{
|
|
return ncselector_offer_input (selector, nc);
|
|
}
|
|
|
|
Plane* get_plane () const noexcept;
|
|
|
|
private:
|
|
ncselector *selector;
|
|
};
|
|
}
|
|
#endif
|