KiCad PCB EDA Suite
Loading...
Searching...
No Matches
property_validators.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 Jon Evans <[email protected]>
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
21#ifndef KICAD_PROPERTY_VALIDATORS_H
22#define KICAD_PROPERTY_VALIDATORS_H
23
25#include <units_provider.h>
26
27/*
28 * This file includes some standard validators and errors. Include it only where needed, use
29 * properties/property_validator.h for most things.
30 */
31
32
33template<typename T>
35{
36public:
40
41 VALIDATION_ERROR_TOO_LARGE( T aActual, T aMaximum,
42 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) :
43 Actual( aActual ),
44 Maximum( aMaximum ),
45 DataType( aType )
46 {}
47
48 wxString Format( UNITS_PROVIDER* aUnits ) const override
49 {
50 bool addUnit = DataType != EDA_DATA_TYPE::UNITLESS;
51 return wxString::Format( wxS( "Value must be less than or equal to %s" ),
52 aUnits->StringFromValue( Maximum, addUnit ) );
53 }
54};
55
56
57template<typename T>
59{
60public:
64
65 VALIDATION_ERROR_TOO_SMALL( T aActual, T aMinimum,
66 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) :
67 Actual( aActual ),
68 Minimum( aMinimum ),
69 DataType( aType )
70 {}
71
72 wxString Format( UNITS_PROVIDER* aUnits ) const override
73 {
74 bool addUnit = DataType != EDA_DATA_TYPE::UNITLESS;
75 return wxString::Format( wxS( "Value must be greater than or equal to %s" ),
76 aUnits->StringFromValue( Minimum, addUnit ) );
77 }
78};
79
80
86{
87public:
88 wxString Message;
89
90 VALIDATION_ERROR_MSG( const wxString& aMessage ) : Message( aMessage ) {}
91
92 wxString Format( UNITS_PROVIDER* aUnits ) const override
93 {
94 return Message;
95 }
96};
97
98
103{
104public:
105
106 template<int Min, int Max>
107 static VALIDATOR_RESULT RangeIntValidator( const wxAny&& aValue, EDA_ITEM* aItem )
108 {
109 wxASSERT_MSG( aValue.CheckType<int>(), "Expecting int-containing value" );
110
111 int val = aValue.As<int>();
112
113 if( val > Max )
114 return std::make_unique<VALIDATION_ERROR_TOO_LARGE<int>>( val, Max );
115 else if( val < Min )
116 return std::make_unique<VALIDATION_ERROR_TOO_SMALL<int>>( val, Min );
117
118 return std::nullopt;
119 }
120
121 static VALIDATOR_RESULT PositiveIntValidator( const wxAny&& aValue, EDA_ITEM* aItem )
122 {
123 wxASSERT_MSG( aValue.CheckType<int>(), "Expecting int-containing value" );
124
125 int val = aValue.As<int>();
126
127 if( val < 0 )
128 return std::make_unique<VALIDATION_ERROR_TOO_SMALL<int>>( val, 0 );
129
130 return std::nullopt;
131 }
132
133 static VALIDATOR_RESULT PositiveRatioValidator( const wxAny&& aValue, EDA_ITEM* aItem )
134 {
135 wxASSERT_MSG( aValue.CheckType<double>(), "Expecting double-containing value" );
136
137 double val = aValue.As<double>();
138
139 if( val > 1.0 )
140 {
141 return std::make_unique<VALIDATION_ERROR_TOO_LARGE<double>>( val, 1.0,
142 EDA_DATA_TYPE::UNITLESS );
143 }
144 else if( val < 0.0 )
145 {
146 return std::make_unique<VALIDATION_ERROR_TOO_SMALL<double>>( val, 0.0,
147 EDA_DATA_TYPE::UNITLESS );
148 }
149
150 return std::nullopt;
151 }
152};
153
154#endif //KICAD_PROPERTY_VALIDATORS_H
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
A set of generic validators.
static VALIDATOR_RESULT PositiveRatioValidator(const wxAny &&aValue, EDA_ITEM *aItem)
static VALIDATOR_RESULT PositiveIntValidator(const wxAny &&aValue, EDA_ITEM *aItem)
static VALIDATOR_RESULT RangeIntValidator(const wxAny &&aValue, EDA_ITEM *aItem)
wxString StringFromValue(double aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aValue in internal units into a united string.
A validator for use when you just need to return an error string rather than also packaging some othe...
wxString Format(UNITS_PROVIDER *aUnits) const override
VALIDATION_ERROR_MSG(const wxString &aMessage)
wxString Format(UNITS_PROVIDER *aUnits) const override
VALIDATION_ERROR_TOO_LARGE(T aActual, T aMaximum, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
VALIDATION_ERROR_TOO_SMALL(T aActual, T aMinimum, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
wxString Format(UNITS_PROVIDER *aUnits) const override
Represents an error returned by a validator and contains enough data to format an error message.
EDA_DATA_TYPE
The type of unit.
Definition: eda_units.h:38
std::optional< std::unique_ptr< VALIDATION_ERROR > > VALIDATOR_RESULT
Null optional means validation succeeded.