diff --git a/modules/base/src/CMakeLists.txt b/modules/base/src/CMakeLists.txt index c5c873955166c5bb3d33d6be6ca52cb3d12e2c5f..e0ac7c7cf188f6bc25c2444770ea5aa82301678d 100644 --- a/modules/base/src/CMakeLists.txt +++ b/modules/base/src/CMakeLists.txt @@ -26,6 +26,7 @@ message.hh platform.hh string_ref.hh pod_vector.hh +fixed_string.hh ) module(NAME base SOURCES ${OST_BASE_SOURCES} diff --git a/modules/base/src/fixed_string.hh b/modules/base/src/fixed_string.hh new file mode 100644 index 0000000000000000000000000000000000000000..a7858cf06185d8f716157228c18fb91feb005d56 --- /dev/null +++ b/modules/base/src/fixed_string.hh @@ -0,0 +1,86 @@ +#ifndef OST_SMALL_STRING_HH +#define OST_SMALL_STRING_HH + +#include <ost/base.hh> +#include <cassert> +#include <string.h> + +namespace ost { + +/// \brief string class that uses an array of static size to hold the +/// characters +template <size_t LENGTH> +class TEMPLATE_EXPORT FixedString { +public: + FixedString() { + ::memset(bytes_, 0, LENGTH+1); + } + + explicit FixedString(const String& str) { + this->assign(str); + } + + explicit FixedString(const char* str) { + this->assign(str); + } + + + size_t length() const { + return strlen(bytes_); + } + + size_t size() const { + return this->length(); + } + + char operator[](size_t index) const { + return bytes_[index]; + } + + char& operator[](size_t index) { + return bytes_[index]; + } + + bool operator==(const String& rhs) const { + return !strcmp(bytes_, rhs.c_str()); + } + + bool operator!=(const String& rhs) const { + return !this->operator==(rhs); + } + bool operator==(const FixedString<LENGTH>& rhs) const { + return !strcmp(bytes_, rhs.bytes_); + } + + bool operator!=(const FixedString<LENGTH>& rhs) const { + return !this->operator==(rhs); + } + + FixedString<LENGTH>& operator=(const String& rhs) { + this->assign(rhs); + return *this; + } + + template <typename DS> + void Serialize(DS& ds) { + RawSerialize(ds, bytes_, LENGTH); + } + const char* c_str() const { + return bytes_; + } +private: + void assign(const String& str) { + assert(str.length()<=LENGTH); + strcpy(bytes_, str.c_str()); + } + + void assign(const char* str) { + assert(strlen(str)<=LENGTH); + strcpy(bytes_, str); + } + char bytes_[LENGTH+1]; +}; + +} + +#endif