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* data) : SimpleString(data, strlen(data))
00017     {
00018     }
00019 
00020     SimpleString(const char* data, std::size_t size) : _size(size)
00021     {
00022         _data = new char[_size + 1];
00023         strncpy(_data, data, _size);
00024         _data[_size] = '\0';
00025     }
00026 
00027     SimpleString(const SimpleString& other) : SimpleString(other.data(), other.size())
00028     {
00029     }
00030 
00031     ~SimpleString()
00032     {
00033         if (_data)
00034         {
00035             delete[] _data;
00036         }
00037     }
00038 
00039     std::string toStdString() const
00040     {
00041         return std::string(_data, _size);
00042     }
00043 
00044     const char* data() const
00045     {
00046         return _data;
00047     }
00048 
00049     std::size_t size() const
00050     {
00051         return _size;
00052     }
00053 
00054   private:
00055     char* _data;
00056     std::size_t _size;
00057 };
00058 }
00059 
00060 #endif   // SIMPLE_STRING_HPP


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10