|
- #pragma once
-
- #include <vector>
- #include <streambuf>
-
- namespace cppcore
- {
-
- template<typename T_data, typename T_char>
- struct basic_vector_streambuf
- : public std::basic_streambuf<T_char>
- {
- private:
- static constexpr size_t min_grow = 32; // 32 Byte
- static constexpr size_t max_grow = 1024 * 1024; // 1 MByte
-
- public:
- using base_type = std::basic_streambuf<T_char>;
- using int_type = typename base_type::int_type;
- using char_type = typename base_type::char_type;
- using traits_type = typename base_type::traits_type;
-
- using data_type = T_data;
- using vector_type = std::vector<data_type>;
-
- static_assert(
- sizeof(data_type) >= sizeof(char_type),
- "Data type of the vector needs to be equal the char type!");
-
- private:
- size_t _grow; //!< Number of bytes to grow on overflow (0 to grow dynamically)
- vector_type _buffer; //!< Buffer to store data
-
- public:
- /**
- * @brief Constructor.
- *
- * @param[in] p_grow Number of bytes to grow on overflow (0 to grow dynamically)
- */
- inline basic_vector_streambuf(size_t p_grow = 0);
-
- /**
- * @brief Get the data that was written to the vector. The data remains in the streambuf.
- */
- inline vector_type get() const;
-
- /**
- * @brief Set the vector to read data from.
- */
- template<typename T_vector>
- inline void set(T_vector&& v);
-
- /**
- * @brief Get the data that was written to the vector. The data is completely extracted from the steambuf.
- */
- inline vector_type extract();
-
- protected:
- /**
- * @brief Buffer underflow. Try to get new data to read.
- */
- int_type underflow() override;
-
- /**
- * @brief Buffer overflow. Try to allocate more memory to write to.
- */
- int_type overflow(int_type ch) override;
- };
-
- using vector_streambuf = basic_vector_streambuf<uint8_t, char>;
-
- }
-
- #include "vector_streambuf.inl"
|