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.

130 lines
3.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef FOOTPRINT_INFO_IMPL_H
  20. #define FOOTPRINT_INFO_IMPL_H
  21. #include <atomic>
  22. #include <functional>
  23. #include <memory>
  24. #include <thread>
  25. #include <vector>
  26. #include <footprint_info.h>
  27. #include <sync_queue.h>
  28. class LOCALE_IO;
  29. class FOOTPRINT_INFO_IMPL : public FOOTPRINT_INFO
  30. {
  31. public:
  32. FOOTPRINT_INFO_IMPL( FOOTPRINT_LIST* aOwner, const wxString& aNickname,
  33. const wxString& aFootprintName )
  34. {
  35. m_nickname = aNickname;
  36. m_fpname = aFootprintName;
  37. m_num = 0;
  38. m_pad_count = 0;
  39. m_unique_pad_count = 0;
  40. m_owner = aOwner;
  41. m_loaded = false;
  42. load();
  43. }
  44. // A constructor for cached items
  45. FOOTPRINT_INFO_IMPL( const wxString& aNickname, const wxString& aFootprintName,
  46. const wxString& aDescription, const wxString& aKeywords,
  47. int aOrderNum, unsigned int aPadCount, unsigned int aUniquePadCount )
  48. {
  49. m_nickname = aNickname;
  50. m_fpname = aFootprintName;
  51. m_num = aOrderNum;
  52. m_pad_count = aPadCount;
  53. m_unique_pad_count = aUniquePadCount;
  54. m_doc = aDescription;
  55. m_keywords = aKeywords;
  56. m_owner = nullptr;
  57. m_loaded = true;
  58. }
  59. // A dummy constructor for use as a target in a binary search
  60. FOOTPRINT_INFO_IMPL( const wxString& aNickname, const wxString& aFootprintName )
  61. {
  62. m_nickname = aNickname;
  63. m_fpname = aFootprintName;
  64. m_owner = nullptr;
  65. m_loaded = true;
  66. }
  67. protected:
  68. virtual void load() override;
  69. };
  70. class FOOTPRINT_LIST_IMPL : public FOOTPRINT_LIST
  71. {
  72. public:
  73. FOOTPRINT_LIST_IMPL();
  74. virtual ~FOOTPRINT_LIST_IMPL();
  75. void WriteCacheToFile( const wxString& aFilePath ) override;
  76. void ReadCacheFromFile( const wxString& aFilePath ) override;
  77. bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = nullptr,
  78. PROGRESS_REPORTER* aProgressReporter = nullptr ) override;
  79. protected:
  80. void startWorkers( FP_LIB_TABLE* aTable, const wxString* aNickname,
  81. FOOTPRINT_ASYNC_LOADER* aLoader, unsigned aNThreads ) override;
  82. bool joinWorkers() override;
  83. void stopWorkers() override;
  84. /**
  85. * Load footprints from m_queue_in.
  86. */
  87. void loader_job();
  88. private:
  89. /**
  90. * Call aFunc, pushing any IO_ERRORs and std::exceptions it throws onto m_errors.
  91. *
  92. * @return true if no error occurred.
  93. */
  94. bool CatchErrors( const std::function<void()>& aFunc );
  95. FOOTPRINT_ASYNC_LOADER* m_loader;
  96. std::vector<std::thread> m_threads;
  97. SYNC_QUEUE<wxString> m_queue_in;
  98. SYNC_QUEUE<wxString> m_queue_out;
  99. std::atomic_size_t m_count_finished;
  100. long long m_list_timestamp;
  101. PROGRESS_REPORTER* m_progress_reporter;
  102. std::atomic_bool m_cancelled;
  103. std::mutex m_join;
  104. };
  105. extern FOOTPRINT_LIST_IMPL GFootprintList; // KIFACE scope.
  106. #endif // FOOTPRINT_INFO_IMPL_H