I have the following situation (live code : https://gcc.godbolt.org/z/d8jG9bs9a):
#include <iostream>
#include <type_traits>
#define ENBALE true // to enable disable test solutions
enum struct Type : unsigned { base = 0, child1, child2, child3 /* so on*/ };
// CRTP Base
template<typename Child> struct Base {
void doSomething() { static_cast<Child*>(this)->doSomething_Impl(); }
private:
Base() = default;
friend Child;
};
struct Child1 : public Base<Child1> {
void doSomething_Impl() { std::cout << "Child1 implementation\n"; }
};
struct Child2 : public Base<Child2> {
void doSomething_Impl() { std::cout << "Child2 implementation\n"; }
};
struct Child3 : public Base<Child3> {
void doSomething_Impl() { std::cout << "Child3 implementation\n"; }
};
// ... so on
class SomeLogicClass
{
Type mClassId{ Type::base };
Child1 mChild1;
Child2 mChild2;
Child3 mChild3;
public:
Type getId() const { return mClassId; }
void setId(Type id) { mClassId = id; } // run time depended!
#if ENBALE // Solution 1 : simple case
/*what in C++11?*/ getInstance()
{
switch (mClassId)
{
case Type::child1: return mChild1;
case Type::child2: return mChild2;
case Type::child3: return mChild3;
default: // error case!
break;
}
}
#elif !ENBALE // Solution 2 : SFINAE
template<Type ID>
auto getInstance() -> typename std::enable_if<ID == Type::child1, Child1&>::type { return mChild1; }
template<Type ID>
auto getInstance() -> typename std::enable_if<ID == Type::child2, Child2&>::type { return mChild2; }
template<Type ID>
auto getInstance() -> typename std::enable_if<ID == Type::child3, Child3&>::type { return mChild3; }
#endif
};
void test(SomeLogicClass& ob, Type id)
{
ob.setId(id);
#if ENBALE // Solution 1
auto& childInstance = ob.getInstance();
#elif !ENBALE // Solution 2
auto& childInstance = ob.getInstance<ob.getId()>();
#endif
childInstance.doSomething(); // calls the corresponding implementations!
}
int main()
{
SomeLogicClass ob;
test(ob, Type::child1);
test(ob, Type::child2);
test(ob, Type::child3);
}
The problem is that the child class selection (to which the doSomething_Impl() must be called), should be taken place by deciding upon a run time variable mClassId of the SomeLogicClass.
The only two possible solutions I can think of are a normal switch case and SFINAE the member functions, as described in the above minimal example. As noted in the comments in the above code, both can't get work, for the reasons
Solution 1: the member function must have a unique return type
Solution 2: SFINAE required a compile time expression to decide which overload to be chosen.
Update
The std::variant(as mentioned by #lorro) would be the easiest solution here. However, require C++17 support.
However, I would like to know if we got some way around which works under the compiler flag c++11?
Note: I am working with a code-base, where external libs such as boost, can not be used, and the CRTP class structure is mostly untouchable.
Since you are restricted to C++11 and are not allowed to use external libraries such as boost::variant, an alternative would be to reverse the logic: Do not attempt to return the child type but instead pass in the operation to perform on the child. Your example could become this (godbolt):
#include <iostream>
#include <type_traits>
enum struct Type : unsigned { base = 0, child1, child2, child3 /* so on*/ };
// CRTP Base
template<typename Child> struct Base {
void doSomething() { static_cast<Child*>(this)->doSomething_Impl(); }
private:
Base() = default;
friend Child;
};
struct Child1 : public Base<Child1> {
void doSomething_Impl() { std::cout << "Child1 implementation\n"; }
};
struct Child2 : public Base<Child2> {
void doSomething_Impl() { std::cout << "Child2 implementation\n"; }
};
struct Child3 : public Base<Child3> {
void doSomething_Impl() { std::cout << "Child3 implementation\n"; }
};
// ... so on
class SomeLogicClass
{
Type mClassId{ Type::base };
Child1 mChild1;
Child2 mChild2;
Child3 mChild3;
// ... child3 so on!
public:
Type getId() const { return mClassId; }
void setId(Type id) { mClassId = id; } // run time depended!
template <class Func>
void apply(Func func)
{
switch (mClassId){
case Type::child1: func(mChild1); break;
case Type::child2: func(mChild2); break;
case Type::child3: func(mChild3); break;
default: // error case!
break;
}
}
};
struct DoSomethingCaller
{
template <class T>
void operator()(T & childInstance){
childInstance.doSomething();
}
};
void test(SomeLogicClass& ob, Type id)
{
ob.setId(id);
ob.apply(DoSomethingCaller{});
// Starting with C++14, you can also simply write:
//ob.apply([](auto & childInstance){ childInstance.doSomething(); });
}
int main()
{
SomeLogicClass ob;
test(ob, Type::child1);
test(ob, Type::child2);
test(ob, Type::child3);
}
Notice how the new function apply() replaces your getInstance(). But instead of attempting to return the child type, it accepts some generic operation that it applies to the correct child. The functor passed in needs to cope (i.e. compile) with all possible child types. Since all of them have a doSomething() method, you can simply use a templated functor (DoSomethingCaller). Before C++14, unfortunately, it cannot simply be a polymorphic lambda but needs to be a proper struct (DoSomethingCaller) outside of the function.
If you care to do so, you can also restrict DoSomethingCaller to the CRTP base class Base<T>:
struct DoSomethingCaller
{
template <class T>
void operator()(Base<T> & childInstance){
childInstance.doSomething();
}
};
which might make it a bit more readable.
Depending on how strict the "no external libraries" restriction is, maybe only boost is not allowed but a single external header (that can be simply included in the code base as any other header file) would be possible? If yes, you might want to also have a look at variant-lite. It aims to be a C++98/C++11 compatible replacement for std::variant.
I recognize here the Bridge pattern in your CRTP code base, although done badly in my opinion. But anyway, if you can't touch that code then you should implement a second bridge code. Fortunately, with templates this won't be that hard.
Here is the code (godbold):
#include <iostream>
enum struct Type : unsigned { base = 0, child1, child2, child3 /* so on*/ };
// CRTP Base
template<typename Child> struct Base {
void doSomething() { static_cast<Child*>(this)->doSomething_Impl(); }
private:
Base() = default;
friend Child;
};
struct Child1 : public Base<Child1> {
void doSomething_Impl() { std::cout << "Child1 implementation\n"; }
};
struct Child2 : public Base<Child2> {
void doSomething_Impl() { std::cout << "Child2 implementation\n"; }
};
struct Child3 : public Base<Child3> {
void doSomething_Impl() { std::cout << "Child3 implementation\n"; }
};
// ... so on
class CommonInterface {
public:
virtual void doSomething() = 0;
};
template<class Child>
class ChildCaller : public CommonInterface {
private:
Child child;
public:
virtual void doSomething() {
child.doSomething_Impl();
}
};
class SomeLogicClass
{
Type mClassId{ Type::base };
ChildCaller<Child1> mChild1;
ChildCaller<Child2> mChild2;
ChildCaller<Child3> mChild3;
CommonInterface* currentChild;
public:
Type getId() const { return mClassId; }
void setId(Type id) {
mClassId = id;
switch(id) {
case Type::child1:
currentChild = &mChild1;
break;
case Type::child2:
currentChild = &mChild2;
break;
case Type::child3:
currentChild = &mChild3;
break;
default:
currentChild = nullptr; // or anything you want
}
}
void doSomething() {
currentChild->doSomething();
}
};
void test(SomeLogicClass& ob, Type id)
{
ob.setId(id);
ob.doSomething(); // calls the corresponding implementations!
}
int main() {
SomeLogicClass ob;
test(ob, Type::child1);
test(ob, Type::child2);
test(ob, Type::child3);
}
I omitted error checking here and there, so the code is more readable.
It is not clear if you want one element or one of each.
If you need one of each, SomeLogicClass should have a std::tuple<Class1, Class2, Class3>. This stores 'one of each', also provides std::get<>(), which returns element i. Thus, getInstance() can essentially delegate to std::get<ID>().
If you need one element of either type, SomeLogicClass should have a std::variant<Class1, Class2, Class3>. This stores the kind of the active element and the element itself. You can either use std::get<>() for it, or - better - you might visit via std::visit().
Related
I have a class TypedNode to store some data:
template <typename Type>
class TypedNode {
public:
TypedNode() {}
void SetNodeData(Type data) { data_ = data; }
Type GetNodeData() { return data_; }
private:
Type data_;
};
Then I can use it:
int main() {
TypedNode<int> int_node;
TypedNode<double> double_node;
TypedNode<Vector3d> vector3_node;
int_node.SetNodeData(1);
double_node.SetNodeData(2.3);
vector3_node.SetNodeData(Vector3d(4,5,6));;
}
But I want to define a function to access then:
void Access(std::list<TypedNode> node_list) {
for (auto node : node_list) {
node.GetNodeData();
// if it is an integer, do one thing
// if it is a double, do another
}
}
The list needs a concrete class, But I need to store any type of nodes.
Some I changed the code of Node:
class NodeBase {
public:
NodeBase() {}
};
template <typename Type>
class TypedNode : NodeBase {
public:
TypedNode() {}
void SetNodeData(Type data) { data_ = data; }
Type GetNodeData() { return data_; }
private:
Type data_;
};
void Access(std::list<NodeBase> node_list) {
for (auto node : node_list) {
node.GetNodeData();
// if it is an integer, do one thing
// if it is a double, do another
}
}
But the Access() function can only call the methods of Base class.
Despite every derived class have one same name interface SetNodeData, They have a different type. So they are different. They cannot override one same interface in the Base class.
What can I do?
==============================================================
This is my solution:
#include <list>
enum NodeType {
kInt,
kDouble,
kVector3,
};
class NodeBase {
public:
NodeBase() {}
virtual int GetDataInt();
virtual double GetDataDouble();
virtual Vector3 GetDataVector3();
NodeType type() const { return type_; }
protected:
void set_type(NodeType type) { type_ = type; }
private:
NodeType type_;
};
class NodeInt : NodeBase {
public:
NodeInt() { set_type(kInt); }
int GetDataInt() override { return data_; }
double GetDataDouble() override { check(false) << "error"; }
Vector3 GetDataVector3() override { check(false) << "error"; }
private:
int data_;
};
class NodeDouble : NodeBase {
public:
NodeDouble() { set_type(kDouble); }
int GetDataInt() override { check(false) << "error"; }
double GetDataDouble() override { return data_; }
Vector3 GetDataVector3() override { check(false) << "error"; }
private:
double data_;
};
void Access(const std::list<NodeBase>& node_list) {
for (auto node : node_list) {
switch (node.type()) {
case kInt: {
int data = node.GetDataInt();
// do something about int
break;
}
case kDouble: {
double data = node.GetDataDouble();
// do something about double
break;
}
case kVector3: {
Vector3 data = node.GetDataVector3();
// do something about Vector3
break;
}
}
}
}
Your TypedNode template provides no apparent value, it is just a getter and a setter of encapsulated data so it is better to eliminate it for simplicity. What you seem to need is a type that can be int, double or Vector3d so those can be held in same container. For that there is std::variant in C++17. People with substandard compilers can use Boost.Variant that is basically same, just works with C++98 too.
#include <variant>
struct Vector3d {int x, y, z;};
using Node = std::variant<int,double,Vector3d>;
Sure, you can have std::variant<TypedNode<int>,TypedNode<double>,TypedNode<Vector3d>> when there is some vital functionality in it. Posted TypedNode had no functionality, other but more bloat to type.
For accessing variant using same interface there are several ways. For example it can be done using a visitor. Here is a visitor NodeOutput for ostream output of every type in Node.
#include <iostream>
struct NodeOutput {
std::ostream& os_;
NodeOutput(std::ostream& os) : os_{os} {}
void operator()(Vector3d const& v3)
{
os_ << "Vector3d (" << v3.x <<", "<< v3.y <<", "<< v3.z <<")\n";
}
void operator()(double const& d) {os_ << "Double " << d <<"\n";}
void operator()(int const& i) {os_ << "Int " << i <<"\n";}
};
Using such visitor we can write operator<< for Node:
std::ostream& operator<< (std::ostream& os, Node const& v) {
std::visit(NodeOutput{os}, v);
return os;
}
Trying it out. The std::list is rarely used container so here it is replaced with std::vector for simplicity, but it will work similarly with other containers.
#include<vector>
int main()
{
std::vector<Node> nodes;
nodes.emplace_back(42);
nodes.emplace_back(6.66);
nodes.emplace_back(Vector3d{3,2,1});
for (auto& n: nodes) {std::cout << n;}
}
Output:
Int 42
Double 6.66
Vector3d (3, 2, 1)
From what i understand, you basically want to access the overridden functions of the NodeBase. You can still do that with templates. I am not going to suggest code changes on your design because i assume that you oversimplified this to give us an idea of what you want your code to do.
With that said, let's assume that NodeBase class is the base class for NodeInt and NoteDouble. and it looks something like this (slightly simplified compared to yours).
class NodeBase
{
public:
...
virtual void DoSomething()
...
};
class NodeInt : public NodeBase
{
public:
...
virtual void DoSomething() //overridden
{
}
...
};
class NodeDouble : public NodeBase
{
public:
...
void DoSomething()//overriden
{
}
...
};
Let's also assume that our Access function looks like;
template<typename Type, typename A>
void Access(std::list<TypedNode<Type>, A> node_list)
{
for (auto node : node_list)
{
node.DoSomething();
}
}
Note that our Access function can now take any type of list containingTypeNode because of TypedNode<Type>
The job of the Access function is just to call the DoSomething. it shouldn't care what type it is. and it should be deduced based on what to call depending on what we pass as an argument to the Access call.
if a list passed to Access is a type <TypedNode<NodeBase> you
want each node to call, NodeBase::DoSomething();
if a list passed to Access is a type <TypedNode<NodeInt> you want
each node to call, NodeInt::DoSomething();
if a list passed to Access is a type <TypedNode<NodeDouble> you
want each node to call, NodeInt::DoSomething();
To do this, first Instead of inheriting from Base class, lets inherit from a parameterized template argument.
template<typename Type>
class TypedNode : public Type
Next, we want to declare and define the DoSomething function for our TypedNode class.
template<typename Type>
class TypedNode : public Type
{
...
void DoSomething();
...
};
template<typename Type>
inline void TypedNode<Type>::DoSomething()
{
Type::DoSomething();//this is where the magic happens.
}
Notice the Type::DoSomething(); this will allow us to call DoSomething() function of the Generic Base class. One thing to pay attention to is that when we initializing an object using template arguments, the class that is used in the template argument initialization has to have a class member named DoSomething otherwise during compilation we will get an error.
for example;
TypedNode<int> intNode; //wont work because int doesnt have a member DoSomething.
TypeNode<NodeBase> baseNode; //fine.
TypeNode<NodeInt> intNode2; //fine
finally, the main code that produces the outcomes for us.
int main()
{
std::list<TypedNode<NodeBase>> baseNodeList;
std::list<TypedNode<NodeInt>> intNodeList;
std::list<TypedNode<NodeDouble>> DoubleNodeList;
TypedNode<NodeBase> baseNode;
TypedNode<NodeInt> intNode;
TypedNode<NodeDouble> doubleNode;
baseNodeList.push_back(baseNode);
intNodeList.push_back(intNode);
DoubleNodeList.push_back(doubleNode);
Access(baseNodeList);
Access(intNodeList);
Access(DoubleNodeList);
return 0;
}
here is the full code https://ideone.com/2jEmBO
I am trying to find a way to express a common base type for two objects that inherit a pair of similar interfaces.
See the code below : it is apparent that there should be a possible common base type for fb1 and fb2 (for example, something like IFizzBuzz).
Does anyone know if that is possible (without requiring to templates if possible :-)
Thanks !
#include <memory>
#include <iostream>
struct IFizz {
virtual void DoFizz() = 0;
};
struct IBuzz {
virtual void DoBuzz() = 0;
};
struct Fizz : public IFizz {
void DoFizz() override {
std::cout << "Fizz\n";
}
};
struct Buzz1 : public IBuzz {
void DoBuzz() override {
std::cout << "Buzz1\n";
}
};
struct Buzz2 : public IBuzz {
void DoBuzz() override {
std::cout << "Buzz2\n";
}
};
struct FizzBuzz1 : public Fizz, public Buzz1 {
};
struct FizzBuzz2 : public Fizz, public Buzz2 {
};
// Expected "Base type" of FizzBuzz1 and FizzBuzz2
struct IFizzBuzz : public IFizz, public IBuzz {
};
int main()
{
{
FizzBuzz1 fb1;
fb1.DoFizz();
fb1.DoBuzz();
}
{
FizzBuzz2 fb2;
fb2.DoFizz();
fb2.DoBuzz();
}
// {
// // The line below is not legit and does not compile
// std::unique_ptr<IFizzBuzz> ifb(new FizzBuzz1());
// ifb->DoFizz();
// ifb->DoBuzz();
// }
return 0;
}
There are 3 common base classes for FizzBuzz1 and FizzBuzz2
IFizz
IBuzz
Fizz
Without changing the definitions of those two, there is no way to have them also have IFizzBuzz as a base.
You can however have an class that derives IFizzBuzz and delegates to one of the two, e.g.
template <typename FizzBuzz>
struct Wrapper : IFizzBuzz
{
void DoFizz() final { fizzBuzz.DoFizz(); }
void DoBuzz() final { fizzBuzz.DoBuzz(); }
private:
FizzBuzz fizzBuzz;
}
This can then be used, e.g.
std::unique_ptr<IFizzBuzz> ifb = std::make_unique<Wrapper<FizzBuzz1>>();
ifb->DoFizz();
ifb->DoBuzz();
ifb = std::make_unique<Wrapper<FizzBuzz2>>();
ifb->DoFizz();
ifb->DoBuzz();
Consider the below code, EventGeneratorBase is a helper class intended to provide the actual implementation for AddEventHandler() and I would like to use that implementation in the class RemoteControl instead of explicity defining it. I know it's not possible to instantiate RemoteControl without defining the method but is there a shortcut or an easy way to avoid manually defining the methods.
Note: The code in it's present form doesn't compile because RemoteControl can't be instantiated.
#include <iostream>
#include <vector>
#include <memory>
template<class TEventHandler> struct IEventGenerator {
virtual ~IEventGenerator() = default;
virtual void AddEventHandler(std::weak_ptr<TEventHandler> eventHandler) = 0;
};
template <class TEvents> struct EventGeneratorBase : IEventGenerator<TEvents> {
void AddEventHandler(std::weak_ptr<TEvents> target) {
_eventHandlers.push_back(target);
}
std::vector<std::weak_ptr<TEvents>> GetEventHandlers() {
return _eventHandlers;
}
private:
std::vector<std::weak_ptr<TEvents>> _eventHandlers;
};
struct IControlEvents {
virtual ~IControlEvents() = default;
virtual void PowerOn() = 0;
virtual void PowerOff() = 0;
};
struct IRemoteControl : IEventGenerator<IControlEvents> {
virtual ~IRemoteControl() = default;
virtual void Toggle() = 0;
};
struct RemoteControl : IRemoteControl, EventGeneratorBase<IControlEvents> {
// I don't want to define AddEventHandler() in this class and
// would like to inherit the implementation from EventGeneratorBase
void Toggle() {
for (auto tref : GetEventHandlers()) {
auto t = tref.lock();
if (t) {
t->PowerOn();
t->PowerOff();
}
}
}
};
struct Light : IControlEvents {
Light(std::string color) : _color(color) { }
void PowerOn() {
std::cout << _color << "::Light ON!" << std::endl;
}
void PowerOff() {
std::cout << _color << "::Light OFF!" << std::endl;
}
private:
std::string _color;
};
int main() {
std::shared_ptr<IRemoteControl> remote(new RemoteControl); // ERROR: Can't instantiate
std::shared_ptr<IControlEvents> light1(new Light("GREEN"));
std::shared_ptr<IControlEvents> light2(new Light("RED"));
remote->AddEventHandler(light1);
remote->AddEventHandler(light2);
remote->Toggle();
return 0;
}
Your problem is that you have two distinct sub-objects of type IEventGenerator<IControlEvents> within your RemoteControl object. One via EventGeneratorBase<IControlEvents> and one via IRemoteControl.
There are two ways to prevent you from having two distinct subobjects. The first is to inherit virtually from IEventGenerator<TEventHandler> in both spots. This has a modest run-time cost. Simply add virtual before every case of inheritance from IEventGenerator<?> and you are done.
A second method is to note that EventGeneratorBase is intended to help with implementing IEventGenerator.
template<class T> struct tag{using type=T;};
template<class Tag> using type_t=typename Tag::type;
template<class TEventHandler>
tag<TEventHandler> get_event_handler_type(
IEventGenerator<TEventHandler> const*
) { return {}; }
template<class X>
using event_handler_type = type_t< decltype( get_event_handler_type( (X*)nullptr ) ) >;
template <class Base, class TEvents = event_handler_type<Base>>
struct EventGeneratorHelper :
Base
{
void AddEventHandler(std::weak_ptr<TEvents> target) override {
_eventHandlers.push_back(target);
}
std::vector<std::weak_ptr<TEvents>> GetEventHandlers() {
return _eventHandlers;
}
private:
std::vector<std::weak_ptr<TEvents>> _eventHandlers;
};
now, go down to here:
struct RemoteControl :
EventGeneratorHelper<IRemoteControl>
{
and change how we inherit. We now interpose EventGeneratorHelper between us and IRemoteControl, so they now share the same common IEventGenerator.
This removes the need for virtual inheritance, but does up your compile time, and can cause some executable code bloat.
We can go a step further. Add this to EventGeneratorHelper:
template<class Action>
void FireEvents( Action&& action ) const {
for (auto tref : GetEventHandlers()) {
auto t = tref.lock();
if (t) {
action(t);
}
}
}
which reduces RemoteControl to:
struct RemoteControl :
EventGeneratorHelper<IRemoteControl>
{
void Toggle() {
this->FireEvents([](std::shared_ptr<IRemoteControl> const& ptr){
t->PowerOn();
t->PowerOff();
});
}
};
which I think is nice -- requiring clients to know the right way of iterating seems silly.
You have a problem in your inheritance hierarchy.
template <class TEvents> struct EventGeneratorBase :IEventGenerator<TEvents> {
[...]
};
struct IRemoteControl : IEventGenerator<IControlEvents> {
[...]
};
struct RemoteControl : IRemoteControl, EventGeneratorBase<IControlEvents> {
[...]
};
This is not doing what you might expect. Instead, your class RemoteControl inherits twice from IEventGenerator, once from IRemoteControl and once from EventGeneratorBase.
I'm trying to implement a modified version of the visitor pattern in a parametric manner, avoiding in this way a "universal visitor" with a overload for each concrete element, but, due to I haven't a lot of experience in template programming I don't know how I can complete the "pattern".
Code:
// test.cpp
#include <iostream>
#include <vector>
using namespace std;
struct Base
{
virtual ~Base() {}
virtual void visit() = 0;
};
template<typename Visitor>
struct ElementBase : public Base
{
// No virtual.
void visit()
{
_e.visit(this);
}
private:
Visitor _e;
};
// Atoms.
template<typename Visitor>
struct ElementA : public ElementBase<Visitor>
{
ElementA() : a(5) {}
int a;
};
// Visitors.
struct VisitorA
{
void visit(ElementBase<VisitorA> *a)
{
ElementA<VisitorA>* elto = dynamic_cast<ElementA<VisitorA>*>(a);
cout << elto->a << endl;
}
/*
void visit(ElementA<VisitorA> *a)
{
cout << a->a << endl;
}
*/
};
std::vector<Base*> v;
int main()
{
v.push_back(new ElementA<VisitorA>());
for (auto i : v)
i->visit();
}
This works fine and its output is 5 (as expected). But that I pretend is to make the same but directly with the second (commented) version of the "visit" in VisitorA.
Obviously, this doesn't work because "this" has the type ElementBase<...>*.
How can I downcast the pointer "this" to the actual derived class inside ElementBase?
Like user786653 says, the Curiously Recurring Template Pattern can solve this
template<typename Visitor, typename Derived>
struct ElementBase : public Base
{
void visit()
{
_e.visit(static_cast<Derived*>(this));
}
private:
Visitor _e;
};
// Atoms.
template<typename Visitor>
struct ElementA : public ElementBase<Visitor, ElementA<Visitor> >
{
ElementA() : a(5) {}
int a;
};
// Visitors.
struct VisitorA
{
void visit(ElementA<VisitorA> *a)
{
cout << a->a << endl;
}
};
I'm trying to figure out a way to dynamically cast an instance of a child class to its parent in a somewhat difficult set of conditions.
Specifically, I have a an object hierarchy that looks something like (I've simplified a lot, so if something doesn't make sense, it might be due to the simplification):
class Object {
public:
virtual ~Object() {}
};
// shown just to give an idea of how Object is used
class IntObject: public Object {
protected:
int value;
public:
IntObject(int v) { value = v; }
int getValue() { return value; }
};
template <class T>
class ObjectProxy: public Object {
protected:
T *instance;
public:
ObjectProxy(T *instance): instance(instance) {}
T *getInstance() { return instance; }
};
The ObjectProxy class essentially acts as a wrapper to allow other types to be used in the Object hierarchy. Specifically, it allows pointers to class instances to be kept, and used later when invoking the instance's methods. For example, suppose I have:
class Parent {
protected:
int a;
public:
Parent(int v) { a = v; }
virtual ~Parent() {}
void setA(int v) { a = v; }
int getA() { return a; }
};
class Child: public Parent {
protected:
int b;
public:
Child(int v1, int v2): Parent(v1) { b = v2; }
void setA(int v) { b = v; }
int getB() { return b; }
};
I might use them in the following situation:
template <typename C>
void callFn(std::list<Object *> &stack, std::function<void (C*)> fn) {
Object *value = stack.front();
stack.pop_front();
ObjectProxy<C> *proxy = dynamic_cast<ObjectProxy<C> *>(value);
if (proxy == nullptr) {
throw std::runtime_error("dynamic cast failed");
}
fn(proxy->getInstance());
}
void doSomething(Parent *parent) {
std::cout << "got: " << parent->getA() << std::endl;
}
int main() {
std::list<Object *> stack;
// this works
stack.push_back(new ObjectProxy<Child>(new Child(1, 2)));
callFn<Child>(stack, doSomething);
// this will fail (can't dynamically cast ObjectProxy<Child> to ObjectProxy<Parent>)
stack.push_back(new ObjectProxy<Child>(new Child(1, 2)));
callFn<Parent>(stack, doSomething);
}
As noted in the above comments, this code fails for a known reason. In the sample code, it's easy to avoid invoking callFn<Parent>(stack, doSomething). However, in my real code, I am using the signature of the function to determine type, and if its a method for the parent class, that will automatically be used for the template parameter.
My question is if there is any way to achieve the dynamic cast from ObjectProxy from an object of type of ObjectProxy. Part of the complication comes from the fact that in the function callFn, you only have the Parent type and not the child type.
I looked into using type-erasure via boost::any (i.e. ObjectProxy stops being templated, and instead has boost::any instance), but still ran into problems when it came to dynamic-casting (boost::any_cast is static). I did find mention to a dynamic_any on SO, but have not gotten it to work properly yet.
Any help or insight into the problem is greatly appreciated.
The dynamic cast is failing because the classes that are instantiations of ObjectProxy do not share the same hierarchy as the types given in the parameterisation of ObjectProxy. I see two approaches that may help. One, you make the types given to ObjectProxy share a single common base class and move the dynamic cast away from ObjectProxy and onto the instances.
namespace approach2 {
struct object_t {
virtual ~object_t() { }
};
struct required_base_t {
virtual ~required_base_t() { }
};
class object_proxy_base_t : public object_t {
required_base_t* instance_;
public:
object_proxy_base_t(required_base_t* i) : instance_ (i) { }
template <class T>
T* cast_to() const
{
return dynamic_cast<T*>(instance_);
}
};
template <class value_t>
class object_proxy_t : public object_proxy_base_t {
value_t* instance_;
public:
object_proxy_t(value_t* i)
: object_proxy_base_t (i),
instance_ (i)
{
}
};
template <class value_t>
object_t* new_with_proxy(value_t const& value)
{
return new object_proxy_t<value_t>(new value_t(value));
}
struct parent_t : required_base_t {
virtual ~parent_t() { }
};
struct child_t : parent_t {
virtual ~child_t() { }
};
void f()
{
object_t* a = new_with_proxy(parent_t());
object_t* b = new_with_proxy(child_t());
std::cout
<< dynamic_cast<object_proxy_base_t*>(a)->cast_to<parent_t>() << '\n' // works
<< dynamic_cast<object_proxy_base_t*>(b)->cast_to<parent_t>() << '\n' // works
;
}
}
This approach is not possible if you cannot change the base classes of all types used by ObjectProxy. Which leads to the second solution where you make ObjectProxy instantiations have the same hierarchy as the types used to parameterise it.
namespace approach3 {
struct object_t {
virtual ~object_t() { }
};
struct empty_t {
template <class T>
empty_t(T*) { }
};
template <class value_t>
class object_proxy_t : public virtual object_t {
value_t* instance_;
public:
object_proxy_t(value_t* i) : instance_ (i) { }
};
template <class value_t, class base_t>
class object_proxy_sub_t :
public object_proxy_t<value_t>,
public base_t {
public:
object_proxy_sub_t(value_t* i)
: object_proxy_t<value_t>(i),
base_t (i)
{
}
};
template <class base_t, class value_t>
object_t* new_with_proxy(value_t const& value)
{
return new object_proxy_sub_t<value_t, base_t>(new value_t(value));
}
struct parent_t {
virtual ~parent_t() { }
};
struct child_t : parent_t {
virtual ~child_t() { }
};
void f()
{
object_t* a = new_with_proxy<empty_t>(parent_t());
object_t* b = new_with_proxy<object_proxy_t<parent_t> >(child_t());
std::cout
<< dynamic_cast<object_proxy_t<parent_t>*>(a) << '\n' // works
<< dynamic_cast<object_proxy_t<parent_t>*>(b) << '\n' // works
;
}
}
This approach places fewer requirements on the types involved but means more work to keep the hierarchies in sync.
Building off of Bowie Owen's first answer, I realized that while the types given would likely not be derived from the same class (it's a library), I could force that to occur:
struct ObjectProxyBaseType {
virtual ~ObjectProxyBaseType() {}
};
template <class T>
class ObjectProxyType: public ObjectProxyBaseType, public T {
public:
// allow construction via parameters
template <typename... Args>
ObjectProxyType(Args &&... args): T(std::move(args)...) {}
// or construction via copy constructor
ObjectProxyType(T *t): T(*t) {}
virtual ~ObjectProxyType() {}
};
Thus, if I have class Child, I can create an instance of ObjectProxyType<Child>, which causes it to also inherit ObjectProxyBaseType. The rest of the code follows Bowie's suggestion:
class ObjectProxy: public Object {
protected:
ObjectProxyBaseType *instance;
public:
template <typename T>
ObjectProxy(ObjectProxyType<T> *i) {
instance = i;
}
template <typename T>
ObjectProxy(T *value) {
instance = new ObjectProxyType<T>(value);
}
template <typename T>
T *castTo() const {
return dynamic_cast<T *>(instance);
}
};
And an example of code that works:
int main() {
std::list<Object *> stack;
stack.push_back(new ObjectProxy(new Child(1, 2)));
callFn<Child>(stack, doSomething);
stack.push_back(new ObjectProxy(new Child(5, 6)));
callFn<Parent>(stack, doSomething);
}
I've had to do something somewhat similar recently. I've used an approach which worked for me, but might not be appropriate in this case; use your discretion. This hinges on the fact that you (or the person extending this code, if any) have full knowledge of what hierarchies will be used as template parameters.
So let's say these hierarchies are the following:
class Parent1
class Child1: public Parent1
class Child11: public Child1
...
class Parent2
class Child2: public Parent2
...
Then you build a holder class. It is a bit complicated for a simple reason - my compiler doesn't support default template parameters on functions, so I am using helper structs to enable SFINAE.
This class needs to be able to hold objects belonging to all hierarchies (through a base class pointer).
class TypeHolder
{
template<class T, class E=void>
struct GetHelper
{
static T* Get(const TypeHolder* th) { return nullptr; }
//you can actually add code here to deal with non-polymorphic types through this class as well, if desirable
};
template<class T>
struct GetHelper<T, typename std::enable_if<std::is_polymorphic<T>::value, void>::type>
{
static T* Get(const TypeHolder* th)
{
switch(th->type)
{
case P1: return dynamic_cast<T*>(th->data.p1);
case P2: return dynamic_cast<T*>(th->data.p2);
//and so on...
default: return nullptr;
}
}
};
template<class T, class E=void>
struct SetHelper
{
static void Set(T*, TypeHolder* th) { th->type = EMPTY; }
};
template<class T>
struct SetHelper<T, typename std::enable_if<std::is_polymorphic<T>::value, void>::type>
{
static void Set(T* t, TypeHolder* th)
{
th->data.p1 = dynamic_cast<Parent1*>(t);
if(th->data.p1) { th->type = P1; return; }
th->data.p2 = dynamic_cast<Parent2*>(t);
if(th->data.p2) { th->type = P2; return; }
//...and so on
th->type = EMPTY;
}
};
public:
TypeHolder(): type(EMPTY) { }
template<class T>
T* GetInstance() const
{
return GetHelper<T>::Get(this);
}
template<class T>
void SetInstance(T* t)
{
SetHelper<T>::Set(t, this);
}
private:
union
{
Parent1* p1;
Parent2* p2;
//...and so on
} data;
enum
{
EMPTY,
P1,
P2
//...and so on
} type;
};
By the way, the reason we need the SFINAE trick is because of the dynamic_casts, which will not compile on non-polymorphic types.
Now all you need to do is modify your classes just a little bit :)
class ObjectProxyBase
{
public:
virtual const TypeHolder& GetTypeHolder() const = 0;
};
template<class T>
class ObjectProxy: public Object, public ObjectProxyBase
{
T* instance;
static TypeHolder th; //or you can store this somewhere else, or make it a normal (but probably mutable) member
public:
ObjectProxy(T* t): instance(t) { }
T* getInstance() const { return instance; }
const TypeHolder& GetTypeHolder() const { th.SetInstance(instance); return th; }
//... and the rest of the class
};
template<class T>
TypeHolder ObjectProxy<T>::th;
I hope this code is actually correct, since I mostly typed it into the browser window (mine used different names).
And now for the final piece: the function.
template <typename C>
void callFn(std::list<Object *> &stack, std::function<void (C*)> fn) {
Object *value = stack.front();
stack.pop_front();
ObjectProxyBase *proxy = dynamic_cast<ObjectProxyBase *>(value);
if (proxy == nullptr) {
throw std::runtime_error("dynamic cast failed");
}
C* heldobj = proxy->GetTypeHolder().GetInstance<C>(); //I used to have a dynamic_cast here but it was unnecessary
if (heldobj == nullptr) {
throw std::runtime_error("object type mismatch");
}
fn(heldobj);
}
You only need to use this approach for hierarchies, and can still use the dynamic_cast directly to ObjectProxy<C>* in other cases (essentially, you'll want to try both and see if one succeeds).
I hope this is at least a little bit helpful.