From 90d597962e6761fe4b6e3999cbea733effcd9c79 Mon Sep 17 00:00:00 2001 From: ackimixs Date: Mon, 15 Apr 2024 18:03:13 +0200 Subject: [PATCH] use c++ file --- CMakeLists.txt | 1 + utils.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++ utils.h | 91 +++++++++++++++++++++++--------------------------- 3 files changed, 123 insertions(+), 49 deletions(-) create mode 100644 utils.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c67a6b9..7ac4f1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,4 +5,5 @@ set(CMAKE_CXX_STANDARD 17) add_executable(socketServer main.cpp TCPServer.cpp + utils.cpp ) diff --git a/utils.cpp b/utils.cpp new file mode 100644 index 0000000..2b39f9d --- /dev/null +++ b/utils.cpp @@ -0,0 +1,80 @@ +#include "utils.h" + +bool TCPUtils::startWith(const std::string& str, const std::string& start) +{ + return str.rfind(start, 0) == 0; +} + +bool TCPUtils::endsWith(const std::string& str, const std::string& end) +{ + if (str.length() >= end.length()) + { + return (0 == str.compare(str.length() - end.length(), end.length(), end)); + } + return false; +} + +bool TCPUtils::contains(const std::string& str, const std::string& sub) +{ + return str.find(sub) != std::string::npos; +} + +std::vector TCPUtils::split(const std::string& str, const std::string& delimiter) +{ + std::vector tokens; + size_t prev = 0, pos = 0; + do + { + pos = str.find(delimiter, prev); + if (pos == std::string::npos) pos = str.length(); + std::string token = str.substr(prev, pos - prev); + if (!token.empty()) tokens.push_back(token); + prev = pos + delimiter.length(); + } while (pos < str.length() && prev < str.length()); + return tokens; +} + + +ArucoTag::ArucoTag(int id, std::string name, std::pair pos) : _id(id), _name(std::move(name)), _pos(pos) {} + +int ArucoTag::id() const { + return _id; +} + +std::string ArucoTag::name() const { + return _name; +} + +std::pair ArucoTag::pos() const { + return _pos; +} + +void ArucoTag::setId(int id) { + _id = id; +} + +void ArucoTag::setName(const std::string& name) { + _name = name; +} + +void ArucoTag::setPos(float x, float y) { + _pos.first[0] = x; + _pos.first[1] = y; +} + +void ArucoTag::setRot(float x, float y, float z) { + _pos.second[0] = x; + _pos.second[1] = y; + _pos.second[2] = z; +} + +ArucoTag& ArucoTag::operator=(const ArucoTag& tag) { + _id = tag.id(); + _name = tag.name(); + _pos.first[0] = tag.pos().first[0]; + _pos.first[1] = tag.pos().first[1]; + _pos.second[0] = tag.pos().second[0]; + _pos.second[1] = tag.pos().second[1]; + _pos.second[2] = tag.pos().second[2]; + return *this; +} \ No newline at end of file diff --git a/utils.h b/utils.h index 34420ea..f598b74 100644 --- a/utils.h +++ b/utils.h @@ -1,57 +1,50 @@ #pragma once +#include #include #include -inline bool startWith(const std::string& str, const std::string& start) -{ - return str.rfind(start, 0) == 0; -} - -inline bool endsWith(const std::string& str, const std::string& end) -{ - if (str.length() >= end.length()) - { - return (0 == str.compare(str.length() - end.length(), end.length(), end)); - } - return false; -} - -inline bool contains(const std::string& str, const std::string& sub) -{ - return str.find(sub) != std::string::npos; -} - -inline std::vector split(const std::string& str, const std::string& delimiter) -{ - std::vector tokens; - size_t prev = 0, pos = 0; - do - { - pos = str.find(delimiter, prev); - if (pos == std::string::npos) pos = str.length(); - std::string token = str.substr(prev, pos - prev); - if (!token.empty()) tokens.push_back(token); - prev = pos + delimiter.length(); - } while (pos < str.length() && prev < str.length()); - return tokens; -} - -struct ClientTCP -{ - std::string name; - int socket = -1; - bool isReady = false; +enum PinceState { + WHITE_FLOWER, + PURPLE_FLOWER, + NONE }; -struct ArucoTag -{ - int id; - std::string name; -}; +namespace TCPUtils { + bool startWith(const std::string& str, const std::string& start); -struct ArucoTagPos -{ - ArucoTag tag; - std::pair pos; -}; \ No newline at end of file + bool endsWith(const std::string& str, const std::string& end); + + bool contains(const std::string& str, const std::string& sub); + + std::vector split(const std::string& str, const std::string& delimiter); +} + +class ArucoTag { + +public: + ArucoTag(int id, std::string name, std::pair pos); + + ArucoTag() = default; + + [[nodiscard]] int id() const; + + [[nodiscard]] std::string name() const; + + [[nodiscard]] std::pair pos() const; + + void setId(int id); + + void setName(const std::string& name); + + void setPos(float x, float y); + + void setRot(float x, float y, float z); + + ArucoTag& operator=(const ArucoTag& tag); + +private: + int _id = 0; + std::string _name; + std::pair _pos; +};