load_all.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015-2019 Antony Polukhin.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // MinGW related workaround
  8. #define BOOST_DLL_FORCE_ALIAS_INSTANTIATION
  9. #include "../b2_workarounds.hpp"
  10. #include "../tutorial4/static_plugin.hpp"
  11. #include <boost/dll/runtime_symbol_info.hpp> // for program_location()
  12. #include <boost/dll/shared_library.hpp>
  13. #include <boost/make_shared.hpp>
  14. #include <boost/container/map.hpp>
  15. #include <boost/filesystem.hpp>
  16. #include <iostream>
  17. //[plugcpp_plugins_collector_def
  18. namespace dll = boost::dll;
  19. class plugins_collector {
  20. // Name => plugin
  21. typedef boost::container::map<std::string, dll::shared_library> plugins_t;
  22. boost::dll::fs::path plugins_directory_;
  23. plugins_t plugins_;
  24. // loads all plugins in plugins_directory_
  25. void load_all();
  26. // Gets `my_plugin_api` instance using "create_plugin" or "plugin" imports,
  27. // stores plugin with its name in the `plugins_` map.
  28. void insert_plugin(BOOST_RV_REF(dll::shared_library) lib);
  29. public:
  30. plugins_collector(const boost::dll::fs::path& plugins_directory)
  31. : plugins_directory_(plugins_directory)
  32. {
  33. load_all();
  34. }
  35. void print_plugins() const;
  36. std::size_t count() const;
  37. // ...
  38. };
  39. //]
  40. //[plugcpp_plugins_collector_load_all
  41. void plugins_collector::load_all() {
  42. namespace fs = ::boost::dll::fs;
  43. typedef fs::path::string_type string_type;
  44. const string_type extension = dll::shared_library::suffix().native();
  45. // Searching a folder for files with '.so' or '.dll' extension
  46. fs::recursive_directory_iterator endit;
  47. for (fs::recursive_directory_iterator it(plugins_directory_); it != endit; ++it) {
  48. if (!fs::is_regular_file(*it)) {
  49. continue;
  50. }
  51. /*<-*/
  52. if ( !b2_workarounds::is_shared_library((*it).path()) ) {
  53. continue;
  54. }
  55. /*->*/
  56. // We found a file. Trying to load it
  57. boost::dll::fs::error_code error;
  58. dll::shared_library plugin(it->path(), error);
  59. if (error) {
  60. continue;
  61. }
  62. std::cout << "Loaded (" << plugin.native() << "):" << it->path() << '\n';
  63. // Gets plugin using "create_plugin" or "plugin" function
  64. insert_plugin(boost::move(plugin));
  65. }
  66. dll::shared_library plugin(dll::program_location());
  67. std::cout << "Loaded self\n";
  68. insert_plugin(boost::move(plugin));
  69. }
  70. //]
  71. //[plugcpp_plugins_collector_insert_plugin
  72. void plugins_collector::insert_plugin(BOOST_RV_REF(dll::shared_library) lib) {
  73. std::string plugin_name;
  74. if (lib.has("create_plugin")) {
  75. plugin_name = lib.get_alias<boost::shared_ptr<my_plugin_api>()>("create_plugin")()->name();
  76. } else if (lib.has("plugin")) {
  77. plugin_name = lib.get<my_plugin_api>("plugin").name();
  78. } else {
  79. return;
  80. }
  81. if (plugins_.find(plugin_name) == plugins_.cend()) {
  82. plugins_[plugin_name] = boost::move(lib);
  83. }
  84. }
  85. //]
  86. void plugins_collector::print_plugins() const {
  87. plugins_t::const_iterator const end = plugins_.cend();
  88. for (plugins_t::const_iterator it = plugins_.cbegin(); it != end; ++it) {
  89. std::cout << '(' << it->second.native() << "): " << it->first << '\n';
  90. }
  91. }
  92. std::size_t plugins_collector::count() const {
  93. return plugins_.size();
  94. }
  95. //[plugcpp_load_all
  96. int main(int argc, char* argv[]) {
  97. /*<-*/
  98. BOOST_ASSERT(argc >= 3);
  99. boost::dll::fs::path path1(argv[1]);
  100. for (int i = 2; i < argc; ++i) {
  101. boost::dll::fs::path path2(argv[i]);
  102. boost::dll::fs::path res;
  103. for (boost::dll::fs::path::iterator it1 = path1.begin(), it2 = path2.begin();
  104. it1 != path1.end() && it2 != path2.end() && *it1 == *it2;
  105. ++it1, ++it2)
  106. {
  107. res /= *it1;
  108. }
  109. path1 = res;
  110. }
  111. std::string new_argv = path1.string();
  112. std::cout << "\nPlugins path: " << new_argv << ":\n";
  113. argv[1] = &new_argv[0];
  114. /*->*/
  115. plugins_collector plugins(argv[1]);
  116. std::cout << "\n\nUnique plugins " << plugins.count() << ":\n";
  117. plugins.print_plugins();
  118. // ...
  119. //]
  120. BOOST_ASSERT(plugins.count() >= 3);
  121. }