TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_fixed_string.hpp
1 
9 #ifndef TurtleBrains_FixedString_hpp
10 #define TurtleBrains_FixedString_hpp
11 
12 #include <turtle_brains/core/tb_configuration.hpp>
13 #include <turtle_brains/core/tb_string.hpp>
14 #include <turtle_brains/core/tb_error.hpp>
15 
16 #include <cstring>
17 
18 namespace TurtleBrains
19 {
20  namespace Core
21  {
22  namespace Unstable
23  {
24 
25  template <size_t Size> class FixedString
26  {
27  public:
28  FixedString(void) :
29  mData{ '\0', }
30  {
31  }
32 
33  FixedString(const char* value) :
34  mData{ '\0', }
35  {
36  strncpy(mData, value, Size - 1);
37  }
38 
39  FixedString(const tbCore::tbString& value) :
40  mData{ '\0', }
41  {
42  strncpy(mData, value.c_str(), Size - 1);
43  }
44 
45  FixedString(const FixedString& value) :
46  mData{ '\0', }
47  {
48  strncpy(mData, value.mData, Size - 1);
49  }
50 
51  bool operator==(const FixedString& other) const { return 0 == strcmp(mData, other.mData); }
52  bool operator==(const char* other) const { return 0 == strcmp(mData, other); }
53  bool operator==(const tbCore::tbString& other) const { return 0 == strcmp(mData, other.c_str()); }
54  bool operator!=(const FixedString& other) const { return 0 != strcmp(mData, other.mData); }
55  bool operator!=(const char* other) const { return 0 != strcmp(mData, other); }
56  bool operator!=(const tbCore::tbString& other) const { return 0 != strcmp(mData, other.c_str()); }
57 
58  inline operator tbCore::tbString(void) const
59  {
60  return tbCore::tbString(mData);
61  }
62 
63  bool empty(void) const { return strlen(mData) == 0; }
64  size_t size(void) const { return strlen(mData); }
65  size_t FixedSize(void) const { return Size; }
66 
67  const char* data(void) const { return mData; }
68  char* data(void) { return mData; }
69 
70  const char* c_str(void) const { return mData; }
71 
72  private:
73  char mData[Size];
74  };
75 
76  template <size_t Size> std::ostream& operator<<(std::ostream& outputStream, const FixedString<Size>& fixed)
77  {
78  outputStream << fixed.data();
79  return outputStream;
80  }
81 
82  template <size_t Size> std::istream& operator<<(std::istream& inputStream, FixedString<Size>& fixed)
83  {
84  tbCore::tbString buffer;
85  inputStream >> buffer;
86  fixed = buffer;
87  return inputStream;
88  }
89 
90  template <size_t Size> bool operator==(const char* left, const FixedString<Size>& right) { return 0 == strcmp(right.c_str(), left); }
91  template <size_t Size> bool operator==(const tbCore::tbString& left, const FixedString<Size>& right) { return 0 == strcmp(right.c_str(), left.c_str()); }
92  template <size_t Size> bool operator!=(const char* left, const FixedString<Size>& right) { return 0 != strcmp(right.c_str(), left); }
93  template <size_t Size> bool operator!=(const tbCore::tbString& left, const FixedString<Size>& right) { return 0 != strcmp(right.c_str(), left.c_str()); }
94 
95  }; /* namespace Unstable */
96  }; /* namespace Core */
97 }; /* namespace TurtleBrains */
98 
99 namespace tbCore = TurtleBrains::Core;
100 
101 #endif /* TurtleBrains_FixedString_hpp */
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:21
Definition: tb_fixed_string.hpp:25
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:88
std::string tbString
Definition: tb_string.hpp:335