Notcurses 3.0.13
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
Plot.hh
Go to the documentation of this file.
1#ifndef __NCPP_PLOT_HH
2#define __NCPP_PLOT_HH
3
4#include <type_traits>
5
7
8#include "NCAlign.hh"
9#include "Plane.hh"
10#include "Utilities.hh"
11#include "Widget.hh"
12
13namespace ncpp
14{
15 template<typename TPlot, typename TCoord>
17 {
18 static constexpr bool is_double = std::is_same_v<TCoord,double>;
19 static constexpr bool is_uint64 = std::is_same_v<TCoord,uint64_t>;
20
21 public:
22 bool add_sample (uint64_t x, TCoord y) const NOEXCEPT_MAYBE
23 {
24 int ret;
25
26 if constexpr (is_double) {
27 ret = ncdplot_add_sample (plot, x, y);
28 } else {
29 ret = nduplot_add_sample (plot, x, y);
30 }
31
32 return error_guard (ret, -1);
33 }
34
35 bool set_sample (uint64_t x, TCoord y) const NOEXCEPT_MAYBE
36 {
37 int ret;
38
39 if constexpr (is_double) {
40 ret = ncdplot_set_sample (plot, x, y);
41 } else {
42 ret = nduplot_set_sample (plot, x, y);
43 }
44 return error_guard (ret, -1);
45 }
46
47 bool sample (uint64_t x, TCoord* y) const NOEXCEPT_MAYBE
48 {
49 int ret;
50
51 if constexpr (is_double) {
52 ret = ncdplot_sample (plot, x, y);
53 } else {
54 ret = ncuplot_sample (plot, x, y);
55 }
56
57 return error_guard (ret, -1);
58 }
59
60 protected:
61 explicit PlotBase (Plane *plane, const ncplot_options *opts, TCoord miny = 0, TCoord maxy = 0)
62 : Widget (Utilities::get_notcurses_cpp (plane))
63 {
64 static_assert (is_double || is_uint64, "PlotBase must be parameterized with either 'double' or 'uint64_t' types");
65 if constexpr (is_double) {
66 static_assert (std::is_same_v<TPlot, ncdplot>, "ncdplot must be used for a plot using double coordinates");
67 } else {
68 static_assert (std::is_same_v<TPlot, ncuplot>, "ncuplot must be used for a plot using uint64_t coordinates");
69 }
70
71 ensure_valid_plane (plane);
72
73 if (!plane->is_valid ())
74 throw invalid_argument ("Invalid Plane object passed in 'plane'. Widgets must not reuse the same plane.");
75
76 if (opts == nullptr)
77 throw invalid_argument ("'opts' must be a valid pointer");
78
79 if constexpr (is_uint64) {
80 plot = ncuplot_create (Utilities::to_ncplane (plane), opts, miny, maxy);
81 } else {
82 plot = ncdplot_create (Utilities::to_ncplane (plane), opts, miny, maxy);
83 }
84
85 if (plot == nullptr)
86 throw init_error ("Notcurses failed to create a new plot");
87
88 take_plane_ownership (plane);
89 }
90
92 {
93 if (!is_notcurses_stopped ()) {
94 if constexpr (is_double) {
95 ncdplot_destroy (plot);
96 } else {
97 ncuplot_destroy (plot);
98 }
99 }
100 }
101
102 TPlot *get_plot () const noexcept
103 {
104 return plot;
105 }
106
107 private:
108 TPlot *plot;
109 };
110
111 class NCPP_API_EXPORT PlotU : public PlotBase<ncuplot, uint64_t>
112 {
113 public:
115
116 public:
117 explicit PlotU (Plane *plane, const ncplot_options *opts = nullptr)
118 : PlotU (static_cast<const Plane*>(plane), opts)
119 {}
120
121 explicit PlotU (Plane const* plane, const ncplot_options *opts = nullptr)
122 : PlotBase (const_cast<Plane*>(plane), opts == nullptr ? &default_options : opts)
123 {}
124
125 explicit PlotU (Plane &plane, const ncplot_options *opts = nullptr)
126 : PlotU (static_cast<Plane const&>(plane), opts)
127 {}
128
129 explicit PlotU (Plane const& plane, const ncplot_options *opts = nullptr)
130 : PlotBase (const_cast<Plane*>(&plane), opts == nullptr ? &default_options : opts)
131 {}
132
133 Plane* get_plane () const noexcept;
134 };
135
136 class NCPP_API_EXPORT PlotD : public PlotBase<ncdplot, double>
137 {
138 public:
140
141 public:
142 explicit PlotD (Plane *plane, const ncplot_options *opts = nullptr)
143 : PlotD (static_cast<const Plane*>(plane), opts)
144 {}
145
146 explicit PlotD (Plane const* plane, const ncplot_options *opts = nullptr)
147 : PlotBase (const_cast<Plane*>(plane), opts == nullptr ? &default_options : opts)
148 {}
149
150 explicit PlotD (Plane &plane, const ncplot_options *opts = nullptr)
151 : PlotD (static_cast<Plane const&>(plane), opts)
152 {}
153
154 explicit PlotD (Plane const& plane, const ncplot_options *opts = nullptr)
155 : PlotBase (const_cast<Plane*>(&plane), opts == nullptr ? &default_options : opts)
156 {}
157
158 Plane* get_plane () const noexcept;
159 };
160
161 using Plot = PlotU;
162}
163#endif
#define NCPP_API_EXPORT
Definition _helpers.hh:9
#define NOEXCEPT_MAYBE
Definition Root.hh:15
bool is_valid() const noexcept
Definition Plane.hh:1239
TPlot * get_plot() const noexcept
Definition Plot.hh:102
bool add_sample(uint64_t x, TCoord y) const NOEXCEPT_MAYBE
Definition Plot.hh:22
bool sample(uint64_t x, TCoord *y) const NOEXCEPT_MAYBE
Definition Plot.hh:47
bool set_sample(uint64_t x, TCoord y) const NOEXCEPT_MAYBE
Definition Plot.hh:35
PlotBase(Plane *plane, const ncplot_options *opts, TCoord miny=0, TCoord maxy=0)
Definition Plot.hh:61
static ncplot_options default_options
Definition Plot.hh:139
PlotD(Plane const *plane, const ncplot_options *opts=nullptr)
Definition Plot.hh:146
PlotD(Plane const &plane, const ncplot_options *opts=nullptr)
Definition Plot.hh:154
PlotD(Plane &plane, const ncplot_options *opts=nullptr)
Definition Plot.hh:150
PlotD(Plane *plane, const ncplot_options *opts=nullptr)
Definition Plot.hh:142
Plane * get_plane() const noexcept
static ncplot_options default_options
Definition Plot.hh:114
PlotU(Plane const &plane, const ncplot_options *opts=nullptr)
Definition Plot.hh:129
PlotU(Plane *plane, const ncplot_options *opts=nullptr)
Definition Plot.hh:117
PlotU(Plane const *plane, const ncplot_options *opts=nullptr)
Definition Plot.hh:121
PlotU(Plane &plane, const ncplot_options *opts=nullptr)
Definition Plot.hh:125
Plane * get_plane() const noexcept
int y
Definition notcurses.h:1905
const struct ncplane_options * opts
Definition notcurses.h:3483
int int x
Definition notcurses.h:1905
int ncdplot_add_sample(ncdplot *n, uint64_t x, double y)
Definition plot.c:709
void ncuplot_destroy(ncuplot *n)
Definition plot.c:681
ncuplot * ncuplot_create(ncplane *n, const ncplot_options *opts, uint64_t miny, uint64_t maxy)
Definition plot.c:647
int ncdplot_set_sample(ncdplot *n, uint64_t x, double y)
Definition plot.c:713
void ncdplot_destroy(ncdplot *n)
Definition plot.c:732
ncdplot * ncdplot_create(ncplane *n, const ncplot_options *opts, double miny, double maxy)
Definition plot.c:690
int ncdplot_sample(const ncdplot *n, uint64_t x, double *y)
Definition plot.c:728
int ncuplot_sample(const ncuplot *n, uint64_t x, uint64_t *y)
Definition plot.c:724