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.

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