File: lib_table_base.h

package info (click to toggle)
kicad 9.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 769,124 kB
  • sloc: cpp: 960,330; ansic: 121,001; xml: 66,428; python: 18,382; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (648 lines) | stat: -rw-r--r-- 21,314 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2010-2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 2012 Wayne Stambaugh <stambaughw@gmail.com>
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#ifndef _LIB_TABLE_BASE_H_
#define _LIB_TABLE_BASE_H_

#include <map>
#include <boost/ptr_container/ptr_vector.hpp>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <project.h>
#include <richio.h>
#include <kicommon.h>

class OUTPUTFORMATTER;
class LIB_TABLE_LEXER;
class LIB_ID;
class LIB_TABLE_ROW;
class LIB_TABLE_GRID;
class LIB_TABLE;
class IO_ERROR;
class wxWindow;


typedef boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS;
typedef LIB_TABLE_ROWS::iterator           LIB_TABLE_ROWS_ITER;
typedef LIB_TABLE_ROWS::const_iterator     LIB_TABLE_ROWS_CITER;


/**
 * Allows boost pointer containers to make clones of the data stored in them.  Since they
 * store pointers the data is cloned.  Copying and assigning pointers would cause ownership
 * issues if the standard C++ containers were used.
 */
KICOMMON_API LIB_TABLE_ROW* new_clone( const LIB_TABLE_ROW& aRow );


/**
 * LIB_TABLE_IO abstracts the file I/O operations for the library table
 * loading and saving.
 *
 * Normally, this is file-based-reading, but that's not a requirement.
 */
class KICOMMON_API LIB_TABLE_IO
{
public:
    virtual ~LIB_TABLE_IO() = default;

    /**
     * Create a reader for the given URI.
     *
     * This can return nullptr, for example if the URI is not a file or
     * is not readable.
     */
    virtual std::unique_ptr<LINE_READER> GetReader( const wxString& aURI ) const = 0;

    /**
     * Check if the given URI is writable.
     */
    virtual bool CanSaveToUri( const wxString& aURI ) const = 0;

    /**
     * Compare two URIs for equivalence.
     *
     * For example, two URIs that point to the same file should be considered equivalent,
     * even if they are not string-wise equal (e.g. symlinks)
     */
    virtual bool UrisAreEquivalent( const wxString& aURI1, const wxString& aURI2 ) const = 0;

    /**
     * Save the given table to the given URI.
     */
    virtual std::unique_ptr<OUTPUTFORMATTER> GetWriter( const wxString& aURI ) const = 0;
};


/**
 * Implementations of LIB_TABLE_IO for file-based I/O.
 *
 * This is the default implementation for real usage.
 */
class KICOMMON_API FILE_LIB_TABLE_IO : public LIB_TABLE_IO
{
public:
    FILE_LIB_TABLE_IO() = default;

    std::unique_ptr<LINE_READER> GetReader( const wxString& aURI ) const override;

    bool CanSaveToUri( const wxString& aURI ) const override;

    bool UrisAreEquivalent( const wxString& aURI1, const wxString& aURI2 ) const override;

    std::unique_ptr<OUTPUTFORMATTER> GetWriter( const wxString& aURI ) const override;
};


/**
 * Hold a record identifying a library accessed by the appropriate plug in object in the
 * #LIB_TABLE.  This is an abstract base class from which to derive library specific rows.
 */
class KICOMMON_API LIB_TABLE_ROW
{
public:
    LIB_TABLE_ROW() :
        enabled( true ),
        visible( true ),
        m_loaded( false ),
        m_parent( nullptr )
    {
    }

    virtual ~LIB_TABLE_ROW()
    {
    }

    LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aOptions,
                   const wxString& aDescr = wxEmptyString, LIB_TABLE* aParent = nullptr ) :
        nickName( aNick ),
        description( aDescr ),
        enabled( true ),
        visible( true ),
        m_loaded( false ),
        m_parent( aParent )
    {
        properties.reset();
        SetOptions( aOptions );
        SetFullURI( aURI );
    }

    bool operator==( const LIB_TABLE_ROW& r ) const;

    bool operator!=( const LIB_TABLE_ROW& r ) const { return !( *this == r ); }

    /**
     * @return the logical name of this library table row.
     */
    const wxString& GetNickName() const { return nickName; }

    /**
     * Change the logical name of this library, useful for an editor.
     */
    void SetNickName( const wxString& aNickName ) { nickName = aNickName; }

    /**
     * @return true if the library was loaded without error
     */
    bool GetIsLoaded() const { return m_loaded; }

    /**
     * Mark the row as being a loaded library
     */
    void SetLoaded( bool aLoaded ) { m_loaded = aLoaded; };

    /**
     * @return the enabled status of this library row
     */
    bool GetIsEnabled() const { return enabled; }

    /**
     * Change the enabled status of this library
     */
    void SetEnabled( bool aEnabled = true ) { enabled = aEnabled; }

    bool GetIsVisible() const { return visible; }

    void SetVisible( bool aVisible = true ) { visible = aVisible; }

    virtual bool LibraryExists() const = 0;

    virtual bool Refresh() { return false; }

    /**
     * Return the type of library represented by this row.
     */
    virtual const wxString GetType() const = 0;

    /**
     * Change the type of library represented by this row that must be implemented in the
     * derived object to provide the library table row type.
     */
    virtual void SetType( const wxString& aType ) = 0;

    virtual bool SupportsSettingsDialog() const { return false; }

    virtual void ShowSettingsDialog( wxWindow* aParent ) const {}

    /**
     * Return the full location specifying URI for the LIB, either in original UI form or
     * in environment variable expanded form.
     *
     * @param aSubstituted Tells if caller wanted the substituted form, else not.
     */
    const wxString GetFullURI( bool aSubstituted = false ) const;

    /**
     * Change the full URI for the library.
     */
    void SetFullURI( const wxString& aFullURI );

    /**
     * Return the options string, which may hold a password or anything else needed to
     * instantiate the underlying library plugin.
     */
    const wxString& GetOptions() const          { return options; }

    /**
     * Change the library options strings.
     */
    void SetOptions( const wxString& aOptions );

    /**
     * Return the description of the library referenced by this row.
     */
    const wxString& GetDescr() const            { return description; }

    /**
     * Change the description of the library referenced by this row.
     */
    void SetDescr( const wxString& aDescr )     { description = aDescr; }

    LIB_TABLE* GetParent() const { return m_parent; }

    void SetParent( LIB_TABLE* aParent ) { m_parent = aParent; }

    std::mutex& GetMutex() { return m_loadMutex; }

    /**
     * Return the constant #PROPERTIES for this library (#LIB_TABLE_ROW).  These are
     * the "options" in a table.
     */
    const std::map<std::string, UTF8>* GetProperties() const     { return properties.get(); }

    /**
     * Serialize this object as utf8 text to an #OUTPUTFORMATTER, and tries to
     * make it look good using multiple lines and indentation.
     *
     * @param out is an #OUTPUTFORMATTER
     * @param nestLevel is the indentation level to base all lines of the output.
     *                  Actual indentation will be 2 spaces for each nestLevel.
     */
    void Format( OUTPUTFORMATTER* out, int nestLevel ) const;

    LIB_TABLE_ROW* clone() const
    {
        return do_clone();
    }

protected:
    LIB_TABLE_ROW( const LIB_TABLE_ROW& aRow ) :
        nickName( aRow.nickName ),
        uri_user( aRow.uri_user ),
        options( aRow.options ),
        description( aRow.description ),
        enabled( aRow.enabled ),
        visible( aRow.visible ),
        m_loaded( aRow.m_loaded ),
        m_parent( aRow.m_parent )
    {
        if( aRow.properties )
            properties = std::make_unique<std::map<std::string, UTF8>>( *aRow.properties.get() );
        else
            properties.reset();
    }

    void operator=( const LIB_TABLE_ROW& aRow );

private:
    virtual LIB_TABLE_ROW* do_clone() const = 0;

    void setProperties( std::map<std::string, UTF8>* aProperties );

private:
    wxString          nickName;
    wxString          uri_user;           ///< what user entered from UI or loaded from disk
    wxString          options;
    wxString          description;

    bool              enabled  = true;    ///< Whether the LIB_TABLE_ROW is enabled
    bool              visible  = true;    ///< Whether the LIB_TABLE_ROW is visible in choosers
    bool              m_loaded = false;   ///< Whether the LIB_TABLE_ROW is loaded
    LIB_TABLE*        m_parent;           ///< Pointer to the table this row lives in (maybe null)

    std::unique_ptr<std::map<std::string, UTF8>> properties;

    std::mutex        m_loadMutex;
};


/**
 * Manage #LIB_TABLE_ROW records (rows), and can be searched based on library nickname.
 *
 * <p>
 * This class owns the <b>library table</b>, which is like fstab in concept and maps
 * logical library name to the library URI, type, and options. It is heavily based on
 * the SWEET parser work done by Dick Hollenbeck and can be seen in new/sch_lib_table.h.
 * A library table has the following columns:
 * <ul>
 * <li> Logical Library Name (Nickname)
 * <li> Library Type, used to determine which plugin to load to access the library.
 * <li> Library URI.  The full URI to the library source, form dependent on Type.
 * <li> Options, used for as yet to be defined information such as user names or passwords
 * </ul>
 * </p>
 * <p>
 * The Library Type can be one of:
 * <ul>
 * <li> "file"
 * <li> "ftp"
 * <li> "http"
 * </ul>
 * </p>
 * <p>
 * For now, the Library URI types needed to support the various types can be one of those
 * shown below, which are typical of each type:
 * <ul>
 * <li> "file://C:/mylibdir"
 * <li> "ftp://kicad.org/partlib/trunk"
 * <li> "http://kicad.org/partlib"
 * </ul>
 * </p>
 * <p>
 * The library table is built up from several additive entries (table fragments), and the
 * final table is a (conceptual) merging of the table fragments. Two anticipated sources
 * of the entries are a personal table saved in the KiCad configuration and a project
 * resident table that resides in project file.  The project footprint table entries are
 * considered a higher priority in the final dynamically assembled library table.  An row
 * in the project file contribution to the library table takes precedence over the personal
 * table if there is a collision of logical library names.  Otherwise, the entries simply
 * combine without issue to make up the applicable library table.
 * </p>
 *
 * @author Wayne Stambaugh
 */
class KICOMMON_API LIB_TABLE : public PROJECT::_ELEM
{
public:
    /**
     * Parse the #LIB_TABLE_LEXER s-expression library table format into the appropriate
     * #LIB_TABLE_ROW objects.
     *
     * @param aLexer is the lexer to parse.
     *
     * @throw IO_ERROR if an I/O error occurs during parsing.
     * @throw PARSER_ERROR if the lexer format to parse is invalid.
     * @throw boost::bad_pointer if an any attempt to add an invalid pointer to the
     *                           boost::ptr_vector.
     * @throw boost::bad_index if an index outside the row table bounds is accessed.
     */
    virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;

    /**
     * Generate the table in s-expression format to \a aOutput with an indentation level
     * of \a aIndentLevel.
     *
     * @param aOutput is the #OUTPUTFORMATTER to format the table into.
     * @param aIndentLevel is the indentation level (2 spaces) to indent.
     *
     * @throw IO_ERROR if an I/O error occurs during output.
     * @throw boost::interprocess::lock_except if separate process attempt to access the table.
     */
    virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;

    /**
     * Build a library table by pre-pending this table fragment in front of \a aFallBackTable.
     * Loading of this table fragment is done by using Parse().
     *
     * @param aFallBackTable is another LIB_TABLE which is searched only when
     *                       a row is not found in this table.  No ownership is
     *                       taken of aFallBackTable.
     * @param aTableIo is the I/O object to use for the table data. nullptr
     *                 means use the default file-based I/O.
     */
    LIB_TABLE( LIB_TABLE*                    aFallBackTable = nullptr,
               std::unique_ptr<LIB_TABLE_IO> aTableIo = nullptr );

    virtual ~LIB_TABLE();

    /**
     * Compares this table against another.
     *
     * This compares the row *contents* against each other.
     * Any fallback tables are not checked.
     */
    bool operator==( const LIB_TABLE& r ) const
    {
        if( m_rows.size() == r.m_rows.size() )
        {
            unsigned i;

            for( i = 0; i < m_rows.size() && m_rows[i] == r.m_rows[i];  ++i )
                ;

            if( i == m_rows.size() )
                return true;
        }

        return false;
    }

    bool operator!=( const LIB_TABLE& r ) const  { return !( *this == r ); }

    /**
     * Get the number of rows contained in the table
     */
    unsigned GetCount() const
    {
        return m_rows.size();
    }

    /**
     * Get the 'n'th #LIB_TABLE_ROW object
     * @param  aIndex index of row (must exist: from 0 to GetCount() - 1)
     * @return        reference to the row
     */
    LIB_TABLE_ROW& At( unsigned aIndex )
    {
        return m_rows[aIndex];
    }

    /**
     * @copydoc At()
     */
    const LIB_TABLE_ROW& At( unsigned aIndex ) const
    {
        return m_rows[aIndex];
    }

    /**
     * Return true if the table is empty.
     *
     * @param aIncludeFallback is used to determine if the fallback table should be
     *                         included in the test.
     *
     * @return true if the footprint library table is empty.
     */
    bool IsEmpty( bool aIncludeFallback = true );

    /**
     * @return the library description from @a aNickname, or an empty string
     *         if @a aNickname does not exist.
     */
    const wxString GetDescription( const wxString& aNickname );

    /**
     * Test for the existence of \a aNickname in the library table.
     *
     * @param aCheckEnabled if true will only return true for enabled libraries
     * @return true if a library \a aNickname exists in the table.
     */
    bool HasLibrary( const wxString& aNickname, bool aCheckEnabled = false ) const;

    /**
     * Test for the existence of \a aPath in the library table.
     *
     * @param aCheckEnabled if true will only return true for enabled libraries
     * @return true if a library \a aNickname exists in the table.
     */
    bool HasLibraryWithPath( const wxString& aPath ) const;

    /**
     * Return the logical library names, all of them that are pertinent to
     * a look up done on this LIB_TABLE.
     */
    std::vector<wxString> GetLogicalLibs();

    /**
     * Return the full URI of the library mapped to \a aLibNickname.
     */
    wxString GetFullURI( const wxString& aLibNickname, bool aExpandEnvVars = true ) const;

    /**
     * Adds \a aRow if it does not already exist or if doReplace is true.  If doReplace
     * is not true and the key for aRow already exists, the function fails and returns false.
     *
     * The key for the table is the nickName, and all in this table must be unique.
     *
     * @param aRow is the new row to insert, or to forcibly add if doReplace is true.
     * @param doReplace if true, means insert regardless of whether aRow's key already
     *                  exists.  If false, then fail if the key already exists.
     *
     * @return bool - true if the operation succeeded.
     */
    bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );

    /**
     * Removes a row from the table and frees the pointer
     * @param aRow is the row to remove
     * @return true if the row was found (and removed)
     */
    bool RemoveRow( const LIB_TABLE_ROW* aRow );

    /**
     * Replaces the Nth row with the given new row
     * @return true if successful
     */
    bool ReplaceRow( size_t aIndex, LIB_TABLE_ROW* aRow );

    /**
     * Moves a row within the table
     * @param aIndex is the current index of the row to move
     * @param aOffset is the number of positions to move it by in the table
     * @return true if the move resulted in a change
     */
    bool ChangeRowOrder( size_t aIndex, int aOffset );

    /**
     * Takes ownership of another list of rows; the original list will be freed
     */
    void TransferRows( LIB_TABLE_ROWS& aRowsList );

    /**
     * @return a #LIB_TABLE_ROW pointer if \a aURI is found in this table or in any chained
     *         fallBack table fragments, else NULL.
     */
    const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );

    /**
     * Load the library table using the path defined by \a aFileName aFallBackTable.
     *
     * @param aFileName contains the full path to the s-expression file.
     *
     * @throw IO_ERROR if an error occurs attempting to load the footprint library
     *                 table.
     */
    void Load( const wxString& aFileName );

    /**
     * Write this library table to \a aFileName in s-expression form.
     *
     * @param aFileName is the name of the file to write to.
     */
    void Save( const wxString& aFileName ) const;

    /**
     * Parses \a aOptionsList and places the result into a #PROPERTIES object which is
     * returned.  If the options field is empty, then the returned PROPERTIES will be
     * a NULL pointer.
     *
     * <p>
     * Typically aOptionsList comes from the "options" field within a LIB_TABLE_ROW and
     * the format is simply a comma separated list of name value pairs. e.g.:
     * [name1[=value1][|name2[=value2]]] etc.  When using the UI to create or edit
     * a library table, this formatting is handled for you.
     * </p>
     */
    static std::map<std::string, UTF8>* ParseOptions( const std::string& aOptionsList );

    /**
     * Returns a list of options from the aProperties parameter.
     *
     * The name=value pairs will be separated with the '|' character.  The =value portion may
     * not be present.  You might expect something like "name1=value1|name2=value2|flag_me".
     * Notice that flag_me does not have a value.  This is ok.
     *
     * @param aProperties is the PROPERTIES to format or NULL.  If NULL the returned
     *                    string will be empty.
     */
    static UTF8 FormatOptions( const std::map<std::string, UTF8>* aProperties );

    /**
     * Returns the version number (0 if unset)
     *
     * @return integer version number read from table
     */
    int GetVersion() const
    {
        return m_version;
    }

protected:
    /*
     * Do not make this public.  It MUST be called with a lock already in place.
     */
    void clear();

    /**
     * Return a #LIB_TABLE_ROW if \a aNickname is found in this table or in any chained
     * fallBack table fragment, else NULL.
     *
     * @param aNickname is the name of the library table entry to find.
     * @param aCheckIfEnabled is a flag to check if the library table entry is enabled.
     * @return a pointer to the #LIB_TABLE_ROW found.
     */
    LIB_TABLE_ROW* findRow( const wxString& aNickname, bool aCheckIfEnabled = false ) const;

    /**
     * Performs the mechanics of inserting a row, but without locking or reindexing.
     */
    bool doInsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );

    /**
     * Updates the env vars from older version of KiCad, provided they do not currently
     * resolve to anything
     *
     * @return True if the tables were modified
     */
    bool migrate();

    /*
     * Do not make this public.  It MUST be called with a lock already in place.
     */
    void reindex();

protected:
    // Injected I/O interface
    std::unique_ptr<LIB_TABLE_IO> m_io;

    LIB_TABLE* m_fallBack;

    /// Versioning to handle importing old tables
    mutable int m_version;

    /// Owning set of rows.
    // TODO: This should really be private; but the lib table grids re-use it
    //       (without using m_rowsMap).
    LIB_TABLE_ROWS m_rows;

    /// this is a non-owning index into the LIB_TABLE_ROWS table
    std::map<wxString, LIB_TABLE_ROWS_ITER> m_rowsMap;

    /// Mutex to protect access to the rows vector
    mutable std::shared_mutex m_mutex;
};

#endif  // _LIB_TABLE_BASE_H_