tclap 1.2.6
ValuesConstraint.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3
4
5/******************************************************************************
6 *
7 * file: ValuesConstraint.h
8 *
9 * Copyright (c) 2005, Michael E. Smoot
10 * Copyright (c) 2017, Google LLC
11 * All rights reserved.
12 *
13 * See the file COPYING in the top directory of this distribution for
14 * more information.
15 *
16 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 *****************************************************************************/
25
26#ifndef TCLAP_VALUESCONSTRAINT_H
27#define TCLAP_VALUESCONSTRAINT_H
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32
33#include <sstream>
34#include <string>
35#include <vector>
36#include <tclap/Constraint.h>
37#include <tclap/sstream.h>
38
39namespace TCLAP {
40
45template<class T>
47{
48
49 public:
50
55 ValuesConstraint(std::vector<T>const& allowed);
56
60 virtual ~ValuesConstraint() {}
61
65 virtual std::string description() const;
66
70 virtual std::string shortID() const;
71
77 virtual bool check(const T& value) const;
78
79 protected:
80
84 std::vector<T> _allowed;
85
89 std::string _typeDesc;
90
91};
92
93template<class T>
94ValuesConstraint<T>::ValuesConstraint(std::vector<T> const& allowed)
95: _allowed(allowed),
96 _typeDesc("")
97{
98 for ( unsigned int i = 0; i < _allowed.size(); i++ )
99 {
100 std::ostringstream os;
101 os << _allowed[i];
102
103 std::string temp( os.str() );
104
105 if ( i > 0 )
106 _typeDesc += "|";
107 _typeDesc += temp;
108 }
109}
110
111template<class T>
112bool ValuesConstraint<T>::check( const T& val ) const
113{
114 if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() )
115 return false;
116 else
117 return true;
118}
119
120template<class T>
122{
123 return _typeDesc;
124}
125
126template<class T>
128{
129 return _typeDesc;
130}
131
132
133} //namespace TCLAP
134#endif
135
The interface that defines the interaction between the Arg and Constraint.
Definition Constraint.h:43
A Constraint that constrains the Arg to only those values specified in the constraint.
ValuesConstraint(std::vector< T >const &allowed)
Constructor.
std::string _typeDesc
The string used to describe the allowed values of this constraint.
virtual bool check(const T &value) const
The method used to verify that the value parsed from the command line meets the constraint.
virtual std::string description() const
Returns a description of the Constraint.
virtual ~ValuesConstraint()
Virtual destructor.
std::vector< T > _allowed
The list of valid values.
virtual std::string shortID() const
Returns the short ID for the Constraint.
Definition Arg.h:48