00001 #ifndef OST_SMALL_STRING_HH
00002 #define OST_SMALL_STRING_HH
00003
00004 #include <ost/base.hh>
00005 #include <cassert>
00006 #include <string.h>
00007
00008 namespace ost {
00009
00012 template <size_t LENGTH>
00013 class TEMPLATE_EXPORT FixedString {
00014 public:
00015 FixedString() {
00016 ::memset(bytes_, 0, LENGTH+1);
00017 }
00018
00019 explicit FixedString(const String& str) {
00020 this->assign(str);
00021 }
00022
00023 explicit FixedString(const char* str) {
00024 this->assign(str);
00025 }
00026
00027
00028 size_t length() const {
00029 return strlen(bytes_);
00030 }
00031
00032 size_t size() const {
00033 return this->length();
00034 }
00035
00036 char operator[](size_t index) const {
00037 return bytes_[index];
00038 }
00039
00040 char& operator[](size_t index) {
00041 return bytes_[index];
00042 }
00043
00044 bool operator==(const String& rhs) const {
00045 return !strcmp(bytes_, rhs.c_str());
00046 }
00047
00048 bool operator!=(const String& rhs) const {
00049 return !this->operator==(rhs);
00050 }
00051 bool operator==(const FixedString<LENGTH>& rhs) const {
00052 return !strcmp(bytes_, rhs.bytes_);
00053 }
00054
00055 bool operator!=(const FixedString<LENGTH>& rhs) const {
00056 return !this->operator==(rhs);
00057 }
00058
00059 FixedString<LENGTH>& operator=(const String& rhs) {
00060 this->assign(rhs);
00061 return *this;
00062 }
00063
00064 template <typename DS>
00065 void Serialize(DS& ds) {
00066 RawSerialize(ds, bytes_, LENGTH);
00067 }
00068 const char* c_str() const {
00069 return bytes_;
00070 }
00071 private:
00072 void assign(const String& str) {
00073 assert(str.length()<=LENGTH);
00074 strcpy(bytes_, str.c_str());
00075 }
00076
00077 void assign(const char* str) {
00078 assert(strlen(str)<=LENGTH);
00079 strcpy(bytes_, str);
00080 }
00081 char bytes_[LENGTH+1];
00082 };
00083
00084 }
00085
00086 #endif