fork(1) download
  1. #include <iostream>
  2. #define ERROR_VALUES ERROR_VALUE(NO_ERROR, 0, "Everything is fine")\
  3.   ERROR_VALUE(FILE_NOT_FOUND, 1, "File is not found")\
  4.   ERROR_VALUE(LABEL_UNINITIALISED, 2, "A component tried to the label before it was initialised")\
  5.   ERROR_VALUE(UKNOWN_ERROR, -1, "Uh oh")
  6.  
  7. enum class Error
  8. {
  9. #define ERROR_VALUE(NAME,VALUE,DESCR) NAME=VALUE,
  10. ERROR_VALUES
  11. #undef ERROR_VALUE
  12. };
  13.  
  14. inline std::ostream& operator<<(std::ostream& os, Error err)
  15. {
  16. int errVal = static_cast<int>(err);
  17. switch (err)
  18. {
  19. #ifndef PRODUCTION_BUILD // Don't print out names in production builds
  20. #define ERROR_VALUE(NAME,VALUE,DESCR) case Error::NAME: return os << "[" #VALUE "]" #NAME <<"; " << DESCR;
  21. ERROR_VALUES
  22. #undef ERROR_VALUE
  23. #endif
  24. default:
  25. return os <<errVal;
  26. }
  27. }
  28.  
  29. int main() {
  30. std::cout << "Error: " << Error::NO_ERROR << std::endl;
  31. std::cout << "Error: " << Error::FILE_NOT_FOUND << std::endl;
  32. std::cout << "Error: " << Error::LABEL_UNINITIALISED << std::endl;
  33. std::cout << "Error: " << Error::UKNOWN_ERROR << std::endl;
  34. return 0;
  35. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Error: [0]NO_ERROR; Everything is fine
Error: [1]FILE_NOT_FOUND; File is not found
Error: [2]LABEL_UNINITIALISED; A component tried to the label before it was initialised
Error: [-1]UKNOWN_ERROR; Uh oh