Main MRPT website > C++ reference for MRPT 1.4.0
ValuesConstraint.h
Go to the documentation of this file.
1/* +---------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | http://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2016, Individual contributors, see AUTHORS file |
6 | See: http://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See details in http://www.mrpt.org/License |
8 +---------------------------------------------------------------------------+ */
9
10
11/******************************************************************************
12 *
13 * file: ValuesConstraint.h
14 *
15 * Copyright (c) 2005, Michael E. Smoot
16 * All rights reverved.
17 *
18 * See the file COPYING in the top directory of this distribution for
19 * more information.
20 *
21 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 *
29 *****************************************************************************/
30
31#ifndef TCLAP_VALUESCONSTRAINT_H
32#define TCLAP_VALUESCONSTRAINT_H
33
34#include <string>
35#include <vector>
37
38//#ifdef HAVE_CONFIG_H
39//#include <config.h>
40//#else
41#define HAVE_SSTREAM
42//#endif
43
44#if defined(HAVE_SSTREAM)
45#include <sstream>
46#elif defined(HAVE_STRSTREAM)
47#include <strstream>
48#else
49#error "Need a stringstream (sstream or strstream) to compile!"
50#endif
51
52namespace TCLAP {
53
54/**
55 * A Constraint that constrains the Arg to only those values specified
56 * in the constraint.
57 */
58template<class T>
60{
61
62 public:
63
64 /**
65 * Constructor.
66 * \param allowed - vector of allowed values.
67 */
68 ValuesConstraint(std::vector<T>& allowed);
69
70 /**
71 * Virtual destructor.
72 */
73 virtual ~ValuesConstraint() {}
74
75 /**
76 * Returns a description of the Constraint.
77 */
78 virtual std::string description() const;
79
80 /**
81 * Returns the short ID for the Constraint.
82 */
83 virtual std::string shortID() const;
84
85 /**
86 * The method used to verify that the value parsed from the command
87 * line meets the constraint.
88 * \param value - The value that will be checked.
89 */
90 virtual bool check(const T& value) const;
91
92 protected:
93
94 /**
95 * The list of valid values.
96 */
97 std::vector<T> _allowed;
98
99 /**
100 * The string used to describe the allowed values of this constraint.
101 */
102 std::string _typeDesc;
103
104};
105
106template<class T>
108: _allowed(allowed)
109{
110 for ( unsigned int i = 0; i < _allowed.size(); i++ )
111 {
112
113#if defined(HAVE_SSTREAM)
114 std::ostringstream os;
115#elif defined(HAVE_STRSTREAM)
116 std::ostrstream os;
117#else
118#error "Need a stringstream (sstream or strstream) to compile!"
119#endif
120
121 os << _allowed[i];
122
123 std::string temp( os.str() );
124
125 if ( i > 0 )
126 _typeDesc += "|";
127 _typeDesc += temp;
128 }
129}
130
131template<class T>
132bool ValuesConstraint<T>::check( const T& val ) const
133{
134 if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() )
135 return false;
136 else
137 return true;
138}
139
140template<class T>
142{
143 return _typeDesc;
144}
145
146template<class T>
148{
149 return _typeDesc;
150}
151
152
153} //namespace TCLAP
154#endif
155
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:47
A Constraint that constrains the Arg to only those values specified in the constraint.
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.
ValuesConstraint(std::vector< T > &allowed)
Constructor.
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:44



Page generated by Doxygen 1.9.6 for MRPT 1.4.0 SVN: at Wed Mar 22 06:08:57 UTC 2023