.. _input_source: ******************** codecs::input_source ******************** Defined in header ````. .. code-block:: cpp template concept input_source = meta::movable && requires(T& is, O dst, mgs::ssize_t n) meta::default_constructible; meta::output_iterator; { is.read(dst, n) } -> std::pair>; }; The **input_source** concept specifies that a type provides sequenced reads of input into a :ref:`output_iterator`. ---- Notation ======== * **is** - value of type ``T&`` * **dst** - value of type ``O`` * **n** - value of type :ref:`ssize_t` Constraints =========== * :ref:`movable` Template arguments ================== Definitions ----------- .. table:: :align: left ===== ============================================= **O** Iterator to be filled with the source's input ===== ============================================= Constraints ----------- .. table:: :align: left ===== ========================================================================= **O** :ref:`meta::output_iterator\ ` ===== ========================================================================= Member types ============ Definitions ----------- .. table:: :align: left ================ ===================================================== **element_type** The type of elements which will be written in ``dst`` ================ ===================================================== Constraints ----------- .. table:: :align: left ================ ============================ **element_type** :ref:`default_constructible` ================ ============================ Valid expressions ================= .. table:: :align: left =================== ========================================================================== Expression Return type =================== ========================================================================== **is.read(dst, n)** :ref:`std::pair\> ` =================== ========================================================================== Expression semantics ==================== .. table:: :align: left =================== ================================================================== Expression Semantics =================== ================================================================== **is.read(dst, n)** | Reads at most ``n`` bytes of input into ``dst``. | Returns a pair containing: | * an iterator which should be given to subsequent ``read`` calls | * the number of bytes written, ``0`` being EOF. =================== ================================================================== Concept emulation ================= .. code-block:: cpp namespace mgs { namespace codecs { template struct is_input_source { /* ... */ }; template constexpr auto is_input_source_v = is_input_source::value; template >> using input_source = T; } // namespace codecs } // namespace mgs Example ======= .. code-block:: cpp #include #include #include using namespace mgs; using namespace mgs::codecs; struct vector_input_source { using element_type = unsigned char; std::vector buffer; int pos = 0; vector_input_source() : buffer(4096) {} int read(unsigned char* dst, int n) { auto const to_read = std::min(n, buffer.size() - pos); std::copy_n(buffer.begin() + pos, to_read, dst); pos += to_read; return to_read; } }; static_assert(is_input_source_v, ""); See also ======== * :ref:`convertible_to` * :ref:`movable` * :ref:`output_iterator` * :ref:`ssize_t`