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

154 lines
4.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 Jon Evans <jon@craftyjon.com>
  5. * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef KICAD_PROPERTY_VALIDATORS_H
  21. #define KICAD_PROPERTY_VALIDATORS_H
  22. #include <properties/property_validator.h>
  23. #include <units_provider.h>
  24. /*
  25. * This file includes some standard validators and errors. Include it only where needed, use
  26. * properties/property_validator.h for most things.
  27. */
  28. template<typename T>
  29. class VALIDATION_ERROR_TOO_LARGE : public VALIDATION_ERROR
  30. {
  31. public:
  32. T Actual;
  33. T Maximum;
  34. EDA_DATA_TYPE DataType;
  35. VALIDATION_ERROR_TOO_LARGE( T aActual, T aMaximum,
  36. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) :
  37. Actual( aActual ),
  38. Maximum( aMaximum ),
  39. DataType( aType )
  40. {}
  41. wxString Format( UNITS_PROVIDER* aUnits ) const override
  42. {
  43. bool addUnit = DataType != EDA_DATA_TYPE::UNITLESS;
  44. return wxString::Format( wxS( "Value must be less than or equal to %s" ),
  45. aUnits->StringFromValue( Maximum, addUnit ) );
  46. }
  47. };
  48. template<typename T>
  49. class VALIDATION_ERROR_TOO_SMALL : public VALIDATION_ERROR
  50. {
  51. public:
  52. T Actual;
  53. T Minimum;
  54. EDA_DATA_TYPE DataType;
  55. VALIDATION_ERROR_TOO_SMALL( T aActual, T aMinimum,
  56. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) :
  57. Actual( aActual ),
  58. Minimum( aMinimum ),
  59. DataType( aType )
  60. {}
  61. wxString Format( UNITS_PROVIDER* aUnits ) const override
  62. {
  63. bool addUnit = DataType != EDA_DATA_TYPE::UNITLESS;
  64. return wxString::Format( wxS( "Value must be greater than or equal to %s" ),
  65. aUnits->StringFromValue( Minimum, addUnit ) );
  66. }
  67. };
  68. /**
  69. * A validator for use when you just need to return an error string rather than also packaging some
  70. * other data (for example, a limit number)
  71. */
  72. class VALIDATION_ERROR_MSG : public VALIDATION_ERROR
  73. {
  74. public:
  75. wxString Message;
  76. VALIDATION_ERROR_MSG( const wxString& aMessage ) : Message( aMessage ) {}
  77. wxString Format( UNITS_PROVIDER* aUnits ) const override
  78. {
  79. return Message;
  80. }
  81. };
  82. /**
  83. * A set of generic validators
  84. */
  85. class PROPERTY_VALIDATORS
  86. {
  87. public:
  88. template<int Min, int Max>
  89. static VALIDATOR_RESULT RangeIntValidator( const wxAny&& aValue, EDA_ITEM* aItem )
  90. {
  91. wxASSERT_MSG( aValue.CheckType<int>(), "Expecting int-containing value" );
  92. int val = aValue.As<int>();
  93. if( val > Max )
  94. return std::make_unique<VALIDATION_ERROR_TOO_LARGE<int>>( val, Max );
  95. else if( val < Min )
  96. return std::make_unique<VALIDATION_ERROR_TOO_SMALL<int>>( val, Min );
  97. return std::nullopt;
  98. }
  99. static VALIDATOR_RESULT PositiveIntValidator( const wxAny&& aValue, EDA_ITEM* aItem )
  100. {
  101. wxASSERT_MSG( aValue.CheckType<int>(), "Expecting int-containing value" );
  102. int val = aValue.As<int>();
  103. if( val < 0 )
  104. return std::make_unique<VALIDATION_ERROR_TOO_SMALL<int>>( val, 0 );
  105. return std::nullopt;
  106. }
  107. static VALIDATOR_RESULT PositiveRatioValidator( const wxAny&& aValue, EDA_ITEM* aItem )
  108. {
  109. wxASSERT_MSG( aValue.CheckType<double>(), "Expecting double-containing value" );
  110. double val = aValue.As<double>();
  111. if( val > 1.0 )
  112. {
  113. return std::make_unique<VALIDATION_ERROR_TOO_LARGE<double>>( val, 1.0,
  114. EDA_DATA_TYPE::UNITLESS );
  115. }
  116. else if( val < 0.0 )
  117. {
  118. return std::make_unique<VALIDATION_ERROR_TOO_SMALL<double>>( val, 0.0,
  119. EDA_DATA_TYPE::UNITLESS );
  120. }
  121. return std::nullopt;
  122. }
  123. };
  124. #endif //KICAD_PROPERTY_VALIDATORS_H