Skip to content
Snippets Groups Projects
Commit 5654e8ac authored by Christian Engwer's avatar Christian Engwer
Browse files

[test] modified extended parameterizedfactorytest

- add more use cases
- add a factory, which is managed by a singleton
- add some predefined types to factory in a separate object to make
  sure that we can properly access the same factory from different
  compilation units
- access to the factory singleton is managed by a global function
parent 316a604b
No related branches found
No related tags found
2 merge requests!212Fix link to build system doc,!3Feature/parameterizedobjectfactory
Pipeline #
......@@ -148,7 +148,8 @@ dune_add_test(NAME mpihelpertest2
dune_add_test(SOURCES overloadsettest.cc
LINK_LIBRARIES dunecommon)
dune_add_test(SOURCES parameterizedobjecttest.cc
dune_add_test(NAME parameterizedobjecttest
SOURCES parameterizedobjecttest.cc parameterizedobjectfactorysingleton.cc
LINK_LIBRARIES dunecommon
)
......
#include "config.h"
#include <iostream>
#include <cassert>
#include <tuple>
#include <dune/common/parameterizedobject.hh>
#include <dune/common/parametertree.hh>
#include <dune/common/shared_ptr.hh>
#include "parameterizedobjectfactorysingleton.hh"
DefineImplementation(InterfaceA, Aix, int);
DefineImplementation(InterfaceA, Bix, int);
int init_Factory()
{
globalPtrFactory<InterfaceA>().define<Aix>("Aix");
globalPtrFactory<InterfaceA>().define("Bix", [](int i) { return Dune::Std::make_unique<Bix>(i); });
return 0;
}
static const int init = init_Factory();
#include <dune/common/parameterizedobject.hh>
#include <dune/common/singleton.hh>
#include <string>
#define DefineImplementation(IF,T,PARAM...) \
struct T : public IF { \
T(PARAM) {} \
virtual std::string info() { \
return #T; \
} \
}
struct InterfaceA
{
virtual std::string info() = 0;
};
struct InterfaceB
{
virtual std::string info() = 0;
};
template<typename Interface>
Dune::ParameterizedObjectFactory<std::unique_ptr<Interface>(int)> &
globalPtrFactory()
{
return Dune::Singleton<Dune::ParameterizedObjectFactory<std::unique_ptr<Interface>(int)>>::instance();
}
......@@ -2,27 +2,10 @@
#include <iostream>
#include <cassert>
#include <tuple>
#include <dune/common/parameterizedobject.hh>
#include <dune/common/parametertree.hh>
#include <dune/common/shared_ptr.hh>
#define DefineImplementation(IF,T,PARAM...) \
struct T : public IF { \
T(PARAM) {} \
virtual std::string info() { \
return #T; \
} \
}
struct InterfaceA
{
virtual std::string info() = 0;
};
struct InterfaceB
{
virtual std::string info() = 0;
};
#include "parameterizedobjectfactorysingleton.hh"
DefineImplementation(InterfaceA, Ai, int);
DefineImplementation(InterfaceA, Bi, int);
......@@ -36,7 +19,6 @@ DefineImplementation(InterfaceB, Bis, int, std::string);
#define CheckInstance(F,T,PARAM...) \
assert(#T == F.create(#T,##PARAM)->info())
struct AImp : public InterfaceA
{
AImp(std::string s) :
......@@ -57,13 +39,16 @@ struct AImp : public InterfaceA
int main()
{
// int as parameter
Dune::ParameterizedObjectFactory<std::unique_ptr<InterfaceA>(int)> FactoryA;
FactoryA.define<Ai>("Ai");
FactoryA.define<Bi>("Bi");
FactoryA.define("Ax", [](int i) { return Dune::Std::make_unique<Ax>(); });
CheckInstance(FactoryA, Ai, 0);
CheckInstance(FactoryA, Bi, 1);
CheckInstance(FactoryA, Ax, 1);
// Dune::ParameterizedObjectFactory<std::unique_ptr<InterfaceA>(int)> FactoryA;
globalPtrFactory<InterfaceA>().define<Ai>("Ai");
globalPtrFactory<InterfaceA>().define<Bi>("Bi");
globalPtrFactory<InterfaceA>().define("Ax", [](int i) { return Dune::Std::make_unique<Ax>(); });
CheckInstance(globalPtrFactory<InterfaceA>(), Ai, 0);
CheckInstance(globalPtrFactory<InterfaceA>(), Bi, 1);
CheckInstance(globalPtrFactory<InterfaceA>(), Ax, 1);
// int as parameter for external factory
CheckInstance(globalPtrFactory<InterfaceA>(), Aix, 0);
CheckInstance(globalPtrFactory<InterfaceA>(), Bix, 1);
// default constructor
Dune::ParameterizedObjectFactory<std::shared_ptr<InterfaceA>()> FactoryAd;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment