Skip to content

Commit

Permalink
Rework big clist macro into template and small macro. Remove unused m…
Browse files Browse the repository at this point in the history
…acros QUOTE_IT and CLISTIZE (source file macro).
  • Loading branch information
egorpugin committed Apr 6, 2021
1 parent 0c18990 commit 18e61d1
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 153 deletions.
2 changes: 0 additions & 2 deletions src/ccstruct/blobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ const TPOINT kDivisibleVerticalItalic(1, 5);
F u n c t i o n s
----------------------------------------------------------------------*/

CLISTIZE(EDGEPT)

// Returns true when the two line segments cross each other.
// (Moved from outlines.cpp).
// Finds where the projected lines would cross and then checks to see if the
Expand Down
1 change: 0 additions & 1 deletion src/ccstruct/pageres.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ struct Pix;
namespace tesseract {

ELISTIZE(BLOCK_RES)
CLISTIZE(BLOCK_RES)
ELISTIZE(ROW_RES)
ELISTIZE(WERD_RES)

Expand Down
1 change: 0 additions & 1 deletion src/ccstruct/pdblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ namespace tesseract {
constexpr ERRCODE BADBLOCKLINE("Y coordinate in block out of bounds");
constexpr ERRCODE LOSTBLOCKLINE("Can't find rectangle for line");

CLISTIZE(PDBLK)
/**********************************************************************
* PDBLK::PDBLK
*
Expand Down
188 changes: 46 additions & 142 deletions src/ccutil/clst.h
Original file line number Diff line number Diff line change
Expand Up @@ -692,154 +692,58 @@ inline void CLIST_ITERATOR::add_to_end( // element to add
}
}

/***********************************************************************
QUOTE_IT MACRO DEFINITION
===========================
Replace <parm> with "<parm>". <parm> may be an arbitrary number of tokens
***********************************************************************/

#define QUOTE_IT(parm) #parm

/***********************************************************************
CLISTIZE(CLASSNAME) MACRO DEFINITION
======================================
CLASSNAME is assumed to be the name of a class to be used in a CONS list
NOTE: Because we don't use virtual functions in the list code, the list code
will NOT work correctly for classes derived from this.
template <typename CLASSNAME>
class X_CLIST : public CLIST {
public:
X_CLIST() = default;
X_CLIST(const X_CLIST &) = delete;
X_CLIST &operator=(const X_CLIST &) = delete;

The macro generates:
- An element deletion function: CLASSNAME##_c1_zapper
- An element copier function:
CLASSNAME##_c1_copier
- A CLIST subclass: CLASSNAME##_CLIST
- A CLIST_ITERATOR subclass:
CLASSNAME##_C_IT
void deep_clear() {
internal_deep_clear([](void *link) {delete static_cast<CLASSNAME *>(link);});
}
};

NOTE:
Generated names do NOT clash with those generated by ELISTIZE and ELIST2ISE.
template <typename CLASSNAME>
class X_C_IT : public CLIST_ITERATOR {
public:
X_C_IT() = default;
protected:
template <typename U>
X_C_IT(U *list) : CLIST_ITERATOR(list) {}

Two macros are provided: CLISTIZE and CLISTIZEH
The ...IZEH macros just define the class names for use in .h files
The ...IZE macros define the code use in .c files
***********************************************************************/
public:
CLASSNAME *data() {
return static_cast<CLASSNAME *>(CLIST_ITERATOR::data());
}
CLASSNAME *data_relative(int8_t offset) {
return static_cast<CLASSNAME *>(CLIST_ITERATOR::data_relative(offset));
}
CLASSNAME *forward() {
return static_cast<CLASSNAME *>(CLIST_ITERATOR::forward());
}
CLASSNAME *extract() {
return static_cast<CLASSNAME *>(CLIST_ITERATOR::extract());
}
CLASSNAME *move_to_first() {
return static_cast<CLASSNAME *>(CLIST_ITERATOR::move_to_first());
}
CLASSNAME *move_to_last() {
return static_cast<CLASSNAME *>(CLIST_ITERATOR::move_to_last());
}
};

/***********************************************************************
CLISTIZEH(CLASSNAME) MACRO
CLISTIZEH is a concatenation of 3 fragments CLISTIZEH_A, CLISTIZEH_B and
CLISTIZEH_C.
***********************************************************************/

#define CLISTIZEH_A(CLASSNAME) \
\
extern void CLASSNAME##_c1_zapper( /*delete a link*/ \
void *link); /*link to delete*/ \
\
extern void *CLASSNAME##_c1_copier( /*deep copy a link*/ \
void *old_element); /*source link */

#define CLISTIZEH_B(CLASSNAME) \
\
/*********************************************************************** \
* CLASS - \
*CLASSNAME##_CLIST \
* \
* List class for class \
*CLASSNAME \
* \
**********************************************************************/ \
\
class CLASSNAME##_CLIST : public CLIST { \
public: \
CLASSNAME##_CLIST() : CLIST() {} \
/* constructor */ \
\
CLASSNAME##_CLIST(const CLASSNAME##_CLIST &) = delete; \
void operator=(const CLASSNAME##_CLIST &) = delete; \
\
void deep_clear() /* delete elements */ \
{ \
CLIST::internal_deep_clear(&CLASSNAME##_c1_zapper); \
} \

#define CLISTIZEH_C(CLASSNAME) \
} \
; \
\
/*********************************************************************** \
* CLASS - CLASSNAME##_C_IT \
* \
* Iterator class for class CLASSNAME##_CLIST \
* \
* Note: We don't need to coerce pointers to member functions input \
* parameters as these are automatically converted to the type of the base \
* type. ("A ptr to a class may be converted to a pointer to a public base \
* class of that class") \
**********************************************************************/ \
\
class CLASSNAME##_C_IT : public CLIST_ITERATOR { \
public: \
CLASSNAME##_C_IT() : CLIST_ITERATOR() {} \
\
CLASSNAME##_C_IT(CLASSNAME##_CLIST *list) : CLIST_ITERATOR(list) {} \
\
CLASSNAME *data() { \
return static_cast<CLASSNAME *>(CLIST_ITERATOR::data()); \
} \
\
CLASSNAME *data_relative(int8_t offset) { \
return static_cast<CLASSNAME *>(CLIST_ITERATOR::data_relative(offset)); \
} \
\
CLASSNAME *forward() { \
return static_cast<CLASSNAME *>(CLIST_ITERATOR::forward()); \
} \
\
CLASSNAME *extract() { \
return static_cast<CLASSNAME *>(CLIST_ITERATOR::extract()); \
} \
\
CLASSNAME *move_to_first() { \
return static_cast<CLASSNAME *>(CLIST_ITERATOR::move_to_first()); \
} \
\
CLASSNAME *move_to_last() { \
return static_cast<CLASSNAME *>(CLIST_ITERATOR::move_to_last()); \
} \
#define CLISTIZEH(CLASSNAME) \
class CLASSNAME##_CLIST : public X_CLIST<CLASSNAME> { \
public: \
using X_CLIST<CLASSNAME>::X_CLIST; \
}; \
class CLASSNAME##_C_IT : public X_C_IT<CLASSNAME> { \
public: \
using X_C_IT<CLASSNAME>::X_C_IT; \
CLASSNAME##_C_IT(CLASSNAME##_CLIST *list) : X_C_IT(list) {} \
};

#define CLISTIZEH(CLASSNAME) \
\
CLISTIZEH_A(CLASSNAME) \
\
CLISTIZEH_B(CLASSNAME) \
\
CLISTIZEH_C(CLASSNAME)

/***********************************************************************
CLISTIZE(CLASSNAME) MACRO
***********************************************************************/

#define CLISTIZE(CLASSNAME) \
\
/*********************************************************************** \
* CLASSNAME##_c1_zapper \
* \
* A function which can delete a CLASSNAME element. This is passed to the \
* generic deep_clear list member function so that when a list is cleared \
*the \
* elements on the list are properly destroyed from the base class, even \
* though we don't use a virtual destructor function. \
**********************************************************************/ \
\
void CLASSNAME##_c1_zapper( /*delete a link*/ \
void *link) /*link to delete*/ \
{ \
delete static_cast<CLASSNAME *>(link); \
}

} // namespace tesseract

#endif
1 change: 0 additions & 1 deletion src/textord/colpartition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
namespace tesseract {

ELIST2IZE(ColPartition)
CLISTIZE(ColPartition)

//////////////// ColPartition Implementation ////////////////

Expand Down
1 change: 0 additions & 1 deletion src/textord/pitsync1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
namespace tesseract {

ELISTIZE(FPSEGPT)
CLISTIZE(FPSEGPT_LIST)

INT_VAR(pitsync_linear_version, 6, "Use new fast algorithm");
double_VAR(pitsync_joined_edge, 0.75, "Dist inside big blob for chopping");
Expand Down
1 change: 0 additions & 1 deletion src/textord/tablefind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ static BOOL_VAR(textord_tablefind_recognize_tables, false,
"Enables the table recognizer for table layout and filtering.");

ELISTIZE(ColSegment)
CLISTIZE(ColSegment)

// Templated helper function used to create destructor callbacks for the
// BBGrid::ClearGridData() method.
Expand Down
1 change: 0 additions & 1 deletion src/textord/tabvector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ void TabConstraint::GetConstraints(TabConstraint_LIST *constraints, int *y_min,
}

ELIST2IZE(TabVector)
CLISTIZE(TabVector)

// The constructor is private. See the bottom of the file...

Expand Down
2 changes: 0 additions & 2 deletions src/textord/tordmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ namespace tesseract {

#define MAX_NEAREST_DIST 600 // for block skew stats

CLISTIZE(WordWithBox)

/**********************************************************************
* SetBlobStrokeWidth
*
Expand Down
1 change: 0 additions & 1 deletion unittest/list_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class Elst2 : public ELIST2_LINK {
};

CLISTIZEH(Clst)
CLISTIZE(Clst)
ELISTIZEH(Elst)
ELISTIZE(Elst)
ELIST2IZEH(Elst2)
Expand Down

0 comments on commit 18e61d1

Please sign in to comment.