From 76f9bd8112563a7f35d843fc5041b4b459015a3e Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 27 Feb 2023 15:13:23 +0100 Subject: [PATCH] luna: Add StaticString, an OOP char[] --- luna/include/luna/StaticString.h | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 luna/include/luna/StaticString.h diff --git a/luna/include/luna/StaticString.h b/luna/include/luna/StaticString.h new file mode 100644 index 00000000..7bdf77be --- /dev/null +++ b/luna/include/luna/StaticString.h @@ -0,0 +1,52 @@ +#pragma once +#include +#include + +template class StaticString +{ + public: + StaticString() = default; + StaticString(const char* string) + { + adopt(string); + } + + void adopt(const char* string) + { + usize length = strlcpy(m_buffer, string, sizeof(m_buffer)); + if (length > Size) { m_length = Size; } + else + m_length = length; + } + + StaticString& operator=(const char* string) + { + adopt(string); + return *this; + } + + template StaticString& operator=(const StaticString& string) + { + if constexpr (OSize == Size) + { + if (this == &string) return *this; + } + + adopt(string.chars()); + return *this; + } + + const char* chars() const + { + return m_buffer; + } + + usize length() const + { + return m_length; + } + + private: + char m_buffer[Size + 1]; + usize m_length { 0 }; +};