【问题标题】:Flexible way of allowing both run-time and compile-time polymorphism?允许运行时和编译时多态的灵活方式?
【发布时间】:2014-08-04 15:51:21
【问题描述】:

示例情况: 我正在创建一个用于游戏开发的物理引擎。我有两种空间划分方法:

  1. 网格
  2. 四叉树

我想允许将要使用我的物理引擎的程序员:

  1. 在编译时通过模板参数选择空间分区方法

    Physics::World<Physics::Grid> world; // chosen at compile-time
    
  2. 在运行时通过多态对象选择空间分区方法

    Physics::WorldRunTime world; 
    world.setSpatialPartitioningMethod(new Physics::Grid); // chosen at run-time
    

您可以从上面的示例中注意到,我必须使用两个不同的类(World 和 WorldRunTime)。这会导致代码重复,因为我将拥有一个没有运行时多态性的 World 模板类和一个具有运行时多态性的 WorldRunTime。

这样就好了。

然而,我想知道是否有一个模式可以处理这个问题,允许程序员将某些东西作为模板参数(编译时多态性)或多态传递具有最少代码重复的对象(运行时多态性)。

某种策略模式可以在编译时或运行时选择策略?


所需的示例 (?) 代码:

{
    // *** Compile-time world
    // No overhead
    Physics::World<Physics::Policy::CompileTime, Physics::Grid> world;
}

{
    // *** Run-time world
    // Allows run-time spatial partitioning method swapping
    // Suffers from run-time polymorphism overhead
    Physics::World<Physics::Policy::RunTime> world;
    world.setSpatialPartitioningMethod(new Physics::Grid);
}

请注意我在两个块中如何使用同一个类,以避免编写两个类和重复代码。有什么办法可以实现上面的代码吗?

【问题讨论】:

  • 假设空间分区方法所花费的时间远大于选择空间分区方法所需的时间,那么为了简化代码,您应该只使用运行时选择。跨度>
  • @NeilKirk:是的,这只是一个例子。这个问题只是技术上的好奇心。
  • 也许添加一个可以用作模板参数的虚拟选项。选择伪选项后,它使用必须选择的运行时方法。 span>
  • 在您的静态多态性示例中,您的用户是否锁定在默认构造的Physics::Grid?

标签: c++ templates design-patterns c++11 polymorphism


【解决方案1】:

对于运行时多态性,使用派生各种具体策略的多态基类实例化模板。

为了在不使用运行时多态性时鼓励去虚拟化,请将具体的策略类标记为final。

【讨论】:

    【解决方案2】:

    我认为只要您愿意将Physics::Policy::CompileTime 制作为模板,这是可以实现的。这是完全未编译的,因为我没有足够的代码来尝试为实际用例进行编译。

    template <typename Child>
    struct CompileTime
    {
        void operation1(int v) { child_.operation1(v); }
        void setSpatialPartitioningMethod(PhysicsPolicy* policy) {} // No work on static policy.
    
        Child child_;
    };
    
    struct RunTime
    {
        RunTime() : policy_(0) {}
        void setSpatialPartitioningMethod(PhysicsPolicy* policy) { policy_ = policy; }
        void operation1(int v) { policy_->operation1(v); }
    
        PhysicsPolicy* policy_;
    };
    
    template <typename Policy>
    class World
    {
    public:
        void setSpatialPartitioningMethod(PhysicsPolicy* policy) { policy_.setSpatialPartitioningMethod(policy); }
    
    private:
        Policy policy_;
    };
    
    
    {
        // *** Compile-time world
        // No overhead
        Physics::World<Physics::Policy::CompileTime<Physics::Grid> > world;
    }
    
    {
        // *** Run-time world
        // Allows run-time spatial partitioning method swapping
        // Suffers from run-time polymorphism overhead
        Physics::World<Physics::Policy::RunTime> world;
        world.setSpatialPartitioningMethod(new Physics::Grid);
    }
    

    【讨论】:

      【解决方案3】:

      作为一种心理练习,我想看看我是否可以通过一个额外的限制尽可能接近您想要的语法:如果编译时间是,我不想招致 vtbl 指针或 vtbl 本身的成本选择。到目前为止,这里的其他解决方案将在运行时或编译时正确绑定,但即使您选择 Policy::CompileTime

      ,它们仍会生成 vtbl 并将 World 对象的大小增加 sizeof(pointer)

      这是迄今为止我能想到的最好的:

      这样做的一大缺点是 Grid 变成了一个模板类,因为我需要让它有选择地从 PhysicsPolicy 继承(以避免生成 vtbl)。

      在你想要的语法和下面的语法之间有一个小的差异:我需要用一个空的模板列表来实例化运行时网格,即。 new Physics::Grid&lt;&gt;

      我已经重写了 operator-> 以便 world->findObject() 将调用该方法,而不必编写一大堆额外的方法——替代方法是将 PhysicsPolicy 中每个方法的定义添加到 @ 987654323@.

      struct Point3 { float x, y, z; };
      
      namespace Physics
      {
          // This is a dummy base class used to avoid vtbl creation for Grid
          class Empty { };
      
          class PhysicsPolicy
          {
          public:
              virtual void* findObject(const Point3& p) = 0;
          };
      
          template<typename BASE=PhysicsPolicy>
          class Grid : public BASE
          {
          public:
              void* findObject(const Point3& p) { return nullptr; } // Just a placeholder
          };
      
          namespace Policy
          {
              template<template<typename> class T> class CompileTime
              {
              public:
                  T<Empty>* operator->() { return &obj; }
      
              private:
                  T<Empty> obj;
              };
      
              // This is just here so that an empty template parameter list is possible
              template<template<typename> class... T> class RunTime;
              template<> class RunTime<>
              {
              public:
                  void setSpatialPartitioningMethod(PhysicsPolicy* aP) { p = aP; }
                  PhysicsPolicy* operator->() { return p; }
      
              private:
                  PhysicsPolicy* p;
              };
          }
      
          // The desired syntax has the number of parameters dependent upon the policy
          // This is done using c++11's variadic templates, and passing the extra
          // parameters into Binding
          template<
              template<template<typename> class...> class Binding,
              template<typename> class... U>
          class World : public Binding<U...>
          {
          public:
          };
      }
      
      int main()
      {
          Physics::World<Physics::Policy::CompileTime, Physics::Grid> compileTimeWorld;
      
          Point3 p{1,2,3};
          printf("%p\n", compileTimeWorld->findObject(p));
      
          Physics::World<Physics::Policy::RunTime> runTimeWorld;
          runTimeWorld.setSpatialPartitioningMethod(new Physics::Grid<>);
          printf("%p\n", runTimeWorld->findObject(p));
      
          return 0;
      }
      

      注意:针对 clang 进行了测试。不知道它是否适用于其他编译器

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-04
        • 2011-01-10
        • 2019-10-11
        • 1970-01-01
        • 1970-01-01
        • 2017-09-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多