CPP-AP 2.2.6
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 <iostream>
13#include <ranges>
14
15namespace ap::detail {
16
21template <typename T>
22concept c_readable = requires(T value, std::istream& input_stream) { input_stream >> value; };
23
28template <typename T>
29concept c_writable = requires(T value, std::ostream& output_stream) { output_stream << value; };
30
35template <typename T>
36concept c_arithmetic = std::is_arithmetic_v<T>;
37
42template <typename T>
43concept c_argument_value_type = c_readable<T> and std::semiregular<T>;
44
50template <typename T, typename... Types>
51concept c_one_of = std::disjunction_v<std::is_same<T, Types>...>;
52
56enum class type_validator : bool {
57 same,
59};
60
71template <typename T, typename U, type_validator TV>
72inline constexpr bool is_valid_type_v = false;
73
79template <typename T, typename U>
80inline constexpr bool is_valid_type_v<T, U, type_validator::same> = std::same_as<T, U>;
81
87template <typename T, typename U>
88inline constexpr bool is_valid_type_v<T, U, type_validator::convertible> =
89 std::convertible_to<T, U>;
90
97template <typename T, typename U, type_validator TV = type_validator::same>
98concept c_valid_type = is_valid_type_v<T, U, TV>;
99
106template <typename R, typename V, type_validator TV = type_validator::same>
107concept c_range_of =
108 std::ranges::range<R>
110
117template <typename R, typename V, type_validator TV = type_validator::same>
119 std::ranges::sized_range<R>
121
122
123} // namespace ap::detail
The concept is used to verify the validity of the arguments' value types.
Definition concepts.hpp:43
The concept is satisfied when T is an arithmetic type.
Definition concepts.hpp:36
Validates that T is the same as one of the types defined by Types.
Definition concepts.hpp:51
Validates that R is a range of type T (ignoring the cvref attributes).
Definition concepts.hpp:107
The concept is satisfied when T overloads the std::istream operator >>.
Definition concepts.hpp:22
Validates that R is a sized range of type T (ignoring the cvref attributes).
Definition concepts.hpp:118
Concept that enforces is_valid_type_v.
Definition concepts.hpp:98
The concept is satisfied when T overloads the std::ostream operator <<.
Definition concepts.hpp:29
constexpr bool is_valid_type_v
Checks if two types satisfy a given type_validator rule.
Definition concepts.hpp:72
type_validator
Specifies the type validation rule.
Definition concepts.hpp:56
@ same
Exact type match.
@ convertible
Implicit conversion allowed.