Skip to content

Commit

Permalink
use LOAD_ATTR macro
Browse files Browse the repository at this point in the history
  • Loading branch information
soblin committed Jan 25, 2022
1 parent 57ec3f6 commit 509b34e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 42 deletions.
23 changes: 15 additions & 8 deletions include/matplotlib_cpp11/axes.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
struct __attribute__((visibility("hidden"))) axes {
// TODO: For "hidden" class does ctor/dtor works ?
void init(pybind11::object instance_) {
instance = instance_;
plot = instance.attr("plot");
set = instance.attr("set");
grid = instance.attr("grid");
struct __attribute__((visibility("hidden"))) Axes {
Axes() {}
Axes(pybind11::object axes) {
self = axes;
load_attrs();
}
pybind11::object instance;
Axes(pybind11::module mod) {
self = mod.attr("axes");
load_attrs();
}
void load_attrs() {
LOAD_ATTR(plot, self);
LOAD_ATTR(set, self);
LOAD_ATTR(grid, self);
}
pybind11::object self;
pybind11::object plot;
pybind11::object set;
pybind11::object grid;
Expand Down
18 changes: 12 additions & 6 deletions include/matplotlib_cpp11/figure.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
struct __attribute__((visibility("hidden"))) figure {
// TODO: For "hidden" class does ctor/dtor works ?
void init(pybind11::object instance_) {
instance = instance_;
savefig = instance.attr("savefig");
struct __attribute__((visibility("hidden"))) Figure {
Figure() {}
Figure(pybind11::object figure) {
self = figure;
load_attrs();
}
pybind11::object instance;
Figure(pybind11::module mod) {
self = mod.attr("figure");
load_attrs();
}
void load_attrs() { LOAD_ATTR(savefig, self); }
pybind11::object self;
pybind11::object savefig;
pybind11::object add_subplot;
};
28 changes: 12 additions & 16 deletions include/matplotlib_cpp11/matplotlib_cpp11.h
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
#ifndef MATPLOTLIBCPP_11
#define MATPLOTLIBCPP_11
#ifndef MATPLOTLIBCPP_11_H
#define MATPLOTLIBCPP_11_H

#include <pybind11/pybind11.h>

namespace matplotlib_cpp11 {

static bool g_imported = false;

#define LOAD_ATTR(attr_, mod_) \
do { \
attr_ = mod_.attr(#attr_); \
} while (0)

#include "axes.h"
#include "figure.h"
#include "pyplot.h"

static pyplot &import() {
static pyplot g_pyplot;
static figure g_figure;
static axes g_axes;
static Figure g_figure;
static Axes g_axes;
if (not g_imported) {
g_imported = true;

// pyplot singleton
g_pyplot.mod = pybind11::module::import("matplotlib.pyplot");
g_pyplot.plot = g_pyplot.mod.attr("plot");
g_pyplot.show = g_pyplot.mod.attr("show");
g_pyplot.subplot_attr = g_pyplot.mod.attr("subplot");
g_pyplot.subplots_attr = g_pyplot.mod.attr("subplots");

// figure singleton
// g_figure.mod = pybind11::module::import("matplotlib.figure");

// axes singleton
auto mod = pybind11::module::import("matplotlib.pyplot");
g_pyplot = pyplot(mod);
}
return g_pyplot;
}

} // namespace matplotlib_cpp11

#endif /* MATPLOTLIBCPP_11 */
#endif /* MATPLOTLIBCPP_11_H */
29 changes: 17 additions & 12 deletions include/matplotlib_cpp11/pyplot.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
struct __attribute__((visibility("hidden"))) pyplot {
pyplot() {}
pyplot(pybind11::module mod_) {
mod = mod_;
load_attrs();
}
void load_attrs() {
LOAD_ATTR(plot, mod);
LOAD_ATTR(show, mod);
subplot_attr = mod.attr("subplot");
subplots_attr = mod.attr("subplots");
}
pybind11::module mod;
pybind11::object plot;
pybind11::object show;
pybind11::object subplot_attr;
pybind11::object subplots_attr;
axes subplot() {
auto ret = subplot_attr();
axes ax;
ax.init(ret);
return ax;
}
std::tuple<figure, axes> subplots() {
Axes subplot() { return Axes(subplot_attr()); }
std::tuple<Figure, Axes> subplots() {
// TODO: a lot of versions like subplots(111) or subplots(py::tuple(1,1,1))
pybind11::list ret = subplots_attr();
figure fig;
fig.init(ret[0]);
axes ax;
ax.init(ret[1]);
return {fig, ax};
pybind11::object fig = ret[0], ax = ret[1];
return {Figure(fig), Axes(ax)};
}
Figure figure() { return Figure(mod); }
Axes axes() { return Axes(mod); }
};

0 comments on commit 509b34e

Please sign in to comment.