abstract_field.h 1.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef __FIELD_H__
#define __FIELD_H__

#include <type_traits>

namespace tinygb {

// AbstractField and AbstractFieldElement are the API for fields and their
// elements. Concrete implementations of fields are implemented in subclasses
// via static polymorphism.

template <typename T> // CRTP
class AbstractField {
  public:
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
15 16
  std::string description() const {
    return static_cast<T*>(this)->description();
17 18
  }

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  template <typename U> // CRTP
  class Element {
    public:
    typedef T Parent;

    void operator+=(const Element<U>& e) {
      static_cast<T*>(this)->operator+=(e);
    }
    void operator-=(const Element<U>& e) {
      static_cast<T*>(this)->operator-=(e);
    }
    void operator*=(const Element<U>& e) {
      static_cast<T*>(this)->operator*=(e);
    }
    void operator/=(const Element<U>& e) {
      static_cast<T*>(this)->operator/=(e);
    }
    void inv() {
      static_cast<T*>(this)->inv();
    }
    bool IsZero() const {
      return static_cast<T*>(this)->isZero();
    }
  };
43 44 45 46 47
};

}  // namespace tinygb
#endif