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 along
18 * with this program. If not, see <http://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
34enum class TYPE
35{
36 FLOAT,
37 BOOLEAN,
38 TEXT,
39 OPTION,
41};
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 TextAttribute : AttributeBase<std::string, TYPE::TEXT>
69{
70 constexpr TextAttribute( const std::string& t ) :
71 AttributeBase<std::string, TYPE::TEXT>( ODB::GenLegalEntityName( t ).ToStdString() )
72 {
73 }
74};
75
76template <typename T>
77struct OPTION_Attribute : AttributeBase<int, TYPE::OPTION>
78{
79 using AttributeBase<int, TYPE::OPTION>::AttributeBase;
80};
81
82
83template <typename T>
85{
86};
87
88// Attribute name and type definitions
89template <typename Tag, template <typename, unsigned int> class Attr, TYPE AttrType, unsigned int N>
91{
92 using TYPE = Attr<Tag, N>;
93};
94
95template <typename Tag, template <typename> class Attr, TYPE AttrType>
97{
98 using TYPE = Attr<Tag>;
99};
100
101// TYPE traits for attributes
102template <typename T>
103struct IsFeature : std::false_type
104{
105};
106template <typename T>
107struct IsNet : std::false_type
108{
109};
110template <typename T>
111struct IsPkg : std::false_type
112{
113};
114template <typename T>
115struct IsLayer : std::false_type
116{
117};
118template <typename T>
119struct IsStep : std::false_type
120{
121};
122template <typename T>
123struct IsComp : std::false_type
124{
125};
126template <typename T>
127struct IsProductModel : std::false_type
128{
129};
130template <typename T>
131struct IsSymbol : std::false_type
132{
133};
134
135
136#define DEFINE_ATTR( Tag, Attr, AttrType, AttrName, ... ) \
137 struct Tag##_t \
138 { \
139 }; \
140 constexpr const char Tag##_name[] = AttrName; \
141 using Tag = Attribute<Tag##_t, Attr, AttrType, __VA_ARGS__>::TYPE; \
142 template <> \
143 struct AttributeName<Tag> \
144 { \
145 static constexpr const char* name = Tag##_name; \
146 };
147
148#define DEFINE_ATTR_SIMPLE( Tag, Attr, AttrType, AttrName ) \
149 struct Tag##_t \
150 { \
151 }; \
152 constexpr const char Tag##_name[] = AttrName; \
153 using Tag = AttributeSimple<Tag##_t, Attr, AttrType>::TYPE; \
154 template <> \
155 struct AttributeName<Tag> \
156 { \
157 static constexpr const char* name = Tag##_name; \
158 };
159
160
161#define DEFINE_FLOAT_ATTR( NAME, N ) DEFINE_ATTR( NAME, FloatAttribute, TYPE::FLOAT, #NAME, N )
162
163#define DEFINE_BOOLEAN_ATTR( NAME ) \
164 DEFINE_ATTR_SIMPLE( NAME, BooleanAttribute, TYPE::BOOLEAN, #NAME )
165
166#define DEFINE_TEXT_ATTR( NAME ) DEFINE_ATTR_SIMPLE( NAME, TextAttribute, TYPE::TEXT, #NAME )
167
168#define DEFINE_OPTION_ATTR( NAME ) \
169 struct NAME##_t \
170 { \
171 }; \
172 template <> \
173 struct AttributeSimple<NAME##_t, OPTION_Attribute, TYPE::OPTION>; \
174 template <> \
175 struct AttributeName<NAME> \
176 { \
177 static constexpr const char* name = #NAME; \
178 };
179
180
181// used by which entity
182#define USED_BY_FEATURE_ENTITY( NAME ) \
183 template <> \
184 struct IsFeature<NAME> : std::true_type \
185 { \
186 };
187
188#define USED_BY_NET_ENTITY( NAME ) \
189 template <> \
190 struct IsNet<NAME> : std::true_type \
191 { \
192 };
193
194#define USED_BY_PKG_ENTITY( NAME ) \
195 template <> \
196 struct IsPkg<NAME> : std::true_type \
197 { \
198 };
199
200#define USED_BY_CMP_ENTITY( NAME ) \
201 template <> \
202 struct IsComp<NAME> : std::true_type \
203 { \
204 };
205
206// Attribute definitions
207// BOOLEAN ATTRIBUTES
210
211DEFINE_BOOLEAN_ATTR( NET_POINT )
212USED_BY_FEATURE_ENTITY( NET_POINT )
213
214DEFINE_BOOLEAN_ATTR( ROUT_PLATED )
215USED_BY_FEATURE_ENTITY( ROUT_PLATED )
216
218
219DEFINE_BOOLEAN_ATTR( MOUNT_HOLE )
220USED_BY_FEATURE_ENTITY( MOUNT_HOLE )
221
222DEFINE_BOOLEAN_ATTR( TEAR_DROP )
223USED_BY_FEATURE_ENTITY( TEAR_DROP )
224
225DEFINE_BOOLEAN_ATTR( TEST_POINT )
226USED_BY_FEATURE_ENTITY( TEST_POINT )
227
228// TEXT ATTRIBUTES
231
232DEFINE_TEXT_ATTR( GEOMETRY )
233USED_BY_FEATURE_ENTITY( GEOMETRY )
234
235DEFINE_TEXT_ATTR( NET_NAME )
236USED_BY_FEATURE_ENTITY( NET_NAME )
237
238
239// FLOAT ATTRIBUTES
240DEFINE_FLOAT_ATTR( BOARD_THICKNESS, 1 ) // 0.0~10.0
241
242DEFINE_FLOAT_ATTR( STRING_ANGLE, 1 ) // 0.0~360.0
243USED_BY_FEATURE_ENTITY( STRING_ANGLE )
244
245
246// OPTION ATTRIBUTES
247enum class DRILL
248{
249 PLATED,
251 VIA
252};
255
256enum class PAD_USAGE
257{
258 TOEPRINT,
259 VIA,
264};
267
268enum class PLATED_TYPE
269{
270 STANDARD,
272};
275
276enum class VIA_TYPE
277{
278 DRILLED,
279 LASER,
280 PHOTO
281};
284
286{
287 OTHER,
288 SMD,
289 THT,
291};
294
295DEFINE_BOOLEAN_ATTR( NO_POP )
296USED_BY_CMP_ENTITY( NO_POP )
297} // namespace ODB_ATTR
298
299
301{
302public:
303 ATTR_MANAGER() = default;
304 virtual ~ATTR_MANAGER() = default;
305
306 template <typename Tr, typename Ta>
307 void AddSystemAttribute( Tr& r, Ta v )
308 {
309 std::string name = std::string( "." ) + std::string( ODB_ATTR::AttributeName<Ta>::name );
310 const auto id = GetAttrNameNumber( name );
311
312 if constexpr( std::is_enum_v<Ta> )
313 r.m_ODBattributes.emplace( id, std::to_string( static_cast<int>( v ) ) );
314 else
315 r.m_ODBattributes.emplace( id, AttrValue2String( v ) );
316 }
317
318 template <typename Tr, typename Ta>
319 void AddUserDefAttribute( Tr& r, Ta v )
320 {
322
323 if constexpr( std::is_enum_v<Ta> )
324 r.m_ODBattributes.emplace( id, std::to_string( static_cast<int>( v ) ) );
325 else
326 r.m_ODBattributes.emplace( id, AttrValue2String( v ) );
327 }
328
329protected:
330 size_t GetAttrNameNumber( const wxString& name );
331
332 void WriteAttributes( std::ostream& ost, const std::string& prefix = "" ) const;
333 void WriteAttributesName( std::ostream& ost, const std::string& prefix = "" ) const;
334 void WriteAttributesText( std::ostream& ost, const std::string& prefix = "" ) const;
335
336
337private:
338 size_t GetAttrTextNumber( const wxString& aName );
339 size_t GetTextIndex( std::unordered_map<std::string, size_t>& aMap,
340 std::vector<std::pair<size_t, std::string>>& aVec,
341 const std::string& aText );
342
343 template <typename T, unsigned int n>
345 {
346 return ODB::Double2String( a.value, a.digits );
347 }
348
349 template <typename T>
351 {
352 return "";
353 }
354
355 template <typename T>
357 {
358 return std::to_string( GetAttrTextNumber( a.value ) );
359 }
360
361
362 std::unordered_map<std::string, size_t> m_attrNames;
363 std::vector<std::pair<size_t, std::string>> m_attrNameVec;
364
365 std::unordered_map<std::string, size_t> m_attrTexts;
366 std::vector<std::pair<size_t, std::string>> m_attrTextVec;
367};
368
370{
371public:
373 virtual ~ATTR_RECORD_WRITER() = default;
374
375 void WriteAttributes( std::ostream& ost ) const;
376
377public:
378 std::map<unsigned int, std::string> m_ODBattributes;
379};
380
381
382#endif // ATTRIBUTE_PROVIDER_H_
const char * name
Definition: DXF_plotter.cpp:59
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
virtual ~ATTR_MANAGER()=default
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
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
Definition: odb_util.cpp:33
wxString Double2String(double aVal)
Definition: odb_util.cpp:127
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_TEXT_ATTR(NAME)
#define USED_BY_FEATURE_ENTITY(NAME)
@ MECHANICAL
a pad used for mechanical support
constexpr AttributeBase(T v)
Definition: odb_attribute.h:49
static constexpr TYPE type
Definition: odb_attribute.h:48
Attr< Tag, N > TYPE
Definition: odb_attribute.h:92
static constexpr unsigned int digits
Definition: odb_attribute.h:57
constexpr TextAttribute(const std::string &t)
Definition: odb_attribute.h:70