diff options
author | HampusM <hampus@hampusmat.com> | 2022-03-14 10:24:36 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-03-14 10:24:36 +0100 |
commit | a119e6ca70ffab14f0a70908fa3eeb83b41bb5ab (patch) | |
tree | 51b72774694f5e1aac0bb17fc1a5e528dbad9b44 /src/std | |
parent | 6b5754655f78a7f93b756ff902ce9fd80d9dc4ec (diff) |
refactor: rename std to common
Diffstat (limited to 'src/std')
-rw-r--r-- | src/std/conversion.cpp | 33 | ||||
-rw-r--r-- | src/std/conversion.hpp | 31 | ||||
-rw-r--r-- | src/std/memory.hpp | 39 | ||||
-rw-r--r-- | src/std/memory.tpp | 71 | ||||
-rw-r--r-- | src/std/smart_string.cpp | 65 | ||||
-rw-r--r-- | src/std/smart_string.hpp | 20 | ||||
-rw-r--r-- | src/std/time.cpp | 37 | ||||
-rw-r--r-- | src/std/time.hpp | 55 |
8 files changed, 0 insertions, 351 deletions
diff --git a/src/std/conversion.cpp b/src/std/conversion.cpp deleted file mode 100644 index 3d75e8a..0000000 --- a/src/std/conversion.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "conversion.hpp" - -UniquePtr<SmartString> doubleToStr(double num, unsigned int width, unsigned int precision) -{ - auto str = make_unique<SmartString>(width + precision); - - dtostrf(num, static_cast<signed char>(width), static_cast<unsigned char>(precision), - str->c_str); - - return str; -} - -UniquePtr<SmartString> intToStr(int num) -{ - auto width = static_cast<unsigned int>(log10(num)); - - auto str = make_unique<SmartString>(width + 1U); - - dtostrf(num, static_cast<signed char>(width + 1U), 0U, str->c_str); - - return str; -} - -UniquePtr<SmartString> uintToStr(unsigned int num) -{ - auto width = static_cast<unsigned int>(log10(num)); - - auto str = make_unique<SmartString>(width + 1U); - - dtostrf(num, static_cast<signed char>(width + 1U), 0U, str->c_str); - - return str; -} diff --git a/src/std/conversion.hpp b/src/std/conversion.hpp deleted file mode 100644 index 726ce88..0000000 --- a/src/std/conversion.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#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<SmartString> 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<SmartString> intToStr(int num); - -/** - * Converts a unsigned integer to a string. - * - * @param num A number - * @returns The number as a string. - */ -UniquePtr<SmartString> uintToStr(unsigned int num); diff --git a/src/std/memory.hpp b/src/std/memory.hpp deleted file mode 100644 index d1ca762..0000000 --- a/src/std/memory.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include <stddef.h> - -template <typename memType> -memType *malloc_s(unsigned int size); - -template <class Target> -class UniquePtr -{ -public: - explicit UniquePtr() = default; - explicit UniquePtr(Target *target); - - // Move constructor - UniquePtr(UniquePtr &&unique_ptr) noexcept; - - // Move assignment operator - UniquePtr &operator=(UniquePtr &&unique_ptr) noexcept; - - // Disable the copy constructor - UniquePtr(const UniquePtr &unique_ptr) = delete; - - // Disable the copy assignment operator - UniquePtr &operator=(const UniquePtr &unique_ptr) = delete; - - ~UniquePtr(); - - Target operator*() const; - Target *operator->() const; - -private: - Target *_target = nullptr; -}; - -template <typename Target, typename... Args> -UniquePtr<Target> make_unique(Args... args); - -#include "memory.tpp" diff --git a/src/std/memory.tpp b/src/std/memory.tpp deleted file mode 100644 index b2f39bf..0000000 --- a/src/std/memory.tpp +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once - -#include "memory.hpp" - -#include "utils.hpp" - -#include <Arduino.h> -#include <stdlib.h> - -template <typename memType> -memType *malloc_s(unsigned int size) -{ - auto *mem = malloc(size); - - if (mem == nullptr) - { - Serial.println("Error: Memory allocation failed"); - while (true) {} - } - - return static_cast<memType *>(mem); -} - -template <class Target> -UniquePtr<Target>::UniquePtr(Target *target) : _target(target) -{ -} - -template <class Target> -UniquePtr<Target>::UniquePtr(UniquePtr &&unique_ptr) noexcept - : _target(unique_ptr._target) -{ - unique_ptr._target = nullptr; -} - -template <class Target> -UniquePtr<Target> &UniquePtr<Target>::operator=(UniquePtr &&unique_ptr) noexcept -{ - if (&unique_ptr != this) - { - delete _target; - _target = unique_ptr._target; - unique_ptr._target = nullptr; - } - - return *this; -} - -template <class Target> -UniquePtr<Target>::~UniquePtr() -{ - delete _target; -} - -template <class Target> -Target UniquePtr<Target>::operator*() const -{ - return *(_target); -} - -template <class Target> -Target *UniquePtr<Target>::operator->() const -{ - return _target; -} - -template <class Target, typename... Args> -UniquePtr<Target> make_unique(Args... args) -{ - return UniquePtr<Target>(new Target(args...)); -} diff --git a/src/std/smart_string.cpp b/src/std/smart_string.cpp deleted file mode 100644 index b24a1a5..0000000 --- a/src/std/smart_string.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "smart_string.hpp" - -#include "std/memory.hpp" - -#include <stdlib.h> - -SmartString::SmartString(char *c_string) : c_str(c_string) -{ -} - -SmartString::SmartString(unsigned int size) : c_str(malloc_s<char>(size + 1)) -{ -} - -SmartString::SmartString(const SmartString &smart_str) - : c_str(malloc_s<char>(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<char>(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 deleted file mode 100644 index 6390465..0000000 --- a/src/std/smart_string.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#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 deleted file mode 100644 index cca8955..0000000 --- a/src/std/time.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "time.hpp" - -#include <Arduino.h> - -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<double>(_time_micros) * MICROS_TO_SECS; -} - -double Time::millisecs() const -{ - return static_cast<double>(_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 deleted file mode 100644 index a544a5a..0000000 --- a/src/std/time.hpp +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include <stdint.h> - -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(); |