Program Listing for File values_constraint.hpp

Return to documentation for file (/tmp/ws/src/ecl_core/ecl_command_line/include/ecl/command_line/values_constraint.hpp)

/*****************************************************************************
** Ifdefs
*****************************************************************************/

#ifndef TCLAP_VALUESCONSTRAINT_H
#define TCLAP_VALUESCONSTRAINT_H

#include <string>
#include <vector>
#include "constraint.hpp"

#define HAVE_SSTREAM

#if defined(HAVE_SSTREAM)
#include <sstream>
#elif defined(HAVE_STRSTREAM)
#include <strstream>
#else
#error "Need a stringstream (sstream or strstream) to compile!"
#endif

namespace ecl {

template<class T>
class ValuesConstraint : public Constraint<T>
{

    public:

        ValuesConstraint(std::vector<T>& 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>& allowed)
: _allowed(allowed)
{
    for ( unsigned int i = 0; i < _allowed.size(); i++ )
    {

#if defined(HAVE_SSTREAM)
        std::ostringstream os;
#elif defined(HAVE_STRSTREAM)
        std::ostrstream os;
#else
#error "Need a stringstream (sstream or strstream) to compile!"
#endif

        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 ecl

#endif