From 757a29d0137b974fff6ddcc945d76e69ae120ecb Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 21 Mar 2022 13:00:36 +0100 Subject: refactor: use MPU6050_light & clean up bloat --- src/common/conversion.cpp | 42 ----------- src/common/conversion.hpp | 36 --------- src/common/memory.hpp | 13 ---- src/common/memory.tpp | 27 ------- src/common/memory/shared_ptr.hpp | 44 ----------- src/common/memory/shared_ptr.tpp | 153 --------------------------------------- src/common/memory/unique_ptr.hpp | 37 ---------- src/common/memory/unique_ptr.tpp | 55 -------------- src/common/string.cpp | 70 ------------------ src/common/string.hpp | 25 ------- src/common/time.cpp | 42 ----------- src/common/time.hpp | 60 --------------- 12 files changed, 604 deletions(-) delete mode 100644 src/common/conversion.cpp delete mode 100644 src/common/conversion.hpp delete mode 100644 src/common/memory.hpp delete mode 100644 src/common/memory.tpp delete mode 100644 src/common/memory/shared_ptr.hpp delete mode 100644 src/common/memory/shared_ptr.tpp delete mode 100644 src/common/memory/unique_ptr.hpp delete mode 100644 src/common/memory/unique_ptr.tpp delete mode 100644 src/common/string.cpp delete mode 100644 src/common/string.hpp delete mode 100644 src/common/time.cpp delete mode 100644 src/common/time.hpp (limited to 'src/common') diff --git a/src/common/conversion.cpp b/src/common/conversion.cpp deleted file mode 100644 index b016537..0000000 --- a/src/common/conversion.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "conversion.hpp" - -#include -#include - -namespace common -{ - -common::UniquePtr doubleToStr(double num, unsigned int width, - unsigned int precision) -{ - auto str = common::make_unique(width + precision); - - dtostrf(num, static_cast(width), static_cast(precision), - str->c_str); - - return str; -} - -common::UniquePtr intToStr(int num) -{ - auto width = static_cast(log10(num)); - - auto str = common::make_unique(width + 1U); - - dtostrf(num, static_cast(width + 1U), 0U, str->c_str); - - return str; -} - -common::UniquePtr uintToStr(unsigned int num) -{ - auto width = static_cast(log10(num)); - - auto str = common::make_unique(width + 1U); - - dtostrf(num, static_cast(width + 1U), 0U, str->c_str); - - return str; -} - -} // namespace common diff --git a/src/common/conversion.hpp b/src/common/conversion.hpp deleted file mode 100644 index e87adc5..0000000 --- a/src/common/conversion.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include "common/memory/unique_ptr.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 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 intToStr(int num); - -/** - * Converts a unsigned integer to a string. - * - * @param num A number - * @returns The number as a string. - */ -common::UniquePtr uintToStr(unsigned int num); - -} // namespace common diff --git a/src/common/memory.hpp b/src/common/memory.hpp deleted file mode 100644 index 888ede3..0000000 --- a/src/common/memory.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include - -namespace common -{ - -template -memType *malloc_s(unsigned int size); - -} // namespace common - -#include "memory.tpp" diff --git a/src/common/memory.tpp b/src/common/memory.tpp deleted file mode 100644 index af833bc..0000000 --- a/src/common/memory.tpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "memory.hpp" - -#include "utils.hpp" - -#include -#include - -namespace common -{ - -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); -} - -} // namespace common diff --git a/src/common/memory/shared_ptr.hpp b/src/common/memory/shared_ptr.hpp deleted file mode 100644 index 7e8a910..0000000 --- a/src/common/memory/shared_ptr.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include - -namespace common -{ -template -class SharedPtr -{ -public: - SharedPtr() noexcept; - SharedPtr(nullptr_t) noexcept; // NOLINT(google-explicit-constructor) - - explicit SharedPtr(Target *target) noexcept; - - SharedPtr(const SharedPtr &shared_ptr) noexcept; - - SharedPtr(SharedPtr &&shared_ptr) noexcept; - - ~SharedPtr() noexcept; - - [[nodiscard]] unsigned int reference_cnt() const noexcept; - - [[nodiscard]] bool is_disposable() const noexcept; - - SharedPtr &operator=(const SharedPtr &rhs) noexcept; - - SharedPtr &operator=(SharedPtr &&rhs) noexcept; - - Target &operator*() const noexcept; - Target *operator->() const noexcept; - -private: - Target *_target = nullptr; - - unsigned int *_reference_cnt; -}; - -template -SharedPtr make_shared(Args &&...args) noexcept; - -} // namespace common - -#include "shared_ptr.tpp" diff --git a/src/common/memory/shared_ptr.tpp b/src/common/memory/shared_ptr.tpp deleted file mode 100644 index d9b57c3..0000000 --- a/src/common/memory/shared_ptr.tpp +++ /dev/null @@ -1,153 +0,0 @@ -#pragma once - -#include "shared_ptr.hpp" - -#include "common/memory.hpp" - -#include - -namespace common -{ - -template -SharedPtr::SharedPtr() noexcept - : _reference_cnt(malloc_s(sizeof(unsigned int))) -{ - (*_reference_cnt) = 0; -} - -template -SharedPtr::SharedPtr(nullptr_t) noexcept - : _reference_cnt(malloc_s(sizeof(unsigned int))) -{ - (*_reference_cnt) = 0; -} - -template -SharedPtr::SharedPtr(Target *target) noexcept - : _target(target), _reference_cnt(malloc_s(sizeof(unsigned int))) -{ - (*_reference_cnt) = 0; -} - -/** - * Copy constructor - */ -template -SharedPtr::SharedPtr(const SharedPtr &shared_ptr) noexcept - : _target(shared_ptr._target), _reference_cnt(shared_ptr._reference_cnt) -{ - (*_reference_cnt)++; -} - -/** - * Move constructor - */ -template -SharedPtr::SharedPtr(SharedPtr &&shared_ptr) noexcept - : _target(shared_ptr._target), _reference_cnt(shared_ptr._reference_cnt) -{ - shared_ptr._target = nullptr; -} - -template -SharedPtr::~SharedPtr() noexcept -{ - if ((*_reference_cnt) != 0U) - { - (*_reference_cnt)--; - } - - if ((*_reference_cnt) == 0U) - { - delete _target; - free(_reference_cnt); - _reference_cnt = nullptr; - } -} - -template -unsigned int SharedPtr::reference_cnt() const noexcept -{ - if (_reference_cnt == nullptr) - { - return 0; - } - - return *_reference_cnt; -} - -template -bool SharedPtr::is_disposable() const noexcept -{ - return _reference_cnt == nullptr || (*_reference_cnt) == 0U; -} - -/** - * Copy assignment operator - */ -template -SharedPtr &SharedPtr::operator=(const SharedPtr &rhs) noexcept -{ - if (&rhs != this) - { - if (is_disposable()) - { - delete _target; - free(_reference_cnt); - } - - _target = nullptr; - _target = rhs._target; - - _reference_cnt = nullptr; - _reference_cnt = rhs._reference_cnt; - (*_reference_cnt)++; - } - - return *this; -} - -/** - * Move assignment operator - */ -template -SharedPtr &SharedPtr::operator=(SharedPtr &&rhs) noexcept -{ - if (&rhs != this) - { - if (is_disposable()) - { - delete _target; - free(_reference_cnt); - } - - _target = rhs._target; - rhs._target = nullptr; - - _reference_cnt = rhs._reference_cnt; - rhs._reference_cnt = nullptr; - } - - return *this; -} - -template -Target &SharedPtr::operator*() const noexcept -{ - return *(_target); -} - -template -Target *SharedPtr::operator->() const noexcept -{ - return _target; -} - -template -SharedPtr make_shared(Args &&...args) noexcept -{ - return SharedPtr(new Target(args...)); -} - -} // namespace common diff --git a/src/common/memory/unique_ptr.hpp b/src/common/memory/unique_ptr.hpp deleted file mode 100644 index 94c02d0..0000000 --- a/src/common/memory/unique_ptr.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -namespace common -{ -template -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 -UniquePtr make_unique(Args... args); -} // namespace common - -#include "unique_ptr.tpp" diff --git a/src/common/memory/unique_ptr.tpp b/src/common/memory/unique_ptr.tpp deleted file mode 100644 index 3cab9ec..0000000 --- a/src/common/memory/unique_ptr.tpp +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include "unique_ptr.hpp" - -namespace common -{ -template -UniquePtr::UniquePtr(Target *target) : _target(target) -{ -} - -template -UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept - : _target(unique_ptr._target) -{ - unique_ptr._target = nullptr; -} - -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() -{ - delete _target; -} - -template -Target UniquePtr::operator*() const -{ - return *(_target); -} - -template -Target *UniquePtr::operator->() const -{ - return _target; -} - -template -UniquePtr make_unique(Args... args) -{ - return UniquePtr(new Target(args...)); -} -} // namespace common diff --git a/src/common/string.cpp b/src/common/string.cpp deleted file mode 100644 index 27b65d4..0000000 --- a/src/common/string.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "string.hpp" - -#include "common/memory.hpp" - -#include - -namespace common -{ - -String::String(char *c_string) : c_str(c_string) -{ -} - -String::String(unsigned int size) : c_str(malloc_s(size + 1)) -{ -} - -String::String(const String &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); -} - -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(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 deleted file mode 100644 index 2641457..0000000 --- a/src/common/string.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#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 deleted file mode 100644 index ee297d7..0000000 --- a/src/common/time.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "time.hpp" - -#include - -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(_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()); - } - -} // namespace common diff --git a/src/common/time.hpp b/src/common/time.hpp deleted file mode 100644 index 8efbd28..0000000 --- a/src/common/time.hpp +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once - -#include - -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 -- cgit v1.2.3-18-g5258