C++ Perfect Generic Abstract Factory with arbitrary Constructor Arguments - c++

for unit testing, I'm trying to create a Factory which meets the following requirements:
(1) It can create arbitrary objects (e.g. Timer and TimerMock)
(2) It returns unique_ptrs to a Base class to these objects (e.g. unique_ptr<TimerInterface>)
(3) The Factory itself can be passed around as a base class pointer too
(4) It shall be possible to call any constructor to create the Object [edit1]using the same factory object[/edit1]
The purpose is to do dependency injection with this factories to be able to exchange objects that are not part of the test with mock objects.
Here's what I have so far:
#include <memory>
#include <iostream>
//"perfect factory method"
//usage: std::unique_ptr<Type> pt(create<Type>(Type-constructor-arguments));
template <typename Type, typename ... ConstructorArgs>
auto create(ConstructorArgs&& ... args){
return std::make_unique<Type>(std::forward<ConstructorArgs>(args)...);
}
//Abstract Factory Base class
template<typename BaseType, typename ... ConstructorArgs>
class IFactory {
public:
virtual ~IFactory() = default;
virtual std::unique_ptr<BaseType> create(ConstructorArgs&& ... args) const = 0;
};
//Abstract Factory class
template <typename BaseType, typename DerivedType, typename ... ConstructorArgs>
class CFactory : public IFactory<BaseType, ConstructorArgs...>
{
public:
using Base = IFactory<BaseType, ConstructorArgs...>;
std::unique_ptr<BaseType> create(ConstructorArgs&& ... args) const override
{
return ::create<DerivedType>(std::forward<ConstructorArgs>(args)...);
}
};
How the real Factory classes are defined:
class TimerInterface {
public:
TimerInterface() = default;
TimerInterface (const char* name);
virtual void whoami() const = 0;
/*...*/
};
class Timer: public TimerInterface {
public:
Timer() = default;
Timer(const char* name) : TimerInterface (name) {}
void whoami() const override { std::cerr << "I'm real!" << std::endl; }
/*...*/
};
class TimerMock : public TimerInterface {
public:
TimerMock () = default;
TimerMock (const char* name) : TimerInterface (name) {}
void whoami() const override { std::cerr << "I fake it!" << std::endl; }
/*...*/
};
using TimerFactory = CFactory<TimerInterface, Timer, const char*>;
using TimerMockFactory = CFactory<TimerInterface, TimerMock, const char*>;
using TimerFactoryInterface = TimerFactory::Base;
And how they are intended to be used:
class ClassUnderTest {
public:
std::unique_ptr<TimerInterface> timer {};
std::unique_ptr<TimerInterface> timer2 {};
ClassUnderTest(const TimerFactoryInterface& factory)
: timer(factory.create("I got a name!"))
//, timer2(factory.create())
{}
};
class Production
{
public:
ClassUnderTest realUsage;
Production() :
realUsage(TimerFactory())
{}
};
class Test
{
public:
ClassUnderTest tested;
Test() :
tested(TimerMockFactory())
{}
};
int main()
{
Production p;
p.realUsage.timer->whoami();
Test t;
t.tested.timer->whoami();
}
My big problem is requirement (4)
ClassUnderTest::timer2 can't be created with the same factory that's used for ClassUnderTest::timer, beause the constructor signatur already needs to be known when defining the CFactory class.
Anyone got an idea?
P.S.: "It can't be done" with an explanation is also an acceptable answer, but not my favourite ;)

Not sure if it helps, but TimerInterface doesn't need to be a complete type when you declare your two std::unique_ptr<TimerInterface> inside ClassUnderTest. Meaning this is legal:
// classundertest.hpp
// A forward-declaration is enough
class TimerInterface;
class TimerFactoryInterface;
class ClassUnderTest {
public:
std::unique_ptr<TimerInterface> timer {};
std::unique_ptr<TimerInterface> timer2 {};
// Both MUST be declared here AND defined in the .cpp for this trick to work
ClassUnderTest(const TimerFactoryInterface& factory);
~ClassUnderTest();
// classundertest.cpp
#include "classundertest.hpp"
// TimerInterface wasn't truly declared until now
#include "TimerInterface.hpp"
ClassUnderTest::ClassUnderTest(const TimerFactoryInterface& factory)
: timer(factory.create("I got a name!"))
, timer2(factory.create("???"))
{}
ClassUnderTest::~ClassUnderTest()
{}
That's basically how unique_ptr-based pimpl work.

You can do it with a std::vector<std::any>, and some index sequences, but it isn't going to be the most pretty or fast solution.
Your factory would be:
template<typename Base>
class IFactory
{
public:
virtual std::unique_ptr<Base> Create(const std::vector<std::any>& args) const = 0;
};
Your concrete factory could then be:
template<typename Base, typename Derived, typename... Args>
class CFactory : IFactory<Base>
{
private:
template<size_t... Indices>
std::unique_ptr<Base> Make(const std::vector<std::any>& args, std::index_sequence<Indices...>) const
{
return std::make_unique<Derived>(std::any_cast<Args>(args[Indices])...);
}
public:
virtual std::unique_ptr<Base> Create(const std::vector<std::any>& args) const
{
return Make(args, std::make_index_sequence<sizeof...(Args)>());
}
};
The only problem here is that you obviously cannot perfectly forward any arguments, you will always end up taking a copy.
It will also throw an std::bad_any_cast if the types passed into the std::any do not match the template.
Here's a simple test I made, and it compiles with both MSVC and Clang with -std=c++17 set:
class A
{
public:
A(int a, int b)
: Val1(a), Val2(b)
{}
A(int a)
: Val1(a), Val2(0)
{}
int Val1, Val2;
};
int main()
{
CFactory<A, A, int> fac1;
CFactory<A, A, int, int> fac2;
auto a1 = fac1.Create({ std::any(10) });
auto a2 = fac2.Create({ std::any(10), std::any(20) });
std::cout << a1->Val1 << " " << a1->Val2 << "\n";
std::cout << a2->Val1 << " " << a2->Val2 << "\n";
}
Edit: This will work for overloaded constructors, as well as any type because its template magic.

#SparkyPotato gave me the idea when stating I'd "have to manually list out the possible constructors and generate overloads for create". I just disliked the manually, so I did it by template meta programming. Thanks for the hint!
#include <memory>
#include <iostream>
#include <tuple>
//"perfect factory method"
//usage: std::unique_ptr<Type> pt(create<Type>(Type-constructor-arguments));
template <typename Type, typename ... ConstructorArgs>
auto create(ConstructorArgs&& ... args){
std::cerr << "calling " << __PRETTY_FUNCTION__ << std::endl;
return std::make_unique<Type>(std::forward<ConstructorArgs>(args)...);
}
//Abstract Factory Variadic class. This is also generic case that ends recursion
template <typename BaseType, typename TupleListOfConstructorArgs>
class IFactory
{
static_assert(std::is_same<TupleListOfConstructorArgs, std::tuple<>>::value, "");
public:
//this method shall never be instatiated, it just exists to satisfy the "using BaseFactory::create" in IFactory
template <typename T> void create(){ static_assert(sizeof(BaseType) + sizeof(T) < 0, ""); }
virtual ~IFactory() = default;
};
//Abstract Factory Variadic class specialization to perform inheritance recursion
template <typename BaseType, typename ... CurrentTupleArgs, typename ... TupleListOfConstructorArgs>
class IFactory<BaseType, std::tuple<std::tuple<CurrentTupleArgs...>, TupleListOfConstructorArgs...>> : public IFactory<BaseType, std::tuple<TupleListOfConstructorArgs...>>
{
public:
using BaseFactory = IFactory<BaseType, std::tuple<TupleListOfConstructorArgs...>>;
using BaseFactory::create;
virtual std::unique_ptr<BaseType> create(const CurrentTupleArgs& ... args) const = 0;
};
//Concrete Factory Variadic class. This is also generic case that ends inheritance recursion
template <typename BaseType, typename DerivedType, typename TupleListOfConstructorArgs, typename FullTupleListOfConstructorArgs>
class CFactory : public IFactory<BaseType, FullTupleListOfConstructorArgs>
{
static_assert(std::is_same<TupleListOfConstructorArgs, std::tuple<>>::value, "");
public:
using Base = IFactory<BaseType, FullTupleListOfConstructorArgs>;
};
//Concrete Factory Variadic class specialization to perform inheritance recursion
template <typename BaseType, typename DerivedType, typename ... CurrentTupleArgs, typename ... TupleListOfConstructorArgs, typename FullTupleListOfConstructorArgs>
class CFactory<BaseType, DerivedType, std::tuple<std::tuple<CurrentTupleArgs...>, TupleListOfConstructorArgs...>, FullTupleListOfConstructorArgs> : public CFactory<BaseType, DerivedType, std::tuple<TupleListOfConstructorArgs...>, FullTupleListOfConstructorArgs>
{
public:
using BaseFactory = CFactory<BaseType, DerivedType, std::tuple<TupleListOfConstructorArgs...>, FullTupleListOfConstructorArgs>;
using BaseFactory::create;
std::unique_ptr<BaseType> create(const CurrentTupleArgs& ... args) const override
{
std::cerr << "calling " << __PRETTY_FUNCTION__ << std::endl;
return ::create<DerivedType>(args...);
}
};
template <typename BaseType, typename DerivedType, typename TupleListOfConstructorArgs>
using CFactoryFrontend = CFactory<BaseType, DerivedType, TupleListOfConstructorArgs, TupleListOfConstructorArgs>;
class TimerInterface {
public:
TimerInterface() = default;
virtual ~TimerInterface() = default;
TimerInterface (const char* name) {}
TimerInterface(int& x, const char* name) { std::cerr << "calling " << __PRETTY_FUNCTION__ << std::endl; }
TimerInterface(const int& x, const char* name) { std::cerr << "calling " << __PRETTY_FUNCTION__ << std::endl; }
virtual void whoami() const = 0;
/*...*/
};
class Timer: public TimerInterface {
public:
Timer() = default;
Timer(const char* name) : TimerInterface (name) {}
Timer(int& x, const char* name) : TimerInterface(x, name) {}
Timer(const int& x, const char* name) : TimerInterface(x, name) {}
void whoami() const override { std::cerr << "I'm real!" << std::endl; }
/*...*/
};
class TimerMock : public TimerInterface {
public:
TimerMock () = default;
TimerMock (const char* name) : TimerInterface (name) {}
TimerMock (int& x, const char* name) : TimerInterface(x, name) {}
TimerMock (const int& x, const char* name) : TimerInterface(x, name) {}
void whoami() const override { std::cerr << "I fake it!" << std::endl; }
/*...*/
};
//using TimerInterfaceConstructors = std::tuple<std::tuple<>, std::tuple<const char*>>;
using Constructors = std::tuple<std::tuple<>, std::tuple<const char*>, std::tuple<int&, const char*>, std::tuple<const int&, const char*>>;
using TestFactory = CFactoryFrontend<TimerInterface, Timer, Constructors>;
using TimerFactory = CFactoryFrontend<TimerInterface, Timer, Constructors>;
using TimerMockFactory = CFactoryFrontend<TimerInterface, TimerMock, Constructors>;
using TimerFactoryInterface = TimerFactory::Base;
class ClassUnderTest {
public:
std::unique_ptr<TimerInterface> timer {};
std::unique_ptr<TimerInterface> timer2 {};
ClassUnderTest(const TimerFactoryInterface& factory)
: timer(factory.create("I got a name!"))
, timer2(factory.create())
{}
};
class Production
{
public:
ClassUnderTest realUsage;
Production() :
realUsage(TimerFactory())
{}
};
class Test
{
public:
ClassUnderTest tested;
Test() :
tested(TimerMockFactory())
{}
};
int main()
{
Production p;
p.realUsage.timer->whoami();
Test t;
t.tested.timer->whoami();
TestFactory tf;
TimerFactoryInterface& tfi(tf);
const char* x = "X";
tfi.create();
tfi.create(x);
int y;
const int cy = 17;
tfi.create(y, x);
tfi.create(cy, "name");
::create<Timer>(x);
}
It compiles using GCC-6 and newer and clang with -std=c++-14

Related

Unforgettable Factory and multiple levels of inheritance and hierachy

I've been experimenting with the factory implementation described here:
http://www.nirfriedman.com/2018/04/29/unforgettable-factory/
The example uses "Animal" as a base and "Dog" and "Cat" as the derived classes that are registered with the factory. But now suppose I wanted to add a "Lion" class that derives from Cat... how can I derive from Cat while still registering with the Animal factory? Further, I would like to be able to create a "Cat" factory as well that would let me create object of type "Cat" instead of "Animal". Can this example be adapted to do this?
Here is some code based off the original post that shows what I'm trying to do.
#include <functional>
#include <memory>
#include <exception>
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
#include <cstdlib>
#include <cxxabi.h>
template<typename BaseT, typename ...Args>
class Factory
{
public:
friend BaseT;
template<class ...T>
static std::shared_ptr<BaseT> create(const std::string& name, T&&... args)
{
try
{
return types().at(name)(std::forward<T>(args)...);
}
catch(std::out_of_range e)
{
throw std::runtime_error("type \"" + name + "\" is not registered with the factory");
};
};
template<typename T>
struct Registrar : BaseT
{
public:
friend T;
static bool register_type()
{
auto demangle = [](const char* name) -> std::string
{
int status = -4;
std::unique_ptr<char, void (*)(void *)> res{abi::__cxa_demangle(name, NULL, NULL, &status), free};
return (status == 0) ? res.get() : name;
};
const auto name = demangle(typeid(T).name());
std::cout << "registering " << name << "\n";
Factory::types()[name] = [](Args... args) -> std::shared_ptr<BaseT>
{
return std::make_shared<T>(std::forward<Args>(args)...);
};
return true;
};
static inline bool registered = register_type();
private:
Registrar() : BaseT() { (void) registered; };
};
private:
using FunctionT = std::function< std::shared_ptr<BaseT>(Args...)>;
static auto& types()
{
static std::unordered_map<std::string, FunctionT> m_types;
return m_types;
};
};
struct Animal : Factory<Animal>
{
virtual void speak() = 0;
virtual ~Animal() = default;
};
// How can I create a Cat factory at this level in the hierarchy?
class Cat : public Animal::Registrar<Cat>
{
public:
Cat() {};
virtual ~Cat() = default;
virtual void speak() { std::cout << "Meow!" << "\n"; };
};
// I would like to register Lion with the Animal factory.
class Lion : public Cat
{
public:
Lion() {};
void speak() { std::cout << "Roar!" << "\n"; };
};
int main(int argc, char** argv)
{
auto a = Animal::create("Cat");
a->speak();
auto lion = Cat::create("Lion"); // this doesn't work
lion->speak();
return 0;
};
I ended up tweaking the factory class slightly by remove inheritance from Base in the Registrar class. I changed this:
template <class T> struct Registrar : Base {
to this:
template <class T> struct Registrar
Going back to my example above I now use the factory like so:
struct Animal
{
virtual void speak() = 0;
virtual ~Animal() = default;
};
using AnimalFactory = Factory<Animal>;
struct Cat : public Animal, public AnimalFactory::Registrar<Cat>
{
<snip>
};
struct Lion : public Cat, public AnimalFactory::Registrar<Lion>
{
<snip>
};
The main difference is that now each class has to derive from an Animal class or sub-class as well as the AnimalFactory::Registrar type. It's a little more verbose but I now have the ability to use the factory with multiple levels of inheritance.

How to pass other template parameter when template class uses parameter pack?

I would like to create template class that implements print() method for each type passed as template parameters.
Something like that:
class Interface
{
public:
virtual ~Interface() = default;
virtual void print(int) = 0;
virtual void print(double) = 0;
};
X x<int, double, Interface>;
class X has public method void print() and it works.
The whole code below:
#include <iostream>
#include <type_traits>
struct Printer
{
void print(int i) {std::cout << i << std::endl; }
void print(double d) {std::cout << d << std::endl; }
};
class Interface
{
public:
virtual ~Interface() = default;
virtual void print(int) = 0;
virtual void print(double) = 0;
};
template <typename... Args>
class X;
template <typename Interface>
class X<Interface> : public Interface
{
static_assert(std::is_abstract<Interface>::value, "Last argument should be an interface");
public:
X(Printer printer) {}
using Interface::print;
};
template <typename Arg, typename... Args>
class X<Arg, Args...> : public X<Args...>
{
using Parent = X<Args...>;
public:
using Parent::print;
X(Printer printer_): Parent(printer), printer{printer_} {}
void print(Arg arg) override { printer.print(arg); }
private:
Printer printer;
};
int main()
{
Printer printer;
X<double, int, Interface> x(printer);
x.print(5);
}
As you see class X uses Printer class but the problem is that I would like to have Printer as a template parameter...
Is it possible? How to do that?
As you see class X uses Printer class but the problem is that I would like to have Printer as a template parameter...
Is it possible? How to do that?
Sorry but... I don't see the problem (with a great simplification suggested by Story Teller: place a single Printer object in the ground case case)
template <typename...>
class X;
template <typename Printer, typename Interface>
class X<Printer, Interface> : public Interface
{
static_assert(std::is_abstract<Interface>::value,
"Last argument should be an interface");
public:
X (Printer p0) : printer{p0}
{ }
using Interface::print; // why?
protected:
Printer printer;
};
template <typename Printer, typename Arg, typename... Args>
class X<Printer, Arg, Args...> : public X<Printer, Args...>
{
using Parent = X<Printer, Args...>;
public:
using Parent::print;
using Parent::printer;
X(Printer printer_): Parent{printer_} {}
void print(Arg arg) override { printer.print(arg); }
};
// ....
X<Printer, double, int, Interface> x(printer);
Off topic: attention: you're using printer uninitialized
X(Printer printer_): Parent(printer), printer{printer_} {}
I suppose you should write Parent(printer_)
Assuming that the polymorphic interface is required.
polymorphism reduces the value of variadic template expansion
preserving the deferral of action to an encapsulated printer
A possible solution:
#include <iostream>
#include <type_traits>
// Abstract interface
class PrintInterface
{
public:
virtual ~PrintInterface() = default;
virtual void print(int) = 0;
virtual void print(double) = 0;
};
// An implmentation of PrintInterface that defers to PrinterType
template<class PrinterType>
class ImplementPrintInterface : public PrintInterface
{
public:
ImplementPrintInterface(PrinterType printer)
: printer_(std::move(printer))
{}
virtual void print(int x) override
{
printer_.print(x);
}
virtual void print(double x) override
{
printer_.print(x);
}
private:
PrinterType printer_;
};
// An implementation of a thing that prints ints and doubles.
// This happens to match PrintInterface but there is no inheritance
struct Printer
{
void print(int i) {std::cout << i << std::endl; }
void print(double d) {std::cout << d << std::endl; }
};
// X *is a* PrinterInterface that *uses a* PrinterType
template <typename PrinterType>
class X : public ImplementPrintInterface<PrinterType>
{
public:
X(PrinterType printer = PrinterType())
: ImplementPrintInterface<PrinterType>(std::move(printer))
{}
};
int main()
{
Printer printer;
X<Printer> x(printer);
x.print(5);
}

How to create flexible templates C++

I am trying to create abstract class which is a template for another classes. Is it possible to create "flexible" template?
Several classes will inherit from this one, all of them will have the functions with the same name, but with different arguments. The abstract class is "Interface" of inheritance classes - I will use pointer of this one to manage another.
For example we have two classes: A and B.
find method of A class needs only type1 type, but the same method of B class needs type1 and type2 types.
This is how I am creating classes that inherit from template:
class A : public Repository<int> {
public void find(int) override; };
class B : public Repository<int, float> {
public void find(int a, float b) override; };
Its all about the part after public keyword. I don't want to type <int, float> to all classes.
I there any way to overload(?) the template<typename type1, typename type2> and the function?
The code of the abstract class.
#ifndef REPOSITORY_HPP
#define REPOSITORY_HPP
#include <string>
//template<typename type1>
template<typename type1, typename type2>
class Repository
{
protected:
typeSTRING name;
public:
virtual void find(type1) = 0;
//virtual void find(type1, type2) = 0;
};
#endif
You would need variadic template in base class, i.e
#include <iostream>
template <typename ... Args>
class Interface
{
public:
virtual void find(Args... args) = 0;
};
class Impl1 : public Interface<int>
{
public:
void find(int value) override
{
std::cout << "found" << value << std::endl;
}
};
class Impl2 : public Interface<int, float>
{
public:
void find(int value, float other_value) override
{
std::cout << "found" << value << " " << other_value << std::endl;
}
};
int main()
{
Impl1 impl1 {};
impl1.find(5);
Impl2 impl2 {};
impl2.find(5, 10.2);
}
To complement the below comment from #KKMKK, this is how you can get an specific type from Args... (from: get the Nth type of variadic template templates?):
template <typename ... Args>
class Interface
{
public:
using FirstType = typename std::tuple_element<0, std::tuple<Args...> >::type;
virtual void add(FirstType) = 0;
virtual void find(Args... args) = 0;
};
class Impl2 : public Interface<int, float>
{
public:
void add(int value) override
{
std::cout << "found" << value << std::endl;
}
void find(int value, float other_value) override
{
std::cout << "found" << value << " " << other_value << std::endl;
}
};
int main()
{
Impl2 impl2 {};
impl2.add(5);
impl2.find(5, 10.2);
}

Is that possible to know all the name of derived classes?

Suppose we have a base class and a bunch of derived classes. Is there any way or mechanism to know all the derived class names programmatically?
Maybe reflection is a good idea, but it's not available on C++. I suppose there will be some kind of template that can finish this job during compilation.
class Base{
public:
virtual void print(){
// This function should print all the names of derived class.
}
virtual Base* getInstance(string class_name){
// This function should return an instance related to the class name.
}
};
class Derived_1 : public Base{ // Suppose we have 100 Derived_X classes,
// so we don't want to add its name to a list manually.
};
int main(){
Base base;
base.print(); // This should print the name of all the derived class.
base.getInstance("Derived_1"); // This should return an instance of Derived_1
return 0;
}
This solution is based on the fact that it seems you are actually looking for a factory. It uses a small macro to ease classes registration, hope you don't care about it.
factory.h
#ifndef __FACTORY_H__
#define __FACTORY_H__
#include <map>
#include <functional>
#include <string>
#include <iostream>
template<class B>
class Factory {
std::map<std::string, std::function<B*()>> s_creators;
public:
static Factory<B>& getInstance() {
static Factory<B> s_instance;
return s_instance;
}
template<class T>
void registerClass(const std::string& name) {
s_creators.insert({name, []() -> B* { return new T(); }});
}
B* create(const std::string& name) {
const auto it = s_creators.find(name);
if (it == s_creators.end()) return nullptr; // not a derived class
return (it->second)();
}
void printRegisteredClasses() {
for (const auto &creator : s_creators) {
std::cout << creator.first << '\n';
}
}
};
#define FACTORY(Class) Factory<Class>::getInstance()
template<class B, class T>
class Creator {
public:
explicit Creator(const std::string& name) {
FACTORY(B).registerClass<T>(name);
}
};
#define REGISTER(base_class, derived_class) \
Creator<base_class, derived_class> s_##derived_class##Creator(#derived_class);
#endif
example.cpp
#include "factory.h"
#include <memory>
class Base {
public:
virtual void printName() const { std::cout << "Base\n"; }
};
class Derived1 : public Base {
public:
virtual void printName() const override { std::cout << "Derived1\n"; }
};
REGISTER(Base, Derived1);
class Derived2 : public Base {
public:
virtual void printName() const override { std::cout << "Derived2\n"; }
};
REGISTER(Base, Derived2);
int main() {
std::cout << "Registered classes:" << std::endl;
FACTORY(Base).printRegisteredClasses();
std::cout << "---" << std::endl;
std::unique_ptr<Base> derived1(FACTORY(Base).create("Derived1"));
derived1->printName();
return 0;
}
Note: requires C++11.
For the getInstance you can declare it a template (needs C++14). To get all the names of the derived classes in the program you probably have to resort to some preprocessor hack.
#include <type_traits>
class Base
{
public:
virtual ~Base () = default;
template < typename T,
typename = std::enable_if_t<std::is_base_of<Base, T>::value, void>
>
T getInstance() { return T{}; }
};
class Derived : public Base {};
class NotDerived {};
int main(){
Base base;
base.getInstance<Derived>();
// error: no matching member function for call to 'getInstance'
//base.getInstance<NotDerived>();
}
Regarding the name of the derived classes, I propose a solution based on a BaseList class/struct, with a static std::set (or other container) of names, a template Base class/struct, that inherit from BaseList and whose template parameter is the derived class (CRTP style) and (to semplify the construction of the derived classes/struct, a C-style macro (I know... macros are distilled evil... but sometimes...) to create the declaration of the derived classes/structs with a necessary static method, that declare the name of the derived class/struct, and a member (that activate the registration of the name).
The following is a full example (unfortunately is a C++11 one)
#include <set>
#include <string>
#include <iostream>
struct BaseList
{
static std::set<std::string> const & derList (std::string const & dn)
{
static std::set<std::string> dl;
if ( dn.size() )
dl.insert(dn);
return dl;
}
static void print ()
{
std::cout << "derived names: ";
for ( auto const & dn : derList("") )
std::cout << dn << ", ";
std::cout << std::endl;
}
};
template <typename Der>
struct Base : public BaseList
{
static std::size_t setNameInList ()
{ return derList(Der::name()).size(); }
static std::size_t id;
};
template <typename Der>
std::size_t Base<Der>::id = setNameInList();
#define setDerived(nameDer) \
struct nameDer : public Base<nameDer>\
{ \
std::size_t idc { id }; \
static std::string name () \
{ return #nameDer; }
setDerived(Derived_1)
// other elements
};
setDerived(Derived_2)
// other elements
};
setDerived(Derived_3)
// other elements
};
int main()
{
BaseList::print();
}
Regarding the getInstance() problem, the only solution that I can imagine is the same solution proposed by Enry Menke (+1), so I suggest that you get the instance through a template type parameter.

Using the Visitor Pattern with template derived classes

I try to implement the Visitor pattern with templated derived classes
I work with gcc 4.5
here is the VisitorTemplate.hpp, I specialized Derived in the class Visitor, but I'd like to be able to handle any type:
edit : thanks to the suggestions of interjay, the code compiles and runs without errors now
#ifndef VISITORTEMPLATE_HPP_
#define VISITORTEMPLATE_HPP_
#include <iostream>
#include <string>
using namespace std;
template<class T> Derived;
class Visitor
{
public:
virtual void visit(Derived<string> *e) = 0;
};
class Base
{
public:
virtual void accept(class Visitor *v) = 0;
};
template<class T>
Derived: public Base
{
public:
virtual void accept(Visitor *v)
{
v->visit(this);
}
string display(T arg)
{
string s = "This is : " + to_string(arg);
return s;
}
};
class UpVisitor: public Visitor
{
virtual void visit(Derived<string> *e)
{
cout << "do Up on " + e->display("test") << '\n';
}
};
class DownVisitor: public Visitor
{
virtual void visit(Derived<string> *e)
{
cout << "do Down on " + e->display("test") << '\n';
}
};
#endif /* VISITORTEMPLATE_HPP_ */
main.cpp
Base* base = new Derived<string>();
Visitor* up = new UpVisitor();
Visitor* down = new DownVisitor();
base->accept(up);
base->accept(down);
Now my goal is to use Derived in visit without specializing; unfortunately, visit is a virtual method so I can't template it
From Modern C++ - Design Generic Programming and Design Patterns Applied - Andrei Alexandrescu
#include <iostream>
class BaseVisitor
{
public:
virtual ~BaseVisitor() {};
};
template <class T, typename R = int>
class Visitor
{
public:
virtual R visit(T &) = 0;
};
template <typename R = int>
class BaseVisitable
{
public:
typedef R ReturnType;
virtual ~BaseVisitable() {};
virtual ReturnType accept(BaseVisitor & )
{
return ReturnType(0);
}
protected:
template <class T>
static ReturnType acceptVisitor(T &visited, BaseVisitor &visitor)
{
if (Visitor<T> *p = dynamic_cast< Visitor<T> *> (&visitor))
{
return p->visit(visited);
}
return ReturnType(-1);
}
#define VISITABLE() \
virtual ReturnType accept(BaseVisitor &v) \
{ return acceptVisitor(*this, v); }
};
/** example of use */
class Visitable1 : public BaseVisitable<int>
{
/* Visitable accept one BaseVisitor */
public:
VISITABLE();
};
class Visitable2 : public BaseVisitable<int>
{
/* Visitable accept one BaseVisitor */
public:
VISITABLE();
};
class VisitorDerived : public BaseVisitor,
public Visitor<Visitable1, int>,
public Visitor<Visitable2, int>
{
public:
int visit(Visitable1 & c)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
int visit(Visitable2 & c)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
int main(int argc, char **argv)
{
VisitorDerived visitor;
Visitable1 visitable1;
Visitable2 visitable2;
visitable1.accept(visitor);
visitable2.accept(visitor);
}
Is possible to avoid dynamic_cast with CRTP pattern like:
#include <iostream>
class BaseVisitor
{
public:
virtual ~BaseVisitor() {};
};
template <class T>
class Visitor
{
public:
virtual void visit(T &) = 0;
};
template <class Visitable>
class BaseVisitable
{
public:
template <typename T>
void accept(T & visitor)
{
visitor.visit(static_cast<Visitable &>(*this));
}
};
/** example of use */
class Visitable1 : public BaseVisitable<Visitable1>
{
};
class Visitable2 : public BaseVisitable<Visitable2>
{
};
class VisitorDerived : public BaseVisitor,
public Visitor<Visitable1>,
public Visitor<Visitable2>
{
public:
void visit(Visitable1 & c)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
void visit(Visitable2 & c)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
int main(int argc, char **argv)
{
VisitorDerived visitor;
Visitable1 visitable1;
Visitable2 visitable2;
visitable1.accept<VisitorDerived>(visitor);
visitable2.accept<VisitorDerived>(visitor);
}
Your Derived class cannot use Visitor because it hasn't been defined yet (it was only forward declared, and is therefore an incomplete type).
You can fix the compile error by putting the Visitor definition before Derived. You will also need to forward-declare Derived before defining Visitor:
template <class T> class Derived;
class Visitor {
public:
virtual void visit(Derived<string> *e) = 0;
};
template <class T>
class Derived : public Base {
//.... can call Visitor methods here ...
};