XorHandler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
  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. #ifndef TCLAP_XORHANDLER_H
  23. #define TCLAP_XORHANDLER_H
  24. #include <tclap/Arg.h>
  25. #include <string>
  26. #include <vector>
  27. #include <algorithm>
  28. #include <iostream>
  29. namespace TCLAP {
  30. /**
  31. * This class handles lists of Arg's that are to be XOR'd on the command
  32. * line. This is used by CmdLine and you shouldn't ever use it.
  33. */
  34. class XorHandler
  35. {
  36. protected:
  37. /**
  38. * The list of of lists of Arg's to be or'd together.
  39. */
  40. std::vector< std::vector<Arg*> > _orList;
  41. public:
  42. /**
  43. * Constructor. Does nothing.
  44. */
  45. XorHandler( ) : _orList(std::vector< std::vector<Arg*> >()) {}
  46. /**
  47. * Add a list of Arg*'s that will be xor'd together.
  48. * \param ors - list of Arg* that will be xor'd.
  49. */
  50. void add( const std::vector<Arg*>& ors );
  51. /**
  52. * Checks whether the specified Arg is in one of the xor lists and
  53. * if it does match one, returns the size of the xor list that the
  54. * Arg matched. If the Arg matches, then it also sets the rest of
  55. * the Arg's in the list. You shouldn't use this.
  56. * \param a - The Arg to be checked.
  57. */
  58. int check( const Arg* a );
  59. /**
  60. * Returns the XOR specific short usage.
  61. */
  62. std::string shortUsage();
  63. /**
  64. * Prints the XOR specific long usage.
  65. * \param os - Stream to print to.
  66. */
  67. void printLongUsage(std::ostream& os);
  68. /**
  69. * Simply checks whether the Arg is contained in one of the arg
  70. * lists.
  71. * \param a - The Arg to be checked.
  72. */
  73. bool contains( const Arg* a );
  74. const std::vector< std::vector<Arg*> >& getXorList() const;
  75. };
  76. //////////////////////////////////////////////////////////////////////
  77. //BEGIN XOR.cpp
  78. //////////////////////////////////////////////////////////////////////
  79. inline void XorHandler::add( const std::vector<Arg*>& ors )
  80. {
  81. _orList.push_back( ors );
  82. }
  83. inline int XorHandler::check( const Arg* a )
  84. {
  85. // iterate over each XOR list
  86. for ( int i = 0; static_cast<unsigned int>(i) < _orList.size(); i++ )
  87. {
  88. // if the XOR list contains the arg..
  89. ArgVectorIterator ait = std::find( _orList[i].begin(),
  90. _orList[i].end(), a );
  91. if ( ait != _orList[i].end() )
  92. {
  93. // first check to see if a mutually exclusive switch
  94. // has not already been set
  95. for ( ArgVectorIterator it = _orList[i].begin();
  96. it != _orList[i].end();
  97. it++ )
  98. if ( a != (*it) && (*it)->isSet() )
  99. throw(CmdLineParseException(
  100. "Mutually exclusive argument already set!",
  101. (*it)->toString()));
  102. // go through and set each arg that is not a
  103. for ( ArgVectorIterator it = _orList[i].begin();
  104. it != _orList[i].end();
  105. it++ )
  106. if ( a != (*it) )
  107. (*it)->xorSet();
  108. // return the number of required args that have now been set
  109. if ( (*ait)->allowMore() )
  110. return 0;
  111. else
  112. return static_cast<int>(_orList[i].size());
  113. }
  114. }
  115. if ( a->isRequired() )
  116. return 1;
  117. else
  118. return 0;
  119. }
  120. inline bool XorHandler::contains( const Arg* a )
  121. {
  122. for ( int i = 0; static_cast<unsigned int>(i) < _orList.size(); i++ )
  123. for ( ArgVectorIterator it = _orList[i].begin();
  124. it != _orList[i].end();
  125. it++ )
  126. if ( a == (*it) )
  127. return true;
  128. return false;
  129. }
  130. inline const std::vector< std::vector<Arg*> >& XorHandler::getXorList() const
  131. {
  132. return _orList;
  133. }
  134. //////////////////////////////////////////////////////////////////////
  135. //END XOR.cpp
  136. //////////////////////////////////////////////////////////////////////
  137. } //namespace TCLAP
  138. #endif