PothosUtilListModules.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (c) 2020 NIcholas Corgan
  2. // SPDX-License-Identifier: BSL-1.0
  3. #include "PothosUtil.hpp"
  4. #include <Pothos/Plugin.hpp>
  5. #include <algorithm>
  6. #include <cassert>
  7. #include <iomanip>
  8. #include <iostream>
  9. #include <map>
  10. #include <string>
  11. static std::map<std::string, std::string> modVers;
  12. static std::map<std::string, size_t> modNumsPlugins;
  13. static size_t maxPathLength = 0;
  14. static void traverseDump(const Pothos::PluginRegistryInfoDump& dump)
  15. {
  16. if(!dump.modulePath.empty())
  17. {
  18. if(modNumsPlugins.count(dump.modulePath) > 0)
  19. {
  20. assert(modVers.count(dump.modulePath) > 0);
  21. ++modNumsPlugins[dump.modulePath];
  22. }
  23. else
  24. {
  25. modVers[dump.modulePath] = dump.moduleVersion;
  26. modNumsPlugins[dump.modulePath] = 1;
  27. maxPathLength = std::max(maxPathLength, dump.modulePath.size());
  28. }
  29. }
  30. for(const auto& sub: dump.subInfo)
  31. {
  32. traverseDump(sub);
  33. }
  34. }
  35. void PothosUtilBase::listModules(const std::string&, const std::string&)
  36. {
  37. Pothos::ScopedInit init;
  38. traverseDump(Pothos::PluginRegistry::dump());
  39. using MapPair = std::map<std::string, size_t>::value_type;
  40. const auto maxNumPluginsIter = std::max_element(
  41. modNumsPlugins.begin(),
  42. modNumsPlugins.end(),
  43. [](const MapPair& p1, const MapPair& p2)
  44. {
  45. return (p1.second < p2.second);
  46. });
  47. const auto maxNumPluginsLength = std::to_string(maxNumPluginsIter->second).size();
  48. for(const auto& modVersMapPair: modVers)
  49. {
  50. const auto& modulePath = modVersMapPair.first;
  51. std::cout << std::left << std::setw(maxPathLength) << modulePath << " ("
  52. << std::right << std::setw(maxNumPluginsLength) << modNumsPlugins[modulePath]
  53. << " plugins) [" << modVersMapPair.second << "]" << std::endl;
  54. }
  55. }