fork(1) download
  1. #include <map>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // build formatters
  6. // in this case just assign int value
  7. enum FormatterType {
  8. simple_formatter,
  9. integer_formatter,
  10. decimal_formatter,
  11. currency_formatter,
  12. date_formatter,
  13. date_local_formatter,
  14. time_formatter,
  15. time_local_formatter,
  16. enum_formatter
  17. };
  18.  
  19. // build formatter index
  20. map<string, FormatterType> index = {
  21. { "text", simple_formatter },
  22. { "integer", integer_formatter },
  23. { "decimal", decimal_formatter },
  24. { "currency", currency_formatter },
  25. { "date", date_formatter },
  26. { "date-local", date_local_formatter },
  27. { "time", time_formatter },
  28. { "time-local", time_local_formatter },
  29. { "enum", enum_formatter }
  30. };
  31.  
  32. FormatterType resolveFormatter(string name)
  33. {
  34. auto it = index.find(name);
  35. return (it != index.end()) ? it->second : simple_formatter;
  36. }
  37.  
  38. int main() {
  39. auto f1 = resolveFormatter("integer");
  40. auto f2 = resolveFormatter("zzzz");
  41. auto f3 = resolveFormatter("date");
  42. cout << "f1 = " << (int)f1 << endl;
  43. cout << "f2 = " << (int)f2 << endl;
  44. cout << "f3 = " << (int)f3 << endl;
  45. return 0;
  46. }
Success #stdin #stdout 0s 16056KB
stdin
Standard input is empty
stdout
f1 = 1
f2 = 0
f3 = 4