KiCad PCB EDA Suite
Loading...
Searching...
No Matches
library_table.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 Jon Evans <[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 LIBRARY_TABLE_H
22#define LIBRARY_TABLE_H
23
24#include <deque>
25#include <map>
26#include <optional>
27#include <tl/expected.hpp>
28#include <wx/filename.h>
29
30#include <kicommon.h>
31#include <core/utf8.h>
32
33
41
49
50struct LIBRARY_TABLE_IR;
52struct LIBRARY_TABLE_INTERNALS;
53class OUTPUTFORMATTER;
54class PROJECT;
55
56
58{
59 LIBRARY_ERROR( const wxString& aMessage ) :
60 message( aMessage )
61 {};
62
63 LIBRARY_ERROR( const wxString& aMessage, const wxString& aExtraDetails ) :
64 message( aMessage ),
65 details( aExtraDetails )
66 {};
67
68 wxString message;
69 wxString details;
70};
71
72
74{
76 LIBRARY_ERROR( wxT( "Table_OK" ) )
77 {}
78};
79
80
81template<typename ResultType>
82using LIBRARY_RESULT = tl::expected<ResultType, LIBRARY_ERROR>;
83
84
86{
87public:
88 friend class LIBRARY_TABLE;
89
90 static const wxString TABLE_TYPE_NAME;
91
92 LIBRARY_TABLE_ROW() = default;
93
94 bool operator==( const LIBRARY_TABLE_ROW& aOther ) const;
95
96 void SetNickname( const wxString& aNickname ) { m_nickname = aNickname; }
97 const wxString& Nickname() const { return m_nickname; }
98
99 void SetURI( const wxString& aUri ) { m_uri = aUri; }
100 const wxString& URI() const { return m_uri; }
101
102 void SetType( const wxString& aType ) { m_type = aType; }
103 const wxString& Type() const { return m_type; }
104
105 void SetOptions( const wxString& aOptions ) { m_options = aOptions; }
106 const wxString& Options() const { return m_options; }
107
108 void SetDescription( const wxString& aDescription ) { m_description = aDescription; }
109 const wxString& Description() const { return m_description; }
110
111 void SetScope( LIBRARY_TABLE_SCOPE aScope ) { m_scope = aScope; }
113
114 void SetDisabled( bool aDisabled = true ) { m_disabled = aDisabled; }
115 bool Disabled() const { return m_disabled; }
116
117 void SetHidden( bool aHidden = true ) { m_hidden = aHidden; }
118 bool Hidden() const { return m_hidden; }
119
120 std::map<std::string, UTF8> GetOptionsMap() const;
121
122 void SetOk( bool aOk = true ) { m_ok = aOk; }
123 bool IsOk() const { return m_ok; }
124
125 void SetErrorDescription( const wxString& aDescription ) { m_errorDescription = aDescription; }
126 const wxString& ErrorDescription() const { return m_errorDescription; }
127
128private:
129 wxString m_nickname;
130 wxString m_uri;
131 wxString m_type;
132 wxString m_options;
134 bool m_disabled = false;
135 bool m_hidden = false;
136
137 bool m_ok = false;
140};
141
142
143// LIBRARY_TABLE_ROW storage uses std::deque (rather than std::vector) so that pointers and
144// references to rows remain valid across push_back/pop_back and erase-at-end operations.
145// Code elsewhere (LIB_DATA::row, LIBRARY_MANAGER::m_rowCache) caches raw row pointers that
146// must survive runtime mutations from paths like EnsureRemoteLibraryEntry().
147typedef std::deque<LIBRARY_TABLE_ROW>::iterator LIBRARY_TABLE_ROWS_ITER;
148typedef std::deque<LIBRARY_TABLE_ROW>::const_iterator LIBRARY_TABLE_ROWS_CITER;
149
150
152{
153public:
159 LIBRARY_TABLE( const wxFileName &aPath, LIBRARY_TABLE_SCOPE aScope,
161
169 LIBRARY_TABLE( bool aFromClipboard, const wxString &aBuffer, LIBRARY_TABLE_SCOPE aScope );
170
171 ~LIBRARY_TABLE() = default;
172
173 bool operator==( const LIBRARY_TABLE& aOther ) const;
174
177
180
181 const wxString& Path() const { return m_path; }
182 void SetPath( const wxString &aPath ) { m_path = aPath; }
183
184 LIBRARY_TABLE_TYPE Type() const { return m_type; }
185 void SetType( const LIBRARY_TABLE_TYPE aType ) { m_type = aType; }
186
187 void SetScope( LIBRARY_TABLE_SCOPE aScope ) { m_scope = aScope; }
189
190 std::optional<int> Version() const{ return m_version; }
191 void SetVersion( const std::optional<int> &aVersion ) { m_version = aVersion; }
192
193 bool IsOk() const { return m_ok; }
194 const wxString& ErrorDescription() const { return m_errorDescription; }
195
197 void SetOk() { m_ok = true; }
198
200 bool IsReadOnly() const;
201
202 const std::deque<LIBRARY_TABLE_ROW>& Rows() const { return m_rows; }
203 std::deque<LIBRARY_TABLE_ROW>& Rows() { return m_rows; }
204
205 void Format( OUTPUTFORMATTER* aOutput ) const;
206
207 bool HasRow( const wxString& aNickname ) const;
208
214 bool HasRowWithURI( const wxString& aUri, const PROJECT& aProject,
215 bool aSubstituted = false ) const;
216
217 std::optional<LIBRARY_TABLE_ROW*> Row( const wxString& aNickname );
218 std::optional<const LIBRARY_TABLE_ROW*> Row( const wxString& aNickname ) const;
219
221
222 static std::map<std::string, UTF8> ParseOptions( const std::string& aOptionsList );
223 static UTF8 FormatOptions( const std::map<std::string, UTF8>* aProperties );
224
225private:
226 bool initFromIR( const LIBRARY_TABLE_IR& aIR );
227 bool addRowFromIR( const LIBRARY_TABLE_ROW_IR& aIR );
228
230 wxString m_path;
231
233
236
238 std::optional<int> m_version;
239
240 bool m_ok = false;
242
243 std::deque<LIBRARY_TABLE_ROW> m_rows;
244};
245
246#endif //LIBRARY_TABLE_H
bool operator==(const wxAuiPaneInfo &aLhs, const wxAuiPaneInfo &aRhs)
void SetOptions(const wxString &aOptions)
LIBRARY_TABLE_ROW()=default
LIBRARY_TABLE_SCOPE Scope() const
void SetNickname(const wxString &aNickname)
void SetOk(bool aOk=true)
void SetType(const wxString &aType)
bool Disabled() const
void SetErrorDescription(const wxString &aDescription)
const wxString & ErrorDescription() const
wxString m_errorDescription
void SetDescription(const wxString &aDescription)
const wxString & Type() const
static const wxString TABLE_TYPE_NAME
void SetURI(const wxString &aUri)
const wxString & Description() const
void SetDisabled(bool aDisabled=true)
bool Hidden() const
void SetScope(LIBRARY_TABLE_SCOPE aScope)
LIBRARY_TABLE_SCOPE m_scope
void SetHidden(bool aHidden=true)
bool IsOk() const
const wxString & URI() const
const wxString & Nickname() const
const wxString & Options() const
friend class LIBRARY_TABLE
void SetScope(LIBRARY_TABLE_SCOPE aScope)
LIBRARY_TABLE_TYPE Type() const
LIBRARY_TABLE_TYPE m_type
What type of content this table contains (footprint, symbol, design block, etc)
void SetType(const LIBRARY_TABLE_TYPE aType)
void SetPath(const wxString &aPath)
LIBRARY_TABLE_ROW & InsertRow()
Builds a new row and inserts it at the end of the table; returning a reference to the row.
~LIBRARY_TABLE()=default
const wxString & Path() const
wxString m_path
The full path to the file this table was parsed from, if any.
wxString m_errorDescription
LIBRARY_TABLE_SCOPE Scope() const
LIBRARY_TABLE_ROW MakeRow() const
Builds a new row that is suitable for this table (does not insert it)
std::optional< int > Version() const
const std::deque< LIBRARY_TABLE_ROW > & Rows() const
const wxString & ErrorDescription() const
LIBRARY_TABLE(const wxFileName &aPath, LIBRARY_TABLE_SCOPE aScope, LIBRARY_TABLE_TYPE aExpectedType=LIBRARY_TABLE_TYPE::UNINITIALIZED)
Creates a library table from a file on disk.
std::optional< int > m_version
The format version, if present in the parsed file.
bool IsOk() const
std::deque< LIBRARY_TABLE_ROW > & Rows()
std::deque< LIBRARY_TABLE_ROW > m_rows
LIBRARY_TABLE_SCOPE m_scope
void SetVersion(const std::optional< int > &aVersion)
void SetOk()
Marks a table valid that was built for a file that does not exist yet.
An interface used to output 8 bit text in a convenient way.
Definition richio.h:294
Container for project specific data.
Definition project.h:63
A base class for LIB_SYMBOL and SCH_SYMBOL.
Definition symbol.h:59
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:67
#define KICOMMON_API
Definition kicommon.h:27
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
LIBRARY_TABLE_TYPE
LIBRARY_TABLE_SCOPE
std::deque< LIBRARY_TABLE_ROW >::const_iterator LIBRARY_TABLE_ROWS_CITER
std::deque< LIBRARY_TABLE_ROW >::iterator LIBRARY_TABLE_ROWS_ITER
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition ptree.cpp:194
wxString details
LIBRARY_ERROR(const wxString &aMessage, const wxString &aExtraDetails)
wxString message
LIBRARY_ERROR(const wxString &aMessage)
The intermediate representation that a library table is parsed into.