C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
bitstream_type_hint.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 "../../abi/string.hpp"
26
27#include <concepts>
28#include <cstddef>
29#include <memory>
30#include <span>
31#include <string_view>
32#include <type_traits>
33#include <utility>
34
35namespace essence::io::abstract {
40 public:
41 template <typename T>
42 requires(!std::same_as<std::decay_t<T>, bitstream_type_hint>)
43 explicit bitstream_type_hint(T&& value) : wrapper_{std::make_shared<wrapper<T>>(std::forward<T>(value))} {}
44
49 [[nodiscard]] abi::string name() const {
50 return wrapper_->name();
51 }
52
57 [[nodiscard]] std::span<const abi::string> file_extensions() const {
58 return wrapper_->file_extensions();
59 }
60
65 [[nodiscard]] std::size_t leading_signature_size() const {
66 return wrapper_->leading_signature_size();
67 }
68
73 [[nodiscard]] std::span<const std::byte> leading_signature() const {
74 return wrapper_->leading_signature();
75 }
76
81 [[nodiscard]] std::string_view leading_signature_str() const {
82 return wrapper_->leading_signature_str();
83 }
84
89 [[nodiscard]] std::size_t trailing_signature_size() const {
90 return wrapper_->trailing_signature_size();
91 }
92
97 [[nodiscard]] std::span<const std::byte> trailing_signature() const {
98 return wrapper_->trailing_signature();
99 }
100
105 [[nodiscard]] std::string_view trailing_signature_str() const {
106 return wrapper_->trailing_signature_str();
107 }
108
109 private:
110 struct base {
111 virtual ~base() = default;
112 virtual abi::string name() = 0;
113 virtual std::span<const abi::string> file_extensions() = 0;
114 virtual std::size_t leading_signature_size() = 0;
115 virtual std::span<const std::byte> leading_signature() = 0;
116 virtual std::string_view leading_signature_str() = 0;
117 virtual std::size_t trailing_signature_size() = 0;
118 virtual std::span<const std::byte> trailing_signature() = 0;
119 virtual std::string_view trailing_signature_str() = 0;
120 };
121
122 template <typename T>
123 class wrapper final : public base {
124 public:
125 template <std::convertible_to<T> U>
126 explicit wrapper(U&& value) : value_{std::forward<U>(value)} {}
127
128 abi::string name() override {
129 return value_.name();
130 }
131
132 std::span<const abi::string> file_extensions() override {
133 return value_.file_extensions();
134 }
135
136 std::size_t leading_signature_size() override {
137 return value_.leading_signature_size();
138 }
139
140 std::span<const std::byte> leading_signature() override {
141 return value_.leading_signature();
142 }
143
144 std::string_view leading_signature_str() override {
145 return value_.leading_signature_str();
146 }
147
148 std::size_t trailing_signature_size() override {
149 return value_.trailing_signature_size();
150 }
151
152 std::span<const std::byte> trailing_signature() override {
153 return value_.trailing_signature();
154 }
155
156 std::string_view trailing_signature_str() override {
157 return value_.trailing_signature_str();
158 }
159
160 private:
161 T value_;
162 };
163
164 std::shared_ptr<base> wrapper_;
165 };
166} // namespace essence::io::abstract
Hints the type of bitstream.
Definition bitstream_type_hint.hpp:39
std::size_t trailing_signature_size() const
Gets the size of the trailing signature.
Definition bitstream_type_hint.hpp:89
std::span< const std::byte > leading_signature() const
Gets the leading byte signature of the type.
Definition bitstream_type_hint.hpp:73
std::size_t leading_signature_size() const
Gets the size of the leading signature.
Definition bitstream_type_hint.hpp:65
std::string_view leading_signature_str() const
Gets the leading byte signature of the type as a string.
Definition bitstream_type_hint.hpp:81
std::string_view trailing_signature_str() const
Gets the trailing byte signature of the type as a string.
Definition bitstream_type_hint.hpp:105
std::span< const abi::string > file_extensions() const
Gets the file extensions of the type.
Definition bitstream_type_hint.hpp:57
abi::string name() const
Gets the name of the type.
Definition bitstream_type_hint.hpp:49
std::span< const std::byte > trailing_signature() const
Gets the trailing byte signature of the type.
Definition bitstream_type_hint.hpp:97