[ create a new paste ] login | about

Link: http://codepad.org/5OxQ2e6b    [ raw code | fork ]

C++, pasted on Dec 24:
enum op_code
{
    DO_OPERATION_1 = 6,
    DO_OPERATION_2 = 12,
    DO_OPERATION_6 = 12,
    DO_OPERATION_BLA = 7,
    DO_OPERATION_UNBOUND = 8,
};

namespace enums
{
  // declaration of the enum values in an MPL vector
  template<> struct get_values< op_code > { typedef boost::mpl::vector_c< op_code, DO_OPERATION_1, DO_OPERATION_2, DO_OPERATION_BLA, DO_OPERATION_UNBOUND > type; };

  // declaration of the handlers signatures for our enum
  template<> struct get_signature< op_code > { typedef signature< void, std::string const&, std::string const& > type; };
}

// declaration of the handlers and binding to the dispatcher
void do_opcode_1(std::string const& param1, std::string const& param2) { std::cout << "doing opcode 1" << std::endl; }
template<> enums::signature_of< op_code > const& enums::get_handler< op_code, DO_OPERATION_1 >::type::value = do_opcode_1;

void do_opcode_2(std::string const& param1, std::string const& param2) { std::cout << "doing opcode 2" << std::endl; }
template<> enums::signature_of< op_code > const& enums::get_handler< op_code, DO_OPERATION_2 >::type::value = do_opcode_2;

void do_opcode_bla(std::string const& param1, std::string const& param2) { std::cout << "doing opcode bla" << std::endl; }
template<> enums::signature_of< op_code > const& enums::get_handler< op_code, DO_OPERATION_BLA >::type::value = do_opcode_bla;


int main(int argc, char const *argv[])
{
  typedef enums::indexer< op_code > op_code_indexer;
  typedef enums::dispatcher< op_code > op_code_dispatcher;

  {
    std::cout << "indexer::values array:" << std::endl;
    size_t i = 0;
    op_code_indexer::values_type constexpr values = op_code_indexer::values;
    for(auto const& e : values) {
      std::cout << "  " << i << ": " << e << std::endl;
      i++;
    }
  }

  {
    std::cout << "indexer::index array:" << std::endl;
    size_t i = op_code_indexer::detail::begin;
    op_code_indexer::index_type constexpr index = op_code_indexer::index;
    for(auto const& e : index) {
      std::cout << "  " << i << " -> " << e << std::endl;
      i++;
    }
  }

  std::cout << "handlers addresses:" << std::endl;
  std::cout << "  do_opcode_1:     " << reinterpret_cast< void* >(&do_opcode_1) << std::endl;
  std::cout << "  do_opcode_2:     " << reinterpret_cast< void* >(&do_opcode_2) << std::endl;
  std::cout << "  do_opcode_bla:   " << reinterpret_cast< void* >(&do_opcode_bla) << std::endl;
  std::cout << "  not_implemented: " << reinterpret_cast< void* >(&enums::signature_of< op_code >::not_implemented) << std::endl;

  {
    std::cout << "dispatcher::handlers array:" << std::endl;
    size_t i = 0;
    for(auto const& e : op_code_dispatcher::handlers) {
      std::cout << "  " << i << ": " << reinterpret_cast< void* >(*e.target< decltype(&enums::signature_of< op_code >::not_implemented) >()) << std::endl;
      i++;
    }
  }

  op_code_dispatcher dispatcher;
  dispatcher[DO_OPERATION_1]("foo", "bar");
  dispatcher[DO_OPERATION_2]("foo", "bar");
  dispatcher[DO_OPERATION_BLA]("foo", "bar");
  dispatcher[DO_OPERATION_UNBOUND]("foo", "bar");

  dispatcher[op_code(123)]("foo", "bar");

  return 0;
}


Create a new paste based on this one


Comments: