【问题标题】:C++ template metaprogramming to create a boost::variant from a shared_ptr and a boost::static_visitorC++ 模板元编程从 shared_ptr 和 boost::static_visitor 创建 boost::variant
【发布时间】:2010-09-09 04:08:58
【问题描述】:

作为个人练习,我想使用 shared_ptr 实现访问者模式。我熟悉 Robert Martin 的非循环访问者论文,但发现虚拟 accept() 的侵入性以及为每个 {X} 类创建 {X}Visitor 类的必要性令人不快。我喜欢 boost::static_visitor 类,因为它在本地封装了所有逻辑,无需 {X}::accept() 和 {X}Visitor。

我正在寻找的是关于如何创建模板函数 function rip 我在下面提到的提示(正如我所说,我正在做这个练习)。我认为它应该是这样的形式:

template <typename U, typename T1, typename T2, ...>
boost::variant<T1, T2, ...> rip(U& p, boost::static_visitor<T1, T2, ...> sv)
{
    if (T1 t1 = dynamic_cast<T1>(p)) return boost::variant<T1, ...>(t1);
    ... and so on, splitting static_visitor
    return 0;  // or throw an exception
}

任何提示或指向做类似事情的教程将不胜感激。谢谢。

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <boost/bind.hpp>
#include <boost/variant.hpp>


struct Base {};
struct A : Base {};
struct B : Base {};
struct C : Base {};

typedef std::shared_ptr<Base> base_ptr;
typedef boost::variant<A*,B*,C*> base_variant;
struct variant_visitor : public boost::static_visitor<void> {
    void operator()(A*, base_ptr) const {std::cout << "A*\n";}
    void operator()(B*, base_ptr) const {std::cout << "B*\n";}
    void operator()(C*, base_ptr) const {std::cout << "C*\n";}
};


int main(int, char**)
{
    // This works, of course.
    base_ptr b(new A());
    base_variant v(new A());
    boost::apply_visitor(boost::bind(variant_visitor(), _1, b), v);

    // How could we use a shared_ptr with a variant?  I almost see
    // the template magic, a function to iterate over the template
    // types from the variant_visitor and return an "any<...>".
    // base_variant rip(base_ptr&, variant_visitor) {...}
    // boost::apply_visitor(boost::bind(variant_visitor(), _1, b), rip(b, variant_visitor()));

    return EXIT_SUCCESS;
}

【问题讨论】:

    标签: c++ boost shared-ptr visitor-pattern boost-variant


    【解决方案1】:

    我可能误解了这个问题,但是如果您想对包含共享指针而不是普通指针的变体使用相同的variant_visitor,也许这可以通过另一个从 shared_ptr 获取指针的访问者来实现将其传递给其他访问者。

    #include <algorithm>
    #include <cstdlib>
    #include <iostream>
    #include <boost/shared_ptr.hpp>
    #include <boost/bind.hpp>
    #include <boost/variant.hpp>
    
    
    struct Base {};
    struct A : Base {};
    struct B : Base {};
    struct C : Base {};
    
    typedef boost::shared_ptr<Base> base_ptr;
    typedef boost::variant<boost::shared_ptr<A>,boost::shared_ptr<B>,boost::shared_ptr<C> > base_variant;
    
    template <class Visitor>
    struct visit_shared_ptr_get: public boost::static_visitor<typename Visitor::result_type>
    {
        //for unary visitors
        template <class FirstArg>
        typename Visitor::result_type operator()(FirstArg& first) const
        {
            return Visitor()(first.get());
        }  
    
        //for binary visitors, only the first argument is "ripped"
        template <class FirstArg, class SecondArg>
        typename Visitor::result_type operator()(FirstArg& first, SecondArg& second) const
        {
            return Visitor()(first.get(), second);
        }  
    };
    
    struct variant_visitor : public boost::static_visitor<void> {
        void operator()(A*, base_ptr) const {std::cout << "A*\n";}
        void operator()(B*, base_ptr) const {std::cout << "B*\n";}
        void operator()(C*, base_ptr) const {std::cout << "C*\n";}
    };
    
    
    int main(int, char**)
    {
        // This works, of course.
        base_ptr b(new A());
        base_variant v(boost::shared_ptr<A>(new A()));
        boost::apply_visitor(boost::bind(visit_shared_ptr_get<variant_visitor>(), _1, b), v);
        return EXIT_SUCCESS;
    }
    

    编辑:一个你似乎想象中的沮丧者。

    #include <stdexcept>
    #include <boost/type_traits.hpp>
    #include <boost/utility/enable_if.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/variant.hpp>
    #include <boost/variant/variant_fwd.hpp>
    #include <boost/preprocessor/repetition.hpp>
    
    //dynamic_cast will only compile if the target type is a pointer
    template <class Derived, class Base, class Variant>
    typename boost::enable_if<boost::is_pointer<Derived>, bool>::type cast_if_pointer( Base* b, Variant& variant)
    {
        if (Derived p = dynamic_cast<Derived>(b)) { variant = p; return true; }
        return false;
    }
    
    //weeds out boost's unused template parameters and other non-pointers
    template <class Derived, class Base, class Variant>
    typename boost::disable_if<boost::is_pointer<Derived>, bool>::type cast_if_pointer( Base*, Variant& )
    {
        return false;
    }
    
    template <class P, BOOST_VARIANT_ENUM_PARAMS(class T)>
    void rip(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& variant, const boost::shared_ptr<P>& smart_ptr)
    {
    #define ATTEMPT_CAST(z, n, type) if (cast_if_pointer<T ## n >(smart_ptr.get(), variant)) return;
        BOOST_PP_REPEAT(BOOST_VARIANT_LIMIT_TYPES, ATTEMPT_CAST, T)
    #undef ATTEMPT_CAST
        throw std::bad_cast();
    }
    
    struct Base 
    {
        virtual ~Base() {}
    };
    
    struct A : Base {};
    struct B : Base {};
    struct C : Base {};
    
    
    typedef boost::shared_ptr<Base> base_ptr;
    typedef boost::variant<A*,B*,C*> base_variant;
    
    
    int main(int, char**)
    {
        base_ptr b(new A());
        base_variant v;
        rip(v, b);
    
        return EXIT_SUCCESS;
    }
    

    【讨论】:

    • 感谢您的回答。请参阅我对 UncleBen 解决方案的评论。我正在玩的是如何将 base_ptr 转换为未命名的 any 值,其唯一目的是充当 variant_visitor::operator() 的鉴别器。不使用 any 值; operator() 将使用传递给它的 base_ptr。
    • @themis:那么您正在寻找的方法是将基指针 向下转换 指向变体中合适的指针类型?我在预处理器库中添加了一些东西,但我对此有很大的怀疑。如果将基类型列在最派生类型之前,则它的性能可能相当差,并且可能会给出错误的结果。
    【解决方案2】:

    您不能使用访问者进行类型推断,因为它只有一个模板参数 - 用于 result_type。它的operator() 可以或不能在任意boost::variant 可能存储的所有类型上调用。

    我只是猜测以下可能是您正在寻找的(我第一次使用 boost 的预处理器库):

    #include <algorithm>
    #include <cstdlib>
    #include <iostream>
    #include <memory>
    #include <boost/bind.hpp>
    #include <boost/variant.hpp>
    
    #include <boost/variant/variant_fwd.hpp>
    #include <boost/preprocessor/repetition.hpp>
    
    #define SHARED_PTR_TO_PTR(z, n, text) BOOST_PP_COMMA_IF(n) typename shared_ptr_to_ptr<text ## n>::type
    
    template <class T>
    struct shared_ptr_to_ptr { typedef T type; };
    
    template <class T>
    struct shared_ptr_to_ptr<std::shared_ptr<T> > {typedef T* type; };
    
    template <class T>
    struct unsmartify_variant;
    
    
    template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
    struct unsmartify_variant<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T) > >
    {
        typedef boost::variant<BOOST_PP_REPEAT(BOOST_VARIANT_LIMIT_TYPES, SHARED_PTR_TO_PTR, T)> type;
    };
    
    template <class Variant>
    struct get_visitor: boost::static_visitor<typename unsmartify_variant<Variant>::type >
    {
        template <class SharedPtr>
        typename unsmartify_variant<Variant>::type operator()(SharedPtr& p) const
        {
            return p.get();
        }
    };
    
    template <class Variant>
    typename unsmartify_variant<Variant>::type rip(Variant& variant)
    {
        return boost::apply_visitor(get_visitor<Variant>(), variant);
    }
    
    struct Base {};
    struct A : Base {};
    struct B : Base {};
    struct C : Base {};
    
    typedef std::shared_ptr<Base> base_ptr;
    typedef boost::variant<std::shared_ptr<A>,std::shared_ptr<B>,std::shared_ptr<C> > base_variant;
    
    struct variant_visitor : public boost::static_visitor<void> {
        void operator()(A*, base_ptr) const {std::cout << "A*\n";}
        void operator()(B*, base_ptr) const {std::cout << "B*\n";}
        void operator()(C*, base_ptr) const {std::cout << "C*\n";}
    };
    
    int main(int, char**)
    {
        // This works, of course.
        base_ptr b(new A());
        base_variant v(std::shared_ptr<A>(new A()));
        unsmartify_variant<base_variant>::type k = rip(v);
        boost::apply_visitor(boost::bind(variant_visitor(), _1, b), k);
        return EXIT_SUCCESS;
    }
    

    最大的问题是boost::variant 接受固定数量的参数。

    另外,variant 似乎通过非常量引用传递给apply_visitor,这意味着rip 的结果必须存储在命名变量中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多