tool_dlc.template.h 1.8 KB

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