OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fixed_string.hh
Go to the documentation of this file.
1 #ifndef OST_SMALL_STRING_HH
2 #define OST_SMALL_STRING_HH
3 
4 #include <ost/base.hh>
5 #include <cassert>
6 #include <string.h>
7 
8 namespace ost {
9 
12 template <size_t LENGTH>
13 class TEMPLATE_EXPORT FixedString {
14 public:
16  ::memset(bytes_, 0, LENGTH+1);
17  }
18 
19  explicit FixedString(const String& str) {
20  this->assign(str);
21  }
22 
23  explicit FixedString(const char* str) {
24  this->assign(str);
25  }
26 
27 
28  size_t length() const {
29  return strlen(bytes_);
30  }
31 
32  size_t size() const {
33  return this->length();
34  }
35 
36  char operator[](size_t index) const {
37  return bytes_[index];
38  }
39 
40  char& operator[](size_t index) {
41  return bytes_[index];
42  }
43 
44  bool operator==(const String& rhs) const {
45  return !strcmp(bytes_, rhs.c_str());
46  }
47 
48  bool operator!=(const String& rhs) const {
49  return !this->operator==(rhs);
50  }
51  bool operator==(const FixedString<LENGTH>& rhs) const {
52  return !strcmp(bytes_, rhs.bytes_);
53  }
54 
55  bool operator!=(const FixedString<LENGTH>& rhs) const {
56  return !this->operator==(rhs);
57  }
58  size_t capacity() const {
59  return LENGTH;
60  }
62  this->assign(rhs);
63  return *this;
64  }
65 
66  template <typename DS>
67  void Serialize(DS& ds) {
68  RawSerialize(ds, bytes_, LENGTH);
69  }
70  const char* c_str() const {
71  return bytes_;
72  }
73  char* data() { return bytes_; }
74  const char* data() const { return bytes_; }
75 private:
76  void assign(const String& str) {
77  assert(str.length()<=LENGTH);
78  strcpy(bytes_, str.c_str());
79  }
80 
81  void assign(const char* str) {
82  assert(strlen(str)<=LENGTH);
83  strcpy(bytes_, str);
84  }
85  char bytes_[LENGTH+1];
86 };
87 
88 }
89 
90 #endif
size_t length() const
Definition: fixed_string.hh:28
size_t size() const
Definition: fixed_string.hh:32
string class that uses an array of static size to hold the characters
Definition: fixed_string.hh:13
std::string String
Definition: base.hh:54
bool operator!=(const FixedString< LENGTH > &rhs) const
Definition: fixed_string.hh:55
size_t capacity() const
Definition: fixed_string.hh:58
bool DLLEXPORT_OST_GEOM operator==(const Line2 &l1, const Line2 &l2)
const char * data() const
Definition: fixed_string.hh:74
bool operator==(const FixedString< LENGTH > &rhs) const
Definition: fixed_string.hh:51
bool operator!=(const String &rhs) const
Definition: fixed_string.hh:48
FixedString(const String &str)
Definition: fixed_string.hh:19
void Serialize(DS &ds)
Definition: fixed_string.hh:67
const char * c_str() const
Definition: fixed_string.hh:70
char operator[](size_t index) const
Definition: fixed_string.hh:36
bool operator==(const String &rhs) const
Definition: fixed_string.hh:44
void RawSerialize(BinaryDataSink &sink, char *value, size_t size)
FixedString< LENGTH > & operator=(const String &rhs)
Definition: fixed_string.hh:61
char & operator[](size_t index)
Definition: fixed_string.hh:40
FixedString(const char *str)
Definition: fixed_string.hh:23