CPP-AP 3.0.1
Command-line argument parser for C++20
Loading...
Searching...
No Matches
concepts.hpp
Go to the documentation of this file.
1// Copyright (c) 2023-2025 Jakub MusiaƂ
2// This file is part of the CPP-AP project (https://github.com/SpectraL519/cpp-ap).
3// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
4
10#pragma once
11
12#include "ap/types.hpp"
13
14#include <iostream>
15#include <ranges>
16
17namespace ap::util {
18
24template <typename T>
25concept c_is_none = std::same_as<T, none_type>;
26
32template <typename T>
33concept c_readable = requires(T value, std::istream& input_stream) { input_stream >> value; };
34
40template <typename T>
41concept c_trivially_readable = std::constructible_from<T, const std::string&>;
42
48template <typename T>
49concept c_writable = requires(T value, std::ostream& output_stream) { output_stream << value; };
50
56template <typename T>
57concept c_arithmetic = std::is_arithmetic_v<T>;
58
64template <typename T>
66 std::same_as<T, ap::none_type>
67 or (std::semiregular<T> and (c_trivially_readable<T> or c_readable<T>));
68
75template <typename T, typename... Types>
76concept c_one_of = std::disjunction_v<std::is_same<T, Types>...>;
77
82enum class type_validator : bool {
83 same,
85};
86
98template <typename T, typename U, type_validator TV>
99inline constexpr bool is_valid_type_v = false;
100
107template <typename T, typename U>
108inline constexpr bool is_valid_type_v<T, U, type_validator::same> = std::same_as<T, U>;
109
116template <typename T, typename U>
117inline constexpr bool is_valid_type_v<T, U, type_validator::convertible> =
118 std::convertible_to<T, U>;
119
127template <typename T, typename U, type_validator TV = type_validator::same>
128concept c_valid_type = is_valid_type_v<T, U, TV>;
129
137template <typename R, typename V, type_validator TV = type_validator::same>
138concept c_range_of =
139 std::ranges::range<R>
141
149template <typename R, typename V, type_validator TV = type_validator::same>
151 std::ranges::range<R>
153
161template <typename It, typename V, type_validator TV = type_validator::same>
163 std::input_iterator<It> and c_valid_type<std::iter_value_t<It>, V, TV>;
164
165} // namespace ap::util
The concept is used to verify the validity of the arguments' value types.
Definition concepts.hpp:65
The concept is satisfied when T is an arithmetic type.
Definition concepts.hpp:57
Validates that It is a forward iterator of type T (ignoring the cvref qualifiers).
Definition concepts.hpp:162
Validates that It is a forward iterator of type T (ignoring the cvref qualifiers).
Definition concepts.hpp:150
The concept is satisfied when T is ap::none_type.
Definition concepts.hpp:25
Validates that T is the same as one of the types defined by Types.
Definition concepts.hpp:76
Validates that R is a range of type T (ignoring the cvref qualifiers).
Definition concepts.hpp:138
The concept is satisfied when T overloads the std::istream operator >>.
Definition concepts.hpp:33
The concept is satisfied when T can be constructed from const std::string&.
Definition concepts.hpp:41
Concept that enforces is_valid_type_v.
Definition concepts.hpp:128
The concept is satisfied when T overloads the std::ostream operator <<.
Definition concepts.hpp:49
type_validator
Specifies the type validation rule.
Definition concepts.hpp:82
constexpr bool is_valid_type_v
Checks if two types satisfy a given type_validator rule.
Definition concepts.hpp:99
@ same
Exact type match.
@ convertible
Implicit conversion allowed.