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

149 行
3.1 KiB

  1. /******************************************************************************
  2. *
  3. * file: ValuesConstraint.h
  4. *
  5. * Copyright (c) 2005, Michael E. Smoot
  6. * All rights reverved.
  7. *
  8. * See the file COPYING in the top directory of this distribution for
  9. * more information.
  10. *
  11. * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
  12. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. * DEALINGS IN THE SOFTWARE.
  18. *
  19. *****************************************************************************/
  20. #ifndef TCLAP_VALUESCONSTRAINT_H
  21. #define TCLAP_VALUESCONSTRAINT_H
  22. #include <string>
  23. #include <vector>
  24. #include "Constraint.h"
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #else
  28. #define HAVE_SSTREAM
  29. #endif
  30. #if defined(HAVE_SSTREAM)
  31. #include <sstream>
  32. #elif defined(HAVE_STRSTREAM)
  33. #include <strstream>
  34. #else
  35. #error "Need a stringstream (sstream or strstream) to compile!"
  36. #endif
  37. namespace TCLAP {
  38. /**
  39. * A Constraint that constrains the Arg to only those values specified
  40. * in the constraint.
  41. */
  42. template<class T>
  43. class ValuesConstraint : public Constraint<T>
  44. {
  45. public:
  46. /**
  47. * Constructor.
  48. * \param allowed - vector of allowed values.
  49. */
  50. ValuesConstraint(std::vector<T>& allowed);
  51. /**
  52. * Virtual destructor.
  53. */
  54. virtual ~ValuesConstraint() {}
  55. /**
  56. * Returns a description of the Constraint.
  57. */
  58. virtual std::string description() const;
  59. /**
  60. * Returns the short ID for the Constraint.
  61. */
  62. virtual std::string shortID() const;
  63. /**
  64. * The method used to verify that the value parsed from the command
  65. * line meets the constraint.
  66. * \param value - The value that will be checked.
  67. */
  68. virtual bool check(const T& value) const;
  69. protected:
  70. /**
  71. * The list of valid values.
  72. */
  73. std::vector<T> _allowed;
  74. /**
  75. * The string used to describe the allowed values of this constraint.
  76. */
  77. std::string _typeDesc;
  78. };
  79. template<class T>
  80. ValuesConstraint<T>::ValuesConstraint(std::vector<T>& allowed)
  81. : _allowed(allowed),
  82. _typeDesc("")
  83. {
  84. for ( unsigned int i = 0; i < _allowed.size(); i++ )
  85. {
  86. #if defined(HAVE_SSTREAM)
  87. std::ostringstream os;
  88. #elif defined(HAVE_STRSTREAM)
  89. std::ostrstream os;
  90. #else
  91. #error "Need a stringstream (sstream or strstream) to compile!"
  92. #endif
  93. os << _allowed[i];
  94. std::string temp( os.str() );
  95. if ( i > 0 )
  96. _typeDesc += "|";
  97. _typeDesc += temp;
  98. }
  99. }
  100. template<class T>
  101. bool ValuesConstraint<T>::check( const T& val ) const
  102. {
  103. if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() )
  104. return false;
  105. else
  106. return true;
  107. }
  108. template<class T>
  109. std::string ValuesConstraint<T>::shortID() const
  110. {
  111. return _typeDesc;
  112. }
  113. template<class T>
  114. std::string ValuesConstraint<T>::description() const
  115. {
  116. return _typeDesc;
  117. }
  118. } //namespace TCLAP
  119. #endif