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
189 template <typename T>
190 concept CmpTraits = requires(const T& a, const T& b) {
191 { a.compareTo(b) } -> std::same_as<integer>;
192 };
193
194 // ==================== Stream Concepts ====================
195
208 template <typename TYPE>
209 concept Printable = requires(std::ostream& os, const TYPE& t) {
210 { os << t } -> std::same_as<std::ostream&>;
211 };
212
219 template <typename TYPE>
220 concept InputStreamable = requires(std::istream& is, TYPE& t) {
221 { is >> t } -> std::same_as<std::istream&>;
222 };
223
229 template <typename TYPE>
231
232 // ==================== Hash Concepts ====================
233
251 template <typename T>
252 concept Hashable =
253 requires(const T& obj) {
254 { std::hash<T>{}(obj) } -> std::convertible_to<std::size_t>;
255 };
256
263 template <typename T>
264 concept Equatable =
265 requires(const T& a, const T& b) {
266 { a == b } -> std::convertible_to<bool>;
267 } ||
268 requires(const T& a, const T& b) {
269 { a.equals(b) } -> std::convertible_to<bool>;
270 };
271
310 template <typename T>
311 concept HashTraits =
312 requires(const T& t) {
313 { t.toHash() } -> std::same_as<u_integer>;
314 } && Equatable<T>;
315
316 // ==================== Callback Concepts ====================
317
324 template <typename F, typename... Args>
325 concept Callable = requires(F&& f, Args&&... args) {
326 std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
327 };
328
344 template <typename Callback, typename ReturnType, typename... Args>
345 concept CallbackOf = requires(Callback callback, Args&&... args) {
346 { callback(std::forward<Args>(args)...) } -> std::convertible_to<ReturnType>;
347 };
348
356 template <typename Callback, typename... Args>
357 concept Predicate = CallbackOf<Callback, bool, Args...>;
358
376 template <typename Callback, typename TYPE>
378
397 template <typename Callback, typename TYPE>
399
418 template <typename Callback, typename TYPE>
420
429 template <typename Callback, typename Input, typename Output>
431
432 // ==================== Type Relationship Concepts ====================
433
451 template <typename Base, typename Derive>
452 concept SuperOf = std::is_base_of_v<Base, Derive> || std::is_same_v<Base, Derive>;
453
471 template <typename Base, typename Derive>
472 concept ExtendsOf = std::derived_from<Derive, Base> || std::is_same_v<Base, Derive>;
473
480 template <typename From, typename To>
481 concept ConvertibleTo = std::is_convertible_v<From, To>;
482
489 template <typename T, typename U>
490 concept SameAs = std::is_same_v<T, U>;
491
492 // ==================== Container Concepts ====================
493
500 template <typename C>
501 concept Container = requires(C& c) {
502 { c.begin() } -> std::input_or_output_iterator;
503 { c.end() } -> std::input_or_output_iterator;
504 { c.size() } -> std::convertible_to<std::size_t>;
505 };
506
513 template <typename C>
514 concept SequenceContainer = Container<C> && requires(C& c, typename C::value_type v) {
515 c.push_back(v);
516 c.pop_back();
517 };
518
519 // ==================== Compile-time Index Sequences ====================
520
527 template <u_integer... INTS>
529 static constexpr u_integer SIZE = sizeof...(INTS);
530 public:
535 static consteval u_integer size() noexcept;
536 };
537
544 template <u_integer NUM, u_integer... INTS>
545 class indexSequenceImpl;
546
547 template <u_integer... INTS>
548 class indexSequenceImpl<0, INTS...> {
549 public:
550 using type = indexSequence<INTS...>;
551 };
552
553 template <u_integer NUM, u_integer... INTS>
554 class indexSequenceImpl : public indexSequenceImpl<NUM - 1, NUM - 1, INTS...> {
555 public:
556 using type = indexSequenceImpl<NUM - 1, NUM - 1, INTS...>::type;
557 };
558 public:
559 template <u_integer NUM>
560 friend consteval auto makeSequence() noexcept;
561 };
562
569 consteval auto makeSequence() noexcept; // NOLINT: Forward declaration for makeReverseSequence
570
579
588 );
589
590 // ==================== Function Traits ====================
591
604
608 using ReturnType = R;
609 using Signature = R(Args...);
610 };
611
613 template <typename R, typename... Args>
614 struct functionTraits<R(Args...)> {
615 using ReturnType = R;
616 using Signature = R(Args...);
617 };
618
620 template <typename C>
622 private:
623 using CallType = functionTraits<decltype(&C::operator())>;
624 public:
625 using ReturnType = CallType::ReturnType;
626 using Signature = CallType::Signature;
627 };
628
630 template <typename C, typename R, typename... Args>
631 struct functionTraits<R(C::*)(Args...) const> {
632 using ReturnType = R;
633 using Signature = R(Args...);
634 };
635
637 template <typename C, typename R, typename... Args>
638 struct functionTraits<R(C::*)(Args...)> {
639 using ReturnType = R;
640 using Signature = R(Args...);
641 };
642
643} // namespace original
644
645consteval original::none::operator bool() const {
646 return false;
647}
648
649consteval bool original::none::operator!() const {
650 return true;
651}
652
653template<original::u_integer... INTS>
657
658template<original::u_integer NUM>
660 using sequence = makeIndexSequence::indexSequenceImpl<NUM>::type;
661 return sequence{};
662}
663
664template <original::u_integer... Indices>
666{
667 return indexSequence<sizeof...(Indices) - 1 - Indices...>{};
668}
669
670#endif // TYPES_H
bool equals(const autoPtr &other) const noexcept override
Equality comparison.
Definition autoPtr.h:740
Compile-time sequence of unsigned integers.
Definition types.h:528
static consteval u_integer size() noexcept
Gets the size of the sequence.
Definition types.h:654
Utility for generating index sequences.
Definition types.h:543
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:649
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
Basic concept for any callable type.
Definition types.h:325
Validates callback signature compatibility.
Definition types.h:345
Requires type to implement the comparable interface with compareTo method.
Definition types.h:190
Definition types.h:157
Combines Comparable and CallbackOf for comparison callbacks.
Definition types.h:377
Constraint for predicate callbacks.
Definition types.h:398
Basic container concept.
Definition types.h:501
Checks if type is convertible to another type.
Definition types.h:481
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:264
Checks derivation or type identity using std::derived_from.
Definition types.h:472
Requires type to implement the hashable interface with toHash method.
Definition types.h:311
Concept checking for types that can be hashed.
Definition types.h:252
Requires type to support input stream extraction.
Definition types.h:220
Ensures the parameter pack is not empty.
Definition types.h:69
Constraint for mutating operations.
Definition types.h:419
Requires type to support either equality or relational comparisons.
Definition types.h:122
Constraint for boolean predicate callbacks.
Definition types.h:357
Requires type to support output stream insertion.
Definition types.h:209
Checks if two types are exactly the same.
Definition types.h:490
Sequence container concept.
Definition types.h:514
Requires type to support both input and output stream operations.
Definition types.h:230
Requires type to support strong ordering via three-way comparison.
Definition types.h:151
Checks inheritance or type equality.
Definition types.h:452
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:430
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:659
consteval auto reverseIndexSequenceImpl(indexSequence< Indices... > seq)
Implementation detail for reversing an index sequence.
Definition types.h:665
Primary template for general callable types.
Definition types.h:621