I have a templated Base class that has multiple child classes and provides an abstract execute method. The children implement this method in different manners and may delegate execute calls to objects in the tail vector of the Base class.
In order to chain objects to the tail, the Base class provides some methods (createChild1, createChild2). Here is the code:
base.h
#pragma once
#include <memory>
#include <vector>
template<typename T>
class Child1;
template<typename T>
class Child2;
template<typename T>
class Base {
public:
std::vector<std::unique_ptr<Base<T>>> tail;
virtual void execute(const T &t) = 0;
Base<T> &createChild1() {
auto child = std::make_unique<Child1<T>>();
tail.push_back(std::move(child));
return *tail.back().get();
}
Base<T> &createChild2() {
auto child = std::make_unique<Child2<T>>();
tail.push_back(std::move(child));
return *tail.back().get();
}
};
child1.h
#pragma once
#include "base.h"
template<typename T>
class Child1 : public Base<T> {
public:
virtual void execute(const T &t) {
// do something with t
}
};
child2.h
#pragma once
#include "base.h"
template<typename T>
class Child2 : public Base<T> {
public:
virtual void execute(const T &t) {
// do something with t
// then delegate to t
for (auto &next : tail) {
next->execute(t);
}
}
};
main.cpp
#include <iostream>
#include "child1.h"
using namespace std;
int main(int argc, char **argv) {
Child1<int> c;
c.createChild2().createChild1();
c.execute(10);
return 0;
}
If I try to compile, I get a "implicit instantiation of undefined template 'Child2'" because in Base, the template class Child2 is only forward declared and its body is not known at that moment. Forward declaring Base in front of Child1 and Child2 and including the definitions of Child1 and Child2 in Base does not solve the problem, because then Child2 cannot access tail.
How can I solve this circular dependency? The code in Child1, Child2 and Base can change, but I want to keep the possibility to chain the create calls in main (therefore the create methods must be declared in the Base class).
The solution was prefixing the access to tail with this->. Therefore just child2.h must be changed:
#pragma once
#include "base.h"
template<typename T>
class Child2 : public Base<T> {
public:
virtual void execute(const T &t) {
// do something with t
// then delegate to t
for (auto &next : this->tail) {
next->execute(t);
}
}
};
Related
Well, i have next code:
#include <type_traits>
#include <iostream>
#include <string>
#include <list>
#include <functional>
class base_main
{
public:
virtual ~base_main()
{
}
// some methods
};
class base_1 : virtual public base_main
{
// some methods
};
class base_2 : virtual public base_main
{
// some methods
};
class base_3 : virtual public base_main
{
// some methods
};
class object : public base_1, public base_2, public base_3
{
// some methods
};
// in other *hpp file
class object_controller_listener
{
public:
virtual void object_created( base_main* o )
{
// well, i want to work only with base_1 and base_2 interfaces, but not with base_3, and also i don't want to know something about object class in this *hpp
// is it good code design?
auto* xxx = dynamic_cast<base_1*>( o );
}
};
class objects_controller
{
void create()
{
std::unique_ptr<object> obj;
// ...
for( auto listener : m_listeners )
listener->object_created( obj.get() );
}
std::list<object_controller_listener*> m_listeners;
};
int main()
{
}
The question is - how can i work only with base_1 and base_2 interfaces? Should i create two separate listeners for them, and send two events in create() function, or should i use dynamic_cast for downcasting and send only one event in create() function? Is this good code design or is this feels like code smell?
UPD:
For example: base_1 - is render_base class, which contains render data, and have functions for set and get this data base_2 - collider base class which contains collider data, and have functions for set and get this data base_3 is physic base class and object is inheritance of all this classes. And when i want work only with render class i use create event which send only render_base class to the render_system, which works only with renders objects and truly use polymorphism. But if i want in some other place work with collider and physic objects, without knowledge about render - how can i use polymorphism here in base classes?
It is hard to tell what design you should choose as this heavily depends on the overall structure of the application.
Generally, I would avoid having a function with the signature virtual void object_created( base_main* o ) in which you dynamically cast to base_* and work on that directly in this function. Because the function signature is part of the documentation of the API.
So I would create distinct functions for base_1 and base_2 and call those.
How to do that depends again on the overall structure. You could create a helper function, that forwards the call to the other functions (this is just a fast implementation how that could look like:
template <typename DestT, typename SrcT, typename T>
void forward_if(SrcT obj, T *o, void (T::*f)(DestT)) noexcept {
if (auto tmp = dynamic_cast<DestT>(obj); tmp != nullptr) {
(o->*f)(tmp);
}
}
And then you could use it like this:
#include <iostream>
#include <vector>
class base_main {
public:
virtual ~base_main() {}
};
class base_1 : virtual public base_main {};
class base_2 : virtual public base_main {};
class base_3 : virtual public base_main {};
class object : public base_1, public base_2, public base_3 {};
template <typename DestT, typename SrcT, typename T>
void forward_if(SrcT obj, T *o, void (T::*f)(DestT)) noexcept {
if (auto tmp = dynamic_cast<DestT>(obj); tmp != nullptr) {
(o->*f)(tmp);
}
}
struct listener_base {
virtual void object_created(base_main *o) = 0;
};
struct specific_listener : public listener_base {
void object_created(base_main *o) override {
forward_if<base_1 *>(o, this, &specific_listener::object_created);
forward_if<base_2 *>(o, this, &specific_listener::object_created);
}
void object_created(base_1 *o) {
std::cout << "object created base_1" << std::endl;
}
void object_created(base_2 *o) {
std::cout << "object created base_2" << std::endl;
}
};
int main() {
std::vector<listener_base *> listeners;
listeners.push_back(new specific_listener());
object o;
for (auto listener : listeners) {
listener->object_created(&o);
}
return 0;
}
I have a sprite class, which has a templatised data member. It holds an object, which has a pointer to this specialised sprite template class.
That object requires a forward declaration of my sprite class, but since sprite is a template class, I need to include the full header. Therefore I get a cyclic dependancy which I am unable to figure out
Sprite.h
#include "myclass.h"
template<typename SpriteType, typename = typename std::enable_if_t<std::is_base_of_v<sf::Transformable, SpriteType> && std::is_base_of_v<sf::Drawable, SpriteType>>>
class Sprite {
public:
SpriteType s;
myclass<SpriteType>;
Sprite() {
}
auto foo() {
return s;
}
private:
};
myclass.h
#include "Sprite.h"
//a sprite of type T, is going to create a myclass<Sprite<T>>, a pointer of the Sprite<T> is held in myclass.
template<typename T>
class myclass
{
public:
std::shared_ptr<Sprite<T>> ptr;
myclass() {
}
private:
};
How could I solve this cyclic dependency?
So in summary:
-Sprite is a template class.
-Sprite holds an object to another class. This other class holds a pointer to my this templated sprite class.
-This gives me a cyclic dependency, since both classes are now templates, and need to have their implementations written in their header files.
Simplified decoupling, based on #Taekahns solution.
template<typename T>
class myclass
{
public:
std::shared_ptr<T> ptr;
myclass() {
}
private:
};
template<typename SpriteType, typename = typename std::enable_if_t<std::is_base_of_v<sf::Transformable, SpriteType> && std::is_base_of_v<sf::Drawable, SpriteType>>>
class Sprite {
public:
SpriteType s;
// DO NOT PASS SpriteType here, put the whole Sprite<SpriteType>
myclass<Sprite<SpriteType>> t;
Sprite() {
}
auto foo() {
return s;
}
private:
};
One of the great thing about templates is breaking type dependencies.
You could do something like this. Simplified for readability.
template<typename T>
class myclass
{
public:
std::shared_ptr<T> ptr;
myclass() {
}
private:
};
template<typename SpriteType, typename = std::enable_if_t<std::is_base_of_v<base_class, SpriteType>>>
class Sprite {
public:
SpriteType s;
myclass<Sprite<SpriteType>> t;
Sprite() {
}
auto foo() {
return s;
}
private:
};
That is one of many options.
Another option is to use an interface. i.e. a pure virtual base class that isn't a template.
Example:
I think something like this should do it. Starting to get a hard to follow though.
class base_sprite
{
public:
virtual ~base_sprite(){};
virtual int foo() = 0;
};
template<typename T>
class myclass
{
public:
std::shared_ptr<base_sprite> ptr;
myclass() : ptr(std::make_shared<T>())
{
};
};
template<typename SpriteType>
class Sprite : public base_sprite{
public:
myclass<Sprite<SpriteType>> l;
int foo() override {return 0;};
};
given this code, (I use different code with constructors, destructors, virtuals, etc.), how would one implement A::createChild which adds a pointer to either B or C
#include <vector>
#include <cassert>
class A {
void createChild(std::vector<A *>);
};
void A::createChild(std::vector<A *>) {
//What code goes here?
}
class B : A {};
class C : A {};
int main() {
std::vector<A *> ptrs;
ptrs.push_back(new B);
ptrs.push_back(new C);
ptrs[0]->createChild(ptrs); //Should add a new class of B to ptrs
ptrs[1]->createChild(ptrs); //Should add a new class of C to ptrs
assert(typeid(ptrs[2])==typeid(ptrs[0]);
assert(typeid(ptrs[3])==typeid(ptrs[1]);
}
You may use a template function to achieve that:
class A {
template<typename Derived>
void createChild(std::vector<A *>);
};
template<typename Derived>
void A::createChild(std::vector<A *> v) {
v.push_back(new Derived);
}
I have a SuperParent class, a Parent class (derived from SuperParent) and both contain a shared_ptr to a Child class (which contains a weak_ptr to a SuperParent). Unfortunately, I'm getting a bad_weak_ptr exception when trying to set the Child's pointer. The code is the following:
#include <boost/enable_shared_from_this.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
using namespace boost;
class SuperParent;
class Child {
public:
void SetParent(shared_ptr<SuperParent> parent)
{
parent_ = parent;
}
private:
weak_ptr<SuperParent> parent_;
};
class SuperParent : public enable_shared_from_this<SuperParent> {
protected:
void InformChild(shared_ptr<Child> grandson)
{
grandson->SetParent(shared_from_this());
grandson_ = grandson;
}
private:
shared_ptr<Child> grandson_;
};
class Parent : public SuperParent, public enable_shared_from_this<Parent> {
public:
void Init()
{
child_ = make_shared<Child>();
InformChild(child_);
}
private:
shared_ptr<Child> child_;
};
int main()
{
shared_ptr<Parent> parent = make_shared<Parent>();
parent->Init();
return 0;
}
This is because your Parent class inherits enable_shared_from_this twice.
Instead, you should inherit it once - through the SuperParent. And if you want to be able to get shared_ptr< Parent > within Parent class, you can inherit also it from the following helper class:
template<class Derived>
class enable_shared_from_This
{
public:
typedef boost::shared_ptr<Derived> Ptr;
Ptr shared_from_This()
{
return boost::static_pointer_cast<Derived>(static_cast<Derived *>(this)->shared_from_this());
}
Ptr shared_from_This() const
{
return boost::static_pointer_cast<Derived>(static_cast<Derived *>(this)->shared_from_this());
}
};
Then,
class Parent : public SuperParent, public enable_shared_from_This<Parent>
I'm trying to design a policy-based class, where a certain interface is implemented by the policy itself, so the class derives from the policy, which itself is a template (I got this kind of thinking from Alexandrescu's book):
#include <iostream>
#include <vector>
class TestInterface {
public:
virtual void test() = 0;
};
class TestImpl1 {
public:
void test() {std::cerr << "Impl1" << std::endl;}
};
template<class TestPolicy>
class Foo : public TestInterface, TestPolicy {
};
Then, in the main() function, I call test() on (potentially) various different objects that all implement the same interface:
int main() {
std::vector<TestInterface*> foos;
foos.push_back(new Foo<TestImpl1>());
foos[0]->test();
delete foos[0];
return 0;
}
It doesn't compile, though, because
the following virtual functions are pure within ‘Foo<TestImpl1>’:
virtual void TestInterface::test()
I thought TestInterface::test() is implemented because we derive from TestImpl1?
For this to work the policy class needs to inherit from the interface class:
class TestInterface {
public:
virtual void test() = 0;
};
template< class Interface >
class TestImpl1 : public Interface {
public:
void test() {std::cerr << "Impl1" << std::endl;}
};
template<class TestPolicy>
class Foo : public TestPolicy<TestInterface> {
// ...
};
You could also try a boost::mpl approach:
keeping your TestInterface and TestImpl as they are:
#include <boost/mpl/inherit.hpp>
using namespace boost::mpl;
template <class TestPolicy>
class Foo: public inherit2< TestPolicy, inherit2< TestInterface , empty_base >::type >::type
{
...
}
should work