notcurses/include/ncpp/Plot.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

68 lines
1.5 KiB
C++

#ifndef __NCPP_PLOT_HH
#define __NCPP_PLOT_HH
#include <notcurses/notcurses.h>
#include "Root.hh"
#include "NCAlign.hh"
namespace ncpp
{
class Plane;
class NCPP_API_EXPORT Plot : public Root
{
public:
static ncplot_options default_options;
public:
explicit Plot (Plane *plane, const ncplot_options *opts = nullptr)
: Plot (reinterpret_cast<ncplane*>(plane), opts)
{}
explicit Plot (Plane const* plane, const ncplot_options *opts = nullptr)
: Plot (const_cast<Plane*>(plane), opts)
{}
explicit Plot (Plane &plane, const ncplot_options *opts = nullptr)
: Plot (reinterpret_cast<ncplane*>(&plane), opts)
{}
explicit Plot (Plane const& plane, const ncplot_options *opts = nullptr)
: Plot (const_cast<Plane*>(&plane), opts)
{}
explicit Plot (ncplane *plane, const ncplot_options *opts = nullptr)
{
if (plane == nullptr)
throw invalid_argument ("'plane' must be a valid pointer");
plot = ncplot_create (plane, opts == nullptr ? &default_options : opts);
if (plot == nullptr)
throw init_error ("notcurses failed to create a new plot");
}
~Plot ()
{
if (!is_notcurses_stopped ())
ncplot_destroy (plot);
}
bool add_sample(uint64_t x, uint64_t y) const NOEXCEPT_MAYBE
{
return error_guard (ncplot_add_sample (plot, x, y), -1);
}
bool set_sample(uint64_t x, uint64_t y) const NOEXCEPT_MAYBE
{
return error_guard (ncplot_set_sample (plot, x, y), -1);
}
Plane* get_plane () const noexcept;
private:
ncplot *plot;
};
}
#endif