8000 Move type casters into files that define their types · matplotlib/matplotlib@bab748c · GitHub
[go: up one dir, main page]

Skip to content

Commit bab748c

Browse files
committed
Move type casters into files that define their types
Since we're using pybind11 everywhere, it should be fine now to access it in any header, and putting the type caster there is clearer. We don't need the weird macro checks to conditionally define them either.
1 parent 2b2baa4 commit bab748c

File tree

3 files changed

+157
-158
lines changed

3 files changed

+157
-158
lines changed

src/_backend_agg_basic_types.h

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
/* Contains some simple types from the Agg backend that are also used
55
by other modules */
66

7+
#include <pybind11/pybind11.h>
8+
9+
#include <unordered_map>
710
#include <vector>
811

912
#include "agg_color_rgba.h"
@@ -13,6 +16,8 @@
1316

1417
#include "py_adaptors.h"
1518

19+
namespace py = pybind11;
20+
1621
struct ClipPath
1722
{
1823
mpl::PathIterator path;
@@ -121,4 +126,135 @@ class GCAgg
121126
GCAgg &operator=(const GCAgg &);
122127
};
123128

129+
namespace PYBIND11_NAMESPACE { namespace detail {
130+
template <> struct type_caster<agg::line_cap_e> {
131+
public:
132+
PYBIND11_TYPE_CASTER(agg::line_cap_e, const_name("line_cap_e"));
133+
134+
bool load(handle src, bool) {
135+
const std::unordered_map<std::string, agg::line_cap_e> enum_values = {
136+
{"butt", agg::butt_cap},
137+
{"round", agg::round_cap},
138+
{"projecting", agg::square_cap},
139+
};
140+
value = enum_values.at(src.cast<std::string>());
141+
return true;
142+
}
143+
};
144+
145+
template <> struct type_caster<agg::line_join_e> {
146+
public:
147+
PYBIND11_TYPE_CASTER(agg::line_join_e, const_name("line_join_e"));
148+
149+
bool load(handle src, bool) {
150+
const std::unordered_map<std::string, agg::line_join_e> enum_values = {
151+
{"miter", agg::miter_join_revert},
152+
{"round", agg::round_join},
153+
{"bevel", agg::bevel_join},
154+
};
155+
value = agg::miter_join_revert;
156+
value = enum_values.at(src.cast<std::string>());
157+
return true;
158+
}
159+
};
160+
161+
template <> struct type_caster<ClipPath> {
162+
public:
163+
PYBIND11_TYPE_CASTER(ClipPath, const_name("ClipPath"));
164+
165+
bool load(handle src, bool) {
166+
if (src.is_none()) {
167+
return true;
168+
}
169+
170+
auto clippath_tuple = src.cast<py::tuple>();
171+
172+
auto path = clippath_tuple[0];
173+
if (!path.is_none()) {
174+
value.path = path.cast<mpl::PathIterator>();
175+
}
176+
value.trans = clippath_tuple[1].cast<agg::trans_affine>();
177+
178+
return true;
179+
}
180+
};
181+
182+
template <> struct type_caster<Dashes> {
183+
public:
184+
PYBIND11_TYPE_CASTER(Dashes, const_name("Dashes"));
185+
186+
bool load(handle src, bool) {
187+
auto dash_tuple = src.cast<py::tuple>();
188+
auto dash_offset = dash_tuple[0].cast<double>();
189+
auto dashes_seq_or_none = dash_tuple[1];
190+
191+
if (dashes_seq_or_none.is_none()) {
192+
return true;
193+
}
194+
195+
auto dashes_seq = dashes_seq_or_none.cast<py::sequence>();
196+
197+
auto nentries = dashes_seq.size();
198+
// If the dashpattern has odd length, iterate through it twice (in
199+
// accordance with the pdf/ps/svg specs).
200+
auto dash_pattern_length = (nentries % 2) ? 2 * nentries : nentries;
201+
202+
for (py::size_t i = 0; i < dash_pattern_length; i += 2) {
203+
auto length = dashes_seq[i % nentries].cast<double>();
204+
auto skip = dashes_seq[(i + 1) % nentries].cast<double>();
205+
206+
value.add_dash_pair(length, skip);
207+
}
208+
209+
value.set_dash_offset(dash_offset);
210+
211+
return true;
212+
}
213+
};
214+
215+
template <> struct type_caster<SketchParams> {
216+
public:
217+
PYBIND11_TYPE_CASTER(SketchParams, const_name("SketchParams"));
218+
219+
bool load(handle src, bool) {
220+
if (src.is_none()) {
221+
value.scale = 0.0;
222+
value.length = 0.0;
223+
value.randomness = 0.0;
224+
return true;
225+
}
226+
227+
auto params = src.cast<std::tuple<double, double, double>>();
228+
std::tie(value.scale, value.length, value.randomness) = params;
229+
230+
return true;
231+
}
232+
};
233+
234+
template <> struct type_caster<GCAgg> {
235+
public:
236+
PYBIND11_TYPE_CASTER(GCAgg, const_name("GCAgg"));
237+
238+
bool load(handle src, bool) {
239+
value.linewidth = src.attr("_linewidth").cast<double>();
240+
value.alpha = src.attr("_alpha").cast<double>();
241+
value.forced_alpha = src.attr("_forced_alpha").cast<bool>();
242+
value.color = src.attr("_rgb").cast<agg::rgba>();
243+
value.isaa = src.attr("_antialiased").cast<bool>();
244+
value.cap = src.attr("_capstyle").cast<agg::line_cap_e>();
245+
value.join = src.attr("_joinstyle").cast<agg::line_join_e>();
246+
value.dashes = src.attr("get_dashes")().cast<Dashes>();
247+
value.cliprect = src.attr("_cliprect").cast<agg::rect_d>();
248+
value.clippath = src.attr("get_clip_path")().cast<ClipPath>();
249+
value.snap_mode = src.attr("get_snap")().cast<e_snap_mode>();
250+
value.hatchpath = src.attr("get_hatch_path")().cast<mpl::PathIterator>();
251+
value.hatch_color = src.attr("get_hatch_color")().cast<agg::rgba>();
252+
value.hatch_linewidth = src.attr("get_hatch_linewidth")().cast<double>();
253+
value.sketch = src.attr("get_sketch_params")().cast<SketchParams>();
254+
255+
return true;
256+
}
257+
};
258+
}} // namespace PYBIND11_NAMESPACE::detail
259+
124260
#endif

src/path_converters.h

Lines changed: 20 additions & 0 deletions
< 558 /tr>
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#ifndef MPL_PATH_CONVERTERS_H
44
#define MPL_PATH_CONVERTERS_H
55

6+
#include <pybind11/pybind11.h>
7+
68
#include <cmath>
79
#include <cstdint>
810
#include <limits>
@@ -530,6 +532,24 @@ enum e_snap_mode {
530532
SNAP_TRUE
531533
};
532534

535+
namespace PYBIND11_NAMESPACE { namespace detail {
536+
template <> struct type_caster<e_snap_mode> {
537+
public:
538+
PYBIND11_TYPE_CASTER(e_snap_mode, const_name("e_snap_mode"));
539+
540+
bool load(handle src, bool) {
541+
if (src.is_none()) {
542+
value = SNAP_AUTO;
543+
return true;
544+
}
545+
546+
value = src.cast<bool>() ? SNAP_TRUE : SNAP_FALSE;
547+
548+
return true;
549+
}
550+
};
551+
}} // namespace PYBIND11_NAMESPACE::detail
552+
533553
template <class VertexSource>
534554
class PathSnapper
535555
{

src/py_converters_11.h

Lines changed: 1 addition & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
namespace py = pybind11;
1010

11-
#include <unordered_map>
12-
1311
#include "agg_basics.h"
1412
#include "agg_color_rgba.h"
1513
#include "agg_trans_affine.h"
16-
#include "path_converters.h"
14+
#include "mplutils.h"
1715

1816
void convert_trans_affine(const py::object& transform, agg::trans_affine& affine);
1917

@@ -150,161 +148,6 @@ namespace PYBIND11_NAMESPACE { namespace detail {
150148
return true;
151149
}
152150
};
153-
154-
template <> struct type_caster<e_snap_mode> {
155-
public:
156-
PYBIND11_TYPE_CASTER(e_snap_mode, const_name("e_snap_mode"));
157-
158-
bool load(handle src, bool) {
159-
if (src.is_none()) {
160-
value = SNAP_AUTO;
161-
return true;
162-
}
163-
164-
value = src.cast<bool>() ? SNAP_TRUE : SNAP_FALSE;
165-
166-
return true;
167-
}
168-
};
169-
170-
/* Remove all this macro magic after dropping NumPy usage and just include `py_adaptors.h`. */
171-
#ifdef MPL_PY_ADAPTORS_H
172-
template <> struct type_caster<agg::line_cap_e> {
173-
public:
174-
PYBIND11_TYPE_CASTER(agg::line_cap_e, const_name("line_cap_e"));
175-
17 F438 6-
bool load(handle src, bool) {
177-
const std::unordered_map<std::string, agg::line_cap_e> enum_values = {
178-
{"butt", agg::butt_cap},
179-
{"round", agg::round_cap},
180-
{"projecting", agg::square_cap},
181-
};
182-
value = enum_values.at(src.cast<std::string>());
183-
return true;
184-
}
185-
};
186-
187-
template <> struct type_caster<agg::line_join_e> {
188-
public:
189-
PYBIND11_TYPE_CASTER(agg::line_join_e, const_name("line_join_e"));
190-
191-
bool load(handle src, bool) {
192-
const std::unordered_map<std::string, agg::line_join_e> enum_values = {
193-
{"miter", agg::miter_join_revert},
194-
{"round", agg::round_join},
195-
{"bevel", agg::bevel_join},
196-
};
197-
value = agg::miter_join_revert;
198-
value = enum_values.at(src.cast<std::string>());
199-
return true;
200-
}
201-
};
202-
#endif
203-
204-
/* Remove all this macro magic after dropping NumPy usage and just include `_backend_agg_basic_types.h`. */
205-
#ifdef MPL_BACKEND_AGG_BASIC_TYPES_H
206-
# ifndef MPL_PY_ADAPTORS_H
207-
# error "py_adaptors.h must be included to get Agg type casters"
208-
# endif
209-
210-
template <> struct type_caster<ClipPath> {
211-
public:
212-
PYBIND11_TYPE_CASTER(ClipPath, const_name("ClipPath"));
213-
214-
bool load(handle src, bool) {
215-
if (src.is_none()) {
216-
return true;
217-
}
218-
219-
auto clippath_tuple = src.cast<py::tuple>();
220-
221-
auto path = clippath_tuple[0];
222-
if (!path.is_none()) {
223-
value.path = path.cast<mpl::PathIterator>();
224-
}
225-
value.trans = clippath_tuple[1].cast<agg::trans_affine>();
226-
227-
return true;
228-
}
229-
};
230-
231-
template <> struct type_caster<Dashes> {
232-
public:
233-
PYBIND11_TYPE_CASTER(Dashes, const_name("Dashes"));
234-
235-
bool load(handle src, bool) {
236-
auto dash_tuple = src.cast<py::tuple>();
237-
auto dash_offset = dash_tuple[0].cast<double>();
238-
auto dashes_seq_or_none = dash_tuple[1];
239-
240-
if (dashes_seq_or_none.is_none()) {
241-
return true;
242-
}
243-
244-
auto dashes_seq = dashes_seq_or_none.cast<py::sequence>();
245-
246-
auto nentries = dashes_seq.size();
247-
// If the dashpattern has odd length, iterate through it twice (in
248-
// accordance with the pdf/ps/svg specs).
249-
auto dash_pattern_length = (nentries % 2) ? 2 * nentries : nentries;
250-
251-
for (py::size_t i = 0; i < dash_pattern_length; i += 2) {
252-
auto length = dashes_seq[i % nentries].cast<double>();
253-
auto skip = dashes_seq[(i + 1) % nentries].cast<double>();
254-
255-
value.add_dash_pair(length, skip);
256-
}
257-
258-
value.set_dash_offset(dash_offset);
259-
260-
return true;
261-
}
262-
};
263-
264-
template <> struct type_caster<SketchParams> {
265-
public:
266-
PYBIND11_TYPE_CASTER(SketchParams, const_name("SketchParams"));
267-
268-
bool load(handle src, bool) {
269-
if (src.is_none()) {
270-
value.scale = 0.0;
271-
value.length = 0.0;
272-
value.randomness = 0.0;
273-
return true;
274-
}
275-
276-
auto params = src.cast<std::tuple<double, double, double>>();
277-
std::tie(value.scale, value.length, value.randomness) = params;
278-
279-
return true;
280-
}
281-
};
282-
283-
template <> struct type_caster<GCAgg> {
284-
public:
285-
PYBIND11_TYPE_CASTER(GCAgg, const_name("GCAgg"));
286-
287-
bool load(handle src, bool) {
288-
value.linewidth = src.attr("_linewidth").cast<double>();
289-
value.alpha = src.attr("_alpha").cast<double>();
290-
value.forced_alpha = src.attr("_forced_alpha").cast<bool>();
291-
value.color = src.attr("_rgb").cast<agg::rgba>();
292-
value.isaa = src.attr("_antialiased").cast<bool>();
293-
value.cap = src.attr("_capstyle").cast<agg::line_cap_e>();
294-
value.join = src.attr("_joinstyle").cast<agg::line_join_e>();
295-
value.dashes = src.attr("get_dashes")().cast<Dashes>();
296-
value.cliprect = src.attr("_cliprect").cast<agg::rect_d>();
297-
value.clippath = src.attr("get_clip_path")().cast<ClipPath>();
298-
value.snap_mode = src.attr("get_snap")().cast<e_snap_mode>();
299-
value.hatchpath = src.attr("get_hatch_path")().cast<mpl::PathIterator>();
300-
value.hatch_color = src.attr("get_hatch_color")().cast<agg::rgba>();
301-
value.hatch_linewidth = src.attr("get_hatch_linewidth")().cast<double>();
302-
value.sketch = src.attr("get_sketch_params")().cast<SketchParams>();
303-
304-
return true;
305-
}
306-
};
307-
#endif
308151
}} // namespace PYBIND11_NAMESPACE::detail
309152

310153
#endif /* MPL_PY_CONVERTERS_11_H */

0 commit comments

Comments
 (0)
0