ORIGINAL
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1#ifndef TYPES_H
2#define TYPES_H
3
4#include <type_traits>
5#include <concepts>
6#include <iosfwd>
7#include <functional>
8#include "config.h"
9
22namespace original {
23
24 // ==================== Fundamental Types ====================
25
33 class none {
34 public:
36 consteval explicit none() = default;
37
39 constexpr ~none() = default;
40
45 consteval explicit operator bool() const;
46
51 consteval bool operator!() const;
52 };
53
54 // ==================== Core Concepts ====================
55
68 template<typename... ARGS>
69 concept NotNull = sizeof...(ARGS) > 0;
70
76 template<typename TYPE>
77 concept EnumType = std::is_enum_v<TYPE>;
78
84 template<typename TYPE>
85 concept EnumClassType = std::is_enum_v<TYPE> && !std::is_convertible_v<TYPE, std::underlying_type_t<TYPE>>;
86
87 // ==================== Comparison Concepts ====================
88
95 template <typename T>
96 concept EqualityComparable = requires(const T& a, const T& b) {
97 { a == b } -> std::convertible_to<bool>;
98 { a != b } -> std::convertible_to<bool>;
99 };
100
107 template <typename T>
108 concept WeaklyOrdered = requires(const T& a, const T& b) {
109 { a < b } -> std::convertible_to<bool>;
110 { a <= b } -> std::convertible_to<bool>;
111 { a > b } -> std::convertible_to<bool>;
112 { a >= b } -> std::convertible_to<bool>;
113 };
114
121 template <typename T>
123
130 template <typename T>
132
139 template <typename T>
140 concept ThreeWayComparable = requires(const T& a, const T& b) {
141 { a <=> b } -> std::convertible_to<std::partial_ordering>;
142 };
143
150 template <typename T>
151 concept StronglyOrdered = requires(const T& a, const T& b) {
152 { a <=> b } -> std::convertible_to<std::strong_ordering>;
153 };
154
155 // Alias of TotallyComparable
156 template <typename T>
158
159 // ==================== Stream Concepts ====================
160
173 template <typename TYPE>
174 concept Printable = requires(std::ostream& os, const TYPE& t) {
175 { os << t } -> std::same_as<std::ostream&>;
176 };
177
184 template <typename TYPE>
185 concept InputStreamable = requires(std::istream& is, TYPE& t) {
186 { is >> t } -> std::same_as<std::istream&>;
187 };
188
194 template <typename TYPE>
196
197 // ==================== Hash Concepts ====================
198
216 template <typename T>
217 concept Hashable =
218 requires(const T& obj) {
219 { std::hash<T>{}(obj) } -> std::convertible_to<std::size_t>;
220 };
221
228 template <typename T>
229 concept Equatable =
230 requires(const T& a, const T& b) {
231 { a == b } -> std::convertible_to<bool>;
232 } ||
233 requires(const T& a, const T& b) {
234 { a.equals(b) } -> std::convertible_to<bool>;
235 };
236
237 // ==================== Callback Concepts ====================
238
245 template <typename F, typename... Args>
246 concept Callable = requires(F&& f, Args&&... args) {
247 std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
248 };
249
265 template <typename Callback, typename ReturnType, typename... Args>
266 concept CallbackOf = requires(Callback callback, Args&&... args) {
267 { callback(std::forward<Args>(args)...) } -> std::convertible_to<ReturnType>;
268 };
269
277 template <typename Callback, typename... Args>
278 concept Predicate = CallbackOf<Callback, bool, Args...>;
279
297 template <typename Callback, typename TYPE>
299
318 template <typename Callback, typename TYPE>
320
339 template <typename Callback, typename TYPE>
341
350 template <typename Callback, typename Input, typename Output>
352
353 // ==================== Type Relationship Concepts ====================
354
372 template <typename Base, typename Derive>
373 concept SuperOf = std::is_base_of_v<Base, Derive> || std::is_same_v<Base, Derive>;
374
392 template <typename Base, typename Derive>
393 concept ExtendsOf = std::derived_from<Derive, Base> || std::is_same_v<Base, Derive>;
394
401 template <typename From, typename To>
402 concept ConvertibleTo = std::is_convertible_v<From, To>;
403
410 template <typename T, typename U>
411 concept SameAs = std::is_same_v<T, U>;
412
413 // ==================== Container Concepts ====================
414
421 template <typename C>
422 concept Container = requires(C& c) {
423 { c.begin() } -> std::input_or_output_iterator;
424 { c.end() } -> std::input_or_output_iterator;
425 { c.size() } -> std::convertible_to<std::size_t>;
426 };
427
434 template <typename C>
435 concept SequenceContainer = Container<C> && requires(C& c, typename C::value_type v) {
436 c.push_back(v);
437 c.pop_back();
438 };
439
440 // ==================== Compile-time Index Sequences ====================
441
448 template <u_integer... INTS>
450 static constexpr u_integer SIZE = sizeof...(INTS);
451 public:
456 static consteval u_integer size() noexcept;
457 };
458
465 template <u_integer NUM, u_integer... INTS>
466 class indexSequenceImpl;
467
468 template <u_integer... INTS>
469 class indexSequenceImpl<0, INTS...> {
470 public:
471 using type = indexSequence<INTS...>;
472 };
473
474 template <u_integer NUM, u_integer... INTS>
475 class indexSequenceImpl : public indexSequenceImpl<NUM - 1, NUM - 1, INTS...> {
476 public:
477 using type = indexSequenceImpl<NUM - 1, NUM - 1, INTS...>::type;
478 };
479 public:
480 template <u_integer NUM>
481 friend consteval auto makeSequence() noexcept;
482 };
483
489 template <u_integer NUM>
490 consteval auto makeSequence() noexcept; // NOLINT: Forward declaration for makeReverseSequence
491
498 template<u_integer... Indices>
499 consteval auto reverseIndexSequenceImpl(indexSequence<Indices...> seq);
500
506 template<u_integer N>
507 using makeReverseSequence = decltype(
509 );
510
511 // ==================== Function Traits ====================
512
523 template <typename Callback>
524 struct functionTraits;
525
527 template <typename R, typename... Args>
528 struct functionTraits<R(*)(Args...)> {
529 using ReturnType = R;
530 using Signature = R(Args...);
531 };
532
534 template <typename R, typename... Args>
535 struct functionTraits<R(Args...)> {
536 using ReturnType = R;
537 using Signature = R(Args...);
538 };
539
541 template <typename C>
543 private:
544 using CallType = functionTraits<decltype(&C::operator())>;
545 public:
546 using ReturnType = CallType::ReturnType;
547 using Signature = CallType::Signature;
548 };
549
551 template <typename C, typename R, typename... Args>
552 struct functionTraits<R(C::*)(Args...) const> {
553 using ReturnType = R;
554 using Signature = R(Args...);
555 };
556
558 template <typename C, typename R, typename... Args>
559 struct functionTraits<R(C::*)(Args...)> {
560 using ReturnType = R;
561 using Signature = R(Args...);
562 };
563
564} // namespace original
565
566consteval original::none::operator bool() const {
567 return false;
568}
569
570consteval bool original::none::operator!() const {
571 return true;
572}
573
574template<original::u_integer... INTS>
576 return SIZE;
577}
578
579template<original::u_integer NUM>
580consteval auto original::makeSequence() noexcept {
581 using sequence = makeIndexSequence::indexSequenceImpl<NUM>::type;
582 return sequence{};
583}
584
585template <original::u_integer... Indices>
587{
588 return indexSequence<sizeof...(Indices) - 1 - Indices...>{};
589}
590
591#endif // TYPES_H
Compile-time sequence of unsigned integers.
Definition types.h:449
static consteval u_integer size() noexcept
Gets the size of the sequence.
Definition types.h:575
Utility for generating index sequences.
Definition types.h:464
friend consteval auto makeSequence() noexcept
Creates an index sequence of given length.
A placeholder type representing the absence of a value.
Definition types.h:33
constexpr ~none()=default
Default destructor (constexpr)
consteval none()=default
Default constructor (consteval)
consteval bool operator!() const
Logical NOT operator.
Definition types.h:570
Basic concept for any callable type.
Definition types.h:246
Validates callback signature compatibility.
Definition types.h:266
Definition types.h:157
Combines Comparable and CallbackOf for comparison callbacks.
Definition types.h:298
Constraint for predicate callbacks.
Definition types.h:319
Basic container concept.
Definition types.h:422
Checks if type is convertible to another type.
Definition types.h:402
Requires type to be a scoped enumeration.
Definition types.h:85
Requires type to be an enumeration.
Definition types.h:77
Requires type to support equality comparison operators.
Definition types.h:96
Requires type to support equality comparison for hashing.
Definition types.h:229
Checks derivation or type identity using std::derived_from.
Definition types.h:393
Concept checking for types that can be hashed.
Definition types.h:217
Requires type to support input stream extraction.
Definition types.h:185
Ensures the parameter pack is not empty.
Definition types.h:69
Constraint for mutating operations.
Definition types.h:340
Requires type to support either equality or relational comparisons.
Definition types.h:122
Constraint for boolean predicate callbacks.
Definition types.h:278
Requires type to support output stream insertion.
Definition types.h:174
Checks if two types are exactly the same.
Definition types.h:411
Sequence container concept.
Definition types.h:435
Requires type to support both input and output stream operations.
Definition types.h:195
Requires type to support strong ordering via three-way comparison.
Definition types.h:151
Checks inheritance or type equality.
Definition types.h:373
Requires type to support three-way comparison (spaceship operator).
Definition types.h:140
Requires type to support all comparison operators.
Definition types.h:131
Constraint for transformation operations.
Definition types.h:351
Requires type to support relational comparison operators.
Definition types.h:108
Platform-independent type definitions and compiler/platform detection.
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:263
Main namespace for the project Original.
Definition algorithms.h:21
consteval auto makeSequence() noexcept
Creates an index sequence of given length.
Definition types.h:580
consteval auto reverseIndexSequenceImpl(indexSequence< Indices... > seq)
Implementation detail for reversing an index sequence.
Definition types.h:586
decltype(reverseIndexSequenceImpl(makeSequence< N >())) makeReverseSequence
Creates a reversed index sequence.
Definition types.h:509
Primary template for general callable types.
Definition types.h:542