2016-08-15 45 views
3

問題:C++句柄/體類自動由模板參數包構成。這可以改善嗎?

多個組件希望共享相同的不可變數據(任意類型),但每個組件都有一組不同的接口要求。

例如,組件A可能要求存在稱爲hash_code(const object&)的可用功能,組件B可能要求該對象具有稱爲type(const object&)的可用功能。

如果組分C發出一個共享指針一些對象O,對於其存在稱爲hash_code可用的自由功能,並呼籲type自由功能,然後將組分C能共享組分A和B O,因爲兼容的接口可以被推斷。

現在我們已經分離了多態身份類型,沒有拷貝開銷。

我做到這一點的方法是有一個模板handle類,這是充滿了method descriptors(更多模板類):

下面是兩種方法一個完整的,工作的測試程序:

一是手動編寫句柄類,另一個是允許模板擴展爲我寫。

目前,有兩個問題:

  1. 在模板的版本,我不得不求助於虛擬繼承,以創建模型/概念層次。
  2. 描述每個方法調用需要一個包含3個相關類型的複雜類:多態概念,該概念的類型化模型以及句柄的接口組件。

必須有一個更優雅的方式......

#include <iostream> 
#include <utility> 
#include <string> 
#include <typeindex> 
#include <boost/functional/hash.hpp> 


// some arbitrary type which supports hashing and type 
struct algo1_ident 
{ 
    std::type_info const& _algo_type = typeid(algo1_ident); 
    std::string arg1; 
    std::string arg2; 
}; 

auto type(algo1_ident const& ident) -> std::type_info const& 
{ 
    return ident._algo_type; 
} 

auto hash_code(algo1_ident const& ident) -> std::size_t 
{ 
    auto seed = ident._algo_type.hash_code(); 
    boost::hash_combine(seed, ident.arg1); 
    boost::hash_combine(seed, ident.arg2); 
    return seed; 
} 


// 
// manual approach 
// 
// step one: define a complete concept 
struct algo_ident_concept 
{ 
    virtual std::type_info const & impl_type(const void* p) const = 0; 
    virtual std::size_t impl_hash_code(const void* p) const = 0; 

}; 

// 
// step two: define the model of that concept 
// 

template<class Impl> 
struct algo_ident_model : algo_ident_concept 
{ 
    std::type_info const& impl_type(const void* p) const override 
    { 
     return type(ref(p)); 
    } 

    std::size_t impl_hash_code(const void* p) const override 
    { 
     return hash_code(ref(p)); 
    } 

private: 
    static const Impl& ref(const void* p) { 
     return *static_cast<Impl const*>(p); 
    } 
}; 

// 
// step three: write the handle class 
// 
template<template <class> class Model, template <class> class PtrType> 
struct algo_ident_handle 
{ 
    template<class Impl> struct model_tag {}; 

    template<class Impl> 
    algo_ident_handle(std::shared_ptr<const Impl> ptr) 
    : _impl(std::move(ptr)) 
    , _access_model(make_access_model(model_tag<const Impl>())) 
    {} 

    template<class Impl> 
    algo_ident_concept const* make_access_model(model_tag<Impl>) 
    { 
     static struct : algo_ident_concept 
     { 
      std::type_info const& impl_type(const void* p) const override 
      { 
       using ::type; 
       return type(ref(p)); 
      } 

      std::size_t impl_hash_code(const void* p) const override 
      { 
       using ::hash_code; 
       return hash_code(ref(p)); 
      } 

     private: 
      static const Impl& ref(const void* p) { 
       return *static_cast<Impl const*>(p); 
      } 
     } const _model {}; 
     return std::addressof(_model); 
    } 

    PtrType<const void> _impl; 
    const algo_ident_concept* _access_model; 

    // 
    // interface 
    // 

    std::size_t hash_code() const { 
     return _access_model->impl_hash_code(_impl.get()); 
    } 

    std::type_info const& type() const { 
     return _access_model->impl_type(_impl.get()); 
    } 

}; 


// 
// now the componentised approach 
// 

// step 1: define the concept, model and handle interface for supporting the 
//   method `hash_code` 
//   This can go in a library 

template<class Host> 
struct has_hash_code 
{ 
    struct concept 
    { 
     virtual std::size_t hash_code(const void*) const = 0; 
    }; 

    template<class Impl> struct model : virtual concept 
    { 
     std::size_t hash_code(const void* p) const override 
     { 
      using ::hash_code; 
      return hash_code(*static_cast<const Impl*>(p)); 
     } 
    }; 

    struct interface 
    { 
     std::size_t hash_code() const 
     { 
      auto self = static_cast<const Host*>(this); 
      return self->model()->hash_code(self->object()); 
     } 
    }; 
}; 

// step 2: define the concept, model and handle interface for supporting the 
//   method `type` 
//   This can go in a library 

template<class Host> 
struct has_type 
{ 
    struct concept 
    { 
     virtual std::type_info const& type(const void*) const = 0; 
    }; 

    template<class Impl> struct model : virtual concept 
    { 
     std::type_info const& type(const void* p) const override 
     { 
      using ::type; 
      return type(*static_cast<const Impl*>(p)); 
     } 
    }; 

    struct interface 
    { 
     std::type_info const& type() const 
     { 
      auto self = static_cast<const Host*>(this); 
      return self->model()->type(self->object()); 
     } 
    }; 
}; 

// step 3: provide a means of turning a pack of methods into a concept base class 

template<class Host, template<class>class...Methods> 
struct make_concept 
{ 
    using type = struct : virtual Methods<Host>::concept... {}; 
}; 

// step 4: provide a means of turning a pack of methods into a model class 

template<class Impl, class Host, template<class>class...Methods> 
struct make_model 
{ 
    using concept_type = typename make_concept<Host, Methods...>::type; 
    using type = struct : Methods<Host>::template model<Impl>... , concept_type {}; 

    static auto apply() 
    { 
     static const type _model {}; 
     return std::addressof(_model); 
    } 
}; 

// step 5: provide a means of turning a pack of methods into an interface 

template<class Host, template<class>class...Methods> 
struct make_interface 
{ 
    using type = struct : Methods<Host>::interface... {}; 
}; 

// step 6: convenience class in which to store the object pointer and the 
//   polymorphic model 

template<class ConceptType> 
struct storage 
{ 
    storage(std::shared_ptr<const void> object, const ConceptType* concept) 
    : _object(object), _model(concept) 
    {} 

    const void* object() const { return _object.get(); } 
    const ConceptType* model() const { return _model; } 

    std::shared_ptr<void const> _object; 
    const ConceptType* _model; 
}; 

// step 7: build a handle which supports the required methods while 
//   storing a shared_ptr to the object 

template<template<class> class...Methods> 
struct handle 
: make_interface<handle<Methods...>, Methods...>::type 
{ 
    using this_class = handle; 
    using concept_type = typename make_concept<this_class, Methods...>::type; 
    using storage_type = storage<concept_type>; 

    template<class Impl> 
    static auto create_storage(std::shared_ptr<Impl> ptr) 
    { 
     using model_type = typename make_model<Impl, this_class, Methods...>::type; 
     const model_type* pm = make_model<Impl, this_class, Methods...>::apply(); 
     return storage_type(ptr, pm); 
    } 

    template<class Impl> 
    handle(std::shared_ptr<Impl> ptr) 
    : _storage(create_storage(ptr)) 
    {} 

    const void* object() const { return _storage.object(); } 
    const concept_type* model() const { return _storage.model(); } 


    storage<concept_type> _storage; 
}; 


// 
// another arbitrary object which also supports the hash_code and type protocols 

namespace algo2 { 
    struct algo2_ident 
    { 
     std::type_info const& _algo_type = typeid(algo2_ident); 
     std::string arg1 = "foo"; 
     std::string arg2 = "bar"; 
    }; 

    auto type(algo2_ident const& ident) -> std::type_info const& 
    { 
     return ident._algo_type; 
    } 

    auto hash_code(algo2_ident const& ident) -> std::size_t 
    { 
     auto seed = ident._algo_type.hash_code(); 
     boost::hash_combine(seed, ident.arg1); 
     boost::hash_combine(seed, ident.arg2); 
     return seed; 
    } 
} 

// 
// test 
// 

int main(int argc, const char * argv[]) 
{ 
    algo_ident_handle<algo_ident_model, std::shared_ptr> h1 = std::make_shared<const algo1_ident>(); 
    algo_ident_handle<algo_ident_model, std::shared_ptr> h2 = std::make_shared<const algo2::algo2_ident>(); 

    // 
    // prove that an h1 is equivalent to the object of which it is a handle 
    // 
    algo1_ident chk1 {}; 
    std::cout << h1.hash_code() << std::endl; 
    std::cout << hash_code(chk1) << std::endl; 

    algo2::algo2_ident chk {}; 

    std::cout << h2.hash_code() << std::endl; 
    std::cout << hash_code(chk) << std::endl; 

    // 
    // same proof for the composed handle 
    // 
    handle<has_hash_code, has_type> ht1 = std::make_shared<const algo2::algo2_ident>(); 
    std::cout << ht1.hash_code() << std::endl; 
    std::cout << hash_code(chk) << std::endl; 

    return 0; 
} 
+0

http://stackoverflow.com/q/38835747/1774667會產生一個值,但適應指針很容易。在這裏,我們創建了我們的概念僞方法和一個聰明的任何維護鉤子來使用它們。所有檢查都是靜態的:通過一些工作可以添加動態檢查。 – Yakk

+0

@Yakk真棒。謝謝。 –

回答

0

我可能會錯過什麼,但似乎之後做的工作:

// To avoid conflict with name and ADL. 
namespace detail 
{ 
    template <typename T> 
    decltype(auto) callHashCode(T&& t) { return hash_code(std::forward<T>(t)); } 

    template <typename T> 
    decltype(auto) callType(T&& t) { return type(std::forward<T>(t)); } 
} 

class HashRunner 
{ 
public: 
    template <typename T> 
    HashRunner(std::shared_ptr<T> p) : 
    hash_code([=](){ return detail::callHashCode(*p); }) 
    {} 

    std::function<std::size_t()> hash_code; 
}; 

class TypeRunner 
{ 
public: 
    template <typename T> 
    TypeRunner(std::shared_ptr<T> p) : 
    type([=]() -> const std::type_info& { return detail::callType(*p); }) 
    {} 

    std::function<const std::type_info&()> type; 
}; 

template <typename ... Ts> 
class MyHandle : public Ts... 
{ 
public: 
    template <typename T> 
    MyHandle(std::shared_ptr<T> p) : Ts(p)... {} 
}; 

Demo

+0

它確實完成了這項工作,但是:1它創建了共享指針的多個副本,2隨着方法數量的增加,句柄類變得越來越大,這使得它的複製輕量化。很好地使用std :: function來提供必要的多態性。 –

相關問題