diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-14 18:18:38 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-02-14 18:18:38 +0100 |
commit | 01ce0af940bd69c94a2fac8b65219262845cca98 (patch) | |
tree | 97c443782ce1cfba90b205183c8aab1e3edb0bb3 /src/utils/smart_string.cpp | |
parent | cb7a167c7dee2fa1a19bd09ede3bae8b140e79da (diff) |
refactor: clean sewage
Diffstat (limited to 'src/utils/smart_string.cpp')
-rw-r--r-- | src/utils/smart_string.cpp | 53 |
1 files changed, 46 insertions, 7 deletions
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 <stdlib.h> -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<char>(size + 1)) { - this->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 (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; } |