diff --git a/src/ccmain/tessedit.cpp b/src/ccmain/tessedit.cpp index 17f0951b..d06151a9 100644 --- a/src/ccmain/tessedit.cpp +++ b/src/ccmain/tessedit.cpp @@ -430,8 +430,9 @@ void Tesseract::SetupUniversalFontIds() { // Note that we can get away with bitwise copying FontInfo in // all_fonts, as it is a temporary structure and we avoid setting the // delete callback. + using namespace std::placeholders; // for _1, _2 UnicityTable all_fonts; - all_fonts.set_compare_callback(NewPermanentTessCallback(CompareFontInfo)); + all_fonts.set_compare_callback(std::bind(CompareFontInfo, _1, _2)); // Create the universal ID table. CollectFonts(get_fontinfo_table(), &all_fonts); diff --git a/src/ccstruct/fontinfo.cpp b/src/ccstruct/fontinfo.cpp index facdbea4..845f4429 100644 --- a/src/ccstruct/fontinfo.cpp +++ b/src/ccstruct/fontinfo.cpp @@ -37,8 +37,8 @@ bool FontInfo::DeSerialize(TFile* fp) { } FontInfoTable::FontInfoTable() { - using namespace std::placeholders; // for _1 - set_compare_callback(NewPermanentTessCallback(CompareFontInfo)); + using namespace std::placeholders; // for _1, _2 + set_compare_callback(std::bind(CompareFontInfo, _1, _2)); set_clear_callback(std::bind(FontInfoDeleteCallback, _1)); } @@ -83,8 +83,8 @@ bool FontInfoTable::SetContainsMultipleFontProperties( // Moves any non-empty FontSpacingInfo entries from other to this. void FontInfoTable::MoveSpacingInfoFrom(FontInfoTable* other) { - set_compare_callback(NewPermanentTessCallback(CompareFontInfo)); - using namespace std::placeholders; // for _1 + using namespace std::placeholders; // for _1, _2 + set_compare_callback(std::bind(CompareFontInfo, _1, _2)); set_clear_callback(std::bind(FontInfoDeleteCallback, _1)); for (int i = 0; i < other->size(); ++i) { GenericVector* spacing_vec = other->get(i).spacing_vec; @@ -106,8 +106,8 @@ void FontInfoTable::MoveSpacingInfoFrom(FontInfoTable* other) { // Moves this to the target unicity table. void FontInfoTable::MoveTo(UnicityTable* target) { target->clear(); - target->set_compare_callback(NewPermanentTessCallback(CompareFontInfo)); - using namespace std::placeholders; // for _1 + using namespace std::placeholders; // for _1, _2 + target->set_compare_callback(std::bind(CompareFontInfo, _1, _2)); target->set_clear_callback(std::bind(FontInfoDeleteCallback, _1)); for (int i = 0; i < size(); ++i) { // Bit copy the FontInfo and steal all the pointers. diff --git a/src/ccutil/genericvector.h b/src/ccutil/genericvector.h index b5ab6429..9f6c1b84 100644 --- a/src/ccutil/genericvector.h +++ b/src/ccutil/genericvector.h @@ -147,7 +147,7 @@ class GenericVector { // Add a callback to be called to compare the elements when needed (contains, // get_id, ...) - void set_compare_callback(TessResultCallback2* cb) { + void set_compare_callback(std::function cb) { compare_cb_ = cb; } @@ -336,8 +336,7 @@ class GenericVector { int32_t size_reserved_{}; T* data_; std::function clear_cb_; - // Mutable because Run method is not const - mutable TessResultCallback2* compare_cb_; + std::function compare_cb_; }; namespace tesseract { @@ -669,12 +668,14 @@ template class GenericVectorEqEq : public GenericVector { public: GenericVectorEqEq() { + using namespace std::placeholders; // for _1 GenericVector::set_compare_callback( - NewPermanentTessCallback(tesseract::cmp_eq)); + std::bind(tesseract::cmp_eq, _1, _2)); } explicit GenericVectorEqEq(int size) : GenericVector(size) { + using namespace std::placeholders; // for _1 GenericVector::set_compare_callback( - NewPermanentTessCallback(tesseract::cmp_eq)); + std::bind(tesseract::cmp_eq, _1, _2)); } }; @@ -808,7 +809,7 @@ template int GenericVector::get_index(const T& object) const { for (int i = 0; i < size_used_; ++i) { assert(compare_cb_ != nullptr); - if (compare_cb_->Run(object, data_[i])) { + if (compare_cb_(object, data_[i])) { return i; } } @@ -892,7 +893,6 @@ void GenericVector::clear() { size_used_ = 0; size_reserved_ = 0; clear_cb_ = nullptr; - delete compare_cb_; compare_cb_ = nullptr; } diff --git a/src/ccutil/unicity_table.h b/src/ccutil/unicity_table.h index 27f614f6..bd6b253a 100644 --- a/src/ccutil/unicity_table.h +++ b/src/ccutil/unicity_table.h @@ -71,7 +71,10 @@ class UnicityTable { /// Add a callback to be called to compare the elements when needed (contains, /// get_id, ...) - void set_compare_callback(TessResultCallback2* cb); + void set_compare_callback(std::function cb) { + table_.set_compare_callback(cb); + compare_cb_ = cb; + } /// Clear the table, calling the callback function if any. /// All the owned Callbacks are also deleted. @@ -93,16 +96,16 @@ class UnicityTable { private: GenericVector table_; - // Mutable because Run method is not const - mutable TessResultCallback2* compare_cb_; + std::function compare_cb_; }; template class UnicityTableEqEq : public UnicityTable { public: UnicityTableEqEq() { + using namespace std::placeholders; // for _1, _2 UnicityTable::set_compare_callback( - NewPermanentTessCallback(tesseract::cmp_eq)); + std::bind(tesseract::cmp_eq, _1, _2)); } }; @@ -167,14 +170,6 @@ int UnicityTable::push_back(T object) { return idx; } -// Add a callback to be called to delete the elements when the table took -// their ownership. -template -void UnicityTable::set_compare_callback(TessResultCallback2* cb) { - table_.set_compare_callback(cb); - compare_cb_ = cb; -} - // Clear the table, calling the callback function if any. template void UnicityTable::clear() { diff --git a/src/classify/classify.cpp b/src/classify/classify.cpp index 04f66b6f..9363ec81 100644 --- a/src/classify/classify.cpp +++ b/src/classify/classify.cpp @@ -186,12 +186,10 @@ Classify::Classify() shape_table_(nullptr), dict_(this), static_classifier_(nullptr) { - using namespace std::placeholders; // for _1 - fontinfo_table_.set_compare_callback( - NewPermanentTessCallback(CompareFontInfo)); + using namespace std::placeholders; // for _1, _2 + fontinfo_table_.set_compare_callback(std::bind(CompareFontInfo, _1, _2)); fontinfo_table_.set_clear_callback(std::bind(FontInfoDeleteCallback, _1)); - fontset_table_.set_compare_callback( - NewPermanentTessCallback(CompareFontSet)); + fontset_table_.set_compare_callback(std::bind(CompareFontSet, _1, _2)); fontset_table_.set_clear_callback(std::bind(FontSetDeleteCallback, _1)); AdaptedTemplates = nullptr; BackupAdaptedTemplates = nullptr;