diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2021-01-25 19:17:01 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2021-01-25 19:18:24 +0000 |
commit | 4912032fc031820b9ac0ed290f38033714d3cd67 (patch) | |
tree | d1690b61652953a68781c1658d8bdc8a64bfb3c4 | |
parent | 8f2798309ee0bff2a2747b982bb509dab45a4a4f (diff) |
Use lambda over closure.
7 files changed, 57 insertions, 34 deletions
diff --git a/document/src/tests/repo/documenttyperepo_test.cpp b/document/src/tests/repo/documenttyperepo_test.cpp index 0bc80ebcd16..7d17a3cfa11 100644 --- a/document/src/tests/repo/documenttyperepo_test.cpp +++ b/document/src/tests/repo/documenttyperepo_test.cpp @@ -392,10 +392,6 @@ TEST("requireThatDocumentsCanUseOtherDocumentTypes") { EXPECT_TRUE(dynamic_cast<const DocumentType *>(&type)); } -void storeId(set<int> *s, const DocumentType &type) { - s->insert(type.getId()); -} - TEST("requireThatDocumentTypesCanBeIterated") { DocumenttypesConfigBuilderHelper builder; builder.document(doc_type_id, type_name, @@ -405,7 +401,8 @@ TEST("requireThatDocumentTypesCanBeIterated") { DocumentTypeRepo repo(builder.config()); set<int> ids; - repo.forEachDocumentType(*makeClosure(storeId, &ids)); + repo.forEachDocumentType(*DocumentTypeRepo::makeLambda( + [&ids](const DocumentType &type) { ids.insert(type.getId()); })); EXPECT_EQUAL(3u, ids.size()); ASSERT_TRUE(ids.count(DataType::T_DOCUMENT)); @@ -436,8 +433,7 @@ TEST("requireThatBuildFromConfigWorks") { TEST("requireThatStructsCanBeRecursive") { DocumenttypesConfigBuilderHelper builder; builder.document(doc_type_id, type_name, - Struct(header_name).setId(header_id).addField(field_name, - header_id), + Struct(header_name).setId(header_id).addField(field_name, header_id), Struct(body_name)); DocumentTypeRepo repo(builder.config()); diff --git a/document/src/vespa/document/fieldset/fieldsetrepo.cpp b/document/src/vespa/document/fieldset/fieldsetrepo.cpp index bf9c9923572..f16387810ce 100644 --- a/document/src/vespa/document/fieldset/fieldsetrepo.cpp +++ b/document/src/vespa/document/fieldset/fieldsetrepo.cpp @@ -119,7 +119,9 @@ FieldSetRepo::FieldSetRepo(const DocumentTypeRepo& repo) : _doumentTyperepo(repo), _configuredFieldSets() { - repo.forEachDocumentType(*vespalib::makeClosure(this, &FieldSetRepo::configureDocumentType)); + repo.forEachDocumentType(*DocumentTypeRepo::makeLambda([&](const DocumentType &type) { + configureDocumentType(type); + })); } FieldSetRepo::~FieldSetRepo() = default; diff --git a/document/src/vespa/document/repo/documenttyperepo.cpp b/document/src/vespa/document/repo/documenttyperepo.cpp index 578d2999038..50ab6aaa646 100644 --- a/document/src/vespa/document/repo/documenttyperepo.cpp +++ b/document/src/vespa/document/repo/documenttyperepo.cpp @@ -587,9 +587,9 @@ DocumentTypeRepo::getAnnotationType(const DocumentType &doc_type, int32_t id) co } void -DocumentTypeRepo::forEachDocumentType(Closure1<const DocumentType &> &c) const { +DocumentTypeRepo::forEachDocumentType(Handler & handler) const { for (const auto & entry : *_doc_types) { - c.call(*entry.second->doc_type); + handler.handle(*entry.second->doc_type); } } diff --git a/document/src/vespa/document/repo/documenttyperepo.h b/document/src/vespa/document/repo/documenttyperepo.h index fd17bd5640a..4e3a1b07619 100644 --- a/document/src/vespa/document/repo/documenttyperepo.h +++ b/document/src/vespa/document/repo/documenttyperepo.h @@ -19,11 +19,21 @@ struct DataTypeRepo; class DocumentType; class DocumentTypeRepo { - std::unique_ptr<internal::DocumentTypeMap> _doc_types; - const DocumentType * _default; - public: using DocumenttypesConfig = const internal::InternalDocumenttypesType; + struct Handler { + virtual ~Handler() = default; + virtual void handle(const DocumentType & type) = 0; + }; + + + template <class FunctionType> + static std::unique_ptr<Handler> + makeLambda(FunctionType &&function) + { + return std::make_unique<LambdaHandler<std::decay_t<FunctionType>>> + (std::forward<FunctionType>(function)); + } // This one should only be used for testing. If you do not have any config. explicit DocumentTypeRepo(const DocumentType & docType); @@ -39,8 +49,22 @@ public: const DataType *getDataType(const DocumentType &doc_type, int32_t id) const; const DataType *getDataType(const DocumentType &doc_type, vespalib::stringref name) const; const AnnotationType *getAnnotationType(const DocumentType &doc_type, int32_t id) const; - void forEachDocumentType(vespalib::Closure1<const DocumentType &> &c) const; + void forEachDocumentType(Handler & handler) const; const DocumentType *getDefaultDocType() const { return _default; } +private: + template <class FunctionType> + class LambdaHandler : public Handler { + FunctionType _func; + public: + LambdaHandler(FunctionType &&func) : _func(std::move(func)) {} + LambdaHandler(const LambdaHandler &) = delete; + LambdaHandler & operator = (const LambdaHandler &) = delete; + ~LambdaHandler() override = default; + void handle(const DocumentType & type) override { _func(type); } + }; + + std::unique_ptr<internal::DocumentTypeMap> _doc_types; + const DocumentType * _default; }; } // namespace document diff --git a/document/src/vespa/document/select/bodyfielddetector.cpp b/document/src/vespa/document/select/bodyfielddetector.cpp index 3d32813621d..d1961810c7a 100644 --- a/document/src/vespa/document/select/bodyfielddetector.cpp +++ b/document/src/vespa/document/select/bodyfielddetector.cpp @@ -28,7 +28,9 @@ BodyFieldDetector::detectFieldType(const FieldValueNode *expr, const DocumentTyp void BodyFieldDetector::visitFieldValueNode(const FieldValueNode& expr) { - _repo.forEachDocumentType(*makeClosure(this, &BodyFieldDetector::detectFieldType, &expr)); + _repo.forEachDocumentType(*DocumentTypeRepo::makeLambda([&](const DocumentType &type) { + detectFieldType(&expr, type); + })); } diff --git a/searchcore/src/apps/tests/persistenceconformance_test.cpp b/searchcore/src/apps/tests/persistenceconformance_test.cpp index 6b4061081ea..4715ff80d03 100644 --- a/searchcore/src/apps/tests/persistenceconformance_test.cpp +++ b/searchcore/src/apps/tests/persistenceconformance_test.cpp @@ -109,12 +109,13 @@ public: DocumenttypesConfigSP getTypeCfg() const { return _typeCfg; } DocTypeVector getDocTypes() const { DocTypeVector types; - _repo->forEachDocumentType(*makeClosure(storeDocType, &types)); + _repo->forEachDocumentType(*DocumentTypeRepo::makeLambda([&types](const DocumentType &type) { + types.push_back(DocTypeName(type.getName())); + })); return types; } DocumentDBConfig::SP create(const DocTypeName &docTypeName) const { - const DocumentType *docType = - _repo->getDocumentType(docTypeName.getName()); + const DocumentType *docType = _repo->getDocumentType(docTypeName.getName()); if (docType == nullptr) { return DocumentDBConfig::SP(); } diff --git a/searchcore/src/tests/proton/documentdb/fileconfigmanager/fileconfigmanager_test.cpp b/searchcore/src/tests/proton/documentdb/fileconfigmanager/fileconfigmanager_test.cpp index 2352fda65a0..e6bcbf18495 100644 --- a/searchcore/src/tests/proton/documentdb/fileconfigmanager/fileconfigmanager_test.cpp +++ b/searchcore/src/tests/proton/documentdb/fileconfigmanager/fileconfigmanager_test.cpp @@ -43,12 +43,12 @@ makeBaseConfigSnapshot() DBCM dbcm(spec, "test"); DocumenttypesConfigSP dtcfg(config::ConfigGetter<DocumenttypesConfig>::getConfig("", spec).release()); - BootstrapConfig::SP b(new BootstrapConfig(1, dtcfg, - std::shared_ptr<const DocumentTypeRepo>(new DocumentTypeRepo(*dtcfg)), - std::make_shared<ProtonConfig>(), - std::make_shared<FiledistributorrpcConfig>(), - std::make_shared<BucketspacesConfig>(), - std::make_shared<TuneFileDocumentDB>(), HwInfo())); + auto b = std::make_shared<BootstrapConfig>(1, dtcfg, + std::make_shared<DocumentTypeRepo>(*dtcfg), + std::make_shared<ProtonConfig>(), + std::make_shared<FiledistributorrpcConfig>(), + std::make_shared<BucketspacesConfig>(), + std::make_shared<TuneFileDocumentDB>(), HwInfo()); dbcm.forwardConfig(b); dbcm.nextGeneration(0ms); DocumentDBConfig::SP snap = dbcm.getConfig(); @@ -71,8 +71,6 @@ makeEmptyConfigSnapshot() return test::DocumentDBConfigBuilder(0, std::make_shared<Schema>(), "client", "test").build(); } -void incInt(int *i, const DocumentType&) { ++*i; } - void assertEqualSnapshot(const DocumentDBConfig &exp, const DocumentDBConfig &act) { @@ -91,10 +89,12 @@ assertEqualSnapshot(const DocumentDBConfig &exp, const DocumentDBConfig &act) int expTypeCount = 0; int actTypeCount = 0; - exp.getDocumentTypeRepoSP()->forEachDocumentType( - *vespalib::makeClosure(incInt, &expTypeCount)); - act.getDocumentTypeRepoSP()->forEachDocumentType( - *vespalib::makeClosure(incInt, &actTypeCount)); + exp.getDocumentTypeRepoSP()->forEachDocumentType(*DocumentTypeRepo::makeLambda([&expTypeCount](const DocumentType &) { + expTypeCount++; + })); + act.getDocumentTypeRepoSP()->forEachDocumentType(*DocumentTypeRepo::makeLambda([&actTypeCount](const DocumentType &) { + actTypeCount++; + })); EXPECT_EQUAL(expTypeCount, actTypeCount); EXPECT_TRUE(*exp.getSchemaSP() == *act.getSchemaSP()); EXPECT_EQUAL(expTypeCount, actTypeCount); @@ -164,8 +164,7 @@ TEST_F("requireThatConfigCanBeLoadedWithoutExtraConfigsDataFile", DocumentDBConf } -TEST_F("requireThatVisibilityDelayIsPropagated", - DocumentDBConfig::SP(makeBaseConfigSnapshot())) +TEST_F("requireThatVisibilityDelayIsPropagated", DocumentDBConfig::SP(makeBaseConfigSnapshot())) { saveBaseConfigSnapshot(*f, 80); DocumentDBConfig::SP esnap(makeEmptyConfigSnapshot()); @@ -177,8 +176,7 @@ TEST_F("requireThatVisibilityDelayIsPropagated", protonConfigBuilder.documentdb.push_back(ddb); protonConfigBuilder.maxvisibilitydelay = 100.0; FileConfigManager cm("out", myId, "dummy"); - using ProtonConfigSP = BootstrapConfig::ProtonConfigSP; - cm.setProtonConfig(ProtonConfigSP(new ProtonConfig(protonConfigBuilder))); + cm.setProtonConfig(std::make_shared<ProtonConfig>(protonConfigBuilder)); cm.loadConfig(*esnap, 70, esnap); } EXPECT_EQUAL(61s, esnap->getMaintenanceConfigSP()->getVisibilityDelay()); |