tclap  1.2.0
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 reverved.
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 
32 namespace TCLAP {
33 
39 {
40  protected:
41 
45  std::vector< std::vector<Arg*> > _orList;
46 
47  public:
48 
52  XorHandler( ) {}
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
95 inline void XorHandler::add( std::vector<Arg*>& ors )
96 {
97  _orList.push_back( ors );
98 }
99 
100 inline 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  // go through and set each arg that is not a
111  for ( ArgVectorIterator it = _orList[i].begin();
112  it != _orList[i].end();
113  it++ )
114  if ( a != (*it) )
115  (*it)->xorSet();
116 
117  // return the number of required args that have now been set
118  if ( (*ait)->allowMore() )
119  return 0;
120  else
121  return static_cast<int>(_orList[i].size());
122  }
123  }
124 
125  if ( a->isRequired() )
126  return 1;
127  else
128  return 0;
129 }
130 
131 inline bool XorHandler::contains( const Arg* a )
132 {
133  for ( int i = 0; static_cast<unsigned int>(i) < _orList.size(); i++ )
134  for ( ArgVectorIterator it = _orList[i].begin();
135  it != _orList[i].end();
136  it++ )
137  if ( a == (*it) )
138  return true;
139 
140  return false;
141 }
142 
143 inline std::vector< std::vector<Arg*> >& XorHandler::getXorList()
144 {
145  return _orList;
146 }
147 
148 
149 
151 //END XOR.cpp
153 
154 } //namespace TCLAP
155 
156 #endif