fork download
  1. #include <map>
  2. #include <iostream>
  3. namespace mylib
  4. {
  5. enum visual
  6. {
  7. v_binary,
  8. v_decimal
  9. };
  10. }
  11. namespace
  12. {
  13. ::std::map< std::ostream const*, mylib::visual> _association;
  14. }
  15. namespace mylib
  16. {
  17. template<visual _v>
  18. struct manip
  19. {
  20. const static visual v = _v;
  21. };
  22. const manip<v_binary> binary;
  23. const manip<v_decimal> decimal;
  24. template<visual _v>
  25. ::std::ostream& operator<< (::std::ostream& os, manip<_v> const& manipulator)
  26. {
  27. ::_association[&os] = manipulator.v;
  28. return os;
  29. }
  30. class storage
  31. {
  32. char _c;
  33. public:
  34. storage(char c = 0) : _c(c) {}
  35. void set(char c) { _c = c; }
  36. char get() const { return _c; }
  37. };
  38. ::std::ostream& operator<< (::std::ostream& os, storage const& data)
  39. {
  40. if(::_association[&os] == v_binary)
  41. {
  42. os << data.get();
  43. }
  44. else if(::_association[&os] == v_decimal)
  45. {
  46. os << static_cast<int>(data.get());
  47. }
  48. else
  49. {
  50. // enum-exception
  51. }
  52. return os;
  53. }
  54. }
  55. int main()
  56. {
  57. mylib::storage inst('h');
  58. std::cout << mylib::binary << inst << '\n';
  59. std::cout << mylib::decimal << inst << '\n';
  60. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
h
104