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 (C) 2019-2022 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 <unordered_map>
29
30#include <properties/property.h>
32
34
35class BOARD;
36class BOARD_ITEM;
37
38class PCBEXPR_VAR_REF;
39
40class PCBEXPR_UCODE final : public LIBEVAL::UCODE
41{
42public:
44 virtual ~PCBEXPR_UCODE() {};
45
46 virtual std::unique_ptr<LIBEVAL::VAR_REF> CreateVarRef( const wxString& aVar,
47 const wxString& aField ) override;
48 virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall( const wxString& aName ) override;
49};
50
51
53{
54public:
55 PCBEXPR_CONTEXT( int aConstraint = 0, PCB_LAYER_ID aLayer = F_Cu ) :
56 m_constraint( aConstraint ),
57 m_layer( aLayer )
58 {
59 m_items[0] = nullptr;
60 m_items[1] = nullptr;
61 }
62
63 void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
64 {
65 m_items[0] = a;
66 m_items[1] = b;
67 }
68
69 BOARD* GetBoard() const;
70
71 int GetConstraint() const { return m_constraint; }
72 BOARD_ITEM* GetItem( int index ) const { return m_items[index]; }
73 PCB_LAYER_ID GetLayer() const { return m_layer; }
74
75private:
79};
80
81
83{
84public:
85 PCBEXPR_VAR_REF( int aItemIndex ) :
86 m_itemIndex( aItemIndex ),
87 m_type( LIBEVAL::VT_UNDEFINED ),
88 m_isEnum( false )
89 {}
90
92
93 void SetIsEnum( bool s ) { m_isEnum = s; }
94 bool IsEnum() const { return m_isEnum; }
95
96 void SetType( LIBEVAL::VAR_TYPE_T type ) { m_type = type; }
97 LIBEVAL::VAR_TYPE_T GetType() const override { return m_type; }
98
99 void AddAllowedClass( TYPE_ID type_hash, PROPERTY_BASE* prop )
100 {
101 m_matchingTypes[type_hash] = prop;
102 }
103
104 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
105
106 BOARD_ITEM* GetObject( const LIBEVAL::CONTEXT* aCtx ) const;
107
108private:
109 std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes;
113};
114
115
116// "Object code" version of a netclass reference (for performance).
118{
119public:
120 PCBEXPR_NETCLASS_REF( int aItemIndex ) :
121 PCBEXPR_VAR_REF( aItemIndex )
122 {
124 }
125
126 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
127};
128
129
130// "Object code" version of a component class reference (for performance).
132{
133public:
134 PCBEXPR_COMPONENT_CLASS_REF( int aItemIndex ) : PCBEXPR_VAR_REF( aItemIndex )
135 {
137 }
138
139 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
140};
141
142
143// "Object code" version of a netname reference (for performance).
145{
146public:
147 PCBEXPR_NETNAME_REF( int aItemIndex ) :
148 PCBEXPR_VAR_REF( aItemIndex )
149 {
151 }
152
153 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
154};
155
156
158{
159public:
160 PCBEXPR_TYPE_REF( int aItemIndex ) :
161 PCBEXPR_VAR_REF( aItemIndex )
162 {
164 }
165
166 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
167};
168
169
171{
172public:
174
176 {
177 static PCBEXPR_BUILTIN_FUNCTIONS self;
178 return self;
179 }
180
181 LIBEVAL::FUNC_CALL_REF Get( const wxString& name )
182 {
183 return m_funcs[ name ];
184 }
185
186 const wxArrayString GetSignatures() const
187 {
188 return m_funcSigs;
189 }
190
191 void RegisterFunc( const wxString& funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr )
192 {
193 wxString funcName = funcSignature.BeforeFirst( '(' );
194 m_funcs[std::string( funcName.Lower() )] = std::move( funcPtr );
195 m_funcSigs.Add( funcSignature );
196 }
197
199
200private:
201 std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
202
203 wxArrayString m_funcSigs;
204};
205
206
208{
209public:
210 const std::vector<wxString>& GetSupportedUnits() const override;
211
212 wxString GetSupportedUnitsMessage() const override;
213
214 double Convert( const wxString& aString, int unitId ) const override;
215};
216
217
219{
220public:
221 const std::vector<wxString>& GetSupportedUnits() const override;
222
223 double Convert( const wxString& aString, int unitId ) const override;
224};
225
226
228{
229public:
231};
232
233
235{
236public:
239
240 bool Evaluate( const wxString& aExpr );
241 int Result() const { return m_result; }
242
243 void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
244 {
245 m_compiler.SetErrorCallback( aCallback );
246 }
247
250
251private:
253
257};
258
259#endif
const char * name
Definition: DXF_plotter.cpp:57
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
void RegisterFunc(const wxString &funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr)
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()
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_UCODE m_ucode
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)
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
virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall(const wxString &aName) override
virtual ~PCBEXPR_UCODE()
const std::vector< wxString > & GetSupportedUnits() const override
double Convert(const wxString &aString, int unitId) const override
double Convert(const wxString &aString, int unitId) const override
wxString GetSupportedUnitsMessage() const override
const std::vector< wxString > & GetSupportedUnits() const override
bool IsEnum() const
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
void SetType(LIBEVAL::VAR_TYPE_T type)
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
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.
Definition: property_mgr.h:47