tclap 1.6.0
PredicateConstraint.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: PredicateConstraint.h
6 *
7 * Copyright (c) 2026, Google LLC
8 * All rights reserved.
9 *
10 * See the file COPYING in the top directory of this distribution for
11 * more information.
12 *
13 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *****************************************************************************/
22
23#ifndef TCLAP_PREDICATE_CONSTRAINT_H
24#define TCLAP_PREDICATE_CONSTRAINT_H
25
26#include <tclap/Constraint.h>
27
28#include <concepts>
29#include <functional>
30#include <type_traits>
31#include <utility>
32
33#include <stdexcept>
34#include <string>
35
36namespace TCLAP {
37namespace internal {
38
39// Text policies receive the stored callable so a rule owns its state once.
41public:
42 FixedConstraintText(std::string description, std::string shortId)
43 : _description(std::move(description)), _shortId(std::move(shortId)) {}
44
45 template <class Function>
46 std::string description(const Function &) const {
47 return _description;
48 }
49 template <class Function>
50 std::string shortID(const Function &) const {
51 return _shortId;
52 }
53
54private:
55 std::string _description;
56 std::string _shortId;
57};
58
60 template <class Rule>
61 requires requires(const Rule &rule) {
62 { rule.description() } -> std::convertible_to<std::string>;
63 }
64 std::string description(const Rule &rule) const {
65 return rule.description();
66 }
67 template <class Rule>
68 requires requires(const Rule &rule) {
69 { rule.shortID() } -> std::convertible_to<std::string>;
70 }
71 std::string shortID(const Rule &rule) const {
72 return rule.shortID();
73 }
74};
75
76// Only known empty representations are checked; a functor's operator bool()
77// may have a meaning unrelated to whether it can be called.
78template <class Function>
79struct IsConstraintStdFunction : std::false_type {};
80
81template <class Signature>
82struct IsConstraintStdFunction<std::function<Signature>> : std::true_type {};
83
84template <class Function>
85void ValidateConstraintPredicate(const Function &function) {
86 if constexpr ((std::is_pointer_v<Function> &&
87 std::is_function_v<std::remove_pointer_t<Function>>) ||
88 std::is_member_pointer_v<Function> ||
89 IsConstraintStdFunction<std::remove_cv_t<Function>>::value) {
90 if (!function)
91 throw std::invalid_argument(
92 "PredicateConstraint requires a non-empty callable");
93 }
94}
95
96template <class T, class Function, class Text>
98 std::predicate<const Function &, const T &> &&
99 requires(const Text &text, const Function &function) {
100 { text.description(function) } -> std::convertible_to<std::string>;
101 { text.shortID(function) } -> std::convertible_to<std::string>;
102 };
103
104} // namespace internal
105
121template <class T, class Function = std::function<bool(const T &)>,
122 class Text = internal::FixedConstraintText>
125public:
126 using Predicate = Function;
127
129 template <class F>
130 requires std::constructible_from<Function, F &&> &&
131 std::constructible_from<Text, std::string, std::string>
132 PredicateConstraint(std::string description, std::string shortId,
133 F &&function)
134 : _function(std::forward<F>(function)),
135 _text(std::move(description), std::move(shortId)) {
137 }
138
141 template <class F>
142 requires std::constructible_from<Function, F &&>
143 PredicateConstraint(F &&function, Text text)
144 : _function(std::forward<F>(function)), _text(std::move(text)) {
146 }
147
152 ~PredicateConstraint() override = default;
153
154 bool check(const T &value) const override {
155 return std::invoke(_function, value);
156 }
157 [[nodiscard]] std::string description() const override {
158 return _text.description(_function);
159 }
160 [[nodiscard]] std::string shortID() const override {
161 return _text.shortID(_function);
162 }
163
164private:
165 Function _function;
166 Text _text;
167};
168
175template <class T, class Function>
176 requires std::predicate<const std::decay_t<Function> &, const T &>
177[[nodiscard]] auto MakeConstraint(std::string description, std::string shortId,
178 Function &&function) {
180 std::move(description), std::move(shortId),
181 std::forward<Function>(function));
182}
183
189template <class T, class Rule>
190 requires std::predicate<const std::decay_t<Rule> &, const T &> &&
191 requires(const std::decay_t<Rule> &rule) {
192 { rule.description() } -> std::convertible_to<std::string>;
193 { rule.shortID() } -> std::convertible_to<std::string>;
194 }
195[[nodiscard]] auto MakeRuleConstraint(Rule &&rule) {
198 std::forward<Rule>(rule), internal::RuleConstraintText());
199}
200
201} // namespace TCLAP
202
203#endif // TCLAP_PREDICATE_CONSTRAINT_H
The interface that defines the interaction between the Arg and Constraint.
Definition Constraint.h:41
Adapts an owned function or const function object to Constraint<T>.
PredicateConstraint(std::string description, std::string shortId, F &&function)
Owns the callable and separate presentation strings.
PredicateConstraint & operator=(PredicateConstraint &&)=default
std::string description() const override
Returns a description of the Constraint.
PredicateConstraint(const PredicateConstraint &)=default
bool check(const T &value) const override
The method used to verify that the value parsed from the command line meets the constraint.
~PredicateConstraint() override=default
PredicateConstraint & operator=(const PredicateConstraint &)=default
std::string shortID() const override
Returns the short ID for the Constraint.
PredicateConstraint(PredicateConstraint &&)=default
PredicateConstraint(F &&function, Text text)
Advanced form used by the rule factory to share validation/text state.
FixedConstraintText(std::string description, std::string shortId)
std::string description(const Function &) const
std::string shortID(const Function &) const
void ValidateConstraintPredicate(const Function &function)
Definition Arg.h:54
auto MakeConstraint(std::string description, std::string shortId, Function &&function)
Creates an owning constraint with fixed presentation text.
auto MakeRuleConstraint(Rule &&rule)
Creates an owning rule constraint.
std::string shortID(const Rule &rule) const
std::string description(const Rule &rule) const