Skip to content

Commit 758a399

Browse files
authored
Merge pull request matplotlib#29280 from QuLogic/ext-modernizing
Apply some modernization to C++ extensions
2 parents 6a84a5d + 86321df commit 758a399

13 files changed

+229
-249
lines changed

src/_backend_agg.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ RendererAgg::RendererAgg(unsigned int width, unsigned int height, double dpi)
1010
height(height),
1111
dpi(dpi),
1212
NUMBYTES((size_t)width * (size_t)height * 4),
13-
pixBuffer(NULL),
13+
pixBuffer(nullptr),
1414
renderingBuffer(),
15-
alphaBuffer(NULL),
15+
alphaBuffer(nullptr),
1616
alphaMaskRenderingBuffer(),
1717
alphaMask(alphaMaskRenderingBuffer),
1818
pixfmtAlphaMask(alphaMaskRenderingBuffer),
@@ -26,7 +26,7 @@ RendererAgg::RendererAgg(unsigned int width, unsigned int height, double dpi)
2626
rendererAA(),
2727
rendererBin(),
2828
theRasterizer(32768),
29-
lastclippath(NULL),
29+
lastclippath(nullptr),
3030
_fill_color(agg::rgba(1, 1, 1, 0))
3131
{
3232
if (dpi <= 0.0) {
@@ -75,7 +75,7 @@ BufferRegion *RendererAgg::copy_from_bbox(agg::rect_d in_rect)
7575
agg::rect_i rect(
7676
(int)in_rect.x1, height - (int)in_rect.y2, (int)in_rect.x2, height - (int)in_rect.y1);
7777

78-
BufferRegion *reg = NULL;
78+
BufferRegion *reg = nullptr;
7979
reg = new BufferRegion(rect);
8080

8181
agg::rendering_buffer rbuf;
@@ -90,21 +90,21 @@ BufferRegion *RendererAgg::copy_from_bbox(agg::rect_d in_rect)
9090

9191
void RendererAgg::restore_region(BufferRegion &region)
9292
{
93-
if (region.get_data() == NULL) {
93+
if (region.get_data() == nullptr) {
9494
throw std::runtime_error("Cannot restore_region from NULL data");
9595
}
9696

9797
agg::rendering_buffer rbuf;
9898
rbuf.attach(region.get_data(), region.get_width(), region.get_height(), region.get_stride());
9999

100-
rendererBase.copy_from(rbuf, 0, region.get_rect().x1, region.get_rect().y1);
100+
rendererBase.copy_from(rbuf, nullptr, region.get_rect().x1, region.get_rect().y1);
101101
}
102102

103103
// Restore the part of the saved region with offsets
104104
void
105105
RendererAgg::restore_region(BufferRegion &region, int xx1, int yy1, int xx2, int yy2, int x, int y )
106106
{
107-
if (region.get_data() == NULL) {
107+
if (region.get_data() == nullptr) {
108108
throw std::runtime_error("Cannot restore_region from NULL data");
109109
}
110110

src/_backend_agg.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ class BufferRegion
6565
delete[] data;
6666
};
6767

68+
// prevent copying
69+
BufferRegion(const BufferRegion &) = delete;
70+
BufferRegion &operator=(const BufferRegion &) = delete;
71+
6872
agg::int8u *get_data()
6973
{
7074
return data;
@@ -96,11 +100,6 @@ class BufferRegion
96100
int width;
97101
int height;
98102
int stride;
99-
100-
private:
101-
// prevent copying
102-
BufferRegion(const BufferRegion &);
103-
BufferRegion &operator=(const BufferRegion &);
104103
};
105104

106105
#define MARKER_CACHE_SIZE 512
@@ -890,7 +889,7 @@ inline void RendererAgg::draw_image(GCAgg &gc,
890889
} else {
891890
set_clipbox(gc.cliprect, rendererBase);
892891
rendererBase.blend_from(
893-
pixf, 0, (int)x, (int)(height - (y + image.shape(0))), (agg::int8u)(alpha * 255));
892+
pixf, nullptr, (int)x, (int)(height - (y + image.shape(0))), (agg::int8u)(alpha * 255));
894893
}
895894

896895
rendererBase.reset_clipping(true);

src/_backend_agg_basic_types.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Dashes
4848
}
4949
void add_dash_pair(double length, double skip)
5050
{
51-
dashes.push_back(std::make_pair(length, skip));
51+
dashes.emplace_back(length, skip);
5252
}
5353
size_t size() const
5454
{
@@ -59,9 +59,7 @@ class Dashes
5959
void dash_to_stroke(T &stroke, double dpi, bool isaa)
6060
{
6161
double scaleddpi = dpi / 72.0;
62-
for (dash_t::const_iterator i = dashes.begin(); i != dashes.end(); ++i) {
63-
double val0 = i->first;
64-
double val1 = i->second;
62+
for (auto [val0, val1] : dashes) {
6563
val0 = val0 * scaleddpi;
6664
val1 = val1 * scaleddpi;
6765
if (!isaa) {

src/_c_internal_utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ mpl_xdisplay_is_valid(void)
4343
&& (libX11 = dlopen("libX11.so.6", RTLD_LAZY))) {
4444
typedef struct Display* (*XOpenDisplay_t)(char const*);
4545
typedef int (*XCloseDisplay_t)(struct Display*);
46-
struct Display* display = NULL;
46+
struct Display* display = nullptr;
4747
XOpenDisplay_t XOpenDisplay = (XOpenDisplay_t)dlsym(libX11, "XOpenDisplay");
4848
XCloseDisplay_t XCloseDisplay = (XCloseDisplay_t)dlsym(libX11, "XCloseDisplay");
4949
if (XOpenDisplay && XCloseDisplay
50-
&& (display = XOpenDisplay(NULL))) {
50+
&& (display = XOpenDisplay(nullptr))) {
5151
XCloseDisplay(display);
5252
}
5353
if (dlclose(libX11)) {
@@ -75,13 +75,13 @@ mpl_display_is_valid(void)
7575
&& (libwayland_client = dlopen("libwayland-client.so.0", RTLD_LAZY))) {
7676
typedef struct wl_display* (*wl_display_connect_t)(char const*);
7777
typedef void (*wl_display_disconnect_t)(struct wl_display*);
78-
struct wl_display* display = NULL;
78+
struct wl_display* display = nullptr;
7979
wl_display_connect_t wl_display_connect =
8080
(wl_display_connect_t)dlsym(libwayland_client, "wl_display_connect");
8181
wl_display_disconnect_t wl_display_disconnect =
8282
(wl_display_disconnect_t)dlsym(libwayland_client, "wl_display_disconnect");
8383
if (wl_display_connect && wl_display_disconnect
84-
&& (display = wl_display_connect(NULL))) {
84+
&& (display = wl_display_connect(nullptr))) {
8585
wl_display_disconnect(display);
8686
}
8787
if (dlclose(libwayland_client)) {

src/_image_resample.h

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,16 @@ namespace agg
6060
value_type a;
6161

6262
//--------------------------------------------------------------------
63-
gray64() {}
63+
gray64() = default;
6464

6565
//--------------------------------------------------------------------
66-
explicit gray64(value_type v_, value_type a_ = 1) :
67-
v(v_), a(a_) {}
66+
explicit gray64(value_type v_, value_type a_ = 1) : v(v_), a(a_) {}
6867

6968
//--------------------------------------------------------------------
70-
gray64(const self_type& c, value_type a_) :
71-
v(c.v), a(a_) {}
69+
gray64(const self_type& c, value_type a_) : v(c.v), a(a_) {}
7270

7371
//--------------------------------------------------------------------
74-
gray64(const gray64& c) :
75-
v(c.v),
76-
a(c.a) {}
72+
gray64(const gray64& c) = default;
7773

7874
//--------------------------------------------------------------------
7975
static AGG_INLINE double to_double(value_type a)
@@ -246,7 +242,7 @@ namespace agg
246242
value_type a;
247243

248244
//--------------------------------------------------------------------
249-
rgba64() {}
245+
rgba64() = default;
250246

251247
//--------------------------------------------------------------------
252248
rgba64(value_type r_, value_type g_, value_type b_, value_type a_= 1) :
@@ -503,51 +499,52 @@ typedef enum {
503499
// T is rgba if and only if it has an T::r field.
504500
template<typename T, typename = void> struct is_grayscale : std::true_type {};
505501
template<typename T> struct is_grayscale<T, std::void_t<decltype(T::r)>> : std::false_type {};
502+
template<typename T> constexpr bool is_grayscale_v = is_grayscale<T>::value;
506503

507504

508505
template<typename color_type>
509506
struct type_mapping
510507
{
511-
using blender_type = typename std::conditional<
512-
is_grayscale<color_type>::value,
508+
using blender_type = std::conditional_t<
509+
is_grayscale_v<color_type>,
513510
agg::blender_gray<color_type>,
514-
typename std::conditional<
515-
std::is_same<color_type, agg::rgba8>::value,
511+
std::conditional_t<
512+
std::is_same_v<color_type, agg::rgba8>,
516513
fixed_blender_rgba_plain<color_type, agg::order_rgba>,
517514
agg::blender_rgba_plain<color_type, agg::order_rgba>
518-
>::type
519-
>::type;
520-
using pixfmt_type = typename std::conditional<
521-
is_grayscale<color_type>::value,
515+
>
516+
>;
517+
using pixfmt_type = std::conditional_t<
518+
is_grayscale_v<color_type>,
522519
agg::pixfmt_alpha_blend_gray<blender_type, agg::rendering_buffer>,
523520
agg::pixfmt_alpha_blend_rgba<blender_type, agg::rendering_buffer>
524-
>::type;
525-
using pixfmt_pre_type = typename std::conditional<
526-
is_grayscale<color_type>::value,
521+
>;
522+
using pixfmt_pre_type = std::conditional_t<
523+
is_grayscale_v<color_type>,
527524
pixfmt_type,
528525
agg::pixfmt_alpha_blend_rgba<
529-
typename std::conditional<
530-
std::is_same<color_type, agg::rgba8>::value,
526+
std::conditional_t<
527+
std::is_same_v<color_type, agg::rgba8>,
531528
fixed_blender_rgba_pre<color_type, agg::order_rgba>,
532529
agg::blender_rgba_pre<color_type, agg::order_rgba>
533-
>::type,
530+
>,
534531
agg::rendering_buffer>
535-
>::type;
536-
template<typename A> using span_gen_affine_type = typename std::conditional<
537-
is_grayscale<color_type>::value,
532+
>;
533+
template<typename A> using span_gen_affine_type = std::conditional_t<
534+
is_grayscale_v<color_type>,
538535
agg::span_image_resample_gray_affine<A>,
539536
agg::span_image_resample_rgba_affine<A>
540-
>::type;
541-
template<typename A, typename B> using span_gen_filter_type = typename std::conditional<
542-
is_grayscale<color_type>::value,
537+
>;
538+
template<typename A, typename B> using span_gen_filter_type = std::conditional_t<
539+
is_grayscale_v<color_type>,
543540
agg::span_image_filter_gray<A, B>,
544541
agg::span_image_filter_rgba<A, B>
545-
>::type;
546-
template<typename A, typename B> using span_gen_nn_type = typename std::conditional<
547-
is_grayscale<color_type>::value,
542+
>;
543+
template<typename A, typename B> using span_gen_nn_type = std::conditional_t<
544+
is_grayscale_v<color_type>,
548545
agg::span_image_filter_gray_nn<A, B>,
549546
agg::span_image_filter_rgba_nn<A, B>
550-
>::type;
547+
>;
551548
};
552549

553550

src/_path.h

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -602,30 +602,26 @@ struct ygt : public bisecty
602602
template <class Filter>
603603
inline void clip_to_rect_one_step(const Polygon &polygon, Polygon &result, const Filter &filter)
604604
{
605-
double sx, sy, px, py, bx, by;
606605
bool sinside, pinside;
607606
result.clear();
608607

609608
if (polygon.size() == 0) {
610609
return;
611610
}
612611

613-
sx = polygon.back().x;
614-
sy = polygon.back().y;
615-
for (Polygon::const_iterator i = polygon.begin(); i != polygon.end(); ++i) {
616-
px = i->x;
617-
py = i->y;
618-
612+
auto [sx, sy] = polygon.back();
613+
for (auto [px, py] : polygon) {
619614
sinside = filter.is_inside(sx, sy);
620615
pinside = filter.is_inside(px, py);
621616

622617
if (sinside ^ pinside) {
618+
double bx, by;
623619
filter.bisect(sx, sy, px, py, &bx, &by);
624-
result.push_back(XY(bx, by));
620+
result.emplace_back(bx, by);
625621
}
626622

627623
if (pinside) {
628-
result.push_back(XY(px, py));
624+
result.emplace_back(px, py);
629625
}
630626

631627
sx = px;
@@ -672,7 +668,7 @@ clip_path_to_rect(PathIterator &path, agg::rect_d &rect, bool inside, std::vecto
672668
polygon1.clear();
673669
do {
674670
if (code == agg::path_cmd_move_to) {
675-
polygon1.push_back(XY(x, y));
671+
polygon1.emplace_back(x, y);
676672
}
677673

678674
code = curve.vertex(&x, &y);
@@ -682,7 +678,7 @@ clip_path_to_rect(PathIterator &path, agg::rect_d &rect, bool inside, std::vecto
682678
}
683679

684680
if (code != agg::path_cmd_move_to) {
685-
polygon1.push_back(XY(x, y));
681+
polygon1.emplace_back(x, y);
686682
}
687683
} while ((code & agg::path_cmd_end_poly) != agg::path_cmd_end_poly);
688684

@@ -978,23 +974,20 @@ void convert_path_to_polygons(PathIterator &path,
978974
simplify_t simplified(clipped, simplify, path.simplify_threshold());
979975
curve_t curve(simplified);
980976

981-
result.push_back(Polygon());
982-
Polygon *polygon = &result.back();
977+
Polygon *polygon = &result.emplace_back();
983978
double x, y;
984979
unsigned code;
985980

986981
while ((code = curve.vertex(&x, &y)) != agg::path_cmd_stop) {
987982
if ((code & agg::path_cmd_end_poly) == agg::path_cmd_end_poly) {
988983
_finalize_polygon(result, 1);
989-
result.push_back(Polygon());
990-
polygon = &result.back();
984+
polygon = &result.emplace_back();
991985
} else {
992986
if (code == agg::path_cmd_move_to) {
993987
_finalize_polygon(result, closed_only);
994-
result.push_back(Polygon());
995-
polygon = &result.back();
988+
polygon = &result.emplace_back();
996989
}
997-
polygon->push_back(XY(x, y));
990+
polygon->emplace_back(x, y);
998991
}
999992
}
1000993

@@ -1086,7 +1079,7 @@ void __add_number(double val, char format_code, int precision,
10861079
buffer += str;
10871080
} else {
10881081
char *str = PyOS_double_to_string(
1089-
val, format_code, precision, Py_DTSF_ADD_DOT_0, NULL);
1082+
val, format_code, precision, Py_DTSF_ADD_DOT_0, nullptr);
10901083
// Delete trailing zeros and decimal point
10911084
char *c = str + strlen(str) - 1; // Start at last character.
10921085
// Rewind through all the zeros and, if present, the trailing decimal

src/_qhull_wrapper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ delaunay_impl(py::ssize_t npoints, const double* x, const double* y,
167167
}
168168

169169
/* qhull expects a FILE* to write errors to. */
170-
FILE* error_file = NULL;
170+
FILE* error_file = nullptr;
171171
if (hide_qhull_errors) {
172172
/* qhull errors are ignored by writing to OS-equivalent of /dev/null.
173173
* Rather than have OS-specific code here, instead it is determined by
174174
* meson.build and passed in via the macro MPL_DEVNULL. */
175175
error_file = fopen(STRINGIFY(MPL_DEVNULL), "w");
176-
if (error_file == NULL) {
176+
if (error_file == nullptr) {
177177
throw std::runtime_error("Could not open devnull");
178178
}
179179
}
@@ -186,7 +186,7 @@ delaunay_impl(py::ssize_t npoints, const double* x, const double* y,
186186
QhullInfo info(error_file, qh);
187187
qh_zero(qh, error_file);
188188
exitcode = qh_new_qhull(qh, ndim, (int)npoints, points.data(), False,
189-
(char*)"qhull d Qt Qbb Qc Qz", NULL, error_file);
189+
(char*)"qhull d Qt Qbb Qc Qz", nullptr, error_file);
190190
if (exitcode != qh_ERRnone) {
191191
std::string msg =
192192
py::str("Error in qhull Delaunay triangulation calculation: {} (exitcode={})")

src/_tkagg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ load_tkinter_funcs()
312312
// Load tkinter global funcs from tkinter compiled module.
313313

314314
// Try loading from the main program namespace first.
315-
auto main_program = dlopen(NULL, RTLD_LAZY);
315+
auto main_program = dlopen(nullptr, RTLD_LAZY);
316316
auto success = load_tcl_tk(main_program);
317317
// We don't need to keep a reference open as the main program always exists.
318318
if (dlclose(main_program)) {

src/array.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ class empty
5656
public:
5757
typedef empty<T> sub_t;
5858

59-
empty()
60-
{
61-
}
59+
empty() = default;
6260

6361
T &operator()(int i, int j = 0, int k = 0)
6462
{

0 commit comments

Comments
 (0)