StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
safe.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
45
46#include <array>
47#include <deque>
48#include <expected>
49#include <forward_list>
50#include <functional>
51#include <initializer_list>
52#include <list>
53#include <map>
54#include <memory>
55#include <optional>
56#include <queue>
57#include <set>
58#include <span>
59#include <stack>
60#include <string>
61#include <string_view>
62#include <tuple>
63#include <type_traits>
64#include <unordered_map>
65#include <unordered_set>
66#include <utility>
67#include <variant>
68#include <vector>
69
74namespace StormByte {
78 class Size;
82 class ByteSize;
86 class Exception;
87
92 namespace Safe {
96 class Binary;
100 class SecureBinary;
104 class String;
108 class WString;
113 template<class T> class Shared;
118 template<class T> class Unique;
123 template<class T> class Weak;
124 }
125
130 namespace Type {
137 template<typename T>
138 struct IsSafe: std::false_type {};
143 template<Type::Arithmetic T> struct IsSafe<T>: std::true_type {};
148 template<Type::Enum T> struct IsSafe<T>: std::true_type {};
152 template<> struct IsSafe<StormByte::Exception>: std::true_type {};
153
157 template<> struct IsSafe<Safe::Binary>: std::true_type {};
161 template<> struct IsSafe<Safe::SecureBinary>: std::true_type {};
165 template<> struct IsSafe<Safe::String>: std::true_type {};
169 template<> struct IsSafe<Safe::WString>: std::true_type {};
173 template<> struct IsSafe<Size>: std::true_type {};
177 template<> struct IsSafe<ByteSize>: std::true_type {};
182 template<typename T> struct IsSafe<Safe::Shared<T>>: std::bool_constant<IsSafe<std::remove_cvref_t<T>>::value> {};
187 template<typename T> struct IsSafe<Safe::Unique<T>>: std::bool_constant<IsSafe<std::remove_cvref_t<T>>::value> {};
192 template<typename T> struct IsSafe<Safe::Weak<T>>: std::bool_constant<IsSafe<std::remove_cvref_t<T>>::value> {};
193
198 namespace Detail {
203 template<typename T>
204 struct IsKnownNeverSafe: std::false_type {};
205
206 template<typename T> struct IsKnownNeverSafe<T*>: std::true_type {};
207 template<typename T> struct IsKnownNeverSafe<T[]>: std::true_type {};
208 template<typename T, std::size_t N> struct IsKnownNeverSafe<T[N]>: std::true_type {};
209 template<typename T, typename Traits, typename Allocator>
210 struct IsKnownNeverSafe<std::basic_string<T, Traits, Allocator>>: std::true_type {};
211 template<typename T, typename Traits>
212 struct IsKnownNeverSafe<std::basic_string_view<T, Traits>>: std::true_type {};
213 template<typename T, std::size_t N> struct IsKnownNeverSafe<std::array<T, N>>: std::true_type {};
214 template<typename T, typename Allocator> struct IsKnownNeverSafe<std::vector<T, Allocator>>: std::true_type {};
215 template<typename T, typename Allocator> struct IsKnownNeverSafe<std::deque<T, Allocator>>: std::true_type {};
216 template<typename T, typename Allocator> struct IsKnownNeverSafe<std::list<T, Allocator>>: std::true_type {};
217 template<typename T, typename Allocator> struct IsKnownNeverSafe<std::forward_list<T, Allocator>>: std::true_type {};
218 template<typename K, typename V, typename Compare, typename Allocator>
219 struct IsKnownNeverSafe<std::map<K, V, Compare, Allocator>>: std::true_type {};
220 template<typename K, typename V, typename Compare, typename Allocator>
221 struct IsKnownNeverSafe<std::multimap<K, V, Compare, Allocator>>: std::true_type {};
222 template<typename K, typename Compare, typename Allocator>
223 struct IsKnownNeverSafe<std::set<K, Compare, Allocator>>: std::true_type {};
224 template<typename K, typename Compare, typename Allocator>
225 struct IsKnownNeverSafe<std::multiset<K, Compare, Allocator>>: std::true_type {};
226 template<typename K, typename V, typename Hash, typename Equal, typename Allocator>
227 struct IsKnownNeverSafe<std::unordered_map<K, V, Hash, Equal, Allocator>>: std::true_type {};
228 template<typename K, typename V, typename Hash, typename Equal, typename Allocator>
229 struct IsKnownNeverSafe<std::unordered_multimap<K, V, Hash, Equal, Allocator>>: std::true_type {};
230 template<typename K, typename Hash, typename Equal, typename Allocator>
231 struct IsKnownNeverSafe<std::unordered_set<K, Hash, Equal, Allocator>>: std::true_type {};
232 template<typename K, typename Hash, typename Equal, typename Allocator>
233 struct IsKnownNeverSafe<std::unordered_multiset<K, Hash, Equal, Allocator>>: std::true_type {};
234 template<typename T, typename Container>
235 struct IsKnownNeverSafe<std::queue<T, Container>>: std::true_type {};
236 template<typename T, typename Container, typename Compare>
237 struct IsKnownNeverSafe<std::priority_queue<T, Container, Compare>>: std::true_type {};
238 template<typename T, typename Container>
239 struct IsKnownNeverSafe<std::stack<T, Container>>: std::true_type {};
240 template<typename T>
241 struct IsKnownNeverSafe<std::initializer_list<T>>: std::true_type {};
242 template<typename T> struct IsKnownNeverSafe<std::optional<T>>: std::true_type {};
243 template<typename... T> struct IsKnownNeverSafe<std::variant<T...>>: std::true_type {};
244 template<typename... T> struct IsKnownNeverSafe<std::tuple<T...>>: std::true_type {};
245 template<typename A, typename B> struct IsKnownNeverSafe<std::pair<A, B>>: std::true_type {};
246 template<typename T, std::size_t Extent> struct IsKnownNeverSafe<std::span<T, Extent>>: std::true_type {};
247 template<typename T> struct IsKnownNeverSafe<std::reference_wrapper<T>>: std::true_type {};
248 template<typename Signature> struct IsKnownNeverSafe<std::function<Signature>>: std::true_type {};
249 template<typename T> struct IsKnownNeverSafe<std::shared_ptr<T>>: std::true_type {};
250 template<typename T, typename Deleter> struct IsKnownNeverSafe<std::unique_ptr<T, Deleter>>: std::true_type {};
251 template<typename T> struct IsKnownNeverSafe<std::weak_ptr<T>>: std::true_type {};
252 }
253
261 template<typename T>
262 struct IsMaybeSafe: std::false_type {};
263
268 template<typename T>
269 requires requires { sizeof(T); } && DerivedFrom<T, StormByte::Exception> && (!SameAs<T, StormByte::Exception>)
270 struct IsMaybeSafe<T>: std::true_type {};
275 template<typename T>
276 struct IsMaybeSafe<Safe::Shared<T>>: std::bool_constant<
277 IsMaybeSafe<std::remove_cvref_t<T>>::value && !Detail::IsKnownNeverSafe<std::remove_cvref_t<T>>::value
278 > {};
279
283 template<typename T>
284 struct IsMaybeSafe<Safe::Unique<T>>: std::bool_constant<
285 IsMaybeSafe<std::remove_cvref_t<T>>::value && !Detail::IsKnownNeverSafe<std::remove_cvref_t<T>>::value
286 > {};
287
291 template<typename T>
292 struct IsMaybeSafe<Safe::Weak<T>>: std::bool_constant<
293 IsMaybeSafe<std::remove_cvref_t<T>>::value && !Detail::IsKnownNeverSafe<std::remove_cvref_t<T>>::value
294 > {};
295
300 template<typename T>
304
310 template<typename T>
312
319 template<typename T, typename E>
320 requires (std::is_void_v<T> || SafeComponent<T>) && SafeComponent<E>
321 struct IsMaybeSafe<std::expected<T, E>>: std::true_type {};
322
328 template<typename T>
329 struct IsSafeOptional: std::false_type {};
330
336 template<typename T>
337 struct IsSafeQueue: std::false_type {};
338
346 template<typename T>
347 struct IsSafeValue: std::false_type {};
348
352 template<> struct IsSafeValue<Safe::Binary>: std::true_type {};
356 template<> struct IsSafeValue<Safe::SecureBinary>: std::true_type {};
360 template<> struct IsSafeValue<Safe::String>: std::true_type {};
364 template<> struct IsSafeValue<Safe::WString>: std::true_type {};
368 template<> struct IsSafeValue<Size>: std::true_type {};
372 template<> struct IsSafeValue<ByteSize>: std::true_type {};
377 template<typename T>
378 requires SafeComponent<T>
379 struct IsSafeValue<Safe::Shared<T>>: std::true_type {};
384 template<Type::Enum T>
385 struct IsSafeValue<T>: std::true_type {};
390 template<Type::Arithmetic T>
391 struct IsSafeValue<T>: std::true_type {};
392
399 template<typename T>
400 concept SafeCursor = requires { typename std::remove_cvref_t<T>::StormByteSafeCursor; };
401
408 template<typename T>
412 }
413}
414
425#define STORMBYTE_DECLARE_MAYBE_SAFE(SafeType) \
426 template<> struct StormByte::Type::IsMaybeSafe<SafeType>: std::true_type {}
Count of octets (uint64_t storage).
Definition byte_size.hxx:105
Base exception type for the suite.
Definition exception.hxx:67
Owned contiguous sequence of std::byte, stored in a Vector.
Definition binary.hxx:79
Binary for secrets: every byte it drops or releases is zeroed first.
Definition secure_binary.hxx:74
Forward declaration of shared ownership.
Definition pointers.hxx:133
Owned UTF-8 text.
Definition string.hxx:113
Forward declaration of unique ownership.
Definition pointers.hxx:343
Wide counterpart of String.
Definition wstring.hxx:104
Forward declaration of weak ownership.
Definition pointers.hxx:485
Count of abstract units (uint64_t storage).
Definition size.hxx:107
Both StormByte::Type::CopyConstructible and StormByte::Type::CopyAssignable.
Definition object_semantics.hxx:304
Type constructible from an empty initializer (T{} / T()).
Definition object_semantics.hxx:156
Recognizes a conditional Safe component after stripping cv and references.
Definition safe.hxx:301
Type that can be move-assigned (operator=(T&&)).
Definition object_semantics.hxx:273
Type that can be move-constructed.
Definition object_semantics.hxx:213
Recognized Safe component after stripping cv and references.
Definition safe.hxx:311
Recognizes a cursor that stores only a Base node pointer.
Definition safe.hxx:400
Value permitted in opaque Safe collections.
Definition safe.hxx:409
Text string types that are not generic containers.
Definition containers.hxx:106
Private helpers.
Owned values that cross a DLL without the caller's CRT.
Named concepts and small type utilities used across the suite.
Root namespace of the StormByte suite.
STL namespace.
Identifies standard-library ownership and view wrappers that Base will not admit.
Definition safe.hxx:204
Recognizes types whose DLL-boundary safety depends on documented provider requirements.
Definition safe.hxx:262
Identifies Safe optional wrappers.
Definition safe.hxx:329
Identifies Safe FIFO wrappers.
Definition safe.hxx:337
Recognizes collection values with Base-controlled lifetime.
Definition safe.hxx:347
Recognizes Base Safe components, not universal ABI compatibility.
Definition safe.hxx:138