123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- // // ******************************************************************
- // // /\ /| @File common_helper.h
- // // \ V/ @Brief
- // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
- // // / | @Creation 2024-05-16
- // // / \\ @Modified 2024-06-24
- // // *(__\_\
- // // ******************************************************************
- #pragma once
- #include <type_traits>
- #include <typeinfo>
- #include <vector>
- #include "loger.h"
- #include "../edge_pulse.h"
- // 检查类型是否为指针
- template <typename T>
- struct IsPointer : std::false_type
- {
- };
- template <typename T>
- struct IsPointer<T*> : std::true_type
- {
- };
- namespace Protocol
- {
- struct TwoLevelEdgePulse;
- class CommonHelper
- {
- public:
- // 没达到预期效果
- /*template <typename EnumType>
- static bool EnumIsDefined(int value)
- {
- static_assert(std::is_enum_v<EnumType>, "EnumType must be an enumeration type");
- int32_t result = std::underlying_type_t<EnumType>(value);
- return result >= 0;
- }*/
- template <typename EnumType>
- struct EnumTypeInfo
- {
- static const std::type_info& GetTypeInfo()
- {
- return typeid(EnumType);
- }
- };
- // 通用函数模板,用于将指针数组转换为std::vector
- template <typename T>
- static std::vector<T> ConvertPointerArrayToVector(T* array, size_t size);
- static uint8_t SwapHighLowBits(uint8_t value);
- static uint8_t ReverseOrderBits(uint8_t value);
- static std::vector<int32_t> RemoveFirstIfMostDifferent(const std::vector<int32_t>& array);
- static bool CheckTwoLevelEdgePulseValid(TwoLevelEdgePulse* node);
- static bool FindNextNode(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s = 2);
- static bool GetNodeByIndex(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s = 2);
- //static bool NeedRemoveFirstIfMostDifferent(const int32_t* start_indexs, const int32_t count);
- };
- template <typename T>
- std::vector<T> CommonHelper::ConvertPointerArrayToVector(T* array, size_t size)
- {
- std::vector<T> vec;
- vec.reserve(size); // 预分配内存以避免多次重新分配
- if (array == nullptr)
- {
- return vec;
- }
- // 根据类型是否为指针,进行不同的处理
- if constexpr (IsPointer<T>::value)
- {
- // T是指针类型,直接移动指针指向的对象
- for (size_t i = 0; i < size; ++i)
- {
- vec.emplace_back(std::move(*array[i]));
- }
- }
- else
- {
- // T不是指针类型,直接复制对象
- for (size_t i = 0; i < size; ++i)
- {
- //vec.emplace_back(array[i]);
- vec.push_back(array[i]);
- }
- }
- // 如果T是指针类型,清理原始指针数组(如果需要)
- if constexpr (IsPointer<T>::value)
- {
- for (size_t i = 0; i < size; ++i)
- {
- delete array[i];
- }
- }
- return vec;
- }
- }
|