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 <unordered_map>
29
30#include <layer_ids.h>
31
32#include <properties/property.h>
34
36
37class BOARD;
38class BOARD_ITEM;
39
40class PCBEXPR_VAR_REF;
41
42class PCBEXPR_UCODE final : public LIBEVAL::UCODE
43{
44public:
46 virtual ~PCBEXPR_UCODE() {};
47
48 virtual std::unique_ptr<LIBEVAL::VAR_REF> CreateVarRef( const wxString& aVar,
49 const wxString& aField ) override;
50 virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall( const wxString& aName ) override;
51};
52
53
55{
56public:
57 PCBEXPR_CONTEXT( int aConstraint = 0, PCB_LAYER_ID aLayer = F_Cu ) :
58 m_constraint( aConstraint ),
59 m_layer( aLayer )
60 {
61 m_items[0] = nullptr;
62 m_items[1] = nullptr;
63 }
64
65 void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
66 {
67 m_items[0] = a;
68 m_items[1] = b;
69 }
70
71 BOARD* GetBoard() const;
72
73 int GetConstraint() const { return m_constraint; }
74 BOARD_ITEM* GetItem( int index ) const { return m_items[index]; }
75 PCB_LAYER_ID GetLayer() const { return m_layer; }
76
77private:
81};
82
83
85{
86public:
87 PCBEXPR_VAR_REF( int aItemIndex ) :
88 m_itemIndex( aItemIndex ),
89 m_type( LIBEVAL::VT_UNDEFINED ),
90 m_isEnum( false ),
91 m_isOptional( false )
92 {}
93
95
96 void SetIsEnum( bool s ) { m_isEnum = s; }
97 bool IsEnum() const { return m_isEnum; }
98
99 void SetIsOptional( bool s = true ) { m_isOptional = s; }
100 bool IsOptional() const { return m_isOptional; }
101
102 void SetType( LIBEVAL::VAR_TYPE_T type ) { m_type = type; }
103 LIBEVAL::VAR_TYPE_T GetType() const override { return m_type; }
104
105 void AddAllowedClass( TYPE_ID type_hash, PROPERTY_BASE* prop )
106 {
107 m_matchingTypes[type_hash] = prop;
108 }
109
110 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
111
112 BOARD_ITEM* GetObject( const LIBEVAL::CONTEXT* aCtx ) const;
113
114private:
115 std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes;
120};
121
122
123// "Object code" version of a netclass reference (for performance).
125{
126public:
127 PCBEXPR_NETCLASS_REF( int aItemIndex ) :
128 PCBEXPR_VAR_REF( aItemIndex )
129 {
131 }
132
133 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
134};
135
136
137// "Object code" version of a component class reference (for performance).
139{
140public:
141 PCBEXPR_COMPONENT_CLASS_REF( int aItemIndex ) : PCBEXPR_VAR_REF( aItemIndex )
142 {
144 }
145
146 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
147};
148
149
150// "Object code" version of a netname reference (for performance).
152{
153public:
154 PCBEXPR_NETNAME_REF( int aItemIndex ) :
155 PCBEXPR_VAR_REF( aItemIndex )
156 {
158 }
159
160 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
161};
162
163
165{
166public:
167 PCBEXPR_TYPE_REF( int aItemIndex ) :
168 PCBEXPR_VAR_REF( aItemIndex )
169 {
171 }
172
173 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
174};
175
176
178{
179public:
181
183 {
184 static PCBEXPR_BUILTIN_FUNCTIONS self;
185 return self;
186 }
187
188 LIBEVAL::FUNC_CALL_REF Get( const wxString& name )
189 {
190 return m_funcs[ name ];
191 }
192
193 const wxArrayString GetSignatures() const
194 {
195 return m_funcSigs;
196 }
197
198 void RegisterFunc( const wxString& funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr )
199 {
200 wxString funcName = funcSignature.BeforeFirst( '(' );
201 m_funcs[std::string( funcName.Lower() )] = std::move( funcPtr );
202 m_funcSigs.Add( funcSignature );
203 }
204
206
207private:
208 std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
209
210 wxArrayString m_funcSigs;
211};
212
213
215{
216public:
217 const std::vector<wxString>& GetSupportedUnits() const override;
218 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
219
220 wxString GetSupportedUnitsMessage() const override;
221
222 double Convert( const wxString& aString, int unitId ) const override;
223};
224
225
227{
228public:
229 const std::vector<wxString>& GetSupportedUnits() const override;
230 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
231
232 double Convert( const wxString& aString, int unitId ) const override;
233};
234
235
237{
238public:
240};
241
242
244{
245public:
248
249 bool Evaluate( const wxString& aExpr );
250 int Result() const { return m_result; }
251 EDA_UNITS Units() const { return m_units; }
252
253 void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
254 {
255 m_compiler.SetErrorCallback( std::move( aCallback ) );
256 }
257
258 bool IsErrorPending() const { return m_errorStatus.pendingError; }
260
261private:
264
268};
269
270#endif
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:79
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:317
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_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
virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall(const wxString &aName) override
virtual ~PCBEXPR_UCODE()
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
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
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.