优化约束
This commit is contained in:
@@ -22,6 +22,64 @@ namespace ranges = std::ranges;
|
||||
namespace views = std::views;
|
||||
|
||||
namespace serial {
|
||||
template <typename... Types> struct _StrongType {};
|
||||
|
||||
template <typename T> struct _StrongType<T> {
|
||||
using Type = T;
|
||||
};
|
||||
|
||||
template <typename Ta, typename Tb> struct _StrongType<Ta, Tb> {
|
||||
using Type = decltype(true ? std::declval<Ta>() : std::declval<Tb>());
|
||||
};
|
||||
|
||||
template <typename T, typename... Types> struct _StrongType<T, Types...> {
|
||||
using Type =
|
||||
typename _StrongType<T, typename _StrongType<Types...>::Type>::Type;
|
||||
};
|
||||
|
||||
template <typename... Types>
|
||||
using _strongType_t = typename _StrongType<Types...>::Type;
|
||||
|
||||
template <typename... Args> struct _IsCastable {};
|
||||
|
||||
template <typename T> struct _IsCastable<T> {
|
||||
static constexpr bool value = true;
|
||||
using Type = T;
|
||||
};
|
||||
|
||||
template <typename T, typename U> struct _IsCastable<T, U> {
|
||||
using __TRUE = char;
|
||||
using __FALSE = struct {
|
||||
char _[2];
|
||||
};
|
||||
static consteval __TRUE __TEST(U);
|
||||
static consteval __FALSE __TEST(...);
|
||||
static constexpr bool value =
|
||||
sizeof(__TEST(std::declval<T>())) == sizeof(__TRUE);
|
||||
using Type = std::conditional_t<value, U, void>;
|
||||
};
|
||||
|
||||
template <typename T, typename... Args> struct _IsCastable<T, Args...> {
|
||||
static constexpr bool value =
|
||||
_IsCastable<T, typename _IsCastable<Args...>::Type>::value;
|
||||
using Type =
|
||||
std::conditional_t<value, typename _IsCastable<Args...>::Type, void>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
concept _SupportString = requires {
|
||||
_IsCastable<T, const char *, char *, std::string, std::string_view>::value;
|
||||
};
|
||||
|
||||
template <_SupportString T> std::string _to_string(T &&str) {
|
||||
if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
|
||||
return str;
|
||||
} else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>) {
|
||||
return std::move(std::string(str.data()));
|
||||
} else if constexpr (std::is_same_v<std::decay_t<T>, const char*>) {
|
||||
return std::string(str);
|
||||
}
|
||||
}
|
||||
|
||||
static std::vector<std::string> GetUsbPorts() {
|
||||
std::vector<std::string> portArray;
|
||||
@@ -43,12 +101,6 @@ static std::vector<std::string> GetUsbPorts() {
|
||||
return portArray;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
concept SupportString = requires {
|
||||
std::is_same_v<T, const char *>;
|
||||
std::is_same_v<T, std::string>;
|
||||
};
|
||||
|
||||
enum class [[maybe_unused]] SerialErrorCode {
|
||||
SUCCESS,
|
||||
TIMEOUT,
|
||||
@@ -73,6 +125,8 @@ class Serial {
|
||||
Serial &operator=(Serial &&other) = delete;
|
||||
|
||||
auto SetRemoveEcho(bool remove) { removeEcho = remove; }
|
||||
auto SetDtr(bool flag) { return flag ? ser.setDTR() : ser.clearDTR(); }
|
||||
auto SetRts(bool flag) { return flag ? ser.setRTS() : ser.clearRTS(); }
|
||||
|
||||
static std::string GetTimeNow() {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
@@ -84,7 +138,7 @@ class Serial {
|
||||
|
||||
bool IsOpen() { return ser.isDeviceOpen(); }
|
||||
|
||||
template <SupportString T>
|
||||
template <_SupportString T>
|
||||
bool OpenDevice(T portName, unsigned int bauds = 115200,
|
||||
int delayTime = 0) {
|
||||
std::string reallyPortName;
|
||||
@@ -118,7 +172,7 @@ class Serial {
|
||||
ser.closeDevice();
|
||||
}
|
||||
|
||||
template <SupportString T>
|
||||
template <_SupportString T>
|
||||
std::expected<std::string, SerialErrorCode>
|
||||
DelayGetResponse(int delayTime, T command, int timeout = 50) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
||||
@@ -126,7 +180,7 @@ class Serial {
|
||||
}
|
||||
|
||||
template <int repeatTime = 5, int delayTime = 200, int timeout = 200,
|
||||
SupportString T, SupportString... Args>
|
||||
_SupportString T, _SupportString... Args>
|
||||
bool GetAtUntilRepeat(T command, Args... args) {
|
||||
std::stringstream ss;
|
||||
int i = 0;
|
||||
@@ -141,16 +195,11 @@ class Serial {
|
||||
return false;
|
||||
}
|
||||
|
||||
template <SupportString T>
|
||||
template <_SupportString T>
|
||||
std::expected<std::string, SerialErrorCode>
|
||||
GetAtResponse(T command, int timeout = 50) {
|
||||
ser.flushReceiver();
|
||||
std::string reallyCommand;
|
||||
if constexpr (std::is_same_v<T, std::string>) {
|
||||
reallyCommand = command + endChar;
|
||||
} else {
|
||||
reallyCommand = std::string(command) + endChar;
|
||||
}
|
||||
std::string reallyCommand = std::string(command) + endChar;
|
||||
ser.writeString(reallyCommand.c_str());
|
||||
Log("Send: " + reallyCommand);
|
||||
std::this_thread::sleep_for(10ms);
|
||||
@@ -161,7 +210,7 @@ class Serial {
|
||||
|
||||
if (size > 0) {
|
||||
buffer[size] = '\0';
|
||||
std::string response = std::string(buffer);
|
||||
std::string response = std::string(buffer.get());
|
||||
Log("Receive: " + response);
|
||||
if (removeEcho)
|
||||
response.replace(0, reallyCommand.length(), "");
|
||||
@@ -170,7 +219,7 @@ class Serial {
|
||||
return std::unexpected(SerialErrorCode::TIMEOUT);
|
||||
}
|
||||
|
||||
template <SupportString T>
|
||||
template <_SupportString T>
|
||||
auto GetAtResponseRepeat(T command, int timeout = 200, int repeatTime = 1)
|
||||
-> void {
|
||||
for (int i = 0; i <= repeatTime; i++) {
|
||||
@@ -178,17 +227,12 @@ class Serial {
|
||||
}
|
||||
}
|
||||
|
||||
template <int timeout = 200, SupportString T, SupportString... Args>
|
||||
template <int timeout = 200, _SupportString T, _SupportString... Args>
|
||||
bool GetAtUntil(T command, Args... expect) {
|
||||
auto endTime = std::chrono::system_clock::now() +
|
||||
std::chrono::milliseconds(timeout);
|
||||
ser.flushReceiver();
|
||||
std::string reallyCommand;
|
||||
if constexpr (std::is_same_v<T, std::string>) {
|
||||
reallyCommand = command + endChar;
|
||||
} else {
|
||||
reallyCommand = std::string(command) + endChar;
|
||||
}
|
||||
std::string reallyCommand = std::string(command) + endChar;
|
||||
ser.writeString(reallyCommand.c_str());
|
||||
Log("Send : " + reallyCommand);
|
||||
while (std::chrono::system_clock::now() < endTime) {
|
||||
|
||||
Reference in New Issue
Block a user