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 The 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>() || aValue.CheckType<std::optional<int>>(),
110 "Expecting int-containing value" );
111
112 int val = 0;
113
114 if( aValue.CheckType<int>() )
115 {
116 val = aValue.As<int>();
117 }
118 else if( aValue.CheckType<std::optional<int>>() )
119 {
120 if( aValue.As<std::optional<int>>().has_value() )
121 val = aValue.As<std::optional<int>>().value();
122 else
123 return std::nullopt; // no value for a std::optional is always valid
124 }
125
126 if( val > Max )
127 return std::make_unique<VALIDATION_ERROR_TOO_LARGE<int>>( val, Max );
128 else if( val < Min )
129 return std::make_unique<VALIDATION_ERROR_TOO_SMALL<int>>( val, Min );
130
131 return std::nullopt;
132 }
133
134 static VALIDATOR_RESULT PositiveIntValidator( const wxAny&& aValue, EDA_ITEM* aItem )
135 {
136 wxASSERT_MSG( aValue.CheckType<int>() || aValue.CheckType<std::optional<int>>(),
137 "Expecting int-containing value" );
138
139 int val = 0;
140
141 if( aValue.CheckType<int>() )
142 {
143 val = aValue.As<int>();
144 }
145 else if( aValue.CheckType<std::optional<int>>() )
146 {
147 if( aValue.As<std::optional<int>>().has_value() )
148 val = aValue.As<std::optional<int>>().value();
149 else
150 return std::nullopt; // no value for a std::optional is always valid
151 }
152
153 if( val < 0 )
154 return std::make_unique<VALIDATION_ERROR_TOO_SMALL<int>>( val, 0 );
155
156 return std::nullopt;
157 }
158
159 static VALIDATOR_RESULT PositiveRatioValidator( const wxAny&& aValue, EDA_ITEM* aItem )
160 {
161 wxASSERT_MSG( aValue.CheckType<double>(), "Expecting double-containing value" );
162
163 double val = aValue.As<double>();
164
165 if( val > 1.0 )
166 {
167 return std::make_unique<VALIDATION_ERROR_TOO_LARGE<double>>( val, 1.0,
168 EDA_DATA_TYPE::UNITLESS );
169 }
170 else if( val < 0.0 )
171 {
172 return std::make_unique<VALIDATION_ERROR_TOO_SMALL<double>>( val, 0.0,
173 EDA_DATA_TYPE::UNITLESS );
174 }
175
176 return std::nullopt;
177 }
178};
179
180#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.