ostream_wrapper.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #ifndef OSTREAM_WRAPPER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  2. #define OSTREAM_WRAPPER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  3. #if defined(_MSC_VER) || \
  4. (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
  5. (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
  6. #pragma once
  7. #endif
  8. #include <string>
  9. #include <vector>
  10. #include "yaml-cpp/dll.h"
  11. namespace YAML {
  12. class YAML_CPP_API ostream_wrapper {
  13. public:
  14. ostream_wrapper();
  15. explicit ostream_wrapper(std::ostream& stream);
  16. ostream_wrapper(const ostream_wrapper&) = delete;
  17. ostream_wrapper(ostream_wrapper&&) = delete;
  18. ostream_wrapper& operator=(const ostream_wrapper&) = delete;
  19. ostream_wrapper& operator=(ostream_wrapper&&) = delete;
  20. ~ostream_wrapper();
  21. void write(const std::string& str);
  22. void write(const char* str, std::size_t size);
  23. void set_comment() { m_comment = true; }
  24. const char* str() const {
  25. if (m_pStream) {
  26. return nullptr;
  27. } else {
  28. m_buffer[m_pos] = '\0';
  29. return &m_buffer[0];
  30. }
  31. }
  32. std::size_t row() const { return m_row; }
  33. std::size_t col() const { return m_col; }
  34. std::size_t pos() const { return m_pos; }
  35. bool comment() const { return m_comment; }
  36. private:
  37. void update_pos(char ch);
  38. private:
  39. mutable std::vector<char> m_buffer;
  40. std::ostream* const m_pStream;
  41. std::size_t m_pos;
  42. std::size_t m_row, m_col;
  43. bool m_comment;
  44. };
  45. template <std::size_t N>
  46. inline ostream_wrapper& operator<<(ostream_wrapper& stream,
  47. const char (&str)[N]) {
  48. stream.write(str, N - 1);
  49. return stream;
  50. }
  51. inline ostream_wrapper& operator<<(ostream_wrapper& stream,
  52. const std::string& str) {
  53. stream.write(str);
  54. return stream;
  55. }
  56. inline ostream_wrapper& operator<<(ostream_wrapper& stream, char ch) {
  57. stream.write(&ch, 1);
  58. return stream;
  59. }
  60. } // namespace YAML
  61. #endif // OSTREAM_WRAPPER_H_62B23520_7C8E_11DE_8A39_0800200C9A66