| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
#include <cassert> |
| 4 |
|
| 5 |
#ifndef DISABLE_CXA_DEMANGLE |
| 6 |
#define DISABLE_CXA_DEMANGLE 1 |
| 7 |
#endif |
| 8 |
|
| 9 |
#include <dune/common/classname.hh> |
| 10 |
#include <dune/istl/common/registry.hh> |
| 11 |
|
| 12 |
template<template<class> class Thing> |
| 13 |
auto defaultThingCreator() |
| 14 |
{ |
| 15 |
return [] (auto m, int i) { |
| 16 |
return std::make_shared<Thing<typename decltype(m)::type> >(i); |
| 17 |
}; |
| 18 |
} |
| 19 |
|
| 20 |
struct ThingTag {}; |
| 21 |
|
| 22 |
template<class V> |
| 23 |
struct ThingBase { |
| 24 |
virtual std::string doSomething() = 0; |
| 25 |
virtual ~ThingBase() {} |
| 26 |
}; |
| 27 |
|
| 28 |
////////////////////////////////////////////////////////////////////// |
| 29 |
// |
| 30 |
// thing A |
| 31 |
// |
| 32 |
|
| 33 |
template<class V> struct ThingA : ThingBase<V> { |
| 34 |
int _v; |
| 35 |
ThingA(int v) : _v(v) {} |
| 36 |
virtual std::string doSomething() { |
| 37 |
return Dune::className<V>() + " A(" + std::to_string(_v) + ")"; |
| 38 |
} |
| 39 |
}; |
| 40 |
namespace Dune { |
| 41 |
DUNE_REGISTRY_PUT(ThingTag, "A", defaultThingCreator<ThingA>()); |
| 42 |
} |
| 43 |
|
| 44 |
////////////////////////////////////////////////////////////////////// |
| 45 |
// |
| 46 |
// thing B |
| 47 |
// |
| 48 |
|
| 49 |
template<class V> struct ThingB : ThingBase<V> { |
| 50 |
int _v; |
| 51 |
std::string arg; |
| 52 |
ThingB(int v, std::string arg_) : _v(v), arg(arg_) {} |
| 53 |
virtual std::string doSomething() { |
| 54 |
return Dune::className<V>() + " B(" + arg + "," + std::to_string(_v) + ")"; |
| 55 |
} |
| 56 |
}; |
| 57 |
namespace Dune { |
| 58 |
DUNE_REGISTRY_PUT(ThingTag, "B", [] (auto m, int i) { |
| 59 |
return std::make_shared<ThingB<typename decltype(m)::type> >(i,"dynamic"); |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
template<typename T> |
| 64 |
bool check(std::string key, int v, std::string reference) |
| 65 |
{ |
| 66 |
std::shared_ptr<ThingBase<T>> p = Dune::registryGet<ThingBase, T>(ThingTag{}, key, v); |
| 67 |
std::string s = p->doSomething(); |
| 68 |
bool r = (s == reference); |
| 69 |
std::cout << "CHECK " << s |
| 70 |
<< " / " << reference |
| 71 |
<< " -> " << r << std::endl; |
| 72 |
return r; |
| 73 |
} |
| 74 |
|
| 75 |
template<typename F> |
| 76 |
bool checkDynamic(F& factory, std::string key, int v, std::string reference) |
| 77 |
{ |
| 78 |
auto p = factory.create(key,v); |
| 79 |
std::string s = p->doSomething(); |
| 80 |
bool r = (s == reference); |
| 81 |
std::cout << "CHECK " << s |
| 82 |
<< " / " << reference |
| 83 |
<< " -> " << r << std::endl; |
| 84 |
return r; |
| 85 |
} |
| 86 |
|
| 87 |
////////////////////////////////////////////////////////////////////// |
| 88 |
int main() { |
| 89 |
bool success = true; |
| 90 |
|
| 91 |
success &= check<int>("A", 1, "i A(1)"); |
| 92 |
success &= check<int>("B", 2, "i B(dynamic,2)"); |
| 93 |
success &= (Dune::registryGet<ThingBase, int>(ThingTag{}, "C", 3) == nullptr); |
| 94 |
success &= check<double>("A", 4, "d A(4)"); |
| 95 |
|
| 96 |
//try to build a parameterizedobjectfactory from registry |
| 97 |
Dune::ParameterizedObjectFactory<std::shared_ptr<ThingBase<int>>(int)> fac; |
| 98 |
Dune::addRegistryToFactory<Dune::MetaType<int>>(fac, ThingTag{}); |
| 99 |
success &= checkDynamic(fac, "A", 1, "i A(1)"); |
| 100 |
success &= checkDynamic(fac, "B", 2, "i B(dynamic,2)"); |
| 101 |
|
| 102 |
std::cout << "SUCCESS: " << success << std::endl; |
| 103 |
|
| 104 |
return success ? 0 : 1; |
| 105 |
} |