fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. using word = unsigned;
  7. const word DD_MASK=100;
  8. struct Cpu {
  9. void Clr(word x) { std::cout<<"Clr "<<x<<endl; };
  10. void Com(word x) { std::cout<<"Com "<<x<<endl; };
  11. };
  12.  
  13. using InstrFunc = void (Cpu::*)(word);
  14. //using InstrFunc = std::function<void(Cpu*, word)>;
  15. struct InstructionDescription
  16. {
  17. std::string name;
  18. word mask;
  19. word code;
  20. InstrFunc func;
  21. word flags;
  22. };
  23.  
  24.  
  25. int main() {
  26. vector<InstructionDescription> instructions {
  27. {"clr", DD_MASK, 0005000, &Cpu::Clr},
  28. {"clrb", DD_MASK, 0105000, &Cpu::Clr},
  29. {"com", DD_MASK, 0005100, &Cpu::Com}
  30. };
  31. Cpu cpu;
  32. for (auto& i:instructions) {
  33. // i.func(&cpu, i.code); // as simple as that with std::function
  34. (cpu.*i.func)(i.code);
  35. }
  36. }
Success #stdin #stdout 0s 4372KB
stdin
Standard input is empty
stdout
Clr 2560
Clr 35328
Com 2624