streamcharsource.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef STREAMCHARSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  2. #define STREAMCHARSOURCE_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 "yaml-cpp/noncopyable.h"
  9. #include <cstddef>
  10. namespace YAML {
  11. class StreamCharSource {
  12. public:
  13. StreamCharSource(const Stream& stream) : m_offset(0), m_stream(stream) {}
  14. StreamCharSource(const StreamCharSource& source)
  15. : m_offset(source.m_offset), m_stream(source.m_stream) {}
  16. ~StreamCharSource() {}
  17. operator bool() const;
  18. char operator[](std::size_t i) const { return m_stream.CharAt(m_offset + i); }
  19. bool operator!() const { return !static_cast<bool>(*this); }
  20. const StreamCharSource operator+(int i) const;
  21. private:
  22. std::size_t m_offset;
  23. const Stream& m_stream;
  24. StreamCharSource& operator=(const StreamCharSource&); // non-assignable
  25. };
  26. inline StreamCharSource::operator bool() const {
  27. return m_stream.ReadAheadTo(m_offset);
  28. }
  29. inline const StreamCharSource StreamCharSource::operator+(int i) const {
  30. StreamCharSource source(*this);
  31. if (static_cast<int>(source.m_offset) + i >= 0)
  32. source.m_offset += i;
  33. else
  34. source.m_offset = 0;
  35. return source;
  36. }
  37. }
  38. #endif // STREAMCHARSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66