KiCad PCB EDA Suite
Loading...
Searching...
No Matches
odb_attribute.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 * Author: SYSUEric <[email protected]>.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef _ATTRIBUTE_PROVIDER_H_
22#define _ATTRIBUTE_PROVIDER_H_
23
24#include "odb_util.h"
25#include "stroke_params.h"
26#include <wx/string.h>
27#include <string>
28#include <type_traits>
29
30
31namespace ODB_ATTR
32{
33
42
43// Base class template for attributes
44template <typename T, TYPE AttrType>
46{
47 using ValueType = T;
48 static constexpr TYPE type = AttrType;
49 constexpr AttributeBase( T v ) : value( v ) {}
51};
52
53// Specialized attribute types
54template <typename T, unsigned int N>
55struct FloatAttribute : AttributeBase<double, TYPE::FLOAT>
56{
57 static constexpr unsigned int digits = N;
58 using AttributeBase<double, TYPE::FLOAT>::AttributeBase;
59};
60
61template <typename T>
62struct BooleanAttribute : AttributeBase<bool, TYPE::BOOLEAN>
63{
64 using AttributeBase<bool, TYPE::BOOLEAN>::AttributeBase;
65};
66
67template <typename T>
68struct IntegerAttribute : AttributeBase<int, TYPE::INTEGER>
69{
70 using AttributeBase<int, TYPE::INTEGER>::AttributeBase;
71};
72
73template <typename T>
74struct TextAttribute : AttributeBase<std::string, TYPE::TEXT>
75{
76 constexpr TextAttribute( const std::string& t ) :
77 AttributeBase<std::string, TYPE::TEXT>( ODB::GenLegalEntityName( wxString::FromUTF8( t ) ).ToStdString() )
78 {
79 }
80};
81
82template <typename T>
83struct ListTextAttribute : AttributeBase<std::string, TYPE::TEXT>
84{
85 using AttributeBase<std::string, TYPE::TEXT>::AttributeBase;
86};
87
88template <typename T>
89struct OPTION_Attribute : AttributeBase<int, TYPE::OPTION>
90{
91 using AttributeBase<int, TYPE::OPTION>::AttributeBase;
92};
93
94
95template <typename T>
97{
98};
99
100// Attribute name and type definitions
101template <typename Tag, template <typename, unsigned int> class Attr, TYPE AttrType, unsigned int N>
103{
104 using TYPE = Attr<Tag, N>;
105};
106
107template <typename Tag, template <typename> class Attr, TYPE AttrType>
109{
110 using TYPE = Attr<Tag>;
111};
112
113// TYPE traits for attributes
114template <typename T>
115struct IsFeature : std::false_type
116{
117};
118template <typename T>
119struct IsNet : std::false_type
120{
121};
122template <typename T>
123struct IsPkg : std::false_type
124{
125};
126template <typename T>
127struct IsLayer : std::false_type
128{
129};
130template <typename T>
131struct IsStep : std::false_type
132{
133};
134template <typename T>
135struct IsComp : std::false_type
136{
137};
138template <typename T>
139struct IsProductModel : std::false_type
140{
141};
142template <typename T>
143struct IsSymbol : std::false_type
144{
145};
146
147
148#define DEFINE_ATTR( Tag, Attr, AttrType, AttrName, ... ) \
149 struct Tag##_t \
150 { \
151 }; \
152 constexpr const char Tag##_name[] = AttrName; \
153 using Tag = Attribute<Tag##_t, Attr, AttrType, __VA_ARGS__>::TYPE; \
154 template <> \
155 struct AttributeName<Tag> \
156 { \
157 static constexpr const char* name = Tag##_name; \
158 };
159
160#define DEFINE_ATTR_SIMPLE( Tag, Attr, AttrType, AttrName ) \
161 struct Tag##_t \
162 { \
163 }; \
164 constexpr const char Tag##_name[] = AttrName; \
165 using Tag = AttributeSimple<Tag##_t, Attr, AttrType>::TYPE; \
166 template <> \
167 struct AttributeName<Tag> \
168 { \
169 static constexpr const char* name = Tag##_name; \
170 };
171
172
173#define DEFINE_FLOAT_ATTR( NAME, N ) DEFINE_ATTR( NAME, FloatAttribute, TYPE::FLOAT, #NAME, N )
174
175#define DEFINE_BOOLEAN_ATTR( NAME ) \
176 DEFINE_ATTR_SIMPLE( NAME, BooleanAttribute, TYPE::BOOLEAN, #NAME )
177
178#define DEFINE_TEXT_ATTR( NAME ) DEFINE_ATTR_SIMPLE( NAME, TextAttribute, TYPE::TEXT, #NAME )
179
180#define DEFINE_LIST_TEXT_ATTR( NAME ) DEFINE_ATTR_SIMPLE( NAME, ListTextAttribute, TYPE::TEXT, #NAME )
181
182#define DEFINE_INTEGER_ATTR( NAME ) DEFINE_ATTR_SIMPLE( NAME, IntegerAttribute, TYPE::INTEGER, #NAME )
183
184#define DEFINE_OPTION_ATTR( NAME ) \
185 struct NAME##_t \
186 { \
187 }; \
188 template <> \
189 struct AttributeSimple<NAME##_t, OPTION_Attribute, TYPE::OPTION>; \
190 template <> \
191 struct AttributeName<NAME> \
192 { \
193 static constexpr const char* name = #NAME; \
194 };
195
196
197// used by which entity
198#define USED_BY_FEATURE_ENTITY( NAME ) \
199 template <> \
200 struct IsFeature<NAME> : std::true_type \
201 { \
202 };
203
204#define USED_BY_NET_ENTITY( NAME ) \
205 template <> \
206 struct IsNet<NAME> : std::true_type \
207 { \
208 };
209
210#define USED_BY_PKG_ENTITY( NAME ) \
211 template <> \
212 struct IsPkg<NAME> : std::true_type \
213 { \
214 };
215
216#define USED_BY_CMP_ENTITY( NAME ) \
217 template <> \
218 struct IsComp<NAME> : std::true_type \
219 { \
220 };
221
222// Attribute definitions
223// BOOLEAN ATTRIBUTES
226
227DEFINE_BOOLEAN_ATTR( NET_POINT )
228USED_BY_FEATURE_ENTITY( NET_POINT )
229
230DEFINE_BOOLEAN_ATTR( ROUT_PLATED )
231USED_BY_FEATURE_ENTITY( ROUT_PLATED )
232
233DEFINE_INTEGER_ATTR( ROUT_CHAIN )
234USED_BY_FEATURE_ENTITY( ROUT_CHAIN )
235
237
238DEFINE_BOOLEAN_ATTR( MOUNT_HOLE )
239USED_BY_FEATURE_ENTITY( MOUNT_HOLE )
240
241DEFINE_BOOLEAN_ATTR( TEAR_DROP )
242USED_BY_FEATURE_ENTITY( TEAR_DROP )
243
244DEFINE_BOOLEAN_ATTR( TEST_POINT )
245USED_BY_FEATURE_ENTITY( TEST_POINT )
246
247DEFINE_BOOLEAN_ATTR( VIA_IN_PAD )
248USED_BY_FEATURE_ENTITY( VIA_IN_PAD )
249
250// TEXT ATTRIBUTES
253
254DEFINE_TEXT_ATTR( GEOMETRY )
255USED_BY_FEATURE_ENTITY( GEOMETRY )
256
257DEFINE_TEXT_ATTR( NET_NAME )
258USED_BY_FEATURE_ENTITY( NET_NAME )
259
260DEFINE_TEXT_ATTR( IPC_VIA_TYPE_TOP )
261USED_BY_FEATURE_ENTITY( IPC_VIA_TYPE_TOP )
262
263DEFINE_TEXT_ATTR( IPC_VIA_TYPE_BOTTOM )
264USED_BY_FEATURE_ENTITY( IPC_VIA_TYPE_BOTTOM )
265
266
267// FLOAT ATTRIBUTES
268DEFINE_FLOAT_ATTR( BOARD_THICKNESS, 1 ) // 0.0~10.0
269
270DEFINE_FLOAT_ATTR( STRING_ANGLE, 1 ) // 0.0~360.0
271USED_BY_FEATURE_ENTITY( STRING_ANGLE )
272
273DEFINE_FLOAT_ATTR( COMP_HEIGHT, 6 )
274USED_BY_CMP_ENTITY( COMP_HEIGHT )
275
276DEFINE_ATTR( post_machining_top_diameter, FloatAttribute, TYPE::FLOAT, "post_machining_top_diameter", 3 )
277USED_BY_FEATURE_ENTITY( post_machining_top_diameter )
278DEFINE_ATTR( post_machining_top_depth, FloatAttribute, TYPE::FLOAT, "post_machining_top_depth", 3 )
279USED_BY_FEATURE_ENTITY( post_machining_top_depth )
280DEFINE_ATTR( post_machining_top_angle, FloatAttribute, TYPE::FLOAT, "post_machining_top_angle", 1 )
281USED_BY_FEATURE_ENTITY( post_machining_top_angle )
282DEFINE_ATTR( post_machining_bottom_diameter, FloatAttribute, TYPE::FLOAT, "post_machining_bottom_diameter", 3 )
283USED_BY_FEATURE_ENTITY( post_machining_bottom_diameter )
284DEFINE_ATTR( post_machining_bottom_depth, FloatAttribute, TYPE::FLOAT, "post_machining_bottom_depth", 3 )
285USED_BY_FEATURE_ENTITY( post_machining_bottom_depth )
286DEFINE_ATTR( post_machining_bottom_angle, FloatAttribute, TYPE::FLOAT, "post_machining_bottom_angle", 1 )
287USED_BY_FEATURE_ENTITY( post_machining_bottom_angle )
288
289
290// OPTION ATTRIBUTES
298
306
307enum class DRILL
308{
312};
315
327
335
336enum class VIA_TYPE
337{
341};
344
354
355DEFINE_BOOLEAN_ATTR( NO_POP )
356USED_BY_CMP_ENTITY( NO_POP )
357
358DEFINE_LIST_TEXT_ATTR( COMP_VARIANT_LIST )
359USED_BY_CMP_ENTITY( COMP_VARIANT_LIST )
360} // namespace ODB_ATTR
361
362
364{
365public:
366 ATTR_MANAGER() = default;
367 virtual ~ATTR_MANAGER() = default;
368
369 template <typename Tr, typename Ta>
370 void AddSystemAttribute( Tr& r, Ta v )
371 {
372 if( isEmptyText( v ) )
373 return;
374
375 std::string name = std::string( "." ) + std::string( ODB_ATTR::AttributeName<Ta>::name );
376 const auto id = GetAttrNameNumber( name );
377
378 if constexpr( std::is_enum_v<Ta> )
379 r.m_ODBattributes.emplace( id, std::to_string( static_cast<int>( v ) ) );
380 else
381 r.m_ODBattributes.emplace( id, AttrValue2String( v ) );
382 }
383
384 template <typename Tr, typename Ta>
385 void AddUserDefAttribute( Tr& r, Ta v )
386 {
387 if( isEmptyText( v ) )
388 return;
389
391
392 if constexpr( std::is_enum_v<Ta> )
393 r.m_ODBattributes.emplace( id, std::to_string( static_cast<int>( v ) ) );
394 else
395 r.m_ODBattributes.emplace( id, AttrValue2String( v ) );
396 }
397
398protected:
399 size_t GetAttrNameNumber( const wxString& name );
400
401 void WriteAttributes( std::ostream& ost, const std::string& prefix = "" ) const;
402 void WriteAttributesName( std::ostream& ost, const std::string& prefix = "" ) const;
403 void WriteAttributesText( std::ostream& ost, const std::string& prefix = "" ) const;
404
405
406private:
408 template <typename Ta>
409 static bool isEmptyText( const Ta& v )
410 {
411 if constexpr( requires { v.value.empty(); } )
412 return v.value.empty();
413 else
414 return false;
415 }
416
417 size_t GetAttrTextNumber( const wxString& aName );
418 size_t GetTextIndex( std::unordered_map<std::string, size_t>& aMap,
419 std::vector<std::pair<size_t, std::string>>& aVec,
420 const std::string& aText );
421
422 template <typename T, unsigned int n>
424 {
425 return ODB::Double2String( a.value, a.digits );
426 }
427
428 template <typename T>
430 {
431 return "";
432 }
433
434 template <typename T>
436 {
437 return std::to_string( GetAttrTextNumber( a.value ) );
438 }
439
440 template <typename T>
442 {
443 return std::to_string( GetTextIndex( m_attrTexts, m_attrTextVec, a.value ) );
444 }
445
446 template <typename T>
448 {
449 return std::to_string( a.value );
450 }
451
452
453 std::unordered_map<std::string, size_t> m_attrNames;
454 std::vector<std::pair<size_t, std::string>> m_attrNameVec;
455
456 std::unordered_map<std::string, size_t> m_attrTexts;
457 std::vector<std::pair<size_t, std::string>> m_attrTextVec;
458};
459
461{
462public:
464 virtual ~ATTR_RECORD_WRITER() = default;
465
466 void WriteAttributes( std::ostream& ost ) const;
467
468public:
469 std::map<unsigned int, std::string> m_ODBattributes;
470};
471
472
473#endif // ATTRIBUTE_PROVIDER_H_
const char * name
ATTR_MANAGER()=default
void WriteAttributesText(std::ostream &ost, const std::string &prefix="") const
std::vector< std::pair< size_t, std::string > > m_attrTextVec
std::vector< std::pair< size_t, std::string > > m_attrNameVec
std::string AttrValue2String(ODB_ATTR::IntegerAttribute< T > a)
virtual ~ATTR_MANAGER()=default
std::string AttrValue2String(ODB_ATTR::ListTextAttribute< T > a)
size_t GetTextIndex(std::unordered_map< std::string, size_t > &aMap, std::vector< std::pair< size_t, std::string > > &aVec, const std::string &aText)
std::string AttrValue2String(ODB_ATTR::FloatAttribute< T, n > a)
void WriteAttributes(std::ostream &ost, const std::string &prefix="") const
size_t GetAttrTextNumber(const wxString &aName)
void AddUserDefAttribute(Tr &r, Ta v)
std::unordered_map< std::string, size_t > m_attrNames
static bool isEmptyText(const Ta &v)
An empty TEXT value is the attribute's default and has no legal text table entry.
size_t GetAttrNameNumber(const wxString &name)
std::string AttrValue2String(ODB_ATTR::BooleanAttribute< T > a)
std::string AttrValue2String(ODB_ATTR::TextAttribute< T > a)
void AddSystemAttribute(Tr &r, Ta v)
void WriteAttributesName(std::ostream &ost, const std::string &prefix="") const
std::unordered_map< std::string, size_t > m_attrTexts
void WriteAttributes(std::ostream &ost) const
virtual ~ATTR_RECORD_WRITER()=default
std::map< unsigned int, std::string > m_ODBattributes
ATTR_RECORD_WRITER()=default
static std::string ToStdString(const wxString &aStr)
wxString Double2String(const ODB_FORMAT &aFormat, double aVal)
Definition odb_util.cpp:382
STL namespace.
#define DEFINE_OPTION_ATTR(NAME)
#define USED_BY_CMP_ENTITY(NAME)
#define DEFINE_FLOAT_ATTR(NAME, N)
#define DEFINE_BOOLEAN_ATTR(NAME)
#define DEFINE_INTEGER_ATTR(NAME)
#define DEFINE_ATTR(Tag, Attr, AttrType, AttrName,...)
#define DEFINE_LIST_TEXT_ATTR(NAME)
#define DEFINE_TEXT_ATTR(NAME)
#define USED_BY_FEATURE_ENTITY(NAME)
SIM_MODEL::TYPE TYPE
Definition sim_model.cpp:55
constexpr AttributeBase(T v)
static constexpr TYPE type
Attr< Tag, N > TYPE
static constexpr unsigned int digits
constexpr TextAttribute(const std::string &t)