common_helper.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // // ******************************************************************
  2. // // /\ /| @File common_helper.h
  3. // // \ V/ @Brief
  4. // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
  5. // // / | @Creation 2024-05-16
  6. // // / \\ @Modified 2024-06-24
  7. // // *(__\_\
  8. // // ******************************************************************
  9. #pragma once
  10. #include <type_traits>
  11. #include <typeinfo>
  12. #include <vector>
  13. #include "loger.h"
  14. #include "../edge_pulse.h"
  15. // 检查类型是否为指针
  16. template <typename T>
  17. struct IsPointer : std::false_type
  18. {
  19. };
  20. template <typename T>
  21. struct IsPointer<T*> : std::true_type
  22. {
  23. };
  24. namespace Protocol
  25. {
  26. struct TwoLevelEdgePulse;
  27. class CommonHelper
  28. {
  29. public:
  30. // 没达到预期效果
  31. /*template <typename EnumType>
  32. static bool EnumIsDefined(int value)
  33. {
  34. static_assert(std::is_enum_v<EnumType>, "EnumType must be an enumeration type");
  35. int32_t result = std::underlying_type_t<EnumType>(value);
  36. return result >= 0;
  37. }*/
  38. template <typename EnumType>
  39. struct EnumTypeInfo
  40. {
  41. static const std::type_info& GetTypeInfo()
  42. {
  43. return typeid(EnumType);
  44. }
  45. };
  46. // 通用函数模板,用于将指针数组转换为std::vector
  47. template <typename T>
  48. static std::vector<T> ConvertPointerArrayToVector(T* array, size_t size);
  49. static uint8_t SwapHighLowBits(uint8_t value);
  50. static uint8_t ReverseOrderBits(uint8_t value);
  51. static std::vector<int32_t> RemoveFirstIfMostDifferent(const std::vector<int32_t>& array);
  52. static bool CheckTwoLevelEdgePulseValid(TwoLevelEdgePulse* node);
  53. static bool FindNextNode(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s = 2);
  54. static bool GetNodeByIndex(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s = 2);
  55. //static bool NeedRemoveFirstIfMostDifferent(const int32_t* start_indexs, const int32_t count);
  56. };
  57. template <typename T>
  58. std::vector<T> CommonHelper::ConvertPointerArrayToVector(T* array, size_t size)
  59. {
  60. std::vector<T> vec;
  61. vec.reserve(size); // 预分配内存以避免多次重新分配
  62. if (array == nullptr)
  63. {
  64. return vec;
  65. }
  66. // 根据类型是否为指针,进行不同的处理
  67. if constexpr (IsPointer<T>::value)
  68. {
  69. // T是指针类型,直接移动指针指向的对象
  70. for (size_t i = 0; i < size; ++i)
  71. {
  72. vec.emplace_back(std::move(*array[i]));
  73. }
  74. }
  75. else
  76. {
  77. // T不是指针类型,直接复制对象
  78. for (size_t i = 0; i < size; ++i)
  79. {
  80. //vec.emplace_back(array[i]);
  81. vec.push_back(array[i]);
  82. }
  83. }
  84. // 如果T是指针类型,清理原始指针数组(如果需要)
  85. if constexpr (IsPointer<T>::value)
  86. {
  87. for (size_t i = 0; i < size; ++i)
  88. {
  89. delete array[i];
  90. }
  91. }
  92. return vec;
  93. }
  94. }