KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbexpr_evaluator.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20
21#ifndef PCBEXPR_EVALUATOR_H
22#define PCBEXPR_EVALUATOR_H
23
24#include <set>
25#include <map>
26#include <unordered_map>
27#include <core/typeinfo.h>
28
29#include <layer_ids.h>
30
31#include <properties/property.h>
33
35
36class BOARD;
37class BOARD_ITEM;
38
39class PCBEXPR_VAR_REF;
40
41
42// A navigation step applied to a variable reference before its property or method is resolved.
43// "A.Parent.getField('x')" resolves item A, then steps to its parent before calling getField().
45{
47};
48
49class PCBEXPR_UCODE final : public LIBEVAL::UCODE
50{
51public:
53 virtual ~PCBEXPR_UCODE() {};
54
55 virtual std::unique_ptr<LIBEVAL::VAR_REF> CreateVarRef( const wxString& aVar,
56 const wxString& aField ) override;
57 virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall( const wxString& aName ) override;
58
60
61private:
63};
64
65
67{
68public:
69 PCBEXPR_CONTEXT( int aConstraint = 0, PCB_LAYER_ID aLayer = F_Cu ) :
70 m_constraint( aConstraint ),
71 m_layer( aLayer )
72 {
73 m_items[0] = nullptr;
74 m_items[1] = nullptr;
75 }
76
77 void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
78 {
79 m_items[0] = a;
80 m_items[1] = b;
81 }
82
83 void SetTypeOverride( const BOARD_ITEM* aItem, KICAD_T aType ) { m_typeOverrides[aItem] = aType; }
84
85 KICAD_T GetEffectiveType( const BOARD_ITEM* aItem ) const;
86
87 BOARD* GetBoard() const;
88
89 int GetConstraint() const { return m_constraint; }
90 BOARD_ITEM* GetItem( int index ) const { return m_items[index]; }
91 PCB_LAYER_ID GetLayer() const { return m_layer; }
92
93private:
97 std::map<const BOARD_ITEM*, KICAD_T> m_typeOverrides;
98};
99
100
102{
103public:
104 PCBEXPR_VAR_REF( int aItemIndex ) :
105 m_itemIndex( aItemIndex ),
106 m_type( LIBEVAL::VT_UNDEFINED ),
107 m_isEnum( false ),
108 m_isOptional( false )
109 {}
110
112
113 void SetIsEnum( bool s ) { m_isEnum = s; }
114 bool IsEnum() const { return m_isEnum; }
115
116 void SetIsOptional( bool s = true ) { m_isOptional = s; }
117 bool IsOptional() const { return m_isOptional; }
118
119 void SetType( LIBEVAL::VAR_TYPE_T type ) { m_type = type; }
120 LIBEVAL::VAR_TYPE_T GetType() const override { return m_type; }
121
122 void AddAllowedClass( TYPE_ID type_hash, PROPERTY_BASE* prop )
123 {
124 m_matchingTypes[type_hash] = prop;
125 }
126
127 // Navigation steps walked from the base item before the property/method is resolved.
128 // An empty chain (the default) resolves the base item directly, as before.
129 void SetNavigation( std::vector<PCBEXPR_NAV_STEP> aNavigation )
130 {
131 m_navigation = std::move( aNavigation );
132 }
133
134 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
135
136 BOARD_ITEM* GetObject( const LIBEVAL::CONTEXT* aCtx ) const;
137
138private:
139 std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes;
144 std::vector<PCBEXPR_NAV_STEP> m_navigation;
145};
146
147
148// "Object code" version of a netclass reference (for performance).
150{
151public:
152 PCBEXPR_NETCLASS_REF( int aItemIndex ) :
153 PCBEXPR_VAR_REF( aItemIndex )
154 {
156 }
157
158 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
159};
160
161
162// "Object code" version of a component class reference (for performance).
164{
165public:
166 PCBEXPR_COMPONENT_CLASS_REF( int aItemIndex ) : PCBEXPR_VAR_REF( aItemIndex )
167 {
169 }
170
171 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
172};
173
174
175// "Object code" version of a netname reference (for performance).
177{
178public:
179 PCBEXPR_NETNAME_REF( int aItemIndex ) :
180 PCBEXPR_VAR_REF( aItemIndex )
181 {
183 }
184
185 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
186};
187
188
190{
191public:
192 PCBEXPR_TYPE_REF( int aItemIndex ) :
193 PCBEXPR_VAR_REF( aItemIndex )
194 {
196 }
197
198 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
199};
200
201
203{
204public:
206
208 {
209 static PCBEXPR_BUILTIN_FUNCTIONS self;
210 return self;
211 }
212
213 LIBEVAL::FUNC_CALL_REF Get( const wxString& name )
214 {
215 return m_funcs[ name ];
216 }
217
218 bool IsGeometryDependent( const wxString& name ) const
219 {
220 return m_geometryDependentFuncs.count( name ) > 0;
221 }
222
223 const wxArrayString GetSignatures() const
224 {
225 return m_funcSigs;
226 }
227
228 void RegisterFunc( const wxString& funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr,
229 bool aIsGeometryDependent = false )
230 {
231 wxString funcName = funcSignature.BeforeFirst( '(' );
232 wxString lower = funcName.Lower();
233 m_funcs[std::string( lower )] = std::move( funcPtr );
234 m_funcSigs.Add( funcSignature );
235
236 if( aIsGeometryDependent )
237 m_geometryDependentFuncs.insert( lower );
238 }
239
241
242private:
243 std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
244 std::set<wxString> m_geometryDependentFuncs;
245
246 wxArrayString m_funcSigs;
247};
248
249
251{
252public:
253 const std::vector<wxString>& GetSupportedUnits() const override;
254 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
255
256 wxString GetSupportedUnitsMessage() const override;
257
258 double Convert( const wxString& aString, int unitId ) const override;
259};
260
261
263{
264public:
265 const std::vector<wxString>& GetSupportedUnits() const override;
266 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
267
268 double Convert( const wxString& aString, int unitId ) const override;
269};
270
271
273{
274public:
276};
277
278
280{
281public:
284
285 bool Evaluate( const wxString& aExpr );
286 int Result() const { return m_result; }
287 EDA_UNITS Units() const { return m_units; }
288
289 void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
290 {
291 m_compiler.SetErrorCallback( std::move( aCallback ) );
292 }
293
294 bool IsErrorPending() const { return m_errorStatus.pendingError; }
296
297private:
300
304};
305
306#endif
int index
const char * name
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:80
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:320
bool IsGeometryDependent(const wxString &name) const
const wxArrayString GetSignatures() const
LIBEVAL::FUNC_CALL_REF Get(const wxString &name)
std::map< wxString, LIBEVAL::FUNC_CALL_REF > m_funcs
static PCBEXPR_BUILTIN_FUNCTIONS & Instance()
void RegisterFunc(const wxString &funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr, bool aIsGeometryDependent=false)
std::set< wxString > m_geometryDependentFuncs
PCBEXPR_COMPILER(LIBEVAL::UNIT_RESOLVER *aUnitResolver)
PCBEXPR_COMPONENT_CLASS_REF(int aItemIndex)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
BOARD_ITEM * m_items[2]
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
PCBEXPR_CONTEXT(int aConstraint=0, PCB_LAYER_ID aLayer=F_Cu)
BOARD * GetBoard() const
int GetConstraint() const
PCB_LAYER_ID m_layer
KICAD_T GetEffectiveType(const BOARD_ITEM *aItem) const
PCB_LAYER_ID GetLayer() const
void SetTypeOverride(const BOARD_ITEM *aItem, KICAD_T aType)
std::map< const BOARD_ITEM *, KICAD_T > m_typeOverrides
BOARD_ITEM * GetItem(int index) const
LIBEVAL::ERROR_STATUS m_errorStatus
bool IsErrorPending() const
PCBEXPR_EVALUATOR(LIBEVAL::UNIT_RESOLVER *aUnitResolver)
PCBEXPR_COMPILER m_compiler
const LIBEVAL::ERROR_STATUS & GetError() const
bool Evaluate(const wxString &aExpr)
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
EDA_UNITS Units() const
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
PCBEXPR_NETCLASS_REF(int aItemIndex)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
PCBEXPR_NETNAME_REF(int aItemIndex)
PCBEXPR_TYPE_REF(int aItemIndex)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
virtual std::unique_ptr< LIBEVAL::VAR_REF > CreateVarRef(const wxString &aVar, const wxString &aField) override
bool HasGeometryDependentFunctions() const
virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall(const wxString &aName) override
virtual ~PCBEXPR_UCODE()
bool m_hasGeometryDependentFunctions
const std::vector< wxString > & GetSupportedUnits() const override
const std::vector< EDA_UNITS > & GetSupportedUnitsTypes() const override
double Convert(const wxString &aString, int unitId) const override
double Convert(const wxString &aString, int unitId) const override
const std::vector< EDA_UNITS > & GetSupportedUnitsTypes() const override
wxString GetSupportedUnitsMessage() const override
const std::vector< wxString > & GetSupportedUnits() const override
std::vector< PCBEXPR_NAV_STEP > m_navigation
std::unordered_map< TYPE_ID, PROPERTY_BASE * > m_matchingTypes
void SetIsEnum(bool s)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
LIBEVAL::VAR_TYPE_T GetType() const override
bool IsOptional() const
void SetType(LIBEVAL::VAR_TYPE_T type)
void SetIsOptional(bool s=true)
PCBEXPR_VAR_REF(int aItemIndex)
void SetNavigation(std::vector< PCBEXPR_NAV_STEP > aNavigation)
void AddAllowedClass(TYPE_ID type_hash, PROPERTY_BASE *prop)
LIBEVAL::VAR_TYPE_T m_type
BOARD_ITEM * GetObject(const LIBEVAL::CONTEXT *aCtx) const
EDA_UNITS
Definition eda_units.h:44
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_Cu
Definition layer_ids.h:60
std::function< void(CONTEXT *, void *)> FUNC_CALL_REF
PCBEXPR_NAV_STEP
size_t TYPE_ID
Unique type identifier.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:71