Skip to content

Commit 58e8538

Browse files
committed
Add more std::unique_ptr
1 parent df49d47 commit 58e8538

File tree

7 files changed

+27
-31
lines changed

7 files changed

+27
-31
lines changed

src/cutil/callcpp.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "host.h"
3737
#include "unichar.h"
3838

39+
#include <memory>
40+
3941
void
4042
cprintf ( //Trace printf
4143
const char *format, ... //special message
@@ -109,16 +111,14 @@ void c_clear_window( /*move pen */
109111

110112

111113
char window_wait(ScrollView* win) {
112-
SVEvent* ev;
113114
// Wait till an input or click event (all others are thrown away)
114115
char ret = '\0';
115116
SVEventType ev_type = SVET_ANY;
116117
do {
117-
ev = win->AwaitEvent(SVET_ANY);
118+
std::unique_ptr<SVEvent> ev(win->AwaitEvent(SVET_ANY));
118119
ev_type = ev->type;
119120
if (ev_type == SVET_INPUT)
120121
ret = ev->parameter[0];
121-
delete ev;
122122
} while (ev_type != SVET_INPUT && ev_type != SVET_CLICK);
123123
return ret;
124124
}

src/dict/dawg.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "tesscallback.h"
3737
#include "tprintf.h"
3838

39+
#include <memory>
40+
3941
/*----------------------------------------------------------------------
4042
F u n c t i o n s f o r D a w g
4143
----------------------------------------------------------------------*/
@@ -112,11 +114,10 @@ void CallWithUTF8(TessCallback1<const char *> *cb, const WERD_CHOICE *wc) {
112114

113115
void Dawg::iterate_words(const UNICHARSET &unicharset,
114116
TessCallback1<const char *> *cb) const {
115-
TessCallback1<const WERD_CHOICE *> *shim =
116-
NewPermanentTessCallback(CallWithUTF8, cb);
117+
std::unique_ptr<TessCallback1<const WERD_CHOICE *>> shim(
118+
NewPermanentTessCallback(CallWithUTF8, cb));
117119
WERD_CHOICE word(&unicharset);
118-
iterate_words_rec(word, 0, shim);
119-
delete shim;
120+
iterate_words_rec(word, 0, shim.get());
120121
}
121122

122123
void Dawg::iterate_words_rec(const WERD_CHOICE &word_so_far,

src/textord/edgblob.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ ICOORD tright): bl(bleft), tr(tright) {
7171
bxdim =(tright.x() - bleft.x()) / BUCKETSIZE + 1;
7272
bydim =(tright.y() - bleft.y()) / BUCKETSIZE + 1;
7373
// make array
74-
buckets = new C_OUTLINE_LIST[bxdim * bydim];
74+
buckets.reset(new C_OUTLINE_LIST[bxdim * bydim]);
7575
index = 0;
7676
}
7777

src/textord/edgblob.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "coutln.h"
2727
#include "crakedge.h"
2828

29+
#include <memory>
30+
2931
#define BUCKETSIZE 16
3032

3133
class OL_BUCKETS
@@ -35,9 +37,8 @@ class OL_BUCKETS
3537
ICOORD bleft, //corners
3638
ICOORD tright);
3739

38-
~OL_BUCKETS () { //cleanup
39-
delete[]buckets;
40-
}
40+
~OL_BUCKETS () = default;
41+
4142
C_OUTLINE_LIST *operator () (//array access
4243
int16_t x, //image coords
4344
int16_t y);
@@ -64,7 +65,7 @@ class OL_BUCKETS
6465
C_OUTLINE_IT *it); //destination iterator
6566

6667
private:
67-
C_OUTLINE_LIST * buckets; //array of buckets
68+
std::unique_ptr<C_OUTLINE_LIST[]> buckets; //array of buckets
6869
int16_t bxdim; //size of array
6970
int16_t bydim;
7071
ICOORD bl; //corners

src/textord/scanedg.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ void block_edges(Pix *t_pix, // thresholded image
4646
int height = pixGetHeight(t_pix);
4747
int wpl = pixGetWpl(t_pix);
4848
// lines in progress
49-
CRACKEDGE **ptrline = new CRACKEDGE*[width + 1];
49+
std::unique_ptr<CRACKEDGE*[]> ptrline(new CRACKEDGE*[width + 1]);
5050
CRACKEDGE *free_cracks = nullptr;
5151

5252
block->bounding_box(bleft, tright); // block box
5353
int block_width = tright.x() - bleft.x();
5454
for (int x = block_width; x >= 0; x--)
5555
ptrline[x] = nullptr; // no lines in progress
5656

57-
uint8_t* bwline = new uint8_t[width];
57+
std::unique_ptr<uint8_t[]> bwline(new uint8_t[width]);
5858

5959
uint8_t margin = WHITE_PIX;
6060

@@ -65,17 +65,15 @@ void block_edges(Pix *t_pix, // thresholded image
6565
for (int x = 0; x < block_width; ++x) {
6666
bwline[x] = GET_DATA_BIT(line, x + bleft.x()) ^ 1;
6767
}
68-
make_margins(block, &line_it, bwline, margin, bleft.x(), tright.x(), y);
68+
make_margins(block, &line_it, bwline.get(), margin, bleft.x(), tright.x(), y);
6969
} else {
70-
memset(bwline, margin, block_width * sizeof(bwline[0]));
70+
memset(bwline.get(), margin, block_width * sizeof(bwline[0]));
7171
}
7272
line_edges(bleft.x(), y, block_width,
73-
margin, bwline, ptrline, &free_cracks, outline_it);
73+
margin, bwline.get(), ptrline.get(), &free_cracks, outline_it);
7474
}
7575

7676
free_crackedges(free_cracks); // really free them
77-
delete[] ptrline;
78-
delete[] bwline;
7977
}
8078

8179

@@ -94,14 +92,13 @@ void make_margins( //get a line
9492
int16_t right,
9593
int16_t y //line coord
9694
) {
97-
PB_LINE_IT *lines;
9895
ICOORDELT_IT seg_it;
9996
int32_t start; //of segment
10097
int16_t xext; //of segment
10198
int xindex; //index to pixel
10299

103100
if (block->poly_block () != nullptr) {
104-
lines = new PB_LINE_IT (block->poly_block ());
101+
std::unique_ptr<PB_LINE_IT> lines(new PB_LINE_IT (block->poly_block ()));
105102
const std::unique_ptr</*non-const*/ ICOORDELT_LIST> segments(
106103
lines->get_line(y));
107104
if (!segments->empty ()) {
@@ -124,7 +121,6 @@ void make_margins( //get a line
124121
for (xindex = left; xindex < right; xindex++)
125122
pixels[xindex - left] = margin;
126123
}
127-
delete lines;
128124
}
129125
else {
130126
start = line_it->get_line (y, xext);

src/textord/topitch.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#include "config_auto.h"
3838
#endif
3939

40+
#include <memory>
41+
4042
#define EXTERN
4143

4244
EXTERN BOOL_VAR (textord_all_prop, FALSE, "All doc is proportial text");
@@ -1275,15 +1277,14 @@ float tune_row_pitch2( //find fp cells
12751277
int16_t end; //of good range
12761278
int32_t best_count; //lowest sum
12771279
float best_sd; //best result
1278-
STATS *sum_proj; //summed projection
12791280

12801281
best_sp_sd = initial_pitch;
12811282

12821283
best_pitch = static_cast<int>(initial_pitch);
12831284
if (textord_disable_pitch_test || best_pitch <= textord_pitch_range) {
12841285
return initial_pitch;
12851286
}
1286-
sum_proj = new STATS[textord_pitch_range * 2 + 1];
1287+
std::unique_ptr<STATS[]> sum_proj(new STATS[textord_pitch_range * 2 + 1]); //summed projection
12871288

12881289
for (pitch_delta = -textord_pitch_range; pitch_delta <= textord_pitch_range;
12891290
pitch_delta++)
@@ -1356,8 +1357,6 @@ float tune_row_pitch2( //find fp cells
13561357
space_size,
13571358
initial_pitch);
13581359

1359-
delete[]sum_proj;
1360-
13611360
return best_sd;
13621361
}
13631362

src/textord/tospace.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#endif
3838

3939
#include <algorithm>
40+
#include <memory>
4041

4142
#define MAXSPACING 128 /*max expected spacing in pix */
4243

@@ -55,16 +56,15 @@ void Textord::to_spacing(
5556
//estimated width of non space gaps for whole block
5657
int16_t block_non_space_gap_width;
5758
BOOL8 old_text_ord_proportional;//old fixed/prop result
58-
GAPMAP *gapmap = nullptr; //map of big vert gaps in blk
5959

6060
block_it.set_to_list (blocks);
6161
block_index = 1;
6262
for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
6363
block_it.forward ()) {
6464
block = block_it.data ();
65-
gapmap = new GAPMAP (block);
65+
std::unique_ptr<GAPMAP> gapmap(new GAPMAP (block)); //map of big vert gaps in blk
6666
block_spacing_stats(block,
67-
gapmap,
67+
gapmap.get(),
6868
old_text_ord_proportional,
6969
block_space_gap_width,
7070
block_non_space_gap_width);
@@ -89,7 +89,7 @@ void Textord::to_spacing(
8989
tprintf ("Block %d Row %d: Now Proportional\n",
9090
block_index, row_index);
9191
row_spacing_stats(row,
92-
gapmap,
92+
gapmap.get(),
9393
block_index,
9494
row_index,
9595
block_space_gap_width,
@@ -108,7 +108,6 @@ void Textord::to_spacing(
108108
#endif
109109
row_index++;
110110
}
111-
delete gapmap;
112111
block_index++;
113112
}
114113
}

0 commit comments

Comments
 (0)