tclap 1.4.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 <stdexcept>
29#include <string>
30
31namespace TCLAP {
32namespace internal {
33
34// Text policies receive the stored callable so a rule owns its state once.
36public:
37 FixedConstraintText(const std::string &description,
38 const std::string &shortId)
39 : _description(description), _shortId(shortId) {}
40
41 template <class Function>
42 std::string description(const Function &) const {
43 return _description;
44 }
45 template <class Function>
46 std::string shortID(const Function &) const {
47 return _shortId;
48 }
49
50private:
51 std::string _description;
52 std::string _shortId;
53};
54
56 template <class Rule>
57 std::string description(const Rule &rule) const {
58 return rule.description();
59 }
60 template <class Rule>
61 std::string shortID(const Rule &rule) const {
62 return rule.shortID();
63 }
64};
65
66// Only known empty representations are checked; a functor's operator bool()
67// may have a meaning unrelated to whether it can be called.
68template <class Function>
70 template <class Actual>
71 static void check(const Actual &) {}
72};
73
74template <class Function>
75struct ValidateConstraintPredicate<const Function>
76 : ValidateConstraintPredicate<Function> {};
77template <class Function>
78struct ValidateConstraintPredicate<volatile Function>
79 : ValidateConstraintPredicate<Function> {};
80template <class Function>
81struct ValidateConstraintPredicate<const volatile Function>
82 : ValidateConstraintPredicate<Function> {};
83
84template <class Result, class Argument>
85struct ValidateConstraintPredicate<Result (*)(Argument)> {
86 static void check(Result (*function)(Argument)) {
87 if (!function)
88 throw std::invalid_argument(
89 "PredicateConstraint requires a non-empty callable");
90 }
91};
92
93// noexcept is part of a function pointer's type since C++17. Keep the
94// C++98 branch safe when a consumer uses a newer language standard too.
95#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
96template <class Result, class Argument>
97struct ValidateConstraintPredicate<Result (*)(Argument) noexcept> {
98 static void check(Result (*function)(Argument) noexcept) {
99 if (!function)
100 throw std::invalid_argument(
101 "PredicateConstraint requires a non-empty callable");
102 }
103};
104#endif
105
106} // namespace internal
107
122template <class T, class Function = bool (*)(const T &),
123 class Text = internal::FixedConstraintText>
125public:
126 typedef Function Predicate;
127
129 PredicateConstraint(const std::string &description,
130 const std::string &shortId, const Function &function)
131 : _function(function), _text(description, shortId) {
133 }
134
137 PredicateConstraint(const Function &function, const Text &text)
138 : _function(function), _text(text) {
140 }
141
143
144 bool check(const T &value) const { return _function(value); }
145 std::string description() const { return _text.description(_function); }
146 std::string shortID() const { return _text.shortID(_function); }
147
148private:
149 Function _function;
150 Text _text;
151};
152
158template <class T, class Function>
160 const std::string &shortId,
161 Function function) {
162 return PredicateConstraint<T, Function>(description, shortId, function);
163}
164
170template <class T, class Rule>
176
177} // namespace TCLAP
178
179#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>.
std::string shortID() const
Returns the short ID for the Constraint.
bool check(const T &value) const
The method used to verify that the value parsed from the command line meets the constraint.
std::string description() const
Returns a description of the Constraint.
PredicateConstraint(const std::string &description, const std::string &shortId, const Function &function)
Copies the callable and the two independently supplied strings.
PredicateConstraint(const Function &function, const Text &text)
Advanced form used by the rule factory to share validation/text state.
FixedConstraintText(const std::string &description, const std::string &shortId)
std::string description(const Function &) const
std::string shortID(const Function &) const
Definition Arg.h:52
PredicateConstraint< T, Function > MakeConstraint(const std::string &description, const std::string &shortId, Function function)
Creates an owning constraint with fixed presentation text.
PredicateConstraint< T, Rule, internal::RuleConstraintText > MakeRuleConstraint(Rule rule)
Creates an owning rule constraint.
std::string description(const Rule &rule) const
std::string shortID(const Rule &rule) const