Program Listing for File xor_handler.hpp

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

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

#ifndef TCLAP_XORHANDLER_H
#define TCLAP_XORHANDLER_H

#include "arg.hpp"
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

namespace ecl {

class XorHandler
{
    protected:

        std::vector< std::vector<Arg*> > _orList;

    public:

        XorHandler( ) {}

        void add( std::vector<Arg*>& ors );

        int check( const Arg* a );

        std::string shortUsage();

        void printLongUsage(std::ostream& os);

        bool contains( const Arg* a );

        std::vector< std::vector<Arg*> >& getXorList();

};


//BEGIN XOR.cpp
inline void XorHandler::add( std::vector<Arg*>& ors )
{
    _orList.push_back( ors );
}

inline int XorHandler::check( const Arg* a )
{
    // iterate over each XOR list
    for ( int i = 0; static_cast<unsigned int>(i) < _orList.size(); i++ )
    {
        // if the XOR list contains the arg..
        ArgVectorIterator ait = std::find( _orList[i].begin(),
                                           _orList[i].end(), a );
        if ( ait != _orList[i].end() )
        {
            // go through and set each arg that is not a
            for ( ArgVectorIterator it = _orList[i].begin();
                  it != _orList[i].end();
                  it++ )
                if ( a != (*it) )
                    (*it)->xorSet();

            // return the number of required args that have now been set
            if ( (*ait)->allowMore() )
                return 0;
            else
                return static_cast<int>(_orList[i].size());
        }
    }

    if ( a->isRequired() )
        return 1;
    else
        return 0;
}

inline bool XorHandler::contains( const Arg* a )
{
    for ( int i = 0; static_cast<unsigned int>(i) < _orList.size(); i++ )
        for ( ArgVectorIterator it = _orList[i].begin();
              it != _orList[i].end();
              it++ )
            if ( a == (*it) )
                return true;

    return false;
}

inline std::vector< std::vector<Arg*> >& XorHandler::getXorList()
{
    return _orList;
}

} // namespace ecl


#endif