3#include <boost/container/container_fwd.hpp>
4#include <boost/interprocess/offset_ptr.hpp>
5#include <boost/interprocess/allocators/allocator.hpp>
25 using allocator_type = bip::allocator<char, pinnable_mapped_file::segment_manager>;
29 template<
typename Iter>
31 std::size_t
size = std::distance(begin, end);
32 impl* new_data = (impl*)&*_alloc.allocate(
sizeof(impl) +
size + 1);
33 new_data->reference_count = 1;
34 new_data->size =
size;
35 std::copy(begin, end, new_data->data);
36 new_data->data[
size] =
'\0';
40 impl* new_data = (impl*)&*_alloc.allocate(
sizeof(impl) +
size + 1);
41 new_data->reference_count = 1;
42 new_data->size =
size;
43 std::memcpy(new_data->data, ptr,
size);
44 new_data->data[
size] =
'\0';
48 impl* new_data = (impl*)&*_alloc.allocate(
sizeof(impl) +
size + 1);
49 new_data->reference_count = 1;
50 new_data->size =
size;
51 new_data->data[
size] =
'\0';
55 if(_data !=
nullptr) {
56 ++_data->reference_count;
60 other._data =
nullptr;
70 other._data =
nullptr;
77 void resize(std::size_t new_size, boost::container::default_init_t) {
78 impl* new_data = (impl*)&*_alloc.allocate(
sizeof(impl) + new_size + 1);
79 new_data->reference_count = 1;
80 new_data->size = new_size;
81 new_data->data[new_size] =
'\0';
87 resize(new_size, boost::container::default_init);
88 static_cast<F&&
>(
f)(_data->data, new_size);
91 impl* new_data = (impl*)&*_alloc.allocate(
sizeof(impl) +
size + 1);
92 new_data->reference_count = 1;
93 new_data->size =
size;
95 std::memcpy(new_data->data, ptr,
size);
96 new_data->data[
size] =
'\0';
104 if (_data)
return _data->data;
108 if (_data)
return _data->size;
113 if (_data)
return _data->data + _data->size;
116 int compare(std::size_t start, std::size_t
count,
const char* other, std::size_t other_size)
const {
117 std::size_t sz =
size();
118 if(start > sz) BOOST_THROW_EXCEPTION(std::out_of_range{
"shared_cow_string::compare"});
120 std::size_t cmp_len = std::min(
count, other_size);
121 const char* start_ptr =
data() + start;
122 int result = std::char_traits<char>::compare(start_ptr, other, cmp_len);
123 if (result != 0)
return result;
124 else if (
count < other_size)
return -1;
125 else if(
count > other_size)
return 1;
134 void dec_refcount() {
135 if(_data && --_data->reference_count == 0) {
139 bip::offset_ptr<impl> _data;
bool operator==(const shared_cow_string &rhs) const
shared_cow_string & operator=(const shared_cow_string &other)
shared_cow_string(std::size_t size, boost::container::default_init_t, const allocator_type &alloc)
const_iterator begin() const
bip::allocator< char, pinnable_mapped_file::segment_manager > allocator_type
shared_cow_string(Iter begin, Iter end, const allocator_type &alloc)
const allocator_type & get_allocator() const
const_iterator end() const
void assign(const unsigned char *ptr, std::size_t size)
const char * const_iterator
shared_cow_string(const shared_cow_string &other)
void resize_and_fill(std::size_t new_size, F &&f)
void assign(const char *ptr, std::size_t size)
shared_cow_string(const char *ptr, std::size_t size, const allocator_type &alloc)
int compare(std::size_t start, std::size_t count, const char *other, std::size_t other_size) const
const char * data() const
shared_cow_string(const allocator_type &alloc)
void resize(std::size_t new_size, boost::container::default_init_t)
shared_cow_string & operator=(shared_cow_string &&other)
bool operator!=(const shared_cow_string &rhs) const
shared_cow_string(shared_cow_string &&other)