simple_string.hpp
Go to the documentation of this file.
00001 #ifndef SIMPLE_STRING_HPP
00002 #define SIMPLE_STRING_HPP
00003 
00004 #include <string>
00005 #include <cstring>
00006 
00007 namespace SafeAny
00008 {
00009 // Version of string that uses only two words. Good for small object optimization in linb::any
00010 class SimpleString
00011 {
00012   public:
00013     SimpleString(const std::string& str) : SimpleString(str.data(), str.size())
00014     {
00015     }
00016     SimpleString(const char* input_data) : SimpleString(input_data, strlen(input_data))
00017     {
00018     }
00019 
00020     SimpleString(const char* input_data, std::size_t size) : _size(size)
00021     {
00022         if(size >= sizeof(void*) )
00023         {
00024             _data.ptr = new char[_size + 1];
00025         }
00026         std::memcpy(data(), input_data, _size);
00027         data()[_size] = '\0';
00028     }
00029 
00030     SimpleString(const SimpleString& other) : SimpleString(other.data(), other.size())
00031     {
00032     }
00033 
00034     ~SimpleString()
00035     {
00036         if ( _size >= sizeof(void*) && _data.ptr )
00037         {
00038             delete[] _data.ptr;
00039         }
00040     }
00041 
00042     std::string toStdString() const
00043     {
00044         return std::string(data(), _size);
00045     }
00046 
00047     const char* data() const
00048     {
00049         if( _size >= sizeof(void*))
00050         {
00051             return _data.ptr;
00052         }
00053         else{
00054             return _data.soo;
00055         }
00056     }
00057 
00058     char* data()
00059     {
00060         if( _size >= sizeof(void*))
00061         {
00062             return _data.ptr;
00063         }
00064         else{
00065             return _data.soo;
00066         }
00067     }
00068 
00069     std::size_t size() const
00070     {
00071         return _size;
00072     }
00073 
00074   private:
00075     union{
00076         char*  ptr;
00077         char   soo[sizeof(void*)] ;
00078     }_data;
00079 
00080     std::size_t _size;
00081 };
00082 
00083 }
00084 
00085 #endif   // SIMPLE_STRING_HPP


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 20:17:15