tclap: XorHandler.h Source File

tclap 1.2.5

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