Skip to content

Commit

Permalink
Add more std::unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
zamazan4ik committed May 22, 2018
1 parent df49d47 commit 58e8538
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/cutil/callcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "host.h"
#include "unichar.h"

#include <memory>

void
cprintf ( //Trace printf
const char *format, ... //special message
Expand Down Expand Up @@ -109,16 +111,14 @@ void c_clear_window( /*move pen */


char window_wait(ScrollView* win) {
SVEvent* ev;
// Wait till an input or click event (all others are thrown away)
char ret = '\0';
SVEventType ev_type = SVET_ANY;
do {
ev = win->AwaitEvent(SVET_ANY);
std::unique_ptr<SVEvent> ev(win->AwaitEvent(SVET_ANY));
ev_type = ev->type;
if (ev_type == SVET_INPUT)
ret = ev->parameter[0];
delete ev;
} while (ev_type != SVET_INPUT && ev_type != SVET_CLICK);
return ret;
}
Expand Down
9 changes: 5 additions & 4 deletions src/dict/dawg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "tesscallback.h"
#include "tprintf.h"

#include <memory>

/*----------------------------------------------------------------------
F u n c t i o n s f o r D a w g
----------------------------------------------------------------------*/
Expand Down Expand Up @@ -112,11 +114,10 @@ void CallWithUTF8(TessCallback1<const char *> *cb, const WERD_CHOICE *wc) {

void Dawg::iterate_words(const UNICHARSET &unicharset,
TessCallback1<const char *> *cb) const {
TessCallback1<const WERD_CHOICE *> *shim =
NewPermanentTessCallback(CallWithUTF8, cb);
std::unique_ptr<TessCallback1<const WERD_CHOICE *>> shim(
NewPermanentTessCallback(CallWithUTF8, cb));
WERD_CHOICE word(&unicharset);
iterate_words_rec(word, 0, shim);
delete shim;
iterate_words_rec(word, 0, shim.get());
}

void Dawg::iterate_words_rec(const WERD_CHOICE &word_so_far,
Expand Down
2 changes: 1 addition & 1 deletion src/textord/edgblob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ICOORD tright): bl(bleft), tr(tright) {
bxdim =(tright.x() - bleft.x()) / BUCKETSIZE + 1;
bydim =(tright.y() - bleft.y()) / BUCKETSIZE + 1;
// make array
buckets = new C_OUTLINE_LIST[bxdim * bydim];
buckets.reset(new C_OUTLINE_LIST[bxdim * bydim]);
index = 0;
}

Expand Down
9 changes: 5 additions & 4 deletions src/textord/edgblob.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "coutln.h"
#include "crakedge.h"

#include <memory>

#define BUCKETSIZE 16

class OL_BUCKETS
Expand All @@ -35,9 +37,8 @@ class OL_BUCKETS
ICOORD bleft, //corners
ICOORD tright);

~OL_BUCKETS () { //cleanup
delete[]buckets;
}
~OL_BUCKETS () = default;

C_OUTLINE_LIST *operator () (//array access
int16_t x, //image coords
int16_t y);
Expand All @@ -64,7 +65,7 @@ class OL_BUCKETS
C_OUTLINE_IT *it); //destination iterator

private:
C_OUTLINE_LIST * buckets; //array of buckets
std::unique_ptr<C_OUTLINE_LIST[]> buckets; //array of buckets
int16_t bxdim; //size of array
int16_t bydim;
ICOORD bl; //corners
Expand Down
16 changes: 6 additions & 10 deletions src/textord/scanedg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ void block_edges(Pix *t_pix, // thresholded image
int height = pixGetHeight(t_pix);
int wpl = pixGetWpl(t_pix);
// lines in progress
CRACKEDGE **ptrline = new CRACKEDGE*[width + 1];
std::unique_ptr<CRACKEDGE*[]> ptrline(new CRACKEDGE*[width + 1]);
CRACKEDGE *free_cracks = nullptr;

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

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

uint8_t margin = WHITE_PIX;

Expand All @@ -65,17 +65,15 @@ void block_edges(Pix *t_pix, // thresholded image
for (int x = 0; x < block_width; ++x) {
bwline[x] = GET_DATA_BIT(line, x + bleft.x()) ^ 1;
}
make_margins(block, &line_it, bwline, margin, bleft.x(), tright.x(), y);
make_margins(block, &line_it, bwline.get(), margin, bleft.x(), tright.x(), y);
} else {
memset(bwline, margin, block_width * sizeof(bwline[0]));
memset(bwline.get(), margin, block_width * sizeof(bwline[0]));
}
line_edges(bleft.x(), y, block_width,
margin, bwline, ptrline, &free_cracks, outline_it);
margin, bwline.get(), ptrline.get(), &free_cracks, outline_it);
}

free_crackedges(free_cracks); // really free them
delete[] ptrline;
delete[] bwline;
}


Expand All @@ -94,14 +92,13 @@ void make_margins( //get a line
int16_t right,
int16_t y //line coord
) {
PB_LINE_IT *lines;
ICOORDELT_IT seg_it;
int32_t start; //of segment
int16_t xext; //of segment
int xindex; //index to pixel

if (block->poly_block () != nullptr) {
lines = new PB_LINE_IT (block->poly_block ());
std::unique_ptr<PB_LINE_IT> lines(new PB_LINE_IT (block->poly_block ()));
const std::unique_ptr</*non-const*/ ICOORDELT_LIST> segments(
lines->get_line(y));
if (!segments->empty ()) {
Expand All @@ -124,7 +121,6 @@ void make_margins( //get a line
for (xindex = left; xindex < right; xindex++)
pixels[xindex - left] = margin;
}
delete lines;
}
else {
start = line_it->get_line (y, xext);
Expand Down
7 changes: 3 additions & 4 deletions src/textord/topitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include "config_auto.h"
#endif

#include <memory>

#define EXTERN

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

best_sp_sd = initial_pitch;

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

for (pitch_delta = -textord_pitch_range; pitch_delta <= textord_pitch_range;
pitch_delta++)
Expand Down Expand Up @@ -1356,8 +1357,6 @@ float tune_row_pitch2( //find fp cells
space_size,
initial_pitch);

delete[]sum_proj;

return best_sd;
}

Expand Down
9 changes: 4 additions & 5 deletions src/textord/tospace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#endif

#include <algorithm>
#include <memory>

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

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

block_it.set_to_list (blocks);
block_index = 1;
for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
block_it.forward ()) {
block = block_it.data ();
gapmap = new GAPMAP (block);
std::unique_ptr<GAPMAP> gapmap(new GAPMAP (block)); //map of big vert gaps in blk
block_spacing_stats(block,
gapmap,
gapmap.get(),
old_text_ord_proportional,
block_space_gap_width,
block_non_space_gap_width);
Expand All @@ -89,7 +89,7 @@ void Textord::to_spacing(
tprintf ("Block %d Row %d: Now Proportional\n",
block_index, row_index);
row_spacing_stats(row,
gapmap,
gapmap.get(),
block_index,
row_index,
block_space_gap_width,
Expand All @@ -108,7 +108,6 @@ void Textord::to_spacing(
#endif
row_index++;
}
delete gapmap;
block_index++;
}
}
Expand Down

0 comments on commit 58e8538

Please sign in to comment.