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
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2010 KiCad Developers, see change_log.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 SCH_LIB_H_
#define SCH_LIB_H_
#include <utf8.h>
#include <richio.h>
#include <import_export.h>
namespace SCH {
class LPID;
class PART;
class LIB_TABLE;
/**
* Class LIB_SOURCE
* is an abstract class from which implementation specific LIB_SOURCEs
* may be derived, one for each kind of library type allowed in the library table.
* The class name stems from the fact that this interface only provides READ ONLY
* functions.
*
* @author Dick Hollenbeck
*/
class LIB_SOURCE
{
friend class LIB; ///< the LIB uses these functions.
protected: ///< derived classes must implement
/**
* Function GetSourceType
* returns the library table entry's type for this library source.
*/
const STRING& GetSourceType() { return sourceType; }
/**
* Function GetSourceURI
* returns absolute location of the library source.
*/
const STRING& GetSourceURI() { return sourceURI; }
//-----<abstract for implementors>---------------------------------------
/**
* Function ReadPart
* fetches @a aPartName's s-expression into @a aResult after clear()ing aResult.
*/
virtual void ReadPart( STR_UTF* aResult, const STRING& aPartName, const STRING& aRev = "" )
throw( IO_ERROR ) = 0;
/**
* Function ReadParts
* fetches the s-expressions for each part given in @a aPartNames, into @a aResults,
* honoring the array indices respectfully.
* @param aPartNames is a list of part names, one name per list element. If a part name
* does not have a version string, then the most recent version is fetched.
* @param aResults receives the s-expressions
*/
virtual void ReadParts( STR_UTFS* aResults, const STRINGS& aPartNames )
throw( IO_ERROR ) = 0;
/**
* Function GetCategories
* fetches all categories present in the library source into @a aResults
*/
virtual void GetCategories( STRINGS* aResults )
throw( IO_ERROR ) = 0;
/**
* Function GetCategoricalPartNames
* fetches all the part names for @a aCategory, which was returned by GetCategories().
*
* @param aCategory is a subdividing navigator within the library source,
* but may default to empty which will be taken to mean all categories.
*
* @param aResults is a place to put the fetched result, one category per STRING.
*/
virtual void GetCategoricalPartNames( STRINGS* aResults, const STRING& aCategory="" )
throw( IO_ERROR ) = 0;
/**
* Function GetRevisions
* fetches all revisions for @a aPartName into @a aResults. Revisions are strings
* like "rev12", "rev279", and are library source agnostic. These do not have to be
* in a contiguous order, but the first 3 characters must be "rev" and subsequent
* characters must consist of at least one decimal digit. If the LIB_SOURCE
* does not support revisions, it is allowed to return a single "" string as
* the only result. This means aPartName is present in the libsource, only once
* without a revision. This is a special case.
*/
virtual void GetRevisions( STRINGS* aResults, const STRING& aPartName )
throw( IO_ERROR ) = 0;
/**
* Function FindParts
* fetches part names for all parts matching the criteria given in @a
* aQuery, into @a aResults. The query string is designed to be easily marshalled,
* i.e. serialized, so that long distance queries can be made with minimal overhead.
* The library source needs to have an intelligent friend on the other end if
* the actual library data is remotely located, otherwise it will be too slow
* to honor this portion of the API contract.
*
* @param aQuery is a string holding a domain specific query language expression.
* One candidate here is an s-expression that uses (and ..) and (or ..) operators
* and uses them as RPN. For example "(and (footprint 0805)(value 33ohm)(category passives))".
* The UI can shield the user from this if it wants.
*
* @param aResults is a place to put the fetched part names, one part per STRING.
*/
virtual void FindParts( STRINGS* aResults, const STRING& aQuery )
throw( IO_ERROR ) = 0;
//-----</abstract for implementors>--------------------------------------
protected:
STRING sourceType;
STRING sourceURI;
};
/**
* Class LIB_SINK
* is an abstract class from which implementation specific LIB_SINKs
* may be derived, one for each kind of library type in the library table that
* supports writing. The class name stems from the fact that this interface
* only provides WRITE functions.
*
* @author Dick Hollenbeck
*/
class LIB_SINK
{
friend class LIB; ///< only the LIB uses these functions.
protected: ///< derived classes must implement
/**
* Function GetSinkType
* returns the library table entry's type for this library sink.
*/
const STRING& GetSinkType() { return sinkType; }
/**
* Function GetSinkURI
* returns absolute location of the library sink.
*/
const STRING& GetSinkURI() { return sinkURI; }
/**
* Function WritePart
* saves the part to non-volatile storage. @a aPartName may have the revision
* portion present. If it is not present, and a overwrite of an existhing
* part is done, then LIB::ReloadPart() must be called on this same part
* and all parts that inherit it must be reparsed.
* @return STRING - if the LIB_SINK support revision numbering, then return a
* revision name that was next in the sequence, e.g. "rev22", else "".
*/
virtual STRING WritePart( const STRING& aPartName, const STRING& aSExpression )
throw( IO_ERROR ) = 0;
protected:
STRING sinkType;
STRING sinkURI;
};
class PARTS;
/**
* Class LIB
* is a cache of parts, and because the LIB_SOURCE is abstracted, there
* should be no need to extend from this class in any case except for the
* PARTS_LIST.
*
* @author Dick Hollenbeck
*/
class MY_API LIB
{
friend class LIB_TABLE; ///< protected constructor, LIB_TABLE may construct
protected: // constructor is not public, called from LIB_TABLE only.
/**
* Constructor LIB
* is not public and is only called from class LIB_TABLE
*
* @param aLogicalLibrary is the name of a well known logical library, and is
* known because it already exists in the library table.
*
* @param aSource is an open LIB_SOURCE whose ownership is
* given over to this LIB.
*
* @param aSink is an open LIB_SINK whose ownership is given over
* to this LIB, and it is normally NULL.
*/
LIB( const STRING& aLogicalLibrary, LIB_SOURCE* aSource, LIB_SINK* aSink = NULL );
public:
~LIB();
/**
* Function HasSink
* returns true if this library has write/save capability. Most LIBs
* are read only.
*/
bool HasSink() { return sink != NULL; }
/**
* Function LogicalName
* returns the logical name of this LIB.
*/
STRING LogicalName();
//-----<use delegates: source and sink>---------------------------------
/**
* Function LookupPart
* returns a PART given @a aPartName, such as "passives/R". No ownership
* is given to the PART, it stays in the cache that is this LIB.
*
* @param aLPID is the part to lookup. The logicalLibName can be empty in it
* since yes, we know which LIB is in play.
*
* @param aLibTable is the LIB_TABLE view that is in effect for inheritance,
* and comes from the big containing SCHEMATIC object.
*
* @return PART* - The desired PART and will never be NULL. No ownership is
* given to caller. PARTs always reside in the cache that is a LIB.
*
* @throw IO_ERROR if the part cannot be found or loaded.
*/
PART* LookupPart( const LPID& aLPID, LIB_TABLE* aLibTable )
throw( IO_ERROR );
/**
* Function ReloadPart
* will reload the part assuming the library source has a changed content
* for it.
*/
void ReloadPart( PART* aPart ) throw( IO_ERROR );
/**
* Function GetCategories
* returns all categories of parts within this LIB into @a aResults.
*/
STRINGS GetCategories() throw( IO_ERROR );
/**
* Function GetCategoricalPartNames
* returns the part names for @a aCategory, and at the same time
* creates cache entries for the very same parts if they do not already exist
* in this LIB (i.e. cache).
*/
STRINGS GetCategoricalPartNames( const STRING& aCategory = "" ) throw( IO_ERROR );
//-----<.use delegates: source and sink>--------------------------------
/**
* Function WritePart
* saves the part to non-volatile storage and returns the next new revision
* name in the sequence established by the LIB_SINK.
*/
STRING WritePart( PART* aPart ) throw( IO_ERROR );
void SetPartBody( PART* aPart, const STRING& aSExpression ) throw( IO_ERROR );
/**
* Function GetRevisions
* returns the revisions of @a aPartName that are present in this LIB.
* The returned STRINGS will look like "rev1", "rev2", etc.
*/
STRINGS GetRevisions( const STRING& aPartName ) throw( IO_ERROR );
/**
* Function FindParts
* returns part names for all parts matching the criteria given in @a
* aQuery, into @a aResults. The query string is designed to be easily marshalled,
* i.e. serialized, so that long distance queries can be made with minimal overhead.
* The library source needs to have an intelligent friend on the other end if
* the actual library data is remotely located, otherwise it will be too slow
* to honor this portion of the API contract.
*
* @param aQuery is a string holding a domain specific language expression. One candidate
* here is an RPN s-expression that uses (and ..) and (or ..) operators. For example
* "(and (footprint 0805)(value 33ohm)(category passives))"
*/
STRINGS FindParts( const STRING& aQuery ) throw( IO_ERROR )
{
// run the query on the cached data first for any PARTS which are fully
// parsed (i.e. cached), then on the LIB_SOURCE to find any that
// are not fully parsed, then unify the results.
return STRINGS();
}
#if defined(DEBUG)
static void Test( int argc, char** argv ) throw( IO_ERROR );
#endif
protected:
STR_UTF fetch; // scratch, used to fetch things, grows to worst case size.
STR_UTFS vfetch; // scratch, used to fetch things.
STRING logicalName;
LIB_SOURCE* source;
LIB_SINK* sink;
STRINGS categories;
bool cachedCategories; /// < is true only after reading categories
/** parts are in various states of readiness:
* 1) not even loaded (if cachedParts is false)
* 2) present, but without member 'body' having been read() yet.
* 3) body has been read, but not parsed yet.
* 4) parsed and inheritance if any has been applied.
*/
PARTS* parts;
/**
* Function lookupPart
* looks up a PART, returns NULL if cannot find in source. Does not parse
* the part. Does not even load the part's Sweet string. No ownership
* is given to the PART, it stays in the cache that is this LIB.
*
* @throw IO_ERROR if there is some kind of communications error reading
* the original list of parts.
*
* @return PART* - the cached PART, or NULL if not found. No ownership transferred.
*/
const PART* lookupPart( const LPID& aLPID ) throw( IO_ERROR );
};
} // namespace SCH
#endif // SCH_LIB_H_
|