From 5e1321515a84c4ff298fc1227c69ec36e1f602cb Mon Sep 17 00:00:00 2001 From: ZanzyTHEbar Date: Mon, 1 Aug 2022 19:07:06 +0100 Subject: [PATCH] big update::FIX - Fixed serialManager undefiend error - Moved the make_unique function into an override of std namespace - properly implemented the make_unique function --- ESP/lib/src/data/utilities/makeunique.hpp | 52 +++++++++++++++++-- .../SerialManager2/serialmanager.cpp | 38 +++++++------- .../SerialManager2/serialmanager.hpp | 20 +++---- .../src/io/SerialManager/serialmanager.cpp | 2 + ESP/src/main.cpp | 12 ++--- 5 files changed, 86 insertions(+), 38 deletions(-) diff --git a/ESP/lib/src/data/utilities/makeunique.hpp b/ESP/lib/src/data/utilities/makeunique.hpp index 58d4891..9ffca8d 100644 --- a/ESP/lib/src/data/utilities/makeunique.hpp +++ b/ESP/lib/src/data/utilities/makeunique.hpp @@ -1,10 +1,56 @@ #pragma once #include +#include +#include +#include namespace Utilities { - template - std::unique_ptr make_unique(Args &&...args) + +} + +/** + * @brief override the STD namespace to add make_unique function + * + * @tparam T + * @tparam Args + * @return std::unique_ptr + */ +namespace std +{ + template + struct _Unique_if { - return std::unique_ptr(new T(std::forward(args)...)); + typedef unique_ptr _Single_object; + }; + + template + struct _Unique_if + { + typedef unique_ptr _Unknown_bound; + }; + + template + struct _Unique_if + { + typedef void _Known_bound; + }; + + template + typename _Unique_if::_Single_object + make_unique(Args &&...args) + { + return unique_ptr(new T(std::forward(args)...)); } + + template + typename _Unique_if::_Unknown_bound + make_unique(size_t n) + { + typedef typename remove_extent::type U; + return unique_ptr(new U[n]()); + } + + template + typename _Unique_if::_Known_bound + make_unique(Args &&...) = delete; } \ No newline at end of file diff --git a/ESP/lib/src/io/SerialManager/SerialManager2/serialmanager.cpp b/ESP/lib/src/io/SerialManager/SerialManager2/serialmanager.cpp index 8f93d61..f47fb80 100644 --- a/ESP/lib/src/io/SerialManager/SerialManager2/serialmanager.cpp +++ b/ESP/lib/src/io/SerialManager/SerialManager2/serialmanager.cpp @@ -41,12 +41,12 @@ void printHex(Stream &port, uint16_t *data, uint8_t length) // prints 16-bit dat } #endif -SerialManager::SerialManager() : userErrorHandler(NULL), _serial(NULL), ManagerCount(0), _serialManagerActive(false), newData(false) +SerialManager2::SerialManager2() : userErrorHandler(NULL), _serial(NULL), ManagerCount(0), _serialManager2Active(false), newData(false) { clear(); } -void SerialManager::begin(Stream &serialPort) +void SerialManager2::begin(Stream &serialPort) { /* Save Serial Port configurations */ _serial = &serialPort; @@ -55,10 +55,10 @@ void SerialManager::begin(Stream &serialPort) // This checks the Serial stream for characters, and assembles them into a buffer. // When the terminator character (defined by EOL constant) is seen, it starts parsing the // buffer for a prefix Manager, and calls handlers setup by addManager() method -void SerialManager::loop(unsigned long timeout) +void SerialManager2::loop(unsigned long timeout) { log_d("Listening to serial"); - _serialManagerActive = true; + _serialManager2Active = true; Serial.setTimeout(timeout); static bool recvInProgress = false; char startDelimiter = '<'; //! we need to decide on a delimiter for the start of a message @@ -88,11 +88,11 @@ void SerialManager::loop(unsigned long timeout) } } delay(timeout); - _serialManagerActive = false; + _serialManager2Active = false; } /* Clear buffer */ -void SerialManager::clear(void) +void SerialManager2::clear(void) { memset(buffer, 0, SERIAL_CMD_BUFF_LEN); pBuff = buffer; @@ -103,7 +103,7 @@ void SerialManager::clear(void) * NOTE: Will execute user defined callback (defined using addDefault method), * if no user defined callback it will send the ERROR message (sendERROR method). */ -void SerialManager::error(void) +void SerialManager2::error(void) { if (NULL != userErrorHandler) { @@ -115,12 +115,12 @@ void SerialManager::error(void) // Retrieve the next token ("word" or "argument") from the Manager buffer. // returns a NULL if no more tokens exist. -char *SerialManager::next(void) +char *SerialManager2::next(void) { return strtok_r(NULL, delimiters, &last); } -void SerialManager::bufferHandler(char c) +void SerialManager2::bufferHandler(char c) { int len; char *lastChars = NULL; @@ -162,7 +162,7 @@ void SerialManager::bufferHandler(char c) } /* Return true if match was found */ -bool SerialManager::ManagerHandler(void) +bool SerialManager2::ManagerHandler(void) { int i; bool ret = false; @@ -268,7 +268,7 @@ bool SerialManager::ManagerHandler(void) // Adds a "Manager" and a handler function to the list of available Managers. // This is used for matching a found token in the buffer, and gives the pointer // to the handler function to deal with it. -void SerialManager::addManager(char *cmd, void (*test)(), void (*read)(), void (*write)(), void (*execute)()) +void SerialManager2::addManager(char *cmd, void (*test)(), void (*read)(), void (*write)(), void (*execute)()) { #if SERIAL_CMD_DBG_EN @@ -278,7 +278,7 @@ void SerialManager::addManager(char *cmd, void (*test)(), void (*read)(), void ( println(cmd); #endif - ManagerList = (serialManagerCallback *)realloc(ManagerList, (ManagerCount + 1) * sizeof(serialManagerCallback)); + ManagerList = (serialManager2Callback *)realloc(ManagerList, (ManagerCount + 1) * sizeof(serialManager2Callback)); strncpy(ManagerList[ManagerCount].Manager, cmd, SERIAL_CMD_BUFF_LEN); ManagerList[ManagerCount].test = test; ManagerList[ManagerCount].read = read; @@ -288,12 +288,12 @@ void SerialManager::addManager(char *cmd, void (*test)(), void (*read)(), void ( } /* Optional user-defined function to call when an error occurs, default is NULL */ -void SerialManager::addError(void (*callback)()) +void SerialManager2::addError(void (*callback)()) { userErrorHandler = callback; } -int SerialManager::available() +int SerialManager2::available() { int bytes = 0; if (NULL != _serial) @@ -303,7 +303,7 @@ int SerialManager::available() return bytes; } -int SerialManager::read() +int SerialManager2::read() { int bytes = 0; if (NULL != _serial) @@ -313,7 +313,7 @@ int SerialManager::read() return bytes; } -int SerialManager::peek() +int SerialManager2::peek() { int bytes = 0; if (NULL != _serial) @@ -323,7 +323,7 @@ int SerialManager::peek() return bytes; } -void SerialManager::flush() +void SerialManager2::flush() { if (NULL != _serial) { @@ -331,10 +331,10 @@ void SerialManager::flush() } } -size_t SerialManager::write(uint8_t x) +size_t SerialManager2::write(uint8_t x) { (void)x; return 0; } -SerialManager serialManager; \ No newline at end of file +SerialManager2 serialManager2; \ No newline at end of file diff --git a/ESP/lib/src/io/SerialManager/SerialManager2/serialmanager.hpp b/ESP/lib/src/io/SerialManager/SerialManager2/serialmanager.hpp index b04f428..1ca6b76 100644 --- a/ESP/lib/src/io/SerialManager/SerialManager2/serialmanager.hpp +++ b/ESP/lib/src/io/SerialManager/SerialManager2/serialmanager.hpp @@ -1,5 +1,5 @@ -#ifndef SERIALMANAGER_HPP -#define SERIALMANAGER_HPP +#ifndef SERIALMANAGER2_HPP +#define SERIALMANAGER2_HPP #include #include @@ -14,7 +14,7 @@ typedef struct void (*read)(); void (*write)(); void (*execute)(); -} serialManagerCallback; +} serialManager2Callback; /* * Token delimiters (setup '=', query '?', separator ',') @@ -28,11 +28,11 @@ const char delimiters[] = "=,?\r\n"; */ const char EOL[] = "\r\n"; -class SerialManager : public Stream +class SerialManager2 : public Stream { public: - SerialManager(); - virtual ~SerialManager(); + SerialManager2(); + virtual ~SerialManager2(); /** * Start connection to serial port @@ -141,7 +141,7 @@ private: /* Serial Port handler */ Stream *_serial; /* Actual definition for Manager/handler array */ - serialManagerCallback *ManagerList; + serialManager2Callback *ManagerList; /* Buffer of stored characters while waiting for terminator character */ char buffer[SERIAL_CMD_BUFF_LEN]; /* Pointer to buffer, used to store data in the buffer */ @@ -151,8 +151,8 @@ private: /* Number of available Managers registered by new() */ uint8_t ManagerCount; - bool _serialManagerActive; + bool _serialManager2Active; }; -extern SerialManager serialManager; -#endif // SerialManager_h \ No newline at end of file +extern SerialManager2 serialManager2; +#endif // SerialManager2_h \ No newline at end of file diff --git a/ESP/lib/src/io/SerialManager/serialmanager.cpp b/ESP/lib/src/io/SerialManager/serialmanager.cpp index 9a4a127..ffe2174 100644 --- a/ESP/lib/src/io/SerialManager/serialmanager.cpp +++ b/ESP/lib/src/io/SerialManager/serialmanager.cpp @@ -119,3 +119,5 @@ void SerialManager::handleSerial() newData = false; // reset new data } } + +SerialManager serialManager; \ No newline at end of file diff --git a/ESP/src/main.cpp b/ESP/src/main.cpp index 39ddac8..323c745 100644 --- a/ESP/src/main.cpp +++ b/ESP/src/main.cpp @@ -18,12 +18,12 @@ uint8_t CONTROL_SERVER_PORT = 81; // Create smart pointers to the various classes that will be used in the program to make sure that they are deleted when the program ends // This is done to make sure that the memory is freed when the program ends and we are not left with dangling pointers to memory that is no longer in use // Make unique is a templated function that takes a class and returns a unique pointer to that class - it is used to create a unique pointer to a class and ensure exception safety -std::unique_ptr ota = Utilities::make_unique(); -std::unique_ptr ledManager = Utilities::make_unique(33); -std::unique_ptr cameraHandler = Utilities::make_unique(&projectConfig); -std::unique_ptr apiServer = Utilities::make_unique(CONTROL_SERVER_PORT, &*cameraHandler); -std::unique_ptr mdnsHandler = Utilities::make_unique(&mdnsStateManager, &projectConfig); -std::unique_ptr streamServer = Utilities::make_unique(STREAM_SERVER_PORT); +std::unique_ptr ota = std::make_unique(); +std::unique_ptr ledManager = std::make_unique(33); +std::unique_ptr cameraHandler = std::make_unique(&projectConfig); +std::unique_ptr apiServer = std::make_unique(CONTROL_SERVER_PORT, &*cameraHandler); +std::unique_ptr mdnsHandler = std::make_unique(&mdnsStateManager, &projectConfig); +std::unique_ptr streamServer = std::make_unique(STREAM_SERVER_PORT); void setup() {