kwsys_ios_sstream.h.in 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. #ifndef @KWSYS_NAMESPACE@_ios_sstream
  11. #define @KWSYS_NAMESPACE@_ios_sstream
  12. #include <@KWSYS_NAMESPACE@/Configure.hxx>
  13. /* Define this macro temporarily to keep the code readable. */
  14. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  15. # define kwsys_stl @KWSYS_NAMESPACE@_stl
  16. #endif
  17. #if @KWSYS_NAMESPACE@_IOS_USE_SSTREAM
  18. # ifdef _MSC_VER
  19. # pragma warning (push, 1)
  20. # pragma warning (disable: 4702)
  21. # endif
  22. # include <sstream>
  23. # ifdef _MSC_VER
  24. # pragma warning(pop)
  25. # endif
  26. #else
  27. # ifdef _MSC_VER
  28. # pragma warning (push, 1)
  29. # pragma warning (disable: 4702)
  30. # pragma warning (disable: 4995) /* Old streams are deprecated. */
  31. # endif
  32. # if @KWSYS_NAMESPACE@_IOS_USE_ANSI
  33. # include <strstream>
  34. # elif @KWSYS_NAMESPACE@_IOS_USE_STRSTREAM_H
  35. # include <strstream.h>
  36. # elif @KWSYS_NAMESPACE@_IOS_USE_STRSTREA_H
  37. # include <strstrea.h>
  38. # endif
  39. # if @KWSYS_NAMESPACE@_IOS_USE_ANSI
  40. # include <new> // Need placement operator new.
  41. # else
  42. # include <new.h> // Need placement operator new.
  43. # endif
  44. # ifdef _MSC_VER
  45. # pragma warning(pop)
  46. # endif
  47. // Only have old std::strstream classes. Wrap them to look like new
  48. // ostringstream and istringstream classes.
  49. # include <@KWSYS_NAMESPACE@/stl/string>
  50. namespace @KWSYS_NAMESPACE@_ios
  51. {
  52. using @KWSYS_NAMESPACE@_ios_namespace::streambuf;
  53. using @KWSYS_NAMESPACE@_ios_namespace::ostream;
  54. using @KWSYS_NAMESPACE@_ios_namespace::istream;
  55. using @KWSYS_NAMESPACE@_ios_namespace::istrstream;
  56. using @KWSYS_NAMESPACE@_ios_namespace::ostrstream;
  57. using @KWSYS_NAMESPACE@_ios_namespace::ios;
  58. using @KWSYS_NAMESPACE@_ios_namespace::endl;
  59. using @KWSYS_NAMESPACE@_ios_namespace::ends;
  60. using @KWSYS_NAMESPACE@_ios_namespace::flush;
  61. class ostringstream_cleanup
  62. {
  63. public:
  64. ostringstream_cleanup(ostrstream& ostr): m_OStrStream(ostr) {}
  65. ~ostringstream_cleanup() { m_OStrStream.rdbuf()->freeze(0); }
  66. static void IgnoreUnusedVariable(const ostringstream_cleanup&) {}
  67. protected:
  68. ostrstream& m_OStrStream;
  69. private:
  70. void operator=(ostringstream_cleanup const&);
  71. };
  72. class ostringstream: public ostrstream
  73. {
  74. public:
  75. typedef ostrstream Superclass;
  76. ostringstream() {}
  77. ostringstream(const kwsys_stl::string& s) { *this << s.c_str(); }
  78. kwsys_stl::string str()
  79. {
  80. ostringstream_cleanup cleanup(*this);
  81. ostringstream_cleanup::IgnoreUnusedVariable(cleanup);
  82. int pcount = this->pcount();
  83. const char* ptr = this->Superclass::str();
  84. return kwsys_stl::string(ptr?ptr:"", pcount);
  85. }
  86. void str(const kwsys_stl::string& s)
  87. {
  88. this->~ostringstream();
  89. new (this) ostringstream(s);
  90. }
  91. private:
  92. ostringstream(const ostringstream&);
  93. void operator=(const ostringstream&);
  94. };
  95. #if defined(_MSC_VER)
  96. # pragma warning (push)
  97. # pragma warning (disable: 4097) /* typedef-name used as synonym for class */
  98. #endif
  99. class istringstream: private kwsys_stl::string, public istrstream
  100. {
  101. public:
  102. typedef kwsys_stl::string StdString;
  103. typedef istrstream IStrStream;
  104. istringstream(): StdString(),
  105. IStrStream(const_cast<char*>(StdString::c_str())) {}
  106. istringstream(const kwsys_stl::string& s):
  107. StdString(s), IStrStream(const_cast<char*>(StdString::c_str())) {}
  108. kwsys_stl::string str() const { return *this; }
  109. void str(const kwsys_stl::string& s)
  110. {
  111. this->~istringstream();
  112. new (this) istringstream(s);
  113. }
  114. private:
  115. istringstream(const istringstream&);
  116. void operator=(const istringstream&);
  117. };
  118. #if defined(_MSC_VER)
  119. # pragma warning (pop)
  120. #endif
  121. } // namespace @KWSYS_NAMESPACE@_ios
  122. #endif
  123. /* Undefine temporary macro. */
  124. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  125. # undef kwsys_stl
  126. #endif
  127. #endif