// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception #include #ifndef DISABLE_CXA_DEMANGLE #define DISABLE_CXA_DEMANGLE 1 #endif #include #include template class Thing> auto defaultThingCreator() { return [] (auto m, int i) { return std::make_shared >(i); }; } struct ThingTag {}; template struct ThingBase { virtual std::string doSomething() = 0; virtual ~ThingBase() {} }; ////////////////////////////////////////////////////////////////////// // // thing A // template struct ThingA : ThingBase { int _v; ThingA(int v) : _v(v) {} virtual std::string doSomething() { return Dune::className() + " A(" + std::to_string(_v) + ")"; } }; namespace Dune { DUNE_REGISTRY_PUT(ThingTag, "A", defaultThingCreator()); } ////////////////////////////////////////////////////////////////////// // // thing B // template struct ThingB : ThingBase { int _v; std::string arg; ThingB(int v, std::string arg_) : _v(v), arg(arg_) {} virtual std::string doSomething() { return Dune::className() + " B(" + arg + "," + std::to_string(_v) + ")"; } }; namespace Dune { DUNE_REGISTRY_PUT(ThingTag, "B", [] (auto m, int i) { return std::make_shared >(i,"dynamic"); }); } template bool check(std::string key, int v, std::string reference) { std::shared_ptr> p = Dune::registryGet(ThingTag{}, key, v); std::string s = p->doSomething(); bool r = (s == reference); std::cout << "CHECK " << s << " / " << reference << " -> " << r << std::endl; return r; } template bool checkDynamic(F& factory, std::string key, int v, std::string reference) { auto p = factory.create(key,v); std::string s = p->doSomething(); bool r = (s == reference); std::cout << "CHECK " << s << " / " << reference << " -> " << r << std::endl; return r; } ////////////////////////////////////////////////////////////////////// int main() { bool success = true; success &= check("A", 1, "i A(1)"); success &= check("B", 2, "i B(dynamic,2)"); success &= (Dune::registryGet(ThingTag{}, "C", 3) == nullptr); success &= check("A", 4, "d A(4)"); //try to build a parameterizedobjectfactory from registry Dune::ParameterizedObjectFactory>(int)> fac; Dune::addRegistryToFactory>(fac, ThingTag{}); success &= checkDynamic(fac, "A", 1, "i A(1)"); success &= checkDynamic(fac, "B", 2, "i B(dynamic,2)"); std::cout << "SUCCESS: " << success << std::endl; return success ? 0 : 1; }