ValuesConstraint.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
  2. /******************************************************************************
  3. *
  4. * file: ValuesConstraint.h
  5. *
  6. * Copyright (c) 2005, Michael E. Smoot
  7. * Copyright (c) 2017, Google LLC
  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_VALUESCONSTRAINT_H
  23. #define TCLAP_VALUESCONSTRAINT_H
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <string>
  28. #include <vector>
  29. #include <tclap/Constraint.h>
  30. #include <tclap/sstream.h>
  31. namespace TCLAP {
  32. /**
  33. * A Constraint that constrains the Arg to only those values specified
  34. * in the constraint.
  35. */
  36. template<class T>
  37. class ValuesConstraint : public Constraint<T>
  38. {
  39. public:
  40. /**
  41. * Constructor.
  42. * \param allowed - vector of allowed values.
  43. */
  44. ValuesConstraint(std::vector<T>const& allowed);
  45. /**
  46. * Virtual destructor.
  47. */
  48. virtual ~ValuesConstraint() {}
  49. /**
  50. * Returns a description of the Constraint.
  51. */
  52. virtual std::string description() const;
  53. /**
  54. * Returns the short ID for the Constraint.
  55. */
  56. virtual std::string shortID() const;
  57. /**
  58. * The method used to verify that the value parsed from the command
  59. * line meets the constraint.
  60. * \param value - The value that will be checked.
  61. */
  62. virtual bool check(const T& value) const;
  63. protected:
  64. /**
  65. * The list of valid values.
  66. */
  67. std::vector<T> _allowed;
  68. /**
  69. * The string used to describe the allowed values of this constraint.
  70. */
  71. std::string _typeDesc;
  72. };
  73. template<class T>
  74. ValuesConstraint<T>::ValuesConstraint(std::vector<T> const& allowed)
  75. : _allowed(allowed),
  76. _typeDesc("")
  77. {
  78. for ( unsigned int i = 0; i < _allowed.size(); i++ )
  79. {
  80. std::ostringstream os;
  81. os << _allowed[i];
  82. std::string temp( os.str() );
  83. if ( i > 0 )
  84. _typeDesc += "|";
  85. _typeDesc += temp;
  86. }
  87. }
  88. template<class T>
  89. bool ValuesConstraint<T>::check( const T& val ) const
  90. {
  91. if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() )
  92. return false;
  93. else
  94. return true;
  95. }
  96. template<class T>
  97. std::string ValuesConstraint<T>::shortID() const
  98. {
  99. return _typeDesc;
  100. }
  101. template<class T>
  102. std::string ValuesConstraint<T>::description() const
  103. {
  104. return _typeDesc;
  105. }
  106. } //namespace TCLAP
  107. #endif