C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
spanstream.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "spanbuf.hpp"
26
27#include <concepts>
28#include <istream>
29#include <ranges>
30#include <span>
31#include <type_traits>
32
33namespace essence::io {
34 template <typename BaseStream, std::ios_base::openmode Mode>
35 class basic_spanstream_impl : public BaseStream {
36 public:
37 explicit basic_spanstream_impl(
38 std::span<typename BaseStream::char_type> buffer, std::ios_base::openmode mode = Mode)
39 : BaseStream{&spanbuf_}, spanbuf_{buffer, (mode | Mode)} {}
40
41 void span(std::span<typename BaseStream::char_type> buffer) noexcept {
42 this->spanbuf_.span(buffer);
43 }
44
45 protected:
47 };
48
49 template <typename CharT, typename Traits = std::char_traits<CharT>>
50 struct basic_ispanstream : basic_spanstream_impl<std::basic_istream<CharT, Traits>, std::ios_base::in> {
52 using base_type::base_type;
53
54 template <std::ranges::borrowed_range Range>
55 requires(
56 std::convertible_to<Range, std::span<const CharT>> && !std::convertible_to<Range, std::span<CharT>>)
57 explicit basic_ispanstream(Range&& range, std::ios_base::openmode mode = std::ios_base::in)
58 : base_type{std::span{const_cast<CharT*>(std::ranges::data(range)), std::ranges::size(range)}, mode} {}
59
60 [[nodiscard]] std::span<const typename base_type::char_type> span() const noexcept {
61 return this->spanbuf_.span();
62 }
63
64 template <std::ranges::borrowed_range Range>
65 requires(
66 std::convertible_to<Range, std::span<const CharT>> && !std::convertible_to<Range, std::span<CharT>>)
67 void span(Range&& range) noexcept {
68 return this->spanbuf_.span(
69 std::span{const_cast<CharT*>(std::ranges::data(range)), std::ranges::size(range)});
70 }
71 };
72
73 template <typename CharT, typename Traits = std::char_traits<CharT>>
74 struct basic_ospanstream : basic_spanstream_impl<std::basic_ostream<CharT, Traits>, std::ios_base::out> {
76 using base_type::base_type;
77
78 [[nodiscard]] std::span<typename base_type::char_type> span() const noexcept {
79 return this->spanbuf_.span();
80 }
81 };
82
83 template <typename CharT, typename Traits = std::char_traits<CharT>>
85 : basic_spanstream_impl<std::basic_iostream<CharT, Traits>, std::ios_base::in | std::ios_base::out> {
86 using base_type =
87 basic_spanstream_impl<std::basic_iostream<CharT, Traits>, std::ios_base::in | std::ios_base::out>;
88 using base_type::base_type;
89
90 [[nodiscard]] std::span<typename base_type::char_type> span() const noexcept {
91 return this->spanbuf_.span();
92 }
93 };
94
98} // namespace essence::io
Definition spanbuf.hpp:31
Definition spanstream.hpp:35
Definition spanstream.hpp:50
Definition spanstream.hpp:74
Definition spanstream.hpp:85