big update::FIX

- Fixed serialManager undefiend error
- Moved the make_unique function into an override of std namespace
- properly implemented the make_unique function
This commit is contained in:
ZanzyTHEbar 2022-08-01 19:07:06 +01:00
parent 1bfb67ca40
commit 5e1321515a
5 changed files with 86 additions and 38 deletions

View File

@ -1,10 +1,56 @@
#pragma once
#include <memory>
#include <cstddef>
#include <type_traits>
#include <utility>
namespace Utilities
{
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&...args)
}
/**
* @brief override the STD namespace to add make_unique function
*
* @tparam T
* @tparam Args
* @return std::unique_ptr<T>
*/
namespace std
{
template <class T>
struct _Unique_if
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
typedef unique_ptr<T> _Single_object;
};
template <class T>
struct _Unique_if<T[]>
{
typedef unique_ptr<T[]> _Unknown_bound;
};
template <class T, size_t N>
struct _Unique_if<T[N]>
{
typedef void _Known_bound;
};
template <class T, class... Args>
typename _Unique_if<T>::_Single_object
make_unique(Args &&...args)
{
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template <class T>
typename _Unique_if<T>::_Unknown_bound
make_unique(size_t n)
{
typedef typename remove_extent<T>::type U;
return unique_ptr<T>(new U[n]());
}
template <class T, class... Args>
typename _Unique_if<T>::_Known_bound
make_unique(Args &&...) = delete;
}

View File

@ -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;
SerialManager2 serialManager2;

View File

@ -1,5 +1,5 @@
#ifndef SERIALMANAGER_HPP
#define SERIALMANAGER_HPP
#ifndef SERIALMANAGER2_HPP
#define SERIALMANAGER2_HPP
#include <Arduino.h>
#include <string.h>
@ -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
extern SerialManager2 serialManager2;
#endif // SerialManager2_h

View File

@ -119,3 +119,5 @@ void SerialManager::handleSerial()
newData = false; // reset new data
}
}
SerialManager serialManager;

View File

@ -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> ota = Utilities::make_unique<OTA>();
std::unique_ptr<LEDManager> ledManager = Utilities::make_unique<LEDManager>(33);
std::unique_ptr<CameraHandler> cameraHandler = Utilities::make_unique<CameraHandler>(&projectConfig);
std::unique_ptr<APIServer> apiServer = Utilities::make_unique<APIServer>(CONTROL_SERVER_PORT, &*cameraHandler);
std::unique_ptr<MDNSHandler> mdnsHandler = Utilities::make_unique<MDNSHandler>(&mdnsStateManager, &projectConfig);
std::unique_ptr<StreamServer> streamServer = Utilities::make_unique<StreamServer>(STREAM_SERVER_PORT);
std::unique_ptr<OTA> ota = std::make_unique<OTA>();
std::unique_ptr<LEDManager> ledManager = std::make_unique<LEDManager>(33);
std::unique_ptr<CameraHandler> cameraHandler = std::make_unique<CameraHandler>(&projectConfig);
std::unique_ptr<APIServer> apiServer = std::make_unique<APIServer>(CONTROL_SERVER_PORT, &*cameraHandler);
std::unique_ptr<MDNSHandler> mdnsHandler = std::make_unique<MDNSHandler>(&mdnsStateManager, &projectConfig);
std::unique_ptr<StreamServer> streamServer = std::make_unique<StreamServer>(STREAM_SERVER_PORT);
void setup()
{