! Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at beginning of input. Does not support changing the underlying std::streambuf in mid-input. Maintains underlying std::istream and std::streambuf to support subsequent use of standard std::istream operations to process any input characters following those used in parsing the JSON input.
| 4788 | subsequent call for input from the std::istream. |
| 4789 | */ |
| 4790 | class input_stream_adapter |
| 4791 | { |
| 4792 | public: |
| 4793 | using char_type = char; |
| 4794 | |
| 4795 | ~input_stream_adapter() |
| 4796 | { |
| 4797 | // clear stream flags; we use underlying streambuf I/O, do not |
| 4798 | // maintain ifstream flags, except eof |
| 4799 | if (is != nullptr) |
| 4800 | { |
| 4801 | is->clear(is->rdstate() & std::ios::eofbit); |
| 4802 | } |
| 4803 | } |
| 4804 | |
| 4805 | explicit input_stream_adapter(std::istream& i) |
| 4806 | : is(&i), sb(i.rdbuf()) |
| 4807 | {} |
| 4808 | |
| 4809 | // delete because of pointer members |
| 4810 | input_stream_adapter(const input_stream_adapter&) = delete; |
| 4811 | input_stream_adapter& operator=(input_stream_adapter&) = delete; |
| 4812 | input_stream_adapter& operator=(input_stream_adapter&& rhs) = delete; |
| 4813 | |
| 4814 | input_stream_adapter(input_stream_adapter&& rhs) noexcept : is(rhs.is), sb(rhs.sb) |
| 4815 | { |
| 4816 | rhs.is = nullptr; |
| 4817 | rhs.sb = nullptr; |
| 4818 | } |
| 4819 | |
| 4820 | // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to |
| 4821 | // ensure that std::char_traits<char>::eof() and the character 0xFF do not |
| 4822 | // end up as the same value, eg. 0xFFFFFFFF. |
| 4823 | std::char_traits<char>::int_type get_character() |
| 4824 | { |
| 4825 | auto res = sb->sbumpc(); |
| 4826 | // set eof manually, as we don't use the istream interface. |
| 4827 | if (JSON_HEDLEY_UNLIKELY(res == EOF)) |
| 4828 | { |
| 4829 | is->clear(is->rdstate() | std::ios::eofbit); |
| 4830 | } |
| 4831 | return res; |
| 4832 | } |
| 4833 | |
| 4834 | private: |
| 4835 | /// the associated input stream |
| 4836 | std::istream* is = nullptr; |
| 4837 | std::streambuf* sb = nullptr; |
| 4838 | }; |
| 4839 | |
| 4840 | // General-purpose iterator-based adapter. It might not be as fast as |
| 4841 | // theoretically possible for some containers, but it is extremely versatile. |