fork download
  1. // demo_string.h
  2.  
  3. #ifndef INCLUDED_STRING
  4. #define INCLUDED_STRING
  5.  
  6. #include <iosfwd>
  7. #include <string>
  8.  
  9. namespace demo
  10. {
  11. // The class string is merely defined to have something which can be used
  12. // to overload the output operator.
  13. struct string
  14. : std::string {
  15. using std::string::string;
  16. };
  17. std::ostream& operator<< (std::ostream&, string const&);
  18.  
  19. std::ostream& noquote(std::ostream&);
  20. std::ostream& squote(std::ostream&);
  21. std::ostream& dquote(std::ostream&);
  22.  
  23. struct prefix
  24. : std::string {
  25. using std::string::string;
  26. };
  27. std::ostream& operator<< (std::ostream&, prefix const&);
  28. }
  29.  
  30. #endif
  31.  
  32. // demo_string.cpp
  33. // #include "demo/string.h"
  34. #include <ostream>
  35.  
  36. namespace
  37. {
  38. int stringFormatIndex() {
  39. static int rc = std::ios_base::xalloc();
  40. return rc;
  41. }
  42.  
  43. void callback(std::ios_base::event ev, std::ios_base& s, int index) {
  44. void* pword = s.pword(index);
  45. switch (ev) {
  46. case std::ios_base::erase_event:
  47. delete static_cast<std::string*>(pword);
  48. pword = 0;
  49. break;
  50. case std::ios_base::copyfmt_event:
  51. pword = new std::string(*static_cast<std::string*>(pword));
  52. break;
  53. default:
  54. break;
  55. }
  56. }
  57. }
  58.  
  59. std::ostream& demo::noquote(std::ostream& out) {
  60. out.iword(stringFormatIndex()) = 0;
  61. return out;
  62. }
  63. std::ostream& demo::squote(std::ostream& out) {
  64. out.iword(stringFormatIndex()) = '\'';
  65. return out;
  66. }
  67. std::ostream& demo::dquote(std::ostream& out) {
  68. out.iword(stringFormatIndex()) = '"';
  69. return out;
  70. }
  71.  
  72. std::ostream& demo::operator<< (std::ostream& out, demo::string const& str) {
  73. int index{stringFormatIndex()};
  74. if (out.pword(index)) {
  75. out << *static_cast<std::string const*>(out.pword(index));
  76. }
  77. char quote(out.iword(index));
  78. return quote? out << quote << str.c_str() << quote: out << str.c_str();
  79. }
  80.  
  81. std::ostream& demo::operator<< (std::ostream& out, demo::prefix const& p) {
  82. void*& pword(out.pword(stringFormatIndex()));
  83. if (pword) {
  84. *static_cast<std::string*>(pword) = p;
  85. }
  86. else {
  87. out.register_callback(&callback, stringFormatIndex());
  88. pword = new std::string(p);
  89. }
  90. return out;
  91. }
  92.  
  93. // main.cpp
  94. // #include "demo/string.h"
  95. #include <iostream>
  96.  
  97. int main()
  98. {
  99. demo::string value("whatever");
  100. std::cout << demo::prefix("value=")
  101. << value << ' '
  102. << demo::squote << value << ' '
  103. << demo::dquote << value << ' '
  104. << demo::noquote << value << ' '
  105. << '\n';
  106. ;
  107. }
  108.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
value=whatever value='whatever' value="whatever" value=whatever