Skip to content

juhyun-nam/invocation-binder

Repository files navigation

invocation-binder

Project Status: Active – The project has reached a stable, usable state and is being actively developed. Build Status Language grade: C/C++ Total alerts Codacy Badge codecov

목적

Enum과 멤버 메소드의 mapped function table을 자동으로 생성.

The Gist

enum class State {
  kON,
  kOFF
};
int main() {
  Object obj;
  obj.Call(State::kON);  // callON
  obj.Call(State::kOFF);  // callOFF
}

AS-IS

struct Object {
  // 반복적인 함수 정의
  void CallON() { state_ = State::kON; }
  void CallOFF() { state_ = State::kOFF; }

  using StateMap = std::map<State, void(Object::*)()>;
  void Call(State s) {
    static StateMap fn_table {
      // element 추가시 실수할 가능성이 있음
      {State::kON, &Object::CallON},
      {State::kOFF, &Object::CallOFF}
    };
    // mapping 구조체에 따라 추가적 연간 가능성
    auto elem = fn_table.find(s);
    this->*(elem->second)();
  }
  State state_{};
};

TO-BE

#include "invocation_binder.h"
// CRTP
struct Object : public InvocationBinder<Object, State> {
  // template 함수로 State 타입의 tag 역할
  template <State S>
  void Invoke() {
    state_ = S;
  }
  State state_{};
};
// element 순서, 추가 고려 등을 하지 않는다.
// 명시적 인스턴스화
template void Object::Invoke<State::kON>();
template void Object::Invoke<State::kOFF>();

About

A function table generator using CRTP

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published