tclap 1.2.2
XorHandler.h
Go to the documentation of this file.
1
2/******************************************************************************
3 *
4 * file: XorHandler.h
5 *
6 * Copyright (c) 2003, Michael E. Smoot .
7 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
8 * All rights reserved.
9 *
10 * See the file COPYING in the top directory of this distribution for
11 * more information.
12 *
13 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *****************************************************************************/
22
23#ifndef TCLAP_XORHANDLER_H
24#define TCLAP_XORHANDLER_H
25
26#include <tclap/Arg.h>
27#include <string>
28#include <vector>
29#include <algorithm>
30#include <iostream>
31
32namespace TCLAP {
33
39{
40 protected:
41
45 std::vector< std::vector<Arg*> > _orList;
46
47 public:
48
52 XorHandler( ) : _orList(std::vector< std::vector<Arg*> >()) {}
53
58 void add( std::vector<Arg*>& ors );
59
67 int check( const Arg* a );
68
72 std::string shortUsage();
73
78 void printLongUsage(std::ostream& os);
79
85 bool contains( const Arg* a );
86
87 std::vector< std::vector<Arg*> >& getXorList();
88
89};
90
91
93//BEGIN XOR.cpp
95inline void XorHandler::add( std::vector<Arg*>& ors )
96{
97 _orList.push_back( ors );
98}
99
100inline int XorHandler::check( const Arg* a )
101{
102 // iterate over each XOR list
103 for ( int i = 0; static_cast<unsigned int>(i) < _orList.size(); i++ )
104 {
105 // if the XOR list contains the arg..
106 ArgVectorIterator ait = std::find( _orList[i].begin(),
107 _orList[i].end(), a );
108 if ( ait != _orList[i].end() )
109 {
110 // first check to see if a mutually exclusive switch
111 // has not already been set
112 for ( ArgVectorIterator it = _orList[i].begin();
113 it != _orList[i].end();
114 it++ )
115 if ( a != (*it) && (*it)->isSet() )
117 "Mutually exclusive argument already set!",
118 (*it)->toString()));
119
120 // go through and set each arg that is not a
121 for ( ArgVectorIterator it = _orList[i].begin();
122 it != _orList[i].end();
123 it++ )
124 if ( a != (*it) )
125 (*it)->xorSet();
126
127 // return the number of required args that have now been set
128 if ( (*ait)->allowMore() )
129 return 0;
130 else
131 return static_cast<int>(_orList[i].size());
132 }
133 }
134
135 if ( a->isRequired() )
136 return 1;
137 else
138 return 0;
139}
140
141inline bool XorHandler::contains( const Arg* a )
142{
143 for ( int i = 0; static_cast<unsigned int>(i) < _orList.size(); i++ )
144 for ( ArgVectorIterator it = _orList[i].begin();
145 it != _orList[i].end();
146 it++ )
147 if ( a == (*it) )
148 return true;
149
150 return false;
151}
152
153inline std::vector< std::vector<Arg*> >& XorHandler::getXorList()
154{
155 return _orList;
156}
157
158
159
161//END XOR.cpp
163
164} //namespace TCLAP
165
166#endif
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:66
bool isSet() const
Indicates whether the argument has already been set.
Definition: Arg.h:576
virtual bool isRequired() const
Indicates whether the argument is required.
Definition: Arg.h:572
Thrown from CmdLine when the arguments on the command line are not properly specified,...
Definition: ArgException.h:144
This class handles lists of Arg's that are to be XOR'd on the command line.
Definition: XorHandler.h:39
std::vector< std::vector< Arg * > > & getXorList()
Definition: XorHandler.h:153
XorHandler()
Constructor.
Definition: XorHandler.h:52
bool contains(const Arg *a)
Simply checks whether the Arg is contained in one of the arg lists.
Definition: XorHandler.h:141
void printLongUsage(std::ostream &os)
Prints the XOR specific long usage.
int check(const Arg *a)
Checks whether the specified Arg is in one of the xor lists and if it does match one,...
Definition: XorHandler.h:100
std::vector< std::vector< Arg * > > _orList
The list of of lists of Arg's to be or'd together.
Definition: XorHandler.h:45
void add(std::vector< Arg * > &ors)
Add a list of Arg*'s that will be xor'd together.
Definition: XorHandler.h:95
std::string shortUsage()
Returns the XOR specific short usage.
Definition: Arg.h:58
std::vector< Arg * >::iterator ArgVectorIterator
Typedef of an Arg vector iterator.
Definition: Arg.h:402