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, PCB_LAYER_ID aLayer ) :
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 netname reference (for performance).
132{
133public:
134 PCBEXPR_NETNAME_REF( int aItemIndex ) :
135 PCBEXPR_VAR_REF( aItemIndex )
136 {
138 }
139
140 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
141};
142
143
145{
146public:
147 PCBEXPR_TYPE_REF( int aItemIndex ) :
148 PCBEXPR_VAR_REF( aItemIndex )
149 {
151 }
152
153 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
154};
155
156
158{
159public:
161
163 {
164 static PCBEXPR_BUILTIN_FUNCTIONS self;
165 return self;
166 }
167
168 LIBEVAL::FUNC_CALL_REF Get( const wxString& name )
169 {
170 return m_funcs[ name ];
171 }
172
173 const wxArrayString GetSignatures() const
174 {
175 return m_funcSigs;
176 }
177
178 void RegisterFunc( const wxString& funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr )
179 {
180 wxString funcName = funcSignature.BeforeFirst( '(' );
181 m_funcs[std::string( funcName.Lower() )] = std::move( funcPtr );
182 m_funcSigs.Add( funcSignature );
183 }
184
186
187private:
188 std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
189
190 wxArrayString m_funcSigs;
191};
192
193
195{
196public:
197 const std::vector<wxString>& GetSupportedUnits() const override;
198
199 wxString GetSupportedUnitsMessage() const override;
200
201 double Convert( const wxString& aString, int unitId ) const override;
202};
203
204
206{
207public:
208 const std::vector<wxString>& GetSupportedUnits() const override;
209
210 double Convert( const wxString& aString, int unitId ) const override;
211};
212
213
215{
216public:
218};
219
220
222{
223public:
226
227 bool Evaluate( const wxString& aExpr );
228 int Result() const { return m_result; }
229
230 void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
231 {
232 m_compiler.SetErrorCallback( aCallback );
233 }
234
237
238private:
240
244};
245
246#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:77
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:282
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()
BOARD_ITEM * m_items[2]
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
PCBEXPR_CONTEXT(int aConstraint, PCB_LAYER_ID aLayer)
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
std::function< void(CONTEXT *, void *)> FUNC_CALL_REF
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:47