2

我有包含指向不同接口的 COM 指针(或智能指针)的包装类。

意图: 一些 COM 类可以从各种其他 COM 接口获得,我想制作一个具有可变参数类型的模板构造函数,这将只允许传递适当类型的参数。

就像是:

template <class T, class = typename 
   std::enable_if<std::is_base_of<IUnknown, T>::value>::type, class ... Types>
   class WithCOMptrbase
{
   protected:
   T* ptr_;

   public: 

   //construct smart pointer by copy
   WithCOMptrbase(T* ptr, bool AddRef = false)
     : ptr_(ptr)
   {
      if (AddRef) ptr_->AddRef();
   }

   /*construct a smart pointer by querying an interface from an argument of 
   a type which is the same as one of the variadics*/
   template <class TypeOther, class = typename
      std::enable_if<syd::is_same<Types... , TypeOther>::value... || 
      ...>::type> /*there needs to be a proper version*/
      WithCOMptrbase(TypeOther* ptr)
        : ptr_(cQueryInterface<T>(ptr))
      {}

//other methods
};

辅助功能:

template <class U, class = typename 
   std::enable_if<std::is_base_of<IUnknown, U>::value>::type>
   T* cQueryInterface<T>(U *ptr)
{
   T* out;
   HRESULT hr = ptr->QueryInterface(__uuidof(T), (void**)&out);
   if (!SUCCEEED(hr)) throw _com_error(hr);
   return out;
}

因此,我将定义我的包装类

class WrapperClass : protected WithCOMptrbase<IThis, IInterface1, IInterface2, IInterface3>
{
   //methods
};

到目前为止,我已经找到了这个线程: How to make a variadic is_same? 但它只是关于结构,而不是函数。我的目标是限制传递不适当的接口指针的可能性,因此不在运行时处理错误的接口错误。

更新: 由于组合比继承更可取,因此我进行了一些重新思考并决定使用模板函数而不是模板类。到目前为止,我已经设法结合给定的答案并想出了这个:

template <bool S, class Out, class Other, typename
std::enable_if<S>::type* = nullptr>
//copy-construct Smart Pointer for same Interfaces
WComPtr<Out> WFilterSame(const WComPtr<Other>& pOther)
{
    return WComPtr<Out>(pOther);
}

template <bool S, class Out, class Other, typename
    std::enable_if<!S>::type* = nullptr>
//Query Interface if differ
WComPtr<Out> WFilterSame(const WComPtr<Other>& pOther)
{
    return pOther.QueryInterface<Out>();
}

template <class Out, class ... Permitted, class Other>
WComPtr<Out> WFilterComInterfPtr(const WComPtr<Other>& pOther)
{

    static_assert(std::is_same<Out, Other>::value ||
        (std::is_same<Permitted, Other>::value || ...),
        "Interface is not supported.");

    return WFilterSame<std::is_same<Out, Other>::value, Out>(pOther);
}

现在我可以定义我的 COM 包装类的构造函数:

class WComClass
{
   private:
   WComPtr<Interface> pComPtr_; //My Smart COM pointer

   template <class Other>
   WComPtr<Interface> WFilter(const WComPtr<Other>& pOther)
   {
      return WFilterComInterfPtr<Interface, IAllowed1, IAllowed2>(pOther);
   }

   public:
   template <class Other>
   WComClass(const WComPtr<Other>& pOther)
      : pComPtr_(WFilter(pOther))
   {}

   //methods
};

到目前为止,它的行为符合预期(WFilterComInterfPtr),我不希望它在包装类构造函数中失败。

4

2 回答 2

4

尝试

   template <class TypeOther, class =
      std::enable_if_t<(std::is_same_v<Types, TypeOther> || ...)>>        
      WithCOMptrbase(TypeOther* ptr)
        : ptr_(cQueryInterface<T>(ptr))
      {}

我的意思是......你使用三个省略号而不是一个(删除后面的省略号::value和后面的省略号Types)并且你需要另外几个括号。

题外话:你确定这行得通吗

template <class T, class ... Types, class = typename 
   std::enable_if<std::is_base_of<IUnknown, T>::value>::type>
   class WithCOMptrbase

?

SFINAE 通过可变参数列表后的默认类型?

于 2018-11-15T18:27:32.320 回答
1

CRTP 如何避免一些模板:

template <typename Base, typename T>
class Impl
{
public:
    Impl() = default;
    explicit Impl(T*) { static_cast<Base*>(this)->ptr_ = cQueryInterface<T>(ptr); }
};

template <class T, class ... Ts>
class WithCOMptrbase : private Impl<WithCOMptrbase<T, Ts...>, Ts>...
{
    static_assert(std::is_base_of<IUnknown, T>::value);
    static_assert((std::is_base_of<IUnknown, Ts>::value && ...));

    template <typename, typename> friend struct Impl; // We don't have variadic friend :/
protected:
    T* ptr_ = nullptr;


public:
    using Impl<WithCOMptrbase, Ts>::Impl...;

    //construct smart pointer by copy
    explicit WithCOMptrbase(T* ptr, bool AddRef = false) : ptr_(ptr)
    {
        if (AddRef) ptr_->AddRef();
    }

    //other methods
};
于 2018-11-15T19:38:46.770 回答