dlc.template.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // Created by jimso on 2022/1/8.
  3. //
  4. #ifndef AFUN_DLC_TEMPLATE_H
  5. #define AFUN_DLC_TEMPLATE_H
  6. #include "dlc.h"
  7. namespace aFuntool {
  8. /**
  9. * 符号句柄
  10. * 注意: 不适用符号后需要 delete
  11. * @tparam SYMBOL 符号类型
  12. */
  13. template <typename SYMBOL>
  14. class DlcSymbol {
  15. public:
  16. DlcSymbol() noexcept = default;
  17. /**
  18. * 从句柄和符号指针创建一个符号
  19. * @param symbol 符号指针
  20. * @param dlc 句柄
  21. */
  22. explicit DlcSymbol(SYMBOL *symbol_, DlcHandle::Handle *dlc_) noexcept : symbol_ {symbol_}, handle_ {dlc_} {
  23. if (this->handle_ != nullptr)
  24. (*handle_)++;
  25. }
  26. DlcSymbol(const DlcSymbol &dlc_symbol) noexcept : symbol_{dlc_symbol.symbol_}, handle_ {dlc_symbol.handle_} {
  27. if (handle_ != nullptr)
  28. (*handle_)++;
  29. }
  30. DlcSymbol(DlcSymbol &&dlc_symbol) noexcept : symbol_{dlc_symbol.symbol_}, handle_ {dlc_symbol.handle_} {
  31. dlc_symbol.handle_ = nullptr;
  32. }
  33. DlcSymbol &operator=(const DlcSymbol &dlc_symbol) noexcept {
  34. if (this == &dlc_symbol)
  35. return *this;
  36. if (handle_ != nullptr)
  37. (*handle_)--;
  38. symbol_ = dlc_symbol.symbol_;
  39. handle_ = dlc_symbol.handle_;
  40. if (handle_ != nullptr)
  41. (*handle_)++;
  42. return *this;
  43. }
  44. ~DlcSymbol() noexcept {
  45. if (handle_ != nullptr)
  46. (*handle_)--;
  47. }
  48. SYMBOL *getSymbol() const {
  49. return symbol_;
  50. }
  51. private:
  52. SYMBOL *symbol_ = nullptr;
  53. DlcHandle::Handle *handle_ = nullptr;
  54. };
  55. }
  56. #endif //AFUN_DLC_TEMPLATE_H