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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24
25#ifndef PCBEXPR_EVALUATOR_H
26#define PCBEXPR_EVALUATOR_H
27
28#include <set>
29#include <map>
30#include <unordered_map>
31#include <core/typeinfo.h>
32
33#include <layer_ids.h>
34
35#include <properties/property.h>
37
39
40class BOARD;
41class BOARD_ITEM;
42
43class PCBEXPR_VAR_REF;
44
45class PCBEXPR_UCODE final : public LIBEVAL::UCODE
46{
47public:
49 virtual ~PCBEXPR_UCODE() {};
50
51 virtual std::unique_ptr<LIBEVAL::VAR_REF> CreateVarRef( const wxString& aVar,
52 const wxString& aField ) override;
53 virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall( const wxString& aName ) override;
54
56
57private:
59};
60
61
63{
64public:
65 PCBEXPR_CONTEXT( int aConstraint = 0, PCB_LAYER_ID aLayer = F_Cu ) :
66 m_constraint( aConstraint ),
67 m_layer( aLayer )
68 {
69 m_items[0] = nullptr;
70 m_items[1] = nullptr;
71 }
72
73 void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
74 {
75 m_items[0] = a;
76 m_items[1] = b;
77 }
78
79 void SetTypeOverride( const BOARD_ITEM* aItem, KICAD_T aType ) { m_typeOverrides[aItem] = aType; }
80
81 KICAD_T GetEffectiveType( const BOARD_ITEM* aItem ) const;
82
83 BOARD* GetBoard() const;
84
85 int GetConstraint() const { return m_constraint; }
86 BOARD_ITEM* GetItem( int index ) const { return m_items[index]; }
87 PCB_LAYER_ID GetLayer() const { return m_layer; }
88
89private:
93 std::map<const BOARD_ITEM*, KICAD_T> m_typeOverrides;
94};
95
96
98{
99public:
100 PCBEXPR_VAR_REF( int aItemIndex ) :
101 m_itemIndex( aItemIndex ),
102 m_type( LIBEVAL::VT_UNDEFINED ),
103 m_isEnum( false ),
104 m_isOptional( false )
105 {}
106
108
109 void SetIsEnum( bool s ) { m_isEnum = s; }
110 bool IsEnum() const { return m_isEnum; }
111
112 void SetIsOptional( bool s = true ) { m_isOptional = s; }
113 bool IsOptional() const { return m_isOptional; }
114
115 void SetType( LIBEVAL::VAR_TYPE_T type ) { m_type = type; }
116 LIBEVAL::VAR_TYPE_T GetType() const override { return m_type; }
117
118 void AddAllowedClass( TYPE_ID type_hash, PROPERTY_BASE* prop )
119 {
120 m_matchingTypes[type_hash] = prop;
121 }
122
123 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
124
125 BOARD_ITEM* GetObject( const LIBEVAL::CONTEXT* aCtx ) const;
126
127private:
128 std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes;
133};
134
135
136// "Object code" version of a netclass reference (for performance).
138{
139public:
140 PCBEXPR_NETCLASS_REF( int aItemIndex ) :
141 PCBEXPR_VAR_REF( aItemIndex )
142 {
144 }
145
146 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
147};
148
149
150// "Object code" version of a component class reference (for performance).
152{
153public:
154 PCBEXPR_COMPONENT_CLASS_REF( int aItemIndex ) : PCBEXPR_VAR_REF( aItemIndex )
155 {
157 }
158
159 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
160};
161
162
163// "Object code" version of a netname reference (for performance).
165{
166public:
167 PCBEXPR_NETNAME_REF( int aItemIndex ) :
168 PCBEXPR_VAR_REF( aItemIndex )
169 {
171 }
172
173 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
174};
175
176
178{
179public:
180 PCBEXPR_TYPE_REF( int aItemIndex ) :
181 PCBEXPR_VAR_REF( aItemIndex )
182 {
184 }
185
186 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
187};
188
189
191{
192public:
194
196 {
197 static PCBEXPR_BUILTIN_FUNCTIONS self;
198 return self;
199 }
200
201 LIBEVAL::FUNC_CALL_REF Get( const wxString& name )
202 {
203 return m_funcs[ name ];
204 }
205
206 bool IsGeometryDependent( const wxString& name ) const
207 {
208 return m_geometryDependentFuncs.count( name ) > 0;
209 }
210
211 const wxArrayString GetSignatures() const
212 {
213 return m_funcSigs;
214 }
215
216 void RegisterFunc( const wxString& funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr,
217 bool aIsGeometryDependent = false )
218 {
219 wxString funcName = funcSignature.BeforeFirst( '(' );
220 wxString lower = funcName.Lower();
221 m_funcs[std::string( lower )] = std::move( funcPtr );
222 m_funcSigs.Add( funcSignature );
223
224 if( aIsGeometryDependent )
225 m_geometryDependentFuncs.insert( lower );
226 }
227
229
230private:
231 std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
232 std::set<wxString> m_geometryDependentFuncs;
233
234 wxArrayString m_funcSigs;
235};
236
237
239{
240public:
241 const std::vector<wxString>& GetSupportedUnits() const override;
242 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
243
244 wxString GetSupportedUnitsMessage() const override;
245
246 double Convert( const wxString& aString, int unitId ) const override;
247};
248
249
251{
252public:
253 const std::vector<wxString>& GetSupportedUnits() const override;
254 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
255
256 double Convert( const wxString& aString, int unitId ) const override;
257};
258
259
261{
262public:
264};
265
266
268{
269public:
272
273 bool Evaluate( const wxString& aExpr );
274 int Result() const { return m_result; }
275 EDA_UNITS Units() const { return m_units; }
276
277 void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
278 {
279 m_compiler.SetErrorCallback( std::move( aCallback ) );
280 }
281
282 bool IsErrorPending() const { return m_errorStatus.pendingError; }
284
285private:
288
292};
293
294#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:84
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
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::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 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:48
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ F_Cu
Definition layer_ids.h:64
std::function< void(CONTEXT *, void *)> FUNC_CALL_REF
size_t TYPE_ID
Unique type identifier.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:75