3

メソッド定義を作成することは可能ですか (正確な数のパラメーターと既知の型の戻り値を使用)。

  • メソッドの引数の型が可変引数パックで「固定」されています
  • 推定メソッドの戻り値の型
  • メソッド名 (マクロに渡される?)

詳細:

メンバー関数の戻り値の型と引数の型を推測する単純なリフレクション構造 (読みやすくするために部分的な特殊化は省略) があります。

template<typename RetType, typename ...ArgTypes>
struct reflect_method<RetType(HostClassType::*)(ArgTypes...)> {
  using method_type = RetType;
  using method_args = type_placeholder<ArgTypes...>;
  using call_type = RetType(HostClassType::*)(ArgTypes...);
};

method_typeメソッドの戻り値の型method_argsで、ヘルパー テンプレート struct で「凍結」されたメソッドの引数の型type_placeholderです。

私がやろうとしているのは、生成されたクラスに、引数を反映し、他のクラスの別のメソッドの型を返すメソッドを作成することです。作成されたメソッドは、反映されたメソッドの装飾を提供します。

擬似コードの実装:

#define RPCCLASS(class_name)    class RPC##class_name : public class_name   \
                                {                                           \
                                  using SelfType = RPC##class_name;         \
                                  using ParentType = class_name;

#define RPCCLASS_END()          };




#define RPCBIND(method_name)   \
    using method_name_##tag = reflect_method<decltype(ParentType::method_name)>; \
    method_name_##tag::method_type
    method_name(method_name_##tag::method_args::at<0>::type arg0, \
                method_name_##tag::method_args::at<1>::type arg1, \
                /* ... */                                         \
                /*don't know how to put correct number of arguments here)*/)    \
    {                                                                           \
      /* do some stuff */                                                       \
      /* ... */                                                                 \
      /* invoke the reflected method */                                         \
      return Invoke<method_name_##tag>::apply(this, method_name,                \
                                              arg0,                             \
                                              arg1                              \
             /*again don't know how to put correct number of arguments here)*/) \
     }


 // USAGE:
 class MyOwnClass {
 public:
   virtual long long doFun(int a, char b, const std::string& c);
 };


 RPCCLASS(MyOwnClass)
   RPCBIND(doFun)
 RPCCLASS_END()
4

1 に答える 1