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 m_isOptional( false )
90 {}
91
93
94 void SetIsEnum( bool s ) { m_isEnum = s; }
95 bool IsEnum() const { return m_isEnum; }
96
97 void SetIsOptional( bool s = true ) { m_isOptional = s; }
98 bool IsOptional() const { return m_isOptional; }
99
100 void SetType( LIBEVAL::VAR_TYPE_T type ) { m_type = type; }
101 LIBEVAL::VAR_TYPE_T GetType() const override { return m_type; }
102
103 void AddAllowedClass( TYPE_ID type_hash, PROPERTY_BASE* prop )
104 {
105 m_matchingTypes[type_hash] = prop;
106 }
107
108 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
109
110 BOARD_ITEM* GetObject( const LIBEVAL::CONTEXT* aCtx ) const;
111
112private:
113 std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes;
118};
119
120
121// "Object code" version of a netclass reference (for performance).
123{
124public:
125 PCBEXPR_NETCLASS_REF( int aItemIndex ) :
126 PCBEXPR_VAR_REF( aItemIndex )
127 {
129 }
130
131 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
132};
133
134
135// "Object code" version of a component class reference (for performance).
137{
138public:
139 PCBEXPR_COMPONENT_CLASS_REF( int aItemIndex ) : PCBEXPR_VAR_REF( aItemIndex )
140 {
142 }
143
144 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
145};
146
147
148// "Object code" version of a netname reference (for performance).
150{
151public:
152 PCBEXPR_NETNAME_REF( int aItemIndex ) :
153 PCBEXPR_VAR_REF( aItemIndex )
154 {
156 }
157
158 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
159};
160
161
163{
164public:
165 PCBEXPR_TYPE_REF( int aItemIndex ) :
166 PCBEXPR_VAR_REF( aItemIndex )
167 {
169 }
170
171 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
172};
173
174
176{
177public:
179
181 {
182 static PCBEXPR_BUILTIN_FUNCTIONS self;
183 return self;
184 }
185
186 LIBEVAL::FUNC_CALL_REF Get( const wxString& name )
187 {
188 return m_funcs[ name ];
189 }
190
191 const wxArrayString GetSignatures() const
192 {
193 return m_funcSigs;
194 }
195
196 void RegisterFunc( const wxString& funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr )
197 {
198 wxString funcName = funcSignature.BeforeFirst( '(' );
199 m_funcs[std::string( funcName.Lower() )] = std::move( funcPtr );
200 m_funcSigs.Add( funcSignature );
201 }
202
204
205private:
206 std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
207
208 wxArrayString m_funcSigs;
209};
210
211
213{
214public:
215 const std::vector<wxString>& GetSupportedUnits() const override;
216
217 wxString GetSupportedUnitsMessage() const override;
218
219 double Convert( const wxString& aString, int unitId ) const override;
220};
221
222
224{
225public:
226 const std::vector<wxString>& GetSupportedUnits() const override;
227
228 double Convert( const wxString& aString, int unitId ) const override;
229};
230
231
233{
234public:
236};
237
238
240{
241public:
244
245 bool Evaluate( const wxString& aExpr );
246 int Result() const { return m_result; }
247
248 void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
249 {
250 m_compiler.SetErrorCallback( aCallback );
251 }
252
255
256private:
258
262};
263
264#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
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
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