You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

XorHandler.h 5.2 kB

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