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 return wxString::Format( wxS( "Value must be less than or equal to %s" ),
51 aUnits->StringFromValue( Maximum, true ) );
52 }
53};
54
55
56template<typename T>
58{
59public:
63
64 VALIDATION_ERROR_TOO_SMALL( T aActual, T aMinimum,
65 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) :
66 Actual( aActual ),
67 Minimum( aMinimum ),
68 DataType( aType )
69 {}
70
71 wxString Format( UNITS_PROVIDER* aUnits ) const override
72 {
73 return wxString::Format( wxS( "Value must be greater than or equal to %s" ),
74 aUnits->StringFromValue( Minimum, true ) );
75 }
76};
77
78
84{
85public:
86 wxString Message;
87
88 VALIDATION_ERROR_MSG( const wxString& aMessage ) : Message( aMessage ) {}
89
90 wxString Format( UNITS_PROVIDER* aUnits ) const override
91 {
92 return Message;
93 }
94};
95
96
101{
102public:
103
104 template<int Min, int Max>
105 static VALIDATOR_RESULT RangeIntValidator( const wxAny&& aValue, EDA_ITEM* aItem )
106 {
107 wxASSERT_MSG( aValue.CheckType<int>(), "Expecting int-containing value" );
108
109 int val = aValue.As<int>();
110
111 if( val > Max )
112 return std::make_unique<VALIDATION_ERROR_TOO_LARGE<int>>( val, Max );
113 else if( val < Min )
114 return std::make_unique<VALIDATION_ERROR_TOO_SMALL<int>>( val, Min );
115
116 return std::nullopt;
117 }
118
119 static VALIDATOR_RESULT PositiveIntValidator( const wxAny&& aValue, EDA_ITEM* aItem )
120 {
121 wxASSERT_MSG( aValue.CheckType<int>(), "Expecting int-containing value" );
122
123 int val = aValue.As<int>();
124
125 if( val < 0 )
126 return std::make_unique<VALIDATION_ERROR_TOO_SMALL<int>>( val, 0 );
127
128 return std::nullopt;
129 }
130};
131
132#endif //KICAD_PROPERTY_VALIDATORS_H
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
A set of generic validators.
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)
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:36
std::optional< std::unique_ptr< VALIDATION_ERROR > > VALIDATOR_RESULT
Null optional means validation succeeded.