1

I'm in an early stage of a C++ project and I wanted to create a Python interface for it. The C++ library uses templated classes to achieve compile time polymorphism. However, porting it to Python would require me to list all the possible template cases and result in a huge binary.

So I'm wondering what is the best way to move from compile time polymorphism to runtime. The basic structure of the library is as follows.

The main classes are child of a template class called Algorithm, that has 3 template arguments

template <template <class> class A, class B, typename data_t>
Algorithm {
 protected:
  ....
  std::vector<A<B>> unique_values;
  std::vector<data_t> data;
  std::shared_ptr<B> params;
  ....

 public:
  update() {
    for (auto& val: unique_values)
        val.update(data);

    if (condition)
      unique_values.push_back(A(params));

    for (int i=unique_values.size(); i > 0; i++)
      if (condition)
        unique_values.erase(unique_values.begin() + i);

    params->update(unique_values);
  }

different classes for the template class A differ mainly on what kind of data structure they store and how they perform the update. Here is an example

template<class UnivParams>
class AUniv {
 protected:
   double mu;
   double sig; // (mu and sig are the 'state')
   std::shared_ptr<UnivParams> params;

 public:
  void update(std::vector<double> data) {
    mu = ...;
    sig = ...;
  }

  std::tuple<double, double> get_state() {
    return std::make_tuple(mu, sig)
  }
}

template<class MultiParams>
class AUniv {
 protected:
   Eigen::VectorXd mu;
   Eigen::MatrixXd sig; // (mu and sig are the 'state')
   std::shared_ptr<MultiParams> params;

 public:
  void update(std::vector<Eigen::VectorXd> data) {
    mu = ...;
    sig = ...;
  }

  std::tuple<Eigen::VectorXd, Eigen::VectorXd> get_state() {
    return std::make_tuple(mu, sig)
  }
}

The goal is to achieve full runtime polymorphism, to create a Python interface for this library. I'm quite unsure on how to proceed, but my guess is that this is rather a common dilemma and many people might have already faced it. My question here is how to refactor my code (maybe create factories?) so that the update() method of the Algorithm class can be performed on a wide range of different classes As and Bs.

Thanks!

mariob6
  • 469
  • 1
  • 6
  • 16
  • 1
    as you say you can use factory desigen pattern or you can you [typeid](https://en.cppreference.com/w/cpp/language/typeid) and [dynamic_cast](https://en.cppreference.com/w/cpp/language/dynamic_cast) and here a good quistion abute [dynamic cast vs static cast](https://stackoverflow.com/questions/2253168/dynamic-cast-and-static-cast-in-c) – yaodav Jun 16 '20 at 07:02

0 Answers0