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/common | |
parent | 6b5754655f78a7f93b756ff902ce9fd80d9dc4ec (diff) |
refactor: rename std to common
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/conversion.cpp | 39 | ||||
-rw-r--r-- | src/common/conversion.hpp | 36 | ||||
-rw-r--r-- | src/common/memory.hpp | 44 | ||||
-rw-r--r-- | src/common/memory.tpp | 76 | ||||
-rw-r--r-- | src/common/string.cpp | 70 | ||||
-rw-r--r-- | src/common/string.hpp | 25 | ||||
-rw-r--r-- | src/common/time.cpp | 42 | ||||
-rw-r--r-- | src/common/time.hpp | 60 |
8 files changed, 392 insertions, 0 deletions
diff --git a/src/common/conversion.cpp b/src/common/conversion.cpp new file mode 100644 index 0000000..0bdc81a --- /dev/null +++ b/src/common/conversion.cpp @@ -0,0 +1,39 @@ +#include "conversion.hpp" + +namespace common +{ + +common::UniquePtr<common::String> doubleToStr(double num, unsigned int width, + unsigned int precision) +{ + auto str = common::make_unique<common::String>(width + precision); + + dtostrf(num, static_cast<signed char>(width), static_cast<unsigned char>(precision), + str->c_str); + + return str; +} + +common::UniquePtr<common::String> intToStr(int num) +{ + auto width = static_cast<unsigned int>(log10(num)); + + auto str = common::make_unique<common::String>(width + 1U); + + dtostrf(num, static_cast<signed char>(width + 1U), 0U, str->c_str); + + return str; +} + +common::UniquePtr<common::String> uintToStr(unsigned int num) +{ + auto width = static_cast<unsigned int>(log10(num)); + + auto str = common::make_unique<common::String>(width + 1U); + + dtostrf(num, static_cast<signed char>(width + 1U), 0U, str->c_str); + + return str; +} + +} // namespace common diff --git a/src/common/conversion.hpp b/src/common/conversion.hpp new file mode 100644 index 0000000..81a4a52 --- /dev/null +++ b/src/common/conversion.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "common/memory.hpp" +#include "common/string.hpp" + +namespace common +{ + +/** + * 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. + */ +common::UniquePtr<common::String> 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. + */ +common::UniquePtr<common::String> intToStr(int num); + +/** + * Converts a unsigned integer to a string. + * + * @param num A number + * @returns The number as a string. + */ +common::UniquePtr<common::String> uintToStr(unsigned int num); + +} // namespace common diff --git a/src/common/memory.hpp b/src/common/memory.hpp new file mode 100644 index 0000000..de72d80 --- /dev/null +++ b/src/common/memory.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include <stddef.h> + +namespace common +{ + +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); + +} // namespace common + +#include "memory.tpp" diff --git a/src/common/memory.tpp b/src/common/memory.tpp new file mode 100644 index 0000000..ace2432 --- /dev/null +++ b/src/common/memory.tpp @@ -0,0 +1,76 @@ +#pragma once + +#include "memory.hpp" + +#include "utils.hpp" + +#include <Arduino.h> +#include <stdlib.h> + +namespace common +{ + +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...)); +} + +} // namespace common diff --git a/src/common/string.cpp b/src/common/string.cpp new file mode 100644 index 0000000..27b65d4 --- /dev/null +++ b/src/common/string.cpp @@ -0,0 +1,70 @@ +#include "string.hpp" + +#include "common/memory.hpp" + +#include <stdlib.h> + +namespace common +{ + +String::String(char *c_string) : c_str(c_string) +{ +} + +String::String(unsigned int size) : c_str(malloc_s<char>(size + 1)) +{ +} + +String::String(const String &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); +} + +String::String(String &&smart_str) noexcept : c_str(smart_str.c_str) +{ + smart_str.c_str = nullptr; +} + +String &String::operator=(const String &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; +} + +String &String::operator=(String &&smart_str) noexcept +{ + if (&smart_str != this) + { + free(c_str); + c_str = smart_str.c_str; + smart_str.c_str = nullptr; + } + + return *this; +} + +String::~String() +{ + if (c_str != nullptr) + { + free(c_str); + } +} + +String::operator char *() const +{ + return c_str; +} + +} // namespace common diff --git a/src/common/string.hpp b/src/common/string.hpp new file mode 100644 index 0000000..2641457 --- /dev/null +++ b/src/common/string.hpp @@ -0,0 +1,25 @@ +#pragma once + +namespace common +{ + +class String +{ +public: + explicit String(char *c_str); + explicit String(unsigned int size); + String(const String &smart_str); + String(String &&smart_str) noexcept; + + String &operator=(const String &smart_str); + + String &operator=(String &&smart_str) noexcept; + + ~String(); + + explicit operator char *() const; + + char *c_str = nullptr; +}; + +} // namespace common diff --git a/src/common/time.cpp b/src/common/time.cpp new file mode 100644 index 0000000..ee297d7 --- /dev/null +++ b/src/common/time.cpp @@ -0,0 +1,42 @@ +#include "time.hpp" + +#include <Arduino.h> + +namespace common +{ + + 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()); + } + +} // namespace common diff --git a/src/common/time.hpp b/src/common/time.hpp new file mode 100644 index 0000000..8efbd28 --- /dev/null +++ b/src/common/time.hpp @@ -0,0 +1,60 @@ +#pragma once + +#include <stdint.h> + +constexpr double MICROS_TO_SECS = 0.000001; +constexpr double MICROS_TO_MILLIS = 0.001; + +namespace common +{ + + /** + * 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(); + +} // namespace common |