您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

167 行
4.3 KiB

  1. /******************************************************************************
  2. *
  3. * file: XorHandler.h
  4. *
  5. * Copyright (c) 2003, Michael E. Smoot .
  6. * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
  7. * All rights reverved.
  8. *
  9. * See the file COPYING in the top directory of this distribution for
  10. * more information.
  11. *
  12. * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. * DEALINGS IN THE SOFTWARE.
  19. *
  20. *****************************************************************************/
  21. #ifndef TCLAP_XORHANDLER_H
  22. #define TCLAP_XORHANDLER_H
  23. #include "Arg.h"
  24. #include <string>
  25. #include <vector>
  26. #include <algorithm>
  27. #include <iostream>
  28. namespace TCLAP {
  29. /**
  30. * This class handles lists of Arg's that are to be XOR'd on the command
  31. * line. This is used by CmdLine and you shouldn't ever use it.
  32. */
  33. class XorHandler
  34. {
  35. protected:
  36. /**
  37. * The list of of lists of Arg's to be or'd together.
  38. */
  39. std::vector< std::vector<Arg*> > _orList;
  40. public:
  41. /**
  42. * Constructor. Does nothing.
  43. */
  44. XorHandler( ) : _orList(std::vector< std::vector<Arg*> >()) {}
  45. /**
  46. * Add a list of Arg*'s that will be orred together.
  47. * \param ors - list of Arg* that will be xor'd.
  48. */
  49. void add( std::vector<Arg*>& ors );
  50. /**
  51. * Checks whether the specified Arg is in one of the xor lists and
  52. * if it does match one, returns the size of the xor list that the
  53. * Arg matched. If the Arg matches, then it also sets the rest of
  54. * the Arg's in the list. You shouldn't use this.
  55. * \param a - The Arg to be checked.
  56. */
  57. int check( const Arg* a );
  58. /**
  59. * Returns the XOR specific short usage.
  60. */
  61. std::string shortUsage();
  62. /**
  63. * Prints the XOR specific long usage.
  64. * \param os - Stream to print to.
  65. */
  66. void printLongUsage(std::ostream& os);
  67. /**
  68. * Simply checks whether the Arg is contained in one of the arg
  69. * lists.
  70. * \param a - The Arg to be checked.
  71. */
  72. bool contains( const Arg* a );
  73. std::vector< std::vector<Arg*> >& getXorList();
  74. };
  75. //////////////////////////////////////////////////////////////////////
  76. //BEGIN XOR.cpp
  77. //////////////////////////////////////////////////////////////////////
  78. inline void XorHandler::add( std::vector<Arg*>& ors )
  79. {
  80. _orList.push_back( ors );
  81. }
  82. inline int XorHandler::check( const Arg* a )
  83. {
  84. // iterate over each XOR list
  85. for ( int i = 0; static_cast<unsigned int>(i) < _orList.size(); i++ )
  86. {
  87. // if the XOR list contains the arg..
  88. ArgVectorIterator ait = std::find( _orList[i].begin(),
  89. _orList[i].end(), a );
  90. if ( ait != _orList[i].end() )
  91. {
  92. // first check to see if a mutually exclusive switch
  93. // has not already been set
  94. for ( ArgVectorIterator it = _orList[i].begin();
  95. it != _orList[i].end();
  96. it++ )
  97. if ( a != (*it) && (*it)->isSet() )
  98. throw(CmdLineParseException(
  99. "Mutually exclusive argument already set!",
  100. (*it)->toString()));
  101. // go through and set each arg that is not a
  102. for ( ArgVectorIterator it = _orList[i].begin();
  103. it != _orList[i].end();
  104. it++ )
  105. if ( a != (*it) )
  106. (*it)->xorSet();
  107. // return the number of required args that have now been set
  108. if ( (*ait)->allowMore() )
  109. return 0;
  110. else
  111. return static_cast<int>(_orList[i].size());
  112. }
  113. }
  114. if ( a->isRequired() )
  115. return 1;
  116. else
  117. return 0;
  118. }
  119. inline bool XorHandler::contains( const Arg* a )
  120. {
  121. for ( int i = 0; static_cast<unsigned int>(i) < _orList.size(); i++ )
  122. for ( ArgVectorIterator it = _orList[i].begin();
  123. it != _orList[i].end();
  124. it++ )
  125. if ( a == (*it) )
  126. return true;
  127. return false;
  128. }
  129. inline std::vector< std::vector<Arg*> >& XorHandler::getXorList()
  130. {
  131. return _orList;
  132. }
  133. //////////////////////////////////////////////////////////////////////
  134. //END XOR.cpp
  135. //////////////////////////////////////////////////////////////////////
  136. } //namespace TCLAP
  137. #endif