Copych
Copych

Reputation: 21

How can I rewrite this code using CRTP approach?

I had a static hard-coded test code. After that I have transformed it into dynamic binding to test the performance, and virtual calls appeared to be approximately two times slower.

#include <vector>

class vUnit {
  public:
    virtual ~vUnit() {;}
    virtual float process(float f) = 0;
};

class vRack {
  public:
    vRack() {;}
    ~vRack() {;}
    
    inline void addUnit(vUnit* unit_ptr) {
      _units.push_back(unit_ptr);
    }
    
    inline float process_all(float  f) { 
      float  res = 0.;
      for (int j = 0 ; j< 60; j++) {
        for (auto u : _units) {
          f = u->process(f);
        }
      }
      res += f;
      return res;
    }

  private:
    std::vector<vUnit*>  _units ; 
    int _i = 0;
};


class vMultiplyF : public vUnit {
  public:
    vMultiplyF(float v) {_f = v;}
    inline float process(float f) final {
      return f * _f;
    }
  private:
    float  _f;
};

class vDivideF : public vUnit {
  public:
    vDivideF(float v) {_f = v;}
    inline float process(float f) final {
      if (f > 0) {
        return _f / f;
      } else return 0.0f;
    }
  private:
    float  _f;
};

int main () {
  float f = 1.5f, r = 0.0f;
  vRack       vR;
  vMultiplyF  vM(5.1f);
  vDivideF    vD(5.5f);

  vR.addUnit(&vM);
  vR.addUnit(&vD);

  for( int i = 0 ; i < 1000 ; i++) {
    f = vR.process_all(f);
    r += f;
  }
  return 0;
}

I think that CRTP could be a reasonable compromise, but I don't quite get how I can rewrite vRack.addUnit() so that it could take different vUnit implementations. Can anyone help?

Upvotes: 1

Views: 154

Answers (1)

Caleth
Caleth

Reputation: 63152

I think that CRTP could be a reasonable compromise.

CRTP doesn't help you here. You need some indirection to distinguish your different operations.

If the configuration is static, then you can swap from iterating a container of vUnits to using a fold expression to combine a tuple of specific objects.

#include <tuple>

template <typename T>
concept Unit = requires(T unit, float f) { f = unit.process(f); };

class vMultiplyF { // No base class needed
  public:
    vMultiplyF(float v) {_f = v;}
    float process(float f) {
      return f * _f;
    }
  private:
    float  _f;
};

class vDivideF { // No base class needed
  public:
    vDivideF(float v) {_f = v;}
    float process(float f) {
      if (f > 0) {
        return _f / f;
      } else return 0.0f;
    }
  private:
    float  _f;
};

template <Unit... Units>
class vRack {
  public:
    vRack(Units... units) : _units(std::move(units)...) {}
    float process_all(float  f) { 
      for (int j = 0 ; j< 60; j++) {
        // evaluates (f = unit.process(f)) for each unit in sequence
        std::apply([&](Units&... unit){ ((f = unit.process(f)), ...); }, _units);
      }
      return f;
    }

  private:
    std::tuple<Units...>  _units; 
};

int main () {
  float f = 1.5f, r = 0.0f;
  vMultiplyF  vM(5.1f);
  vDivideF    vD(5.5f);
  vRack       vR(vM, vD); // deduces vRack<vMultiplyF, vDivideF>

  for( int i = 0 ; i < 1000 ; i++) {
    f = vR.process_all(f);
    r += f;
  }
  return 0;
}

Upvotes: 5

Related Questions