How to use Kangaru C++ auto-wiring - c++

Following my previous question, I would like to achieve the following with minimum boilerplate code.
I understand that Kangaru has autowiring feature to reduce code.
Here the original working version of the code.
#include <kangaru/kangaru.hpp>
#include <iostream>
struct IA
{
virtual void run() const = 0;
};
struct A : IA
{
void run() const override
{
std::cout << "A" << std::endl;
}
};
struct IB
{
virtual void runB() const = 0;
};
struct B : IB
{
IA& _a;
B(IA& a)
:
_a (a)
{}
void runB() const override
{
std::cout << "B" << std::endl;
}
};
struct IAService : kgr::abstract_service<IA> {};
struct IBService : kgr::abstract_service<IB> {};
struct AService : kgr::single_service<A>, kgr::overrides<IAService> {};
struct BService : kgr::single_service<B, kgr::dependency<IAService>>, kgr::overrides<IBService> {};
void main()
{
kgr::container container;
container.service<AService>().run();
container.service<IAService>().run();
container.service <BService>().runB();
container.service<IBService>().runB();
}
I have tried the following struct BService : kgr::single_service<B, kgr::autowire>, kgr::overrides<IBService> {}; but it does not work well for me

I have also posted the question in the Kangaru chat, and the author answered to me the following and it works well
#include <kangaru/kangaru.hpp>
#include <iostream>
struct IA
{
virtual void run() const = 0;
};
struct A : IA
{
A()
{
std::cout << "Ctor A" << std::endl;
}
void run() const override
{
std::cout << "A::run" << std::endl;
}
};
struct IB
{
virtual void runB() const = 0;
};
struct B : IB
{
IA& _a;
B(IA& a)
:
_a(a)
{
std::cout << "Ctor B" << std::endl;
}
void runB() const override
{
std::cout << "From B: "; _a.run();
std::cout << "B" << std::endl;
}
};
auto service_map(IA const&) -> struct IAService;
auto service_map(A const&) -> struct AService;
auto service_map(IB const&) -> struct IBService;
auto service_map(B const&) -> struct BService;
struct IAService : kgr::abstract_service<IA> {};
struct IBService : kgr::abstract_service<IB> {};
struct AService : kgr::single_service<A, kgr::autowire>, kgr::overrides<IAService> {};
struct BService : kgr::single_service<B, kgr::autowire>, kgr::overrides<IBService> {};
void main()
{
kgr::container container;
// configure which abstract service will be used
container.emplace<AService>();
container.emplace<BService>();
// Use the abstract serivces
container.service<IAService>().run();
container.service<IBService>().runB();
container.invoke([](IA& a, IB& b) {
a.run();
b.runB();
});
}

Related

store shared_ptr to container with base class

I want to store some data to container. For example I have such code:
#include <iostream>
#include <string>
#include <memory>
#include <map>
class Base
{
public:
Base() {}
virtual ~Base() {}
};
class Class1 : public Base
{
public:
Class1() : Base() {}
~Class1() {}
};
class Class2 : public Base
{
public:
Class2() : Base() {}
~Class2() {}
};
class Class3 : public Base
{
public:
Class3() : Base() {}
~Class3() {}
};
std::map<std::string, std::shared_ptr<Base>> myContainer;
void save(const std::string& id, std::shared_ptr<Base> obj)
{
auto obj1 = std::dynamic_pointer_cast<Class1>(obj);
if (obj1)
{
std::cout << "save obj1" << std::endl;
myContainer.emplace(std::piecewise_construct,
std::make_tuple(id),
std::make_tuple(std::move(obj1))
);
}
auto obj2 = std::dynamic_pointer_cast<Class2>(obj);
if (obj2)
{
std::cout << "save obj2" << std::endl;
myContainer.emplace(std::piecewise_construct,
std::make_tuple(id),
std::make_tuple(std::move(obj2))
);
}
auto obj3 = std::dynamic_pointer_cast<Class3>(obj);
if (obj3)
{
std::cout << "save obj3" << std::endl;
myContainer.emplace(std::piecewise_construct,
std::make_tuple(id),
std::make_tuple(std::move(obj3))
);
}
}
int main()
{
std::shared_ptr<Class1> a1 = std::make_shared<Class1>();
std::shared_ptr<Class2> a2 = std::make_shared<Class2>();
std::shared_ptr<Class3> a3 = std::make_shared<Class3>();
save("id1", a1);
save("id2", a2);
save("id3", a3);
std::cout << "size is " << myContainer.size() << std::endl;
return 0;
}
But function save() has too much complicated implementation. How to make it easier? Somehow to get correct object type and invoke save() once but not in every checking. Maybe it possible to implement it with std::variant or std::tuple? What is much optimized solution you can propose?
You seem to understand virtual functions.
Your entire save function could be implemented as:
void save(const std::string& id, std::shared_ptr<Base> obj)
{
std::cout << "save " << obj->name() << std::endl;
myContainer.emplace(std::piecewise_construct,
std::make_tuple(id),
std::make_tuple(std::move(obj))
);
}
name() would be a virtual function that returns the correct string for the type.
Note that this implementation always saves the pointer passed to it, while your implementation may not save anything.
Assuming you've provide a shared pointer containing the real class instead of a std::shared_ptr<Base> when calling the function, you can rewrite this as a template:
template<class T>
char const* TypeName();
template<>
char const* TypeName<Class1>() { return "obj1"; }
template<>
char const* TypeName<Class2>() { return "obj2"; }
template<>
char const* TypeName<Class3>() { return "obj3"; }
template<class T>
void save(const std::string& id, std::shared_ptr<T> obj)
{
std::cout << "save " << TypeName<T>() << std::endl;
myContainer.emplace(std::piecewise_construct,
std::make_tuple(id),
std::make_tuple(std::move(obj))
);
}

Choose C++ template at runtime

Is there any way to achieve the functionality of below code without creating the mapping between strings and classes manually?
template<class base, typename T>
base* f(const std::string &type, T &c) {
if(type == "ClassA") return new ClassA(c);
else if(type == "ClassB") return new ClassB(c);
// many more else if...
return nullptr;
}
All classes looks something like this:
class ClassA: public BaseClass {
public:
std::string label="ClassA";
...
};
And we can use it as:
BaseClass *b = f<BaseClass>("ClassA", DifferentObject);
Each new class results in a new if else line of code. Is there any way to automate this so f function "updates" itself when new supported class is added? The solution must work for C++11.
A possible macro:
#include <memory>
#include <string>
class BaseClass {};
class ClassA : public BaseClass {
public:
std::string label = "ClassA";
explicit ClassA(int /*unused*/) {}
};
class ClassB : public BaseClass {
public:
std::string label = "ClassB";
explicit ClassB(int /*unused*/) {}
};
template<class base, typename T>
auto f(const std::string &type, T c) -> std::unique_ptr<base> {
#define CASE(NAME) \
if (type == "NAME") { \
return std::unique_ptr<base>(new NAME(c)); \
}
CASE(ClassA)
CASE(ClassB)
//...
#undef CASE
return nullptr; // Statement at the end needed for last else!
}
auto main() -> int {
auto b = f<BaseClass>("ClassA", 0);
}
Also use unique_ptr since memory managing raw pointers are EVIL.
In that case if the class name is equal to the string, you can simplify your code with the following macro:
#define STRING_TO_CLASS (className) if(type == "className") return new className(c);
template<class base, typename T>
base* f(const std::string &type, T &c) {
STRING_TO_CLASS(ClassA)
STRING_TO_CLASS(ClassB)
return nullptr;
}
Personally I hate macros, but it disturbs only me. However, at compile time, the following code wil be generated, after the macros are resolved.
template<class base, typename T>
base* f(const std::string &type, T &c) {
if(type == "ClassA") return new ClassA(c);
if(type == "ClassB") return new ClassB(c);
return nullptr;
}
As you see, in the end only the else keyword is removed. Also, you need to modify your code if a new class is added.
You could use the registry pattern like this:
#include <map>
#include <functional>
#include <string>
template< typename T, typename X >
using Factory = std::function< T* ( X& ) >;
template< typename Base, typename X >
struct Registry {
using Map = std::map<std::string,Factory<Base,X> >;
static Map registry;
template< typename T >
struct Register {
Register( const std::string& name ) {
registry[ name ] = []( X& x ) -> T* { return new T(x); };
}
};
};
template< typename Base, typename X >
Base* factory(const std::string &type, X &c ) {
auto it = Registry<Base,X>::registry.find( type );
if ( it!=Registry<Base,X>::registry.end() ) {
return (it->second)(c);
}
return nullptr;
}
struct X {};
struct A {
A( X& x ) {};
virtual ~A() {}
};
struct B : public A {
B( X& x ) : A(x) {};
};
struct C : public A {
C( X& x ) : A(x) {};
};
struct D : public B {
D( X& x ) : B(x) {};
};
// Register class
template<> Registry<A,X>::Map Registry<A,X>::registry{};
Registry<A,X>::Register<B> regB( "B" );
Registry<A,X>::Register<C> regC( "C" );
Registry<A,X>::Register<D> regD( "D" );
#include <iostream>
int main() {
X x;
A* ptr = factory<A,X>( "B", x );
B* bptr = dynamic_cast<B*>( ptr );
if ( bptr!= nullptr ) {
std::cout << "Success!" << std::endl;
return 0;
}
std::cout << "Failed!" << std::endl;
return 1;
}
The correct pattern to use here is the "Abstract Factory" pattern.
Maybe you can look it up.
To give you and idea (and not more), what is possible, I will show you the below code, which even accepts constructors with different signature.
#include <iostream>
#include <map>
#include <utility>
#include <any>
// Some demo classes ----------------------------------------------------------------------------------
struct Base {
Base(int d) : data(d) {};
virtual ~Base() { std::cout << "Destructor Base\n"; }
virtual void print() { std::cout << "Print Base\n"; }
int data{};
};
struct Child1 : public Base {
Child1(int d, std::string s) : Base(d) { std::cout << "Constructor Child1 " << d << " " << s << "\n"; }
virtual ~Child1() { std::cout << "Destructor Child1\n"; }
virtual void print() { std::cout << "Print Child1: " << data << "\n"; }
};
struct Child2 : public Base {
Child2(int d, char c, long l) : Base(d) { std::cout << "Constructor Child2 " << d << " " << c << " " << l << "\n"; }
virtual ~Child2() { std::cout << "Destructor Child2\n"; }
virtual void print() { std::cout << "Print Child2: " << data << "\n"; }
};
struct Child3 : public Base {
Child3(int d, long l, char c, std::string s) : Base(d) { std::cout << "Constructor Child3 " << d << " " << l << " " << c << " " << s << "\n"; }
virtual ~Child3() { std::cout << "Destructor Child3\n"; }
virtual void print() { std::cout << "Print Child3: " << data << "\n"; }
};
using UPTRB = std::unique_ptr<Base>;
template <class Child, typename ...Args>
UPTRB createClass(Args...args) { return std::make_unique<Child>(args...); }
// The Factory ----------------------------------------------------------------------------------------
template <class Key, class Object>
class Factory
{
std::map<Key, std::any> selector;
public:
Factory() : selector() {}
Factory(std::initializer_list<std::pair<const Key, std::any>> il) : selector(il) {}
template<typename Function>
void add(Key key, Function&& someFunction) { selector[key] = std::any(someFunction); };
template <typename ... Args>
Object create(Key key, Args ... args) {
if (selector.find(key) != selector.end()) {
return std::any_cast<std::add_pointer_t<Object(Args ...)>>(selector[key])(args...);
}
else return nullptr;
}
};
int main()
{
// Define the factory with an initializer list
Factory<int, UPTRB> factory{
{1, createClass<Child1, int, std::string>},
{2, createClass<Child2, int, char, long>}
};
// Add a new entry for the factory
factory.add(3, createClass<Child3, int, long, char, std::string>);
// Some test values
std::string s1(" Hello1 "); std::string s3(" Hello3 ");
int i = 1; const int ci = 1; int& ri = i; const int& cri = i; int&& rri = 1;
UPTRB b1 = factory.create(1, 1, s1);
UPTRB b2 = factory.create(2, 2, '2', 2L);
UPTRB b3 = factory.create(3, 3, 3L, '3', s3);
b1->print();
b2->print();
b3->print();
b1 = factory.create(2, 4, '4', 4L);
b1->print();
return 0;
}

class inheriting from multiple template class instances returning their types

I want to inherit multiple times from a base template class and have a method that returns the corresponding value for every type used in the inheritance, look at the test2 instance (following code definitely won't compile, because the getNextData() methods have the same signature and the problem is that I don't know how to fix it), I marked the important lines in the code:
#include <iostream>
namespace ns
{
template<typename Output>
class Base
{
protected:
struct ResultData { bool success; Output data; };
public:
virtual ResultData getNextData() = 0;
};
template<typename Output>
class Derived : public Base<Output> {/*implements some pure virtual method*/};
struct Data
{
int32_t member;
};
struct Final final : public Derived<Data>
{
ResultData getNextData() final
{
return { true, {1} };
}
};
template<typename T1, typename T2 = int32_t>
class DerivedMultiple : public virtual Derived<T1> // <= important
, public Base<T2> // <= important
{
public:
ResultData<T1> getNextData() override { return { false, {} }; }
virtual ResultData<T2> getNextData() { return { false, {} }; }
};
struct FinalMultiple final : public DerivedMultiple<Data, std::string>
{
ResultData getNextData() final { return {true, {1} }; } // <= important
ResultData getNextData() final { return {true, "test"}; } // <= important
};
struct ReferenceT1 {
ReferenceT1(Base<Data>& base) {}; // <= important
};
struct ReferenceT2 {
ReferenceT2(Base<std::string>& base) {}; // <= important
};
}
int main()
{
ns::Final test1;
std::cout << test1.getNextData().success << std::endl;
std::cout << test1.getNextData().data.member << std::endl;
ns::FinalMultiple test2;
std::cout << test2.getNextData().success << std::endl;
std::cout << test2.getNextData()/*for Data*/.data.member << std::endl; // <= important
std::cout << test2.getNextData().success << std::endl;
std::cout << test2.getNextData()/*for std::string*/.data << std::endl; // <= important
ns::ReferenceT1 referenceT1(test2); // <= important
ns::ReferenceT2 referenceT2(test2); // <= important
}
There was an obvious solution to pass the values as reference and fill them in the getNextData(<type>&) method, but that's how it started, I refactored that at the beginning, because I want to return the values, not fill them (many reasons):
bool getNextData(T1& output) override { return false; }
bool getNextData(T2& output) override { return false; }
I tried virtual inheritance, but it didn't help in this context.
I came up with 2 solutions, both bypass the need for different signature in a very dirty way and neither of them works when you uncomment the 2 commented lines in them (can't derive from the base class twice and have those methods returning only the expected type).
First solution:
#include <iostream>
namespace ns
{
template<typename Output>
class Base
{
protected:
struct ResultData { bool success; Output data; };
public:
virtual ResultData getNextData() = 0;
};
template<typename Output>
class Derived : public Base<Output> {};
struct Data
{
int32_t member;
};
struct Final final : public Derived<Data>
{
ResultData getNextData() final
{
return { true, {1} };
}
};
template<typename T1, typename T2 = int32_t>
class DerivedMultiple : public virtual Derived<T1>
//, public Base<T2>
{
protected:
struct ResultDataT2 { bool success; T2 data; };
public:
virtual ResultDataT2 getNextDataT2() { return { false, {} }; }
};
struct FinalMultiple final : public DerivedMultiple<Data, std::string>
{
ResultData getNextData() final
{
return {true, {1} };
}
ResultDataT2 getNextDataT2() final
{
return {true, "test"};
}
};
struct ReferenceT1 {
ReferenceT1(Base<Data>& base) {};
};
struct ReferenceT2 {
ReferenceT2(Base<std::string>& base) {};
};
}
int main()
{
ns::Final test1;
std::cout << test1.getNextData().success << std::endl;
std::cout << test1.getNextData().data.member << std::endl;
ns::FinalMultiple test2;
std::cout << test2.getNextData().success << std::endl;
std::cout << test2.getNextData().data.member << std::endl;
std::cout << test2.getNextDataT2().success << std::endl;
std::cout << test2.getNextDataT2().data << std::endl;
ns::ReferenceT1 referenceT1(test2);
//ns::ReferenceT2 referenceT2(test2);
}
Second solution:
#include <iostream>
namespace ns
{
template<typename Output>
struct ResultData { bool success; Output data; };
template<typename Output>
class Base
{
public:
virtual ResultData<Output> getNextData() = 0;
};
template<typename Output>
class Derived : public Base<Output> {};
struct Data
{
int32_t member;
};
struct Final final : public Derived<Data>
{
ResultData<Data> getNextData() final
{
return { true, {1} };
}
};
template<typename T1, typename T2 = int32_t>
class DerivedMultiple : public virtual Derived<T1>
//, public Base<T2>
{
protected:
struct ResultDataT2 { bool success; T2 data; };
public:
virtual ResultDataT2 getNextDataT2() { return { false, {} }; }
};
struct FinalMultiple final : public DerivedMultiple<Data, std::string>
{
ResultData<Data> getNextData() final
{
return {true, {1} };
}
ResultDataT2 getNextDataT2() final
{
return {true, "test"};
}
};
struct ReferenceT1 {
ReferenceT1(Base<Data>& base) {};
};
struct ReferenceT2 {
ReferenceT2(Base<std::string>& base) {};
};
}
int main()
{
ns::Final test1;
std::cout << test1.getNextData().success << std::endl;
std::cout << test1.getNextData().data.member << std::endl;
ns::FinalMultiple test2;
std::cout << test2.getNextData().success << std::endl;
std::cout << test2.getNextData().data.member << std::endl;
std::cout << test2.getNextDataT2().success << std::endl;
std::cout << test2.getNextDataT2().data << std::endl;
ns::ReferenceT1 referenceT1(test2);
//ns::ReferenceT2 referenceT2(test2);
}
Is there a way in C++ to overcome this problem?
Another thing would be to switch to variadic templates in the solution (so the base class can be inherited more than twice and the multiply derived class can return more than 2 types), but that's optional.
I'm not sure I quite understand what you are trying to achieve, but see if this gives you some ideas.
template <typename Impl, typename Intf, typename Tag>
class ForwardingShim;
template <typename Impl, typename Intf>
class ForwardingShim<Impl, Intf, struct ForwardOne> : public Intf {
public:
typename Intf::ResultData getNextData() final {
return static_cast<Impl*>(this)->getNextDataOne();
}
};
template <typename Impl, typename Intf>
class ForwardingShim<Impl, Intf, struct ForwardTwo> : public Intf {
public:
typename Intf::ResultData getNextData() final {
return static_cast<Impl*>(this)->getNextDataTwo();
}
};
template <typename T1, typename T2>
class MultiDerived :
public ForwardingShim<MultiDerived, Base<T1>, struct ForwardOne>,
public ForwardingShim<MultiDerived, Base<T2>, struct ForwardTwo> {
public:
typename Base<T1>::ResultData getNextDataOne();
typename Base<T2>::ResultData getNextDataTwo();
};
Demo

C++ self-registering factory, multi parameters constructors

I read this article about C++ factory class with a self registering capability of the concrete classes. Really like it, expecially the demangled name solution used as a key for the registered classes.
There is one main issue I would like to solve: how can we modify the factory to be able to support concrete classes with different constructor parameters?
// Dog and Cat both derive from Animal, but have different constructor parameters
class Dog : public Animal::Registrar<Dog> {
public:
Dog(int param1, std::string param2) : m_x(param1) {}
void makeNoise() { std::cerr << "Dog: " << m_x << "\n"; }
private:
int m_x;
};
class Cat : public Animal::Registrar<Cat> {
public:
Cat(bool param1) : m_flag(param1) {}
void makeNoise() { std::cerr << "Cat: " << m_x << "\n"; }
private:
bool m_flag;
};
I was thinking that maybe the parameter pack of template <class Base, class... Args> class Factory should be moved to the template <class T> struct Registrar, but I can't found a proper solution.
Full original code below
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <cstdlib>
#include <cxxabi.h>
std::string demangle(const char *name) {
int status = -4; // some arbitrary value to eliminate the compiler warning
std::unique_ptr<char, void (*)(void *)> res{
abi::__cxa_demangle(name, NULL, NULL, &status), std::free};
return (status == 0) ? res.get() : name;
}
template <class Base, class... Args> class Factory {
public:
template <class ... T>
static std::unique_ptr<Base> make(const std::string &s, T&&... args) {
return data().at(s)(std::forward<T>(args)...);
}
template <class T> struct Registrar : Base {
friend T;
static bool registerT() {
const auto name = demangle(typeid(T).name());
Factory::data()[name] = [](Args... args) -> std::unique_ptr<Base> {
return std::make_unique<T>(std::forward<Args>(args)...);
};
return true;
}
static bool registered;
private:
Registrar() : Base(Key{}) { (void)registered; }
};
friend Base;
private:
class Key {
Key(){};
template <class T> friend struct Registrar;
};
using FuncType = std::unique_ptr<Base> (*)(Args...);
Factory() = default;
static auto &data() {
static std::unordered_map<std::string, FuncType> s;
return s;
}
};
template <class Base, class... Args>
template <class T>
bool Factory<Base, Args...>::Registrar<T>::registered =
Factory<Base, Args...>::Registrar<T>::registerT();
struct Animal : Factory<Animal, int> {
Animal(Key) {}
virtual void makeNoise() = 0;
};
class Dog : public Animal::Registrar<Dog> {
public:
Dog(int x) : m_x(x) {}
void makeNoise() { std::cerr << "Dog: " << m_x << "\n"; }
private:
int m_x;
};
class Cat : public Animal::Registrar<Cat> {
public:
Cat(int x) : m_x(x) {}
void makeNoise() { std::cerr << "Cat: " << m_x << "\n"; }
private:
int m_x;
};
// Won't compile because of the private CRTP constructor
// class Spider : public Animal::Registrar<Cat> {
// public:
// Spider(int x) : m_x(x) {}
// void makeNoise() { std::cerr << "Spider: " << m_x << "\n"; }
// private:
// int m_x;
// };
// Won't compile because of the pass key idiom
// class Zob : public Animal {
// public:
// Zob(int x) : Animal({}), m_x(x) {}
// void makeNoise() { std::cerr << "Zob: " << m_x << "\n"; }
// std::unique_ptr<Animal> clone() const { return
// std::make_unique<Zob>(*this); }
// private:
// int m_x;
// };
// An example that shows that rvalues are handled correctly, and
// that this all works with move only types
struct Creature : Factory<Creature, std::unique_ptr<int>> {
Creature(Key) {}
virtual void makeNoise() = 0;
};
class Ghost : public Creature::Registrar<Ghost> {
public:
Ghost(std::unique_ptr<int>&& x) : m_x(*x) {}
void makeNoise() { std::cerr << "Ghost: " << m_x << "\n"; }
private:
int m_x;
};
int main() {
auto x = Animal::make("Dog", 3);
auto y = Animal::make("Cat", 2);
x->makeNoise();
y->makeNoise();
auto z = Creature::make("Ghost", std::make_unique<int>(4));
z->makeNoise();
return 0;
}

Visitor pattern from base class

I am trying to implement the visitor pattern in c++ https://en.wikipedia.org/wiki/Visitor_pattern
How can I use this pattern when dealing with objects as their base class? I want the visitor to be able to parse the class hierarchy. In my specific case, not the example I give, I want to parse an array of objects into a json list where each object has both the parent class members and the specific subclass members. Is the visitor pattern not the correct approach here?
#include <iostream>
#include <vector>
class Animal;
class Dog;
class Cat;
class Visitor {
public:
virtual void visit(Animal *a) = 0;
virtual void visit(Dog *d) = 0;
virtual void visit(Cat *c) = 0;
};
struct Animal {
void accept(Visitor &v) { v.visit(this); }
int a;
protected:
Animal(int an) : a(an) {}
};
struct Dog : Animal {
Dog(int dn, int an) : Animal(an), d(dn) {}
void accept(Visitor &v) { v.visit(this); }
int d;
};
struct Cat : Animal {
Cat(int cn, int an) : Animal(an), c(cn) {}
void accept(Visitor &v) { v.visit(this); }
int c;
};
struct ConcreteVisitor : Visitor {
void visit(Animal *a) {
std::cout << "Animal<" << a->a << ">";
}
void visit(Dog *d) {
std::cout << "Dog<" << d->d << ", ";
visit((Animal *)d);
std::cout << ">" << std::endl;
}
void visit(Cat *c) {
std::cout << "Cat<" << c->c << ", ";
visit((Animal *)c);
std::cout << ">" << std::endl;
}
};
int main() {
std::vector<Animal *> animals;
animals.push_back(new Dog(4, 5));
animals.push_back(new Cat(6, 7));
ConcreteVisitor v;
for (Animal *a : animals) {
a->accept(v);
}
}
This prints Animal<5> Animal<7>
You should make accept virtual as well.