diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-15 12:33:52 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-02-15 12:33:52 +0100 |
commit | bcdce9633dc351d3bc7f347a165348b8fab87cd9 (patch) | |
tree | 88c2f5d8f0c5fac2bca28e4b543e5209f3bc98fb /src/std/smart_string.cpp | |
parent | 917adc6a2b6b166e37fc3d4f94b41488f0c245a5 (diff) |
refactor: reorganize files & improve classes
Diffstat (limited to 'src/std/smart_string.cpp')
-rw-r--r-- | src/std/smart_string.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
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 <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; +} |