From bcdce9633dc351d3bc7f347a165348b8fab87cd9 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 15 Feb 2022 12:33:52 +0100 Subject: refactor: reorganize files & improve classes --- src/std/conversion.cpp | 32 +++++++++++++++++ src/std/conversion.hpp | 31 ++++++++++++++++ src/std/memory.hpp | 33 +++++++++++++++++ src/std/memory.tpp | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ src/std/smart_string.cpp | 65 +++++++++++++++++++++++++++++++++ src/std/smart_string.hpp | 20 +++++++++++ src/std/time.cpp | 37 +++++++++++++++++++ src/std/time.hpp | 55 ++++++++++++++++++++++++++++ 8 files changed, 367 insertions(+) create mode 100644 src/std/conversion.cpp create mode 100644 src/std/conversion.hpp create mode 100644 src/std/memory.hpp create mode 100644 src/std/memory.tpp create mode 100644 src/std/smart_string.cpp create mode 100644 src/std/smart_string.hpp create mode 100644 src/std/time.cpp create mode 100644 src/std/time.hpp (limited to 'src/std') diff --git a/src/std/conversion.cpp b/src/std/conversion.cpp new file mode 100644 index 0000000..a2a624d --- /dev/null +++ b/src/std/conversion.cpp @@ -0,0 +1,32 @@ +#include "conversion.hpp" + +UniquePtr doubleToStr(double num, unsigned int width, unsigned int precision) +{ + auto str = make_unique(width + precision); + + 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/std/conversion.hpp b/src/std/conversion.hpp new file mode 100644 index 0000000..726ce88 --- /dev/null +++ b/src/std/conversion.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include "std/memory.hpp" +#include "std/smart_string.hpp" + +/** + * Converts a double number to 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. + */ +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/std/memory.hpp b/src/std/memory.hpp new file mode 100644 index 0000000..e870b0b --- /dev/null +++ b/src/std/memory.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include + +template +memType *malloc_s(unsigned int size); + +template +class UniquePtr +{ +public: + 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; + +private: + Target *_target = nullptr; +}; + +template +UniquePtr make_unique(Args... args); + +#include "memory.tpp" diff --git a/src/std/memory.tpp b/src/std/memory.tpp new file mode 100644 index 0000000..fbfcd32 --- /dev/null +++ b/src/std/memory.tpp @@ -0,0 +1,94 @@ +#pragma once + +#include "utils.hpp" + +#include +#include + +template +memType *malloc_s(unsigned int size) +{ + auto *mem = malloc(size); + + if (mem == nullptr) + { + Serial.println("Error: Memory allocation failed"); + while (true) {} + } + + return static_cast(mem); +} + +template +UniquePtr::UniquePtr() = default; + +template +UniquePtr::UniquePtr(Target *target) : _target(target) +{ +} + +template +UniquePtr::UniquePtr(const UniquePtr &unique_ptr) + : _target(new Target(*(unique_ptr._target))) +{ +} + +template +UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept + : _target(unique_ptr._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 UniquePtr::operator*() const +{ + return *(this->_target); +} + +template +Target *UniquePtr::operator->() const +{ + return this->_target; +} + +template +UniquePtr make_unique(Args... args) +{ + return UniquePtr(new Target(args...)); +} diff --git a/src/std/smart_string.cpp b/src/std/smart_string.cpp new file mode 100644 index 0000000..b24a1a5 --- /dev/null +++ b/src/std/smart_string.cpp @@ -0,0 +1,65 @@ +#include "smart_string.hpp" + +#include "std/memory.hpp" + +#include + +SmartString::SmartString(char *c_string) : c_str(c_string) +{ +} + +SmartString::SmartString(unsigned int size) : 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 (c_str != nullptr) + { + free(c_str); + } +} + +SmartString::operator char *() const +{ + return c_str; +} diff --git a/src/std/smart_string.hpp b/src/std/smart_string.hpp new file mode 100644 index 0000000..6390465 --- /dev/null +++ b/src/std/smart_string.hpp @@ -0,0 +1,20 @@ +#pragma once + +class SmartString +{ +public: + 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(); + + explicit operator char *() const; + + char *c_str = nullptr; +}; diff --git a/src/std/time.cpp b/src/std/time.cpp new file mode 100644 index 0000000..cca8955 --- /dev/null +++ b/src/std/time.cpp @@ -0,0 +1,37 @@ +#include "time.hpp" + +#include + +Time::Time(uint64_t time_micros) : _time_micros(time_micros) +{ +} + +void Time::update() +{ + _time_micros = micros(); +} + +Time Time::diff(Time prev_time) const +{ + return Time(_time_micros - prev_time.microsecs()); +} + +double Time::secs() const +{ + return static_cast(_time_micros) * MICROS_TO_SECS; +} + +double Time::millisecs() const +{ + return static_cast(_time_micros) * MICROS_TO_MILLIS; +} + +uint64_t Time::microsecs() const +{ + return _time_micros; +} + +Time time_now() +{ + return Time(micros()); +} diff --git a/src/std/time.hpp b/src/std/time.hpp new file mode 100644 index 0000000..a544a5a --- /dev/null +++ b/src/std/time.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include + +constexpr double MICROS_TO_SECS = 0.000001; +constexpr double MICROS_TO_MILLIS = 0.001; + +/** + * A representation of time. + */ +class Time +{ +public: + /** + * A representation of time. + * + * @param time_micros Time in microseconds + */ + explicit Time(uint64_t time_micros); + + /** + * Updates the time to the current time. + */ + void update(); + + /** + * Returns the difference between two points in time. + * + * @param prev_time A previous point in time + */ + Time diff(Time prev_time) const; + + /** + * Returns the time in seconds. + */ + double secs() const; + + /** + * Returns the time in milliseconds. + */ + double millisecs() const; + + /** + * Returns the time in microseconds. + */ + uint64_t microsecs() const; + +private: + uint64_t _time_micros; +}; + +/** + * Returns a time object for the time since the program started. + */ +Time time_now(); -- cgit v1.2.3-18-g5258