dlc.inline.h 1.8 KB

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