[C++] Sync API changes

Added:

  * class FDPlane (`ncfdplane*`)
  * class Subproc (`ncsubproc*`)
  * NotCurses: get_inputready_fd (`notcurses_inputready_fd`)
  * Plane: qrcode (`ncplane_qrcode`)
  * class PlotBase: templated base class for Plot variations
  * class PlotU: `uint64_t` instantiation of PlotBase (aliased to previous
    `Plot` class for source compatibility), `ncuplot*`
  * class PlotD: `double` instantiation of PlotBase, `ncdplot*`
pull/587/head
Marek Habersack 4 years ago committed by Nick Black
parent f14444cfca
commit e23d5baea4

@ -192,13 +192,16 @@ target_compile_definitions(notcurses-static
# libnotcurses++ # libnotcurses++
set(NCPP_SOURCES set(NCPP_SOURCES
src/libcpp/FDPlane.cc
src/libcpp/Menu.cc src/libcpp/Menu.cc
src/libcpp/MultiSelector.cc src/libcpp/MultiSelector.cc
src/libcpp/NotCurses.cc src/libcpp/NotCurses.cc
src/libcpp/Plane.cc src/libcpp/Plane.cc
src/libcpp/Plot.cc
src/libcpp/Reel.cc src/libcpp/Reel.cc
src/libcpp/Root.cc src/libcpp/Root.cc
src/libcpp/Selector.cc src/libcpp/Selector.cc
src/libcpp/Subproc.cc
src/libcpp/Tablet.cc src/libcpp/Tablet.cc
src/libcpp/Visual.cc src/libcpp/Visual.cc
) )

@ -0,0 +1,69 @@
#ifndef __NCPP_FDPLANE_HH
#define __NCPP_FDPLANE_HH
#include <notcurses/notcurses.h>
#include "Root.hh"
#include "Plane.hh"
namespace ncpp
{
class NCPP_API_EXPORT FDPlane : public Root
{
public:
static ncfdplane_options default_options;
public:
explicit FDPlane (Plane* n, int fd, ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
: FDPlane (n, fd, nullptr, cbfxn, donecbfxn)
{}
explicit FDPlane (Plane* n, int fd, ncfdplane_options *opts = nullptr, ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
{
if (n == nullptr)
throw invalid_argument ("'n' must be a valid pointer");
create_fdplane (*n, fd, opts, cbfxn, donecbfxn);
}
explicit FDPlane (Plane& n, int fd, ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
: FDPlane (n, fd, nullptr, cbfxn, donecbfxn)
{}
explicit FDPlane (Plane& n, int fd, ncfdplane_options *opts = nullptr, ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
{
create_fdplane (n, fd, opts, cbfxn, donecbfxn);
}
~FDPlane ()
{
if (is_notcurses_stopped ())
return;
ncfdplane_destroy (fdplane);
}
Plane* get_plane () const noexcept
{
return Plane::map_plane (ncfdplane_plane (fdplane));
}
private:
void create_fdplane (Plane& n, int fd, ncfdplane_options *opts, ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
{
fdplane = ncfdplane_create (
n,
opts == nullptr ? &default_options : opts,
fd,
cbfxn,
donecbfxn
);
if (fdplane == nullptr)
throw init_error ("NotCurses failed to create an ncfdplane instance");
}
private:
ncfdplane *fdplane;
};
}
#endif // __NCPP_FDPLANE_HH

@ -203,6 +203,11 @@ namespace ncpp
return notcurses_at_yx (nc, yoff, xoff, attr, channels); return notcurses_at_yx (nc, yoff, xoff, attr, channels);
} }
int get_inputready_fd () const noexcept
{
return notcurses_inputready_fd (nc);
}
void drop_planes () const noexcept void drop_planes () const noexcept
{ {
notcurses_drop_planes (nc); notcurses_drop_planes (nc);

@ -1018,7 +1018,13 @@ namespace ncpp
bool ret = ncblit_rgba (plane, placey, placex, linesize, data, begy, begx, leny, lenx) < 0; bool ret = ncblit_rgba (plane, placey, placex, linesize, data, begy, begx, leny, lenx) < 0;
return error_guard_cond<bool, bool> (ret, ret); return error_guard_cond<bool, bool> (ret, ret);
} }
int qrcode (int maxversion, const void *data, size_t len) const NOEXCEPT_MAYBE
{
int ret = ncplane_qrcode (plane, maxversion, data, len);
return error_guard_cond<int> (ret, ret < 0);
}
protected: protected:
explicit Plane (ncplane *_plane, bool _is_stdplane) explicit Plane (ncplane *_plane, bool _is_stdplane)
: plane (_plane), : plane (_plane),

@ -1,6 +1,8 @@
#ifndef __NCPP_PLOT_HH #ifndef __NCPP_PLOT_HH
#define __NCPP_PLOT_HH #define __NCPP_PLOT_HH
#include <type_traits>
#include <notcurses/notcurses.h> #include <notcurses/notcurses.h>
#include "Root.hh" #include "Root.hh"
@ -10,59 +12,144 @@ namespace ncpp
{ {
class Plane; class Plane;
class NCPP_API_EXPORT Plot : public Root template<typename TPlot, typename TCoord>
class NCPP_API_EXPORT PlotBase : public Root
{ {
public: static constexpr bool is_double = std::is_same_v<TCoord,double>;
static ncplot_options default_options; static constexpr bool is_uint64 = std::is_same_v<TCoord,uint64_t>;
public: public:
explicit Plot (Plane *plane, const ncplot_options *opts = nullptr) bool add_sample(TCoord x, TCoord y) const NOEXCEPT_MAYBE
: Plot (reinterpret_cast<ncplane*>(plane), opts) {
{} int ret;
explicit Plot (Plane const* plane, const ncplot_options *opts = nullptr) if constexpr (is_double) {
: Plot (const_cast<Plane*>(plane), opts) ret = ncdplot_add_sample (plot, x, y);
{} } else {
ret = nduplot_add_sample (plot, x, y);
}
explicit Plot (Plane &plane, const ncplot_options *opts = nullptr) return error_guard (ret, -1);
: Plot (reinterpret_cast<ncplane*>(&plane), opts) }
{}
explicit Plot (Plane const& plane, const ncplot_options *opts = nullptr) bool set_sample(TCoord x, TCoord y) const NOEXCEPT_MAYBE
: Plot (const_cast<Plane*>(&plane), opts) {
{} int ret;
explicit Plot (ncplane *plane, const ncplot_options *opts = nullptr, if constexpr (is_double) {
uint64_t miny = 0, uint64_t maxy = 0) ret = ncdplot_set_sample (plot, x, y);
} else {
ret = nduplot_set_sample (plot, x, y);
}
return error_guard (ret, -1);
}
protected:
explicit PlotBase (ncplane *plane, const ncplot_options *opts, TCoord miny = 0, TCoord maxy = 0)
{ {
static_assert (is_double || is_uint64, "PlotBase must be parameterized with either 'double' or 'uint64_t' types");
if constexpr (is_double) {
static_assert (std::is_same_v<TPlot, ncdplot>, "ncdplot must be used for a plot using double coordinates");
} else {
static_assert (std::is_same_v<TPlot, ncuplot>, "ncuplot must be used for a plot using uint64_t coordinates");
}
if (plane == nullptr) if (plane == nullptr)
throw invalid_argument ("'plane' must be a valid pointer"); throw invalid_argument ("'plane' must be a valid pointer");
plot = ncuplot_create (plane, opts == nullptr ? &default_options : opts, miny, maxy); if (opts == nullptr)
throw invalid_argument ("'opts' must be a valid pointer");
if constexpr (is_uint64) {
plot = ncuplot_create (plane, opts, miny, maxy);
} else {
plot = ncdplot_create (plane, opts, miny, maxy);
}
if (plot == nullptr) if (plot == nullptr)
throw init_error ("notcurses failed to create a new plot"); throw init_error ("notcurses failed to create a new plot");
} }
~Plot () ~PlotBase ()
{ {
if (!is_notcurses_stopped ()) if (!is_notcurses_stopped ()) {
ncuplot_destroy (plot); if constexpr (is_double) {
ncdplot_destroy (plot);
} else {
ncuplot_destroy (plot);
}
}
} }
bool add_sample(uint64_t x, uint64_t y) const NOEXCEPT_MAYBE TPlot *get_plot () const noexcept
{ {
return error_guard (ncuplot_add_sample (plot, x, y), -1); return plot;
} }
bool set_sample(uint64_t x, uint64_t y) const NOEXCEPT_MAYBE private:
{ TPlot *plot;
return error_guard (ncuplot_set_sample (plot, x, y), -1); };
}
class NCPP_API_EXPORT PlotU : public PlotBase<ncuplot, uint64_t>
{
public:
static ncplot_options default_options;
public:
explicit PlotU (Plane *plane, const ncplot_options *opts = nullptr)
: PlotU (reinterpret_cast<ncplane*>(plane), opts)
{}
explicit PlotU (Plane const* plane, const ncplot_options *opts = nullptr)
: PlotU (const_cast<Plane*>(plane), opts)
{}
explicit PlotU (Plane &plane, const ncplot_options *opts = nullptr)
: PlotU (reinterpret_cast<ncplane*>(&plane), opts)
{}
explicit PlotU (Plane const& plane, const ncplot_options *opts = nullptr)
: PlotU (const_cast<Plane*>(&plane), opts)
{}
explicit PlotU (ncplane *plane, const ncplot_options *opts = nullptr,
uint64_t miny = 0, uint64_t maxy = 0)
: PlotBase (plane, opts == nullptr ? &default_options : opts, miny, maxy)
{}
Plane* get_plane () const noexcept; Plane* get_plane () const noexcept;
};
private: class NCPP_API_EXPORT PlotD : public PlotBase<ncdplot, double>
ncuplot *plot; {
public:
static ncplot_options default_options;
public:
explicit PlotD (Plane *plane, const ncplot_options *opts = nullptr)
: PlotD (reinterpret_cast<ncplane*>(plane), opts)
{}
explicit PlotD (Plane const* plane, const ncplot_options *opts = nullptr)
: PlotD (const_cast<Plane*>(plane), opts)
{}
explicit PlotD (Plane &plane, const ncplot_options *opts = nullptr)
: PlotD (reinterpret_cast<ncplane*>(&plane), opts)
{}
explicit PlotD (Plane const& plane, const ncplot_options *opts = nullptr)
: PlotD (const_cast<Plane*>(&plane), opts)
{}
explicit PlotD (ncplane *plane, const ncplot_options *opts = nullptr,
double miny = 0, double maxy = 0)
: PlotBase (plane, opts == nullptr ? &default_options : opts, miny, maxy)
{}
Plane* get_plane () const noexcept;
}; };
using Plot = PlotU;
} }
#endif #endif

@ -0,0 +1,93 @@
#ifndef __NCPP_SUBPROC_HH
#define __NCPP_SUBPROC_HH
#include <notcurses/notcurses.h>
#include "Root.hh"
#include "Plane.hh"
namespace ncpp
{
class NCPP_API_EXPORT Subproc : public Root
{
public:
static ncsubproc_options default_options;
public:
explicit Subproc (Plane* n, const char* bin, bool use_path = true,
char* const arg[] = nullptr, char* const env[] = nullptr,
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
: Subproc (n, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
{}
explicit Subproc (Plane* n, const char* bin, const ncsubproc_options* opts, bool use_path = true,
char* const arg[] = nullptr, char* const env[] = nullptr,
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
{
if (n == nullptr)
throw invalid_argument ("'n' must be a valid pointer");
create_subproc (*n, bin, opts, use_path, arg, env, cbfxn, donecbfxn);
}
explicit Subproc (Plane& n, const char* bin, bool use_path = true,
char* const arg[] = nullptr, char* const env[] = nullptr,
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
: Subproc (n, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
{}
explicit Subproc (Plane& n, const char* bin, const ncsubproc_options* opts, bool use_path = true,
char* const arg[] = nullptr, char* const env[] = nullptr,
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
{
create_subproc (n, bin, opts, use_path, arg, env, cbfxn, donecbfxn);
}
~Subproc ()
{
if (is_notcurses_stopped ())
return;
ncsubproc_destroy (subproc);
}
Plane* get_plane () const noexcept
{
return Plane::map_plane (ncsubproc_plane (subproc));
}
private:
void create_subproc (Plane& n, const char* bin, const ncsubproc_options* opts, bool use_path,
char* const arg[], char* const env[],
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
{
if (bin == nullptr)
throw invalid_argument ("'bin' must be a valid pointer");
if (opts == nullptr)
opts = &default_options;
if (use_path) {
if (env != nullptr) {
subproc = ncsubproc_createvpe (
n, opts, bin, arg, env, cbfxn, donecbfxn
);
} else {
subproc = ncsubproc_createvp (
n, opts, bin, arg, cbfxn, donecbfxn
);
}
} else {
subproc = ncsubproc_createv (
n, opts, bin, arg, cbfxn, donecbfxn
);
}
if (subproc == nullptr)
throw new init_error ("NotCurses failed to create ncsubproc instance");
}
private:
ncsubproc *subproc;
};
}
#endif // __NCPP_SUBPROC_HH

@ -0,0 +1,8 @@
#include <ncpp/FDPlane.hh>
using namespace ncpp;
ncfdplane_options FDPlane::default_options = {
nullptr, // curry
false, // follow
};

@ -3,19 +3,32 @@
using namespace ncpp; using namespace ncpp;
ncplot_options Plot::default_options = { ncplot_options PlotD::default_options = {
0, // maxchannel 0, // maxchannel
0, // minchannel 0, // minchannel
ncgridgeom_e::NCPLOT_1x1, // ncgridgeom_e ncgridgeom_e::NCPLOT_1x1, // ncgridgeom_e
0, // rangex 0, // rangex
0, // miny
0, // maxy
false, // labelaxisd, false, // labelaxisd,
false, // exponentialy false, // exponentially
false, // verticalindep false, // verticalindep
}; };
Plane* Plot::get_plane () const noexcept ncplot_options PlotU::default_options = {
0, // maxchannel
0, // minchannel
ncgridgeom_e::NCPLOT_1x1, // ncgridgeom_e
0, // rangex
false, // labelaxisd,
false, // exponentially
false, // verticalindep
};
Plane* PlotD::get_plane () const noexcept
{
return Plane::map_plane (ncdplot_plane (get_plot ()));
}
Plane* PlotU::get_plane () const noexcept
{ {
return Plane::map_plane (ncplot_plane (plot)); return Plane::map_plane (ncuplot_plane (get_plot ()));
} }

@ -0,0 +1,8 @@
#include <ncpp/Subproc.hh>
using namespace ncpp;
ncsubproc_options Subproc::default_options = {
nullptr, // curry
0, // restart_period
};

@ -16,6 +16,8 @@
#include <ncpp/Visual.hh> #include <ncpp/Visual.hh>
#include <ncpp/Direct.hh> #include <ncpp/Direct.hh>
#include <ncpp/Plot.hh> #include <ncpp/Plot.hh>
#include <ncpp/FDPlane.hh>
#include <ncpp/Subproc.hh>
using namespace ncpp; using namespace ncpp;
@ -25,6 +27,9 @@ int run ()
const char *ncver = nc.version (); const char *ncver = nc.version ();
Plane plane (1, 1, 0, 0); Plane plane (1, 1, 0, 0);
Plot plot1 (plane);
PlotU plot2 (plane);
PlotD plot3 (plane);
nc.stop (); nc.stop ();

@ -17,6 +17,8 @@
#include <ncpp/Visual.hh> #include <ncpp/Visual.hh>
#include <ncpp/Direct.hh> #include <ncpp/Direct.hh>
#include <ncpp/Plot.hh> #include <ncpp/Plot.hh>
#include <ncpp/FDPlane.hh>
#include <ncpp/Subproc.hh>
using namespace ncpp; using namespace ncpp;
@ -26,6 +28,9 @@ int run ()
const char *ncver = nc.version (); const char *ncver = nc.version ();
Plane plane (1, 1, 0, 0); Plane plane (1, 1, 0, 0);
Plot plot1 (plane);
PlotU plot2 (plane);
PlotD plot3 (plane);
nc.stop (); nc.stop ();

Loading…
Cancel
Save