Debian Codegraph @

podofo/3rdparty/tclap/ValuesConstraint.h

raw symbols SCIP
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3 /******************************************************************************
4 *
5 * file: ValuesConstraint.h
6 *
7 * Copyright (c) 2005, Michael E. Smoot
8 * Copyright (c) 2017, Google LLC
9 * All rights reserved.
10 *
11 * See the file COPYING in the top directory of this distribution for
12 * more information.
13 *
14 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *
22 *****************************************************************************/
23
24 #ifndef TCLAP_VALUES_CONSTRAINT_H
25 #define TCLAP_VALUES_CONSTRAINT_H
26
27 #include <tclap/Constraint.h>
28 #include <tclap/sstream.h>
29
30 #include <string>
31 #include <vector>
32
33 namespace TCLAP {
34
35 /**
36 * A Constraint that constrains the Arg to only those values specified
37 * in the constraint.
38 */
39 template <class T>
40 class ValuesConstraint : public Constraint<T> {
41 public:
42 /**
43 * Constructor.
44 * \param allowed - vector of allowed values.
45 */
46 explicit ValuesConstraint(std::vector<T> &allowed);
47
48 /**
49 * Virtual destructor.
50 */
51 virtual ~ValuesConstraint() {}
52
53 /**
54 * Returns a description of the Constraint.
55 */
56 virtual std::string description() const;
57
58 /**
59 * Returns the short ID for the Constraint.
60 */
61 virtual std::string shortID() const;
62
63 /**
64 * The method used to verify that the value parsed from the command
65 * line meets the constraint.
66 * \param value - The value that will be checked.
67 */
68 virtual bool check(const T &value) const;
69
70 protected:
71 /**
72 * The list of valid values.
73 */
74 std::vector<T> _allowed;
75
76 /**
77 * The string used to describe the allowed values of this constraint.
78 */
79 std::string _typeDesc;
80 };
81
82 template <class T>
83 ValuesConstraint<T>::ValuesConstraint(std::vector<T> &allowed)
84 : _allowed(allowed), _typeDesc("") {
85 for (unsigned int i = 0; i < _allowed.size(); i++) {
86 std::ostringstream os;
87 os << _allowed[i];
88
89 std::string temp(os.str());
90
91 if (i > 0) _typeDesc += "|";
92 _typeDesc += temp;
93 }
94 }
95
96 template <class T>
97 bool ValuesConstraint<T>::check(const T &val) const {
98 if (std::find(_allowed.begin(), _allowed.end(), val) == _allowed.end())
99 return false;
100 else
101 return true;
102 }
103
104 template <class T>
105 std::string ValuesConstraint<T>::shortID() const {
106 return _typeDesc;
107 }
108
109 template <class T>
110 std::string ValuesConstraint<T>::description() const {
111 return _typeDesc;
112 }
113
114 } // namespace TCLAP
115 #endif // TCLAP_VALUES_CONSTRAINT_H