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 <unordered_map>
30
31#include <layer_ids.h>
32
33#include <properties/property.h>
35
37
38class BOARD;
39class BOARD_ITEM;
40
41class PCBEXPR_VAR_REF;
42
43class PCBEXPR_UCODE final : public LIBEVAL::UCODE
44{
45public:
47 virtual ~PCBEXPR_UCODE() {};
48
49 virtual std::unique_ptr<LIBEVAL::VAR_REF> CreateVarRef( const wxString& aVar,
50 const wxString& aField ) override;
51 virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall( const wxString& aName ) override;
52
54
55private:
57};
58
59
61{
62public:
63 PCBEXPR_CONTEXT( int aConstraint = 0, PCB_LAYER_ID aLayer = F_Cu ) :
64 m_constraint( aConstraint ),
65 m_layer( aLayer )
66 {
67 m_items[0] = nullptr;
68 m_items[1] = nullptr;
69 }
70
71 void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
72 {
73 m_items[0] = a;
74 m_items[1] = b;
75 }
76
77 BOARD* GetBoard() const;
78
79 int GetConstraint() const { return m_constraint; }
80 BOARD_ITEM* GetItem( int index ) const { return m_items[index]; }
81 PCB_LAYER_ID GetLayer() const { return m_layer; }
82
83private:
87};
88
89
91{
92public:
93 PCBEXPR_VAR_REF( int aItemIndex ) :
94 m_itemIndex( aItemIndex ),
95 m_type( LIBEVAL::VT_UNDEFINED ),
96 m_isEnum( false ),
97 m_isOptional( false )
98 {}
99
101
102 void SetIsEnum( bool s ) { m_isEnum = s; }
103 bool IsEnum() const { return m_isEnum; }
104
105 void SetIsOptional( bool s = true ) { m_isOptional = s; }
106 bool IsOptional() const { return m_isOptional; }
107
108 void SetType( LIBEVAL::VAR_TYPE_T type ) { m_type = type; }
109 LIBEVAL::VAR_TYPE_T GetType() const override { return m_type; }
110
111 void AddAllowedClass( TYPE_ID type_hash, PROPERTY_BASE* prop )
112 {
113 m_matchingTypes[type_hash] = prop;
114 }
115
116 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
117
118 BOARD_ITEM* GetObject( const LIBEVAL::CONTEXT* aCtx ) const;
119
120private:
121 std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes;
126};
127
128
129// "Object code" version of a netclass reference (for performance).
131{
132public:
133 PCBEXPR_NETCLASS_REF( int aItemIndex ) :
134 PCBEXPR_VAR_REF( aItemIndex )
135 {
137 }
138
139 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
140};
141
142
143// "Object code" version of a component class reference (for performance).
145{
146public:
147 PCBEXPR_COMPONENT_CLASS_REF( int aItemIndex ) : PCBEXPR_VAR_REF( aItemIndex )
148 {
150 }
151
152 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
153};
154
155
156// "Object code" version of a netname reference (for performance).
158{
159public:
160 PCBEXPR_NETNAME_REF( int aItemIndex ) :
161 PCBEXPR_VAR_REF( aItemIndex )
162 {
164 }
165
166 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
167};
168
169
171{
172public:
173 PCBEXPR_TYPE_REF( int aItemIndex ) :
174 PCBEXPR_VAR_REF( aItemIndex )
175 {
177 }
178
179 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
180};
181
182
184{
185public:
187
189 {
190 static PCBEXPR_BUILTIN_FUNCTIONS self;
191 return self;
192 }
193
194 LIBEVAL::FUNC_CALL_REF Get( const wxString& name )
195 {
196 return m_funcs[ name ];
197 }
198
199 bool IsGeometryDependent( const wxString& name ) const
200 {
201 return m_geometryDependentFuncs.count( name ) > 0;
202 }
203
204 const wxArrayString GetSignatures() const
205 {
206 return m_funcSigs;
207 }
208
209 void RegisterFunc( const wxString& funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr,
210 bool aIsGeometryDependent = false )
211 {
212 wxString funcName = funcSignature.BeforeFirst( '(' );
213 wxString lower = funcName.Lower();
214 m_funcs[std::string( lower )] = std::move( funcPtr );
215 m_funcSigs.Add( funcSignature );
216
217 if( aIsGeometryDependent )
218 m_geometryDependentFuncs.insert( lower );
219 }
220
222
223private:
224 std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
225 std::set<wxString> m_geometryDependentFuncs;
226
227 wxArrayString m_funcSigs;
228};
229
230
232{
233public:
234 const std::vector<wxString>& GetSupportedUnits() const override;
235 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
236
237 wxString GetSupportedUnitsMessage() const override;
238
239 double Convert( const wxString& aString, int unitId ) const override;
240};
241
242
244{
245public:
246 const std::vector<wxString>& GetSupportedUnits() const override;
247 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
248
249 double Convert( const wxString& aString, int unitId ) const override;
250};
251
252
254{
255public:
257};
258
259
261{
262public:
265
266 bool Evaluate( const wxString& aExpr );
267 int Result() const { return m_result; }
268 EDA_UNITS Units() const { return m_units; }
269
270 void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
271 {
272 m_compiler.SetErrorCallback( std::move( aCallback ) );
273 }
274
275 bool IsErrorPending() const { return m_errorStatus.pendingError; }
277
278private:
281
285};
286
287#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:322
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
PCB_LAYER_ID GetLayer() const
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.