Use C++-11 code instead of TessCallback for ObjectCache::Get

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2019-07-03 16:58:52 +02:00
parent 56d8210909
commit 3fb15b3891
2 changed files with 4 additions and 8 deletions

View File

@ -2,7 +2,6 @@
// File: object_cache.h // File: object_cache.h
// Description: A string indexed object cache. // Description: A string indexed object cache.
// Author: David Eger // Author: David Eger
// Created: Fri Jan 27 12:08:00 PST 2012
// //
// (C) Copyright 2012, Google Inc. // (C) Copyright 2012, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -20,10 +19,10 @@
#ifndef TESSERACT_CCUTIL_OBJECT_CACHE_H_ #ifndef TESSERACT_CCUTIL_OBJECT_CACHE_H_
#define TESSERACT_CCUTIL_OBJECT_CACHE_H_ #define TESSERACT_CCUTIL_OBJECT_CACHE_H_
#include <functional> // for std::function
#include "ccutil.h" #include "ccutil.h"
#include "errcode.h" #include "errcode.h"
#include "genericvector.h" #include "genericvector.h"
#include "tesscallback.h"
namespace tesseract { namespace tesseract {
@ -57,8 +56,7 @@ class ObjectCache {
// and return nullptr -- further attempts to load will fail (even // and return nullptr -- further attempts to load will fail (even
// with a different loader) until DeleteUnusedObjects() is called. // with a different loader) until DeleteUnusedObjects() is called.
// We delete the given loader. // We delete the given loader.
T *Get(STRING id, T* Get(STRING id, std::function<T*()> loader) {
TessResultCallback<T *> *loader) {
T *retval = nullptr; T *retval = nullptr;
mu_.Lock(); mu_.Lock();
for (int i = 0; i < cache_.size(); i++) { for (int i = 0; i < cache_.size(); i++) {
@ -68,14 +66,13 @@ class ObjectCache {
cache_[i].count++; cache_[i].count++;
} }
mu_.Unlock(); mu_.Unlock();
delete loader;
return retval; return retval;
} }
} }
cache_.push_back(ReferenceCount()); cache_.push_back(ReferenceCount());
ReferenceCount &rc = cache_.back(); ReferenceCount &rc = cache_.back();
rc.id = id; rc.id = id;
retval = rc.object = loader->Run(); retval = rc.object = loader();
rc.count = (retval != nullptr) ? 1 : 0; rc.count = (retval != nullptr) ? 1 : 0;
mu_.Unlock(); mu_.Unlock();
return retval; return retval;

View File

@ -2,7 +2,6 @@
// File: dawg_cache.cpp // File: dawg_cache.cpp
// Description: A class that knows about loading and caching dawgs. // Description: A class that knows about loading and caching dawgs.
// Author: David Eger // Author: David Eger
// Created: Fri Jan 27 12:08:00 PST 2012
// //
// (C) Copyright 2012, Google Inc. // (C) Copyright 2012, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -48,7 +47,7 @@ Dawg *DawgCache::GetSquishedDawg(const STRING &lang,
STRING data_id = data_file->GetDataFileName(); STRING data_id = data_file->GetDataFileName();
data_id += kTessdataFileSuffixes[tessdata_dawg_type]; data_id += kTessdataFileSuffixes[tessdata_dawg_type];
DawgLoader loader(lang, tessdata_dawg_type, debug_level, data_file); DawgLoader loader(lang, tessdata_dawg_type, debug_level, data_file);
return dawgs_.Get(data_id, NewTessCallback(&loader, &DawgLoader::Load)); return dawgs_.Get(data_id, std::bind(&DawgLoader::Load, &loader));
} }
Dawg *DawgLoader::Load() { Dawg *DawgLoader::Load() {