simple_string.hpp
Go to the documentation of this file.
1 #ifndef SIMPLE_STRING_HPP
2 #define SIMPLE_STRING_HPP
3 
4 #include <string>
5 #include <cstring>
6 
7 namespace SafeAny
8 {
9 // Version of string that uses only two words. Good for small object optimization in linb::any
11 {
12  public:
13  SimpleString(const std::string& str) : SimpleString(str.data(), str.size())
14  {
15  }
16  SimpleString(const char* input_data) : SimpleString(input_data, strlen(input_data))
17  {
18  }
19 
20  SimpleString(const char* input_data, std::size_t size) : _size(size)
21  {
22  if(size >= sizeof(void*) )
23  {
24  _data.ptr = new char[_size + 1];
25  }
26  std::memcpy(data(), input_data, _size);
27  data()[_size] = '\0';
28  }
29 
30  SimpleString(const SimpleString& other) : SimpleString(other.data(), other.size())
31  {
32  }
33 
35  {
36  _data = other._data;
37  _size = other._size;
38  return *this;
39  }
40 
42  {
43  if ( _size >= sizeof(void*) && _data.ptr )
44  {
45  delete[] _data.ptr;
46  }
47  }
48 
49  std::string toStdString() const
50  {
51  return std::string(data(), _size);
52  }
53 
54  const char* data() const
55  {
56  if( _size >= sizeof(void*))
57  {
58  return _data.ptr;
59  }
60  else{
61  return _data.soo;
62  }
63  }
64 
65  char* data()
66  {
67  if( _size >= sizeof(void*))
68  {
69  return _data.ptr;
70  }
71  else{
72  return _data.soo;
73  }
74  }
75 
76  std::size_t size() const
77  {
78  return _size;
79  }
80 
81  private:
82  union{
83  char* ptr;
84  char soo[sizeof(void*)] ;
85  }_data;
86 
87  std::size_t _size;
88 };
89 
90 }
91 
92 #endif // SIMPLE_STRING_HPP
SimpleString(const char *input_data, std::size_t size)
char soo[sizeof(void *)]
SimpleString & operator=(const SimpleString &other)
SimpleString(const std::string &str)
SimpleString(const char *input_data)
SimpleString(const SimpleString &other)
union SafeAny::SimpleString::@2 _data
std::string toStdString() const
std::size_t size() const
const char * data() const


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:25