You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
12 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2010 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU 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, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef SCH_LIB_H_
  25. #define SCH_LIB_H_
  26. #include <utf8.h>
  27. #include <richio.h>
  28. #include <import_export.h>
  29. namespace SCH {
  30. class LPID;
  31. class PART;
  32. class LIB_TABLE;
  33. /**
  34. * Class LIB_SOURCE
  35. * is an abstract class from which implementation specific LIB_SOURCEs
  36. * may be derived, one for each kind of library type allowed in the library table.
  37. * The class name stems from the fact that this interface only provides READ ONLY
  38. * functions.
  39. *
  40. * @author Dick Hollenbeck
  41. */
  42. class LIB_SOURCE
  43. {
  44. friend class LIB; ///< the LIB uses these functions.
  45. protected: ///< derived classes must implement
  46. /**
  47. * Function GetSourceType
  48. * returns the library table entry's type for this library source.
  49. */
  50. const STRING& GetSourceType() { return sourceType; }
  51. /**
  52. * Function GetSourceURI
  53. * returns absolute location of the library source.
  54. */
  55. const STRING& GetSourceURI() { return sourceURI; }
  56. //-----<abstract for implementors>---------------------------------------
  57. /**
  58. * Function ReadPart
  59. * fetches @a aPartName's s-expression into @a aResult after clear()ing aResult.
  60. */
  61. virtual void ReadPart( STR_UTF* aResult, const STRING& aPartName, const STRING& aRev = "" ) = 0;
  62. /**
  63. * Function ReadParts
  64. * fetches the s-expressions for each part given in @a aPartNames, into @a aResults,
  65. * honoring the array indices respectfully.
  66. * @param aPartNames is a list of part names, one name per list element. If a part name
  67. * does not have a version string, then the most recent version is fetched.
  68. * @param aResults receives the s-expressions
  69. */
  70. virtual void ReadParts( STR_UTFS* aResults, const STRINGS& aPartNames ) = 0;
  71. /**
  72. * Function GetCategories
  73. * fetches all categories present in the library source into @a aResults
  74. */
  75. virtual void GetCategories( STRINGS* aResults ) = 0;
  76. /**
  77. * Function GetCategoricalPartNames
  78. * fetches all the part names for @a aCategory, which was returned by GetCategories().
  79. *
  80. * @param aCategory is a subdividing navigator within the library source,
  81. * but may default to empty which will be taken to mean all categories.
  82. *
  83. * @param aResults is a place to put the fetched result, one category per STRING.
  84. */
  85. virtual void GetCategoricalPartNames( STRINGS* aResults, const STRING& aCategory="" ) = 0;
  86. /**
  87. * Function GetRevisions
  88. * fetches all revisions for @a aPartName into @a aResults. Revisions are strings
  89. * like "rev12", "rev279", and are library source agnostic. These do not have to be
  90. * in a contiguous order, but the first 3 characters must be "rev" and subsequent
  91. * characters must consist of at least one decimal digit. If the LIB_SOURCE
  92. * does not support revisions, it is allowed to return a single "" string as
  93. * the only result. This means aPartName is present in the libsource, only once
  94. * without a revision. This is a special case.
  95. */
  96. virtual void GetRevisions( STRINGS* aResults, const STRING& aPartName ) = 0;
  97. /**
  98. * Function FindParts
  99. * fetches part names for all parts matching the criteria given in @a
  100. * aQuery, into @a aResults. The query string is designed to be easily marshalled,
  101. * i.e. serialized, so that long distance queries can be made with minimal overhead.
  102. * The library source needs to have an intelligent friend on the other end if
  103. * the actual library data is remotely located, otherwise it will be too slow
  104. * to honor this portion of the API contract.
  105. *
  106. * @param aQuery is a string holding a domain specific query language expression.
  107. * One candidate here is an s-expression that uses (and ..) and (or ..) operators
  108. * and uses them as RPN. For example "(and (footprint 0805)(value 33ohm)(category passives))".
  109. * The UI can shield the user from this if it wants.
  110. *
  111. * @param aResults is a place to put the fetched part names, one part per STRING.
  112. */
  113. virtual void FindParts( STRINGS* aResults, const STRING& aQuery ) = 0;
  114. //-----</abstract for implementors>--------------------------------------
  115. protected:
  116. STRING sourceType;
  117. STRING sourceURI;
  118. };
  119. /**
  120. * Class LIB_SINK
  121. * is an abstract class from which implementation specific LIB_SINKs
  122. * may be derived, one for each kind of library type in the library table that
  123. * supports writing. The class name stems from the fact that this interface
  124. * only provides WRITE functions.
  125. *
  126. * @author Dick Hollenbeck
  127. */
  128. class LIB_SINK
  129. {
  130. friend class LIB; ///< only the LIB uses these functions.
  131. protected: ///< derived classes must implement
  132. /**
  133. * Function GetSinkType
  134. * returns the library table entry's type for this library sink.
  135. */
  136. const STRING& GetSinkType() { return sinkType; }
  137. /**
  138. * Function GetSinkURI
  139. * returns absolute location of the library sink.
  140. */
  141. const STRING& GetSinkURI() { return sinkURI; }
  142. /**
  143. * Function WritePart
  144. * saves the part to non-volatile storage. @a aPartName may have the revision
  145. * portion present. If it is not present, and a overwrite of an existhing
  146. * part is done, then LIB::ReloadPart() must be called on this same part
  147. * and all parts that inherit it must be reparsed.
  148. * @return STRING - if the LIB_SINK support revision numbering, then return a
  149. * revision name that was next in the sequence, e.g. "rev22", else "".
  150. */
  151. virtual STRING WritePart( const STRING& aPartName, const STRING& aSExpression ) = 0;
  152. protected:
  153. STRING sinkType;
  154. STRING sinkURI;
  155. };
  156. class PARTS;
  157. /**
  158. * Class LIB
  159. * is a cache of parts, and because the LIB_SOURCE is abstracted, there
  160. * should be no need to extend from this class in any case except for the
  161. * PARTS_LIST.
  162. *
  163. * @author Dick Hollenbeck
  164. */
  165. class MY_API LIB
  166. {
  167. friend class LIB_TABLE; ///< protected constructor, LIB_TABLE may construct
  168. protected: // constructor is not public, called from LIB_TABLE only.
  169. /**
  170. * Constructor LIB
  171. * is not public and is only called from class LIB_TABLE
  172. *
  173. * @param aLogicalLibrary is the name of a well known logical library, and is
  174. * known because it already exists in the library table.
  175. *
  176. * @param aSource is an open LIB_SOURCE whose ownership is
  177. * given over to this LIB.
  178. *
  179. * @param aSink is an open LIB_SINK whose ownership is given over
  180. * to this LIB, and it is normally NULL.
  181. */
  182. LIB( const STRING& aLogicalLibrary, LIB_SOURCE* aSource, LIB_SINK* aSink = NULL );
  183. public:
  184. ~LIB();
  185. /**
  186. * Function HasSink
  187. * returns true if this library has write/save capability. Most LIBs
  188. * are read only.
  189. */
  190. bool HasSink() { return sink != NULL; }
  191. /**
  192. * Function LogicalName
  193. * returns the logical name of this LIB.
  194. */
  195. STRING LogicalName();
  196. //-----<use delegates: source and sink>---------------------------------
  197. /**
  198. * Function LookupPart
  199. * returns a PART given @a aPartName, such as "passives/R". No ownership
  200. * is given to the PART, it stays in the cache that is this LIB.
  201. *
  202. * @param aLPID is the part to lookup. The logicalLibName can be empty in it
  203. * since yes, we know which LIB is in play.
  204. *
  205. * @param aLibTable is the LIB_TABLE view that is in effect for inheritance,
  206. * and comes from the big containing SCHEMATIC object.
  207. *
  208. * @return PART* - The desired PART and will never be NULL. No ownership is
  209. * given to caller. PARTs always reside in the cache that is a LIB.
  210. *
  211. * @throw IO_ERROR if the part cannot be found or loaded.
  212. */
  213. PART* LookupPart( const LPID& aLPID, LIB_TABLE* aLibTable );
  214. /**
  215. * Function ReloadPart
  216. * will reload the part assuming the library source has a changed content
  217. * for it.
  218. */
  219. void ReloadPart( PART* aPart );
  220. /**
  221. * Function GetCategories
  222. * returns all categories of parts within this LIB into @a aResults.
  223. */
  224. STRINGS GetCategories();
  225. /**
  226. * Function GetCategoricalPartNames
  227. * returns the part names for @a aCategory, and at the same time
  228. * creates cache entries for the very same parts if they do not already exist
  229. * in this LIB (i.e. cache).
  230. */
  231. STRINGS GetCategoricalPartNames( const STRING& aCategory = "" );
  232. //-----<.use delegates: source and sink>--------------------------------
  233. /**
  234. * Function WritePart
  235. * saves the part to non-volatile storage and returns the next new revision
  236. * name in the sequence established by the LIB_SINK.
  237. */
  238. STRING WritePart( PART* aPart );
  239. void SetPartBody( PART* aPart, const STRING& aSExpression );
  240. /**
  241. * Function GetRevisions
  242. * returns the revisions of @a aPartName that are present in this LIB.
  243. * The returned STRINGS will look like "rev1", "rev2", etc.
  244. */
  245. STRINGS GetRevisions( const STRING& aPartName );
  246. /**
  247. * Function FindParts
  248. * returns part names for all parts matching the criteria given in @a
  249. * aQuery, into @a aResults. The query string is designed to be easily marshalled,
  250. * i.e. serialized, so that long distance queries can be made with minimal overhead.
  251. * The library source needs to have an intelligent friend on the other end if
  252. * the actual library data is remotely located, otherwise it will be too slow
  253. * to honor this portion of the API contract.
  254. *
  255. * @param aQuery is a string holding a domain specific language expression. One candidate
  256. * here is an RPN s-expression that uses (and ..) and (or ..) operators. For example
  257. * "(and (footprint 0805)(value 33ohm)(category passives))"
  258. */
  259. STRINGS FindParts( const STRING& aQuery )
  260. {
  261. // run the query on the cached data first for any PARTS which are fully
  262. // parsed (i.e. cached), then on the LIB_SOURCE to find any that
  263. // are not fully parsed, then unify the results.
  264. return STRINGS();
  265. }
  266. #if defined(DEBUG)
  267. static void Test( int argc, char** argv );
  268. #endif
  269. protected:
  270. STR_UTF fetch; // scratch, used to fetch things, grows to worst case size.
  271. STR_UTFS vfetch; // scratch, used to fetch things.
  272. STRING logicalName;
  273. LIB_SOURCE* source;
  274. LIB_SINK* sink;
  275. STRINGS categories;
  276. bool cachedCategories; /// < is true only after reading categories
  277. /** parts are in various states of readiness:
  278. * 1) not even loaded (if cachedParts is false)
  279. * 2) present, but without member 'body' having been read() yet.
  280. * 3) body has been read, but not parsed yet.
  281. * 4) parsed and inheritance if any has been applied.
  282. */
  283. PARTS* parts;
  284. /**
  285. * Function lookupPart
  286. * looks up a PART, returns NULL if cannot find in source. Does not parse
  287. * the part. Does not even load the part's Sweet string. No ownership
  288. * is given to the PART, it stays in the cache that is this LIB.
  289. *
  290. * @throw IO_ERROR if there is some kind of communications error reading
  291. * the original list of parts.
  292. *
  293. * @return PART* - the cached PART, or NULL if not found. No ownership transferred.
  294. */
  295. const PART* lookupPart( const LPID& aLPID );
  296. };
  297. } // namespace SCH
  298. #endif // SCH_LIB_H_