dlc.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef AFUN_DLC_HPP
  2. #define AFUN_DLC_HPP
  3. #include <iostream>
  4. #include "aFunToolExport.h"
  5. #include "dlfcn.h" // CMake 处理 dlfcn.h 的位置
  6. /* 动态库工具(dlc): 处理动态库的使用 */
  7. /*
  8. * NEW_DLC_SYMBOL: 用于定义指定类型的symbol结构体
  9. * DLC_SYMBOL: 指定类型的symbol结构体名
  10. * GET_SYMBOL: 访问symbol成员的值
  11. * MAKE_SYMBOL: 生成一个symbol
  12. * COPY_SYMBOL: 拷贝一个symbol(拷贝其引用)
  13. * READ_SYMBOL: 在dlc中获取一个symbol
  14. * FREE_SYMBOL: 释放symbol
  15. *
  16. * openLibary: 打开动态库
  17. * freeLibary: 释放动态库
  18. * dlcExit: 释放所有动态库
  19. */
  20. namespace aFuntool {
  21. class DlcHandle;
  22. template <typename SYMBOL>
  23. class DlcSymbol;
  24. AFUN_TOOL_EXPORT void dlcExit();
  25. AFUN_TOOL_EXPORT DlcHandle *openLibrary(const char *file, int mode);
  26. /**
  27. * DlcHandle: 动态库句柄
  28. * 注意: 仅能通过 openLibrary生成
  29. * 不需要 delete 释放 (自动管理释放)
  30. */
  31. AFUN_TOOL_EXPORT class DlcHandle {
  32. friend AFUN_TOOL_EXPORT void dlcExit();
  33. friend AFUN_TOOL_EXPORT DlcHandle *openLibrary(const char *file, int mode);
  34. explicit DlcHandle(void *handle); // 仅 openLibary 可用
  35. void *handle;
  36. int link; // 引用计数
  37. struct DlcHandle *next;
  38. struct DlcHandle *prev;
  39. public:
  40. DlcHandle(const DlcHandle &dlc)=delete;
  41. DlcHandle &operator=(const DlcHandle *dlc)=delete;
  42. ~DlcHandle();
  43. /**
  44. * 获得动态库中指定名字的符号
  45. * @tparam SYMBOL 符号类型
  46. * @param name 名字
  47. * @return 符号
  48. */
  49. template<typename SYMBOL>
  50. DlcSymbol<SYMBOL> *get_symbol(const std::string &name) {
  51. auto symbol = (SYMBOL *)dlsym(handle, name.c_str());
  52. if (symbol == nullptr)
  53. return nullptr;
  54. return new DlcSymbol<SYMBOL>(symbol, this);
  55. }
  56. /**
  57. * 关闭动态库句柄
  58. */
  59. void close();
  60. int operator++(int);
  61. int operator--(int);
  62. };
  63. /**
  64. * 符号句柄
  65. * 注意: 不适用符号后需要 delete
  66. * @tparam SYMBOL 符号类型
  67. */
  68. template <typename SYMBOL>
  69. class DlcSymbol {
  70. const SYMBOL *symbol;
  71. const DlcHandle *dlc = nullptr;
  72. public:
  73. /**
  74. * 从句柄和符号指针创建一个符号
  75. * @param symbol 符号指针
  76. * @param dlc 句柄
  77. */
  78. explicit DlcSymbol(SYMBOL *symbol_, class DlcHandle *dlc_) : symbol {symbol_}, dlc {dlc_} {
  79. if (this->dlc != nullptr)
  80. this->dlc++;
  81. }
  82. DlcSymbol(const DlcSymbol &dlc_symbol) : symbol{dlc_symbol.symbol}, dlc {dlc_symbol.dlc} {
  83. if (this->dlc != nullptr)
  84. this->dlc++;
  85. }
  86. DlcSymbol &operator=(const DlcSymbol &dlc_symbol) {
  87. if (this == &dlc_symbol)
  88. return *this;
  89. if (this->dlc != nullptr)
  90. this->dlc--;
  91. symbol = dlc_symbol.symbol;
  92. dlc = dlc_symbol.dlc;
  93. if (this->dlc != nullptr)
  94. this->dlc++;
  95. return *this;
  96. }
  97. /**
  98. * 复制符号
  99. * @param symbol
  100. */
  101. explicit DlcSymbol(class DlcSymbol<SYMBOL> *symbol) {
  102. this->symbol = symbol->symbol;
  103. this->dlc = symbol->dlc;
  104. if (this->dlc != nullptr)
  105. this->dlc++;
  106. }
  107. ~DlcSymbol(){
  108. if (dlc != nullptr)
  109. dlc--;
  110. }
  111. const SYMBOL *getSymbol() const {
  112. return symbol;
  113. }
  114. };
  115. }
  116. #endif //AFUN_DLC_HPP