ValuesConstraint.h
Go to the documentation of this file.
// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
/******************************************************************************
*
* file: ValuesConstraint.h
*
* Copyright (c) 2005, Michael E. Smoot
* Copyright (c) 2017, Google LLC
* All rights reserved.
*
* See the file COPYING in the top directory of this distribution for
* more information.
*
* THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#ifndef TCLAP_VALUESCONSTRAINT_H
#define TCLAP_VALUESCONSTRAINT_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <vector>
#include <tclap/Constraint.h>
#include <tclap/sstream.h>
namespace TCLAP {
template<class T>
class ValuesConstraint : public Constraint<T>
{
public:
ValuesConstraint(std::vector<T>const& allowed);
virtual ~ValuesConstraint() {}
virtual std::string description() const;
virtual std::string shortID() const;
virtual bool check(const T& value) const;
protected:
std::vector<T> _allowed;
std::string _typeDesc;
};
template<class T>
ValuesConstraint<T>::ValuesConstraint(std::vector<T> const& allowed)
: _allowed(allowed),
_typeDesc("")
{
for ( unsigned int i = 0; i < _allowed.size(); i++ )
{
std::ostringstream os;
os << _allowed[i];
std::string temp( os.str() );
if ( i > 0 )
_typeDesc += "|";
_typeDesc += temp;
}
}
template<class T>
bool ValuesConstraint<T>::check( const T& val ) const
{
if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() )
return false;
else
return true;
}
template<class T>
std::string ValuesConstraint<T>::shortID() const
{
return _typeDesc;
}
template<class T>
std::string ValuesConstraint<T>::description() const
{
return _typeDesc;
}
} //namespace TCLAP
#endif
Constraint.h
TCLAP::Constraint
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:43
TCLAP::ValuesConstraint
A Constraint that constrains the Arg to only those values specified in the constraint.
Definition: ValuesConstraint.h:46
TCLAP::ValuesConstraint::ValuesConstraint
ValuesConstraint(std::vector< T >const &allowed)
Constructor.
Definition: ValuesConstraint.h:93
TCLAP::ValuesConstraint::_typeDesc
std::string _typeDesc
The string used to describe the allowed values of this constraint.
Definition: ValuesConstraint.h:88
TCLAP::ValuesConstraint::check
virtual bool check(const T &value) const
The method used to verify that the value parsed from the command line meets the constraint.
Definition: ValuesConstraint.h:111
TCLAP::ValuesConstraint::description
virtual std::string description() const
Returns a description of the Constraint.
Definition: ValuesConstraint.h:126
TCLAP::ValuesConstraint::~ValuesConstraint
virtual ~ValuesConstraint()
Virtual destructor.
Definition: ValuesConstraint.h:59
TCLAP::ValuesConstraint::_allowed
std::vector< T > _allowed
The list of valid values.
Definition: ValuesConstraint.h:83
TCLAP::ValuesConstraint::shortID
virtual std::string shortID() const
Returns the short ID for the Constraint.
Definition: ValuesConstraint.h:120
TCLAP
Definition: Arg.h:48
TCLAP::ostringstream
std::ostringstream ostringstream
Definition: sstream.h:38
sstream.h