From 01ce0af940bd69c94a2fac8b65219262845cca98 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 14 Feb 2022 18:18:38 +0100 Subject: refactor: clean sewage --- src/utils/general.cpp | 27 +++++++++++++++++-- src/utils/general.hpp | 33 ++++++++++++++++------- src/utils/memory.hpp | 26 +++++++++++++------ src/utils/memory.tpp | 65 +++++++++++++++++++++++++++++++++++++++------- src/utils/serial.cpp | 31 +++++++++++++++------- src/utils/serial.hpp | 8 +++--- src/utils/smart_string.cpp | 53 ++++++++++++++++++++++++++++++++----- src/utils/smart_string.hpp | 12 ++++++--- src/utils/time.cpp | 22 +++++++++------- src/utils/time.hpp | 17 ++++++------ 10 files changed, 223 insertions(+), 71 deletions(-) (limited to 'src/utils') diff --git a/src/utils/general.cpp b/src/utils/general.cpp index bbcd4dd..3464c30 100644 --- a/src/utils/general.cpp +++ b/src/utils/general.cpp @@ -5,10 +5,33 @@ void stop() while (true) {} } -unique_ptr floatToStr(float num, unsigned int width, unsigned int precision) +UniquePtr doubleToStr(double num, unsigned int width, unsigned int precision) { auto str = make_unique(width + precision); - dtostrf(num, width, precision, str->c_str); + + dtostrf(num, static_cast(width), precision, str->c_str); + + return str; +} + +UniquePtr intToStr(int num) +{ + auto width = static_cast(log10(num)); + + auto str = make_unique(width + 1U); + + dtostrf(num, static_cast(width + 1U), 0, str->c_str); + + return str; +} + +UniquePtr uintToStr(unsigned int num) +{ + auto width = static_cast(log10(num)); + + auto str = make_unique(width + 1U); + + dtostrf(num, static_cast(width + 1U), 0, str->c_str); return str; } diff --git a/src/utils/general.hpp b/src/utils/general.hpp index 9ad0655..dbdbdfc 100644 --- a/src/utils/general.hpp +++ b/src/utils/general.hpp @@ -9,15 +9,28 @@ void stop(); /** - * Converts a floating point number to a string. + * Converts a double number to a string. * - * @param num A floating point number - * @param width The desired float width - * @param precision The desired float precision - * @returns The float as a string. + * @param num A double number + * @param width The desired double width + * @param precision The desired double precision + * @returns The double as a string. */ -unique_ptr floatToStr( - float num, - unsigned int width = 3, - unsigned int precision = 2 -); +UniquePtr doubleToStr(double num, unsigned int width = 3, + unsigned int precision = 2); + +/** + * Converts a integer to a string. + * + * @param num A number + * @returns The number as a string. + */ +UniquePtr intToStr(int num); + +/** + * Converts a unsigned integer to a string. + * + * @param num A number + * @returns The number as a string. + */ +UniquePtr uintToStr(unsigned int num); diff --git a/src/utils/memory.hpp b/src/utils/memory.hpp index 81e3757..e870b0b 100644 --- a/src/utils/memory.hpp +++ b/src/utils/memory.hpp @@ -1,23 +1,33 @@ #pragma once +#include + template memType *malloc_s(unsigned int size); template -class unique_ptr +class UniquePtr { public: - unique_ptr(Target *target); - ~unique_ptr(); + explicit UniquePtr(); + explicit UniquePtr(Target *target); + UniquePtr(const UniquePtr &unique_ptr); + UniquePtr(UniquePtr &&unique_ptr) noexcept; + + UniquePtr &operator=(const UniquePtr &unique_ptr); + + UniquePtr &operator=(UniquePtr &&unique_ptr) noexcept; + + ~UniquePtr(); - Target operator *() const; - Target *operator ->() const; + Target operator*() const; + Target *operator->() const; private: - Target *_target; + Target *_target = nullptr; }; -template -unique_ptr make_unique(Args&... args); +template +UniquePtr make_unique(Args... args); #include "memory.tpp" diff --git a/src/utils/memory.tpp b/src/utils/memory.tpp index 276b0b4..6261c81 100644 --- a/src/utils/memory.tpp +++ b/src/utils/memory.tpp @@ -1,8 +1,9 @@ -#include "memory.hpp" +#pragma once -#include "Arduino.h" #include "general.hpp" +#include + template memType *malloc_s(unsigned int size) { @@ -18,31 +19,75 @@ memType *malloc_s(unsigned int size) } template -unique_ptr::unique_ptr(Target *target) +UniquePtr::UniquePtr() = default; + +template +UniquePtr::UniquePtr(Target *target) : _target(target) +{ +} + +template +UniquePtr::UniquePtr(const UniquePtr &unique_ptr) + : _target(new Target(*(unique_ptr._target))) { - this->_target = target; } template -unique_ptr::~unique_ptr() +UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept + : _target(unique_ptr._target) { - delete this->_target; + unique_ptr._target = nullptr; +} + +template +UniquePtr &UniquePtr::operator=(const UniquePtr &unique_ptr) +{ + if (&unique_ptr != this) + { + delete _target; + _target = nullptr; + _target = new Target(*(unique_ptr._target)); + } + + return *this; +} + +template +UniquePtr &UniquePtr::operator=(UniquePtr &&unique_ptr) noexcept +{ + if (&unique_ptr != this) + { + delete _target; + _target = unique_ptr._target; + unique_ptr._target = nullptr; + } + + return *this; +} + +template +UniquePtr::~UniquePtr() +{ + if (this->_target != nullptr) + { + delete this->_target; + } } template -Target unique_ptr::operator*() const +Target UniquePtr::operator*() const { return *(this->_target); } template -Target *unique_ptr::operator->() const +Target *UniquePtr::operator->() const { return this->_target; } template -unique_ptr make_unique(Args... args) +UniquePtr make_unique(Args... args) { - return unique_ptr(new Target(args...)); + return UniquePtr(new Target(args...)); } diff --git a/src/utils/serial.cpp b/src/utils/serial.cpp index 11be550..8e4194f 100644 --- a/src/utils/serial.cpp +++ b/src/utils/serial.cpp @@ -2,20 +2,13 @@ #include "general.hpp" -SerialStream::SerialStream(Serial_ serial, unsigned long baud_rate) +SerialStream::SerialStream(Serial_ serial, uint64_t baud_rate) : _serial(serial) { - this->_serial = serial; - this->_serial.begin(baud_rate); while (!this->_serial) {} } -SerialStream::~SerialStream() -{ - Serial.end(); -} - SerialStream &SerialStream::operator<<(const char *str) { this->write(str); @@ -28,9 +21,27 @@ SerialStream &SerialStream::operator<<(const SmartString &str) return *this; } -SerialStream &SerialStream::operator<<(const float num) +SerialStream &SerialStream::operator<<(const double &num) +{ + this->write(doubleToStr(num)->c_str); + return *this; +} + +SerialStream &SerialStream::operator<<(const int &num) +{ + this->write(intToStr(num)->c_str); + return *this; +} + +SerialStream &SerialStream::operator<<(const unsigned int &num) +{ + this->write(uintToStr(num)->c_str); + return *this; +} + +SerialStream &SerialStream::operator<<(const uint32_t &num) { - this->write(floatToStr(num)->c_str); + this->write(uintToStr(num)->c_str); return *this; } diff --git a/src/utils/serial.hpp b/src/utils/serial.hpp index f97e1d8..0d4f929 100644 --- a/src/utils/serial.hpp +++ b/src/utils/serial.hpp @@ -7,12 +7,14 @@ class SerialStream { public: - SerialStream(Serial_ serial, unsigned long baud_rate); - ~SerialStream(); + SerialStream(Serial_ serial, uint64_t baud_rate); SerialStream &operator<<(const char *str); SerialStream &operator<<(const SmartString &str); - SerialStream &operator<<(const float num); + SerialStream &operator<<(const double &num); + SerialStream &operator<<(const int &num); + SerialStream &operator<<(const unsigned int &num); + SerialStream &operator<<(const uint32_t &num); SerialStream &operator<<(void (*manipulator)(SerialStream *)); diff --git a/src/utils/smart_string.cpp b/src/utils/smart_string.cpp index 5258ffe..ac9a862 100644 --- a/src/utils/smart_string.cpp +++ b/src/utils/smart_string.cpp @@ -4,23 +4,62 @@ #include -SmartString::SmartString(char *c_string) +SmartString::SmartString(char *c_string) : c_str(c_string) { - this->c_str = c_string; } -SmartString::SmartString(unsigned int size) +SmartString::SmartString(unsigned int size) : c_str(malloc_s(size + 1)) { - this->c_str = malloc_s(size + 1); +} + +SmartString::SmartString(const SmartString &smart_str) + : c_str(malloc_s(strlen(smart_str.c_str) + 1)) +{ + memcpy(c_str, smart_str.c_str, strlen(smart_str.c_str) + 1); +} + +SmartString::SmartString(SmartString &&smart_str) noexcept : c_str(smart_str.c_str) +{ + smart_str.c_str = nullptr; +} + +SmartString &SmartString::operator=(const SmartString &smart_str) +{ + if (&smart_str != this) + { + free(c_str); + c_str = nullptr; + + auto str_size = strlen(smart_str.c_str) + 1; + + c_str = malloc_s(str_size); + memcpy(c_str, smart_str.c_str, str_size); + } + + return *this; +} + +SmartString &SmartString::operator=(SmartString &&smart_str) noexcept +{ + if (&smart_str != this) + { + free(c_str); + c_str = smart_str.c_str; + smart_str.c_str = nullptr; + } + + return *this; } SmartString::~SmartString() { - if (this->c_str != nullptr) - free(this->c_str); + if (c_str != nullptr) + { + free(c_str); + } } SmartString::operator char *() const { - return this->c_str; + return c_str; } diff --git a/src/utils/smart_string.hpp b/src/utils/smart_string.hpp index fcaff98..6390465 100644 --- a/src/utils/smart_string.hpp +++ b/src/utils/smart_string.hpp @@ -3,12 +3,18 @@ class SmartString { public: - SmartString(char *c_str); - SmartString(unsigned int size); + explicit SmartString(char *c_str); + explicit SmartString(unsigned int size); + SmartString(const SmartString &smart_str); + SmartString(SmartString &&smart_str) noexcept; + + SmartString &operator=(const SmartString &smart_str); + + SmartString &operator=(SmartString &&smart_str) noexcept; ~SmartString(); - operator char *() const; + explicit operator char *() const; char *c_str = nullptr; }; diff --git a/src/utils/time.cpp b/src/utils/time.cpp index adc33db..c6d981c 100644 --- a/src/utils/time.cpp +++ b/src/utils/time.cpp @@ -1,9 +1,9 @@ #include "time.hpp" -#include "Arduino.h" -Time::Time(unsigned long time_micros) +#include + +Time::Time(uint64_t time_micros) : _time_micros(time_micros) { - _time_micros = time_micros; } void Time::update() @@ -16,19 +16,23 @@ Time Time::diff(Time prev_time) return Time(_time_micros - prev_time.microsecs()); } -unsigned long Time::microsecs() +double Time::secs() { - return _time_micros; + const double micros_to_secs = 0.000001; + + return static_cast(_time_micros) * micros_to_secs; } -unsigned long Time::millisecs() +double Time::millisecs() { - return _time_micros * 0.001; + const double micros_to_millis = 0.001; + + return static_cast(_time_micros) * micros_to_millis; } -float Time::secs() +uint64_t Time::microsecs() { - return _time_micros * 0.000001; + return _time_micros; } Time time_now() diff --git a/src/utils/time.hpp b/src/utils/time.hpp index e0385ef..1577bb7 100644 --- a/src/utils/time.hpp +++ b/src/utils/time.hpp @@ -1,5 +1,6 @@ -#ifndef TIME_HPP -#define TIME_HPP +#pragma once + +#include /** * A representation of time. @@ -12,7 +13,7 @@ public: * * @param time_micros Time in microseconds */ - Time(unsigned long time_micros); + explicit Time(uint64_t time_micros); /** * Updates the time to the current time. @@ -29,25 +30,23 @@ public: /** * Returns the time in seconds. */ - float secs(); + double secs(); /** * Returns the time in milliseconds. */ - unsigned long millisecs(); + double millisecs(); /** * Returns the time in microseconds. */ - unsigned long microsecs(); + uint64_t microsecs(); private: - unsigned long _time_micros; + uint64_t _time_micros; }; /** * Returns a time object for the time since the program started. */ Time time_now(); - -#endif -- cgit v1.2.3-18-g5258