tool_dlc.inline.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef AFUN_TOOL_DLC_INLINE_H
  2. #define AFUN_TOOL_DLC_INLINE_H
  3. #ifdef __cplusplus
  4. #include "tool_dlc.h"
  5. #ifndef AFUN_TOOL_C
  6. namespace aFuntool {
  7. #endif
  8. template<typename SYMBOL>
  9. DlcSymbol<SYMBOL> DlcHandle::getSymbol(const std::string &name){
  10. return handle_ != nullptr ? handle_->getSymbol<SYMBOL>(name) : DlcSymbol<SYMBOL>();
  11. }
  12. DlcHandle::~DlcHandle() noexcept {
  13. this->close();
  14. }
  15. bool DlcHandle::isOpen() const {
  16. return handle_ != nullptr ? handle_->isOpen() : false;
  17. }
  18. void DlcHandle::close(){
  19. if (handle_ == nullptr)
  20. return;
  21. (*handle_)--;
  22. handle_ = nullptr;
  23. }
  24. int DlcHandle::operator++(int){
  25. return (*handle_)++;
  26. }
  27. int DlcHandle::operator--(int){
  28. return (*handle_)--;
  29. }
  30. DlcHandle &DlcHandle::operator=(const DlcHandle &dlc_handle) noexcept {
  31. if (&dlc_handle == this)
  32. return *this;
  33. this->close();
  34. handle_ = dlc_handle.handle_;
  35. if (handle_ != nullptr)
  36. (*handle_)++;
  37. return *this;
  38. }
  39. DlcHandle::DlcHandle(const DlcHandle &dlc_handle) noexcept {
  40. handle_ = dlc_handle.handle_;
  41. if (handle_ != nullptr)
  42. (*handle_)++;
  43. }
  44. DlcHandle::DlcHandle(DlcHandle &&dlc_handle)noexcept {
  45. handle_ = dlc_handle.handle_;
  46. dlc_handle.handle_ = nullptr;
  47. }
  48. template<typename SYMBOL>
  49. DlcSymbol<SYMBOL> DlcHandle::Handle::getSymbol(const std::string &name){
  50. if (handle_ == nullptr)
  51. return DlcSymbol<SYMBOL>();
  52. auto symbol = (SYMBOL *)dlsym(handle_, name.c_str());
  53. if (symbol == nullptr)
  54. return DlcSymbol<SYMBOL>();
  55. return DlcSymbol<SYMBOL>(symbol, this);
  56. }
  57. bool DlcHandle::Handle::isOpen() const {
  58. return handle_ != nullptr;
  59. }
  60. #ifndef AFUN_TOOL_C
  61. }
  62. #endif
  63. #endif
  64. #endif //AFUN_TOOL_DLC_INLINE_H