tclap  1.4.0
ValuesConstraint.h
Go to the documentation of this file.
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 
39 template <class T>
40 class ValuesConstraint : public Constraint<T> {
41 public:
46  explicit ValuesConstraint(std::vector<T> &allowed);
47 
51  virtual ~ValuesConstraint() {}
52 
56  virtual std::string description() const;
57 
61  virtual std::string shortID() const;
62 
68  virtual bool check(const T &value) const;
69 
70 protected:
74  std::vector<T> _allowed;
75 
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++) {
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
A Constraint that constrains the Arg to only those values specified in the constraint.
ValuesConstraint(std::vector< T > &allowed)
Constructor.
virtual std::string shortID() const
Returns the short ID for the Constraint.
virtual std::string description() const
Returns a description of the Constraint.
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:41
std::vector< T > _allowed
The list of valid values.
std::ostringstream ostringstream
Definition: sstream.h:38
std::string _typeDesc
The string used to describe the allowed values of this constraint.
Definition: Arg.h:46
virtual ~ValuesConstraint()
Virtual destructor.
virtual bool check(const T &value) const
The method used to verify that the value parsed from the command line meets the constraint.