Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclass of class that inherits from enable_shared_from_this returning shared_ptr of self

Tags:

c++

I'm wondering if there is anyway to do something like this pseudo code:

class A : public std::enable_shared_from_this<A> {
    public:
        std::shared_ptr<self_t> getPtr(){
            return std::static_pointer_cast<self_t>(shared_from_this());
        }
};

class B : public A {
    std::vector<A> container;

    std::shared_ptr<self_t> addChild(A child){
        container.push_back(child);
        return getPtr();
    }
};

class C : public B {
    public:
        std::shared_ptr<self_t> doSomething(){
            // something
            return getPtr();
        }
};

int main(){
    A obja = new A();
    C obj = new C();
    obj->addChild(obja)->doSomething()
}

My goal is that an object represents a view (as in the V in MVC), and for methods to be able to return itself for chained calling. Eg: ->setTop(0)->addChild(child1)->setBottom(0).

I've read that it may be more approachable to do something like overloading the << operator, but I don't see that working to well or looking very pretty.

One thought I had was to make a macro called VIEW(name,parent) that would use templates to expand out, but I had issue with self-refferental default template arguments.

Any help would be greatly appreciated.

-- edit --

In a nut shell, I'm hoping to have a base class inherit from enable_shared_from_this. The base class would have a method such as doSomething that returns the shared pointer. When a derived class inherits from the base class, I want the doSomething method to return a shared pointer to the derived class. I want to be able to do this without overriding doSomething in the derived class.

like image 297
Kyle Avatar asked Nov 10 '22 00:11

Kyle


1 Answers

This kind of concept would be covered by extension methods which exist outside a class definition, do not violate the class permissions but can be called like a method... which exists in C# but not currently in C++. The code in C# looks like this:

// c# syntax
namespace MyBaseExtensions {
    public static class MyBaseExt {
        public static shared_ptr<T> getPtr<T>(this T self) where T : MyBase 
        {
            return static_pointer_cast<T>(self.shared_from_this());
        }
    }
}

This allows for operator chaining because each inheritance of a class line MyBase would have its own definition of the function because the function is not an inherited method but instead applied directly to each related type.

The argument against is that extensions pollute the object with often unneeded functionality and that a standalone template function will do the same thing. The issue is that with that logic:

int main(){
    A obja = new A();
    C obj = new C();
    obj->getPtr()->addChild(obja)->doSomething()
}

ends up looking like

int main(){
    A obja = new A();
    C obj = new C();
    doSomething(addChild(getPtr(obj),obja)); //eyeroll.
}

and you would still be declaring the template functions such like

// C++ syntax
namespace MyBaseExtensions {
    template<typename T> std::shared_ptr<T> getPtr<T>(T self)
    {
        return std::static_pointer_cast<T>(self->shared_from_this());
    }
}

As for a simple internal way of applying a template uniquely to each derived type, I am not sure of any. The reason for this is that the functionality you want is not method inheritance but that each future class inherits a template which it automatically specializes(and of which the resulting method is either not inherited or hidden.) For that purpose C++ classes would need to have non-inherited specialized public methods, which are not covered by the current access permissions public, private, and protected or template capabilities.

I would be overjoyed to find a nice way to pull off operator chaining.

And since I have gone and wasted your time I made an attempt at this:

#include <vector>
#include <memory>

// 0 argument, creates an overload method (and hides parent class method)
//           from template method func_name
// template method specialization of a parent method does not work
//      so we use C++11 automatic type deduction to figure the      
//         template return type and return what the template returns
#define FUNC_DEF_0(base, cur, func_name) \
    auto func_name() \
            -> decltype(base().func_name<cur>()) { \
        return base::func_name<cur>(); \
    }

// 1 argument
#define FUNC_DEF_1(base, cur, func_name, arg1_t) \
    auto func_name(arg1_t param1) \
            -> decltype(base().func_name<cur>(param1)) { \
        return base::func_name<cur>(param1); \
    }

// class A
// add to class to hide class A methods
#define HIDE_A(current) \
    FUNC_DEF_0(A, current, getPtr)

class A : public std::enable_shared_from_this<A> {
public:
    template<typename _T = A>
    std::shared_ptr<_T> getPtr(){
        return std::static_pointer_cast<_T>(shared_from_this());
    }
};


// class B
// add to class to hide class B methods with new methods
#define HIDE_B(current) \
    HIDE_A(current) \
    FUNC_DEF_1(B, current, addChild, A)

class B : public A {
public:
    std::vector<A> container;

    template<typename _T = B>
    std::shared_ptr<_T> addChild(A child){
        container.push_back(child);
        return A::getPtr<_T>();
    }

    HIDE_A(B); // hide A methods with B specialized methods

    // Example method hiding
    // auto getPtr() -> decltype(A().getPtr<B>()) {
    //     return base::getPtr<B>();
    // }
};


// class C
// add to class to hide class C methods
#define HIDE_C(current) \
    HIDE_B(current) \
    FUNC_DEF_0(C, current, doSomething)

class C : public B {
public:
    template<typename _T = C>
    std::shared_ptr<_T> doSomething(){
        // something
        return A::getPtr<_T>();
    }
    HIDE_B(C); // hide B methods
};

int main() {
    auto obja = std::make_shared<A>();
    auto obj = std::make_shared<C>();
    obj->addChild(*obja)->doSomething();
}

Edit: Fixed attempt. Compiles for me.

class A;

struct virtual_enable_shared_from_this_base :
    std::enable_shared_from_this<virtual_enable_shared_from_this_base> {
    virtual ~virtual_enable_shared_from_this_base() {}
};

#define HIDE_AMix(type) \
    using type::getPtr;

template<typename _T>
class AMix : public virtual virtual_enable_shared_from_this_base {
public:
    std::shared_ptr<_T> getPtr() {
        auto sptr = shared_from_this();
        return std::dynamic_pointer_cast<_T>(sptr);
    }
};

#define HIDE_BMix(type) \
    HIDE_AMix(type) \
    using type::addChild;

template<typename _T>
class BMix : public AMix<_T>{
public:
    std::vector<std::shared_ptr<A>> container;

    std::shared_ptr<_T> addChild(A* child){
        container.push_back(child->getPtr());
        return getPtr();
    }
};

#define HIDE_CMix(type) \
    HIDE_BMix(type) \
    using type::addChild;

template<typename _T>
class CMix : public BMix<_T>{
public:
    std::shared_ptr<_T> doSomething(){
        // something
        return getPtr();
    }
};


class A : public AMix<A> {
public:

};

class B : public A, public BMix<B> {
public:
    HIDE_AMix(BMix<B>);
    //using BMix<B>::getPtr;
    //using BMix<B>::addChild;
};

class C : public B, public CMix<C> {
public:
    HIDE_BMix(CMix<C>);
    //using CMix<C>::getPtr;
    //using CMix<C>::addChild;
    //using CMix<C>::doSomething;
};

int main() {
    auto obja = std::make_shared<B>();
    auto obj = std::make_shared<C>();
    obja->getPtr();
    obj->addChild(obja.get())->doSomething();
}

Edit2: Here is another version from fiddling around with templates.

like image 72
Gamenotcore Avatar answered Nov 15 '22 13:11

Gamenotcore