9 #ifndef LIBPMEMOBJ_CPP_INLINE_STRING_HPP
10 #define LIBPMEMOBJ_CPP_INLINE_STRING_HPP
26 namespace experimental
29 template <
typename CharT,
typename Traits = std::
char_traits<CharT>>
30 class basic_inline_string_base {
32 using traits_type = Traits;
33 using value_type = CharT;
34 using size_type = std::size_t;
35 using difference_type = std::ptrdiff_t;
36 using reference = value_type &;
37 using const_reference =
const value_type &;
38 using pointer = value_type *;
39 using const_pointer =
const value_type *;
41 basic_inline_string_base(basic_string_view<CharT, Traits> v);
42 basic_inline_string_base(size_type capacity);
43 basic_inline_string_base(
const basic_inline_string_base &rhs);
45 basic_inline_string_base &
46 operator=(
const basic_inline_string_base &rhs);
48 basic_inline_string_base &
49 operator=(basic_string_view<CharT, Traits> rhs);
51 basic_inline_string_base(basic_inline_string_base<CharT> &&) =
delete;
53 basic_inline_string_base &
54 operator=(basic_inline_string_base &&) =
delete;
55 operator basic_string_view<CharT, Traits>()
const;
57 size_type size() const noexcept;
58 size_type capacity() const noexcept;
61 const_pointer data() const noexcept;
62 const_pointer cdata() const noexcept;
64 int compare(basic_string_view<CharT, Traits> rhs) const noexcept;
66 reference operator[](size_type p);
67 const_reference operator[](size_type p) const noexcept;
69 reference at(size_type p);
70 const_reference at(size_type p) const;
72 slice<pointer> range(size_type p, size_type count);
74 basic_inline_string_base &assign(basic_string_view<CharT, Traits> rhs);
77 pointer snapshotted_data(
size_t p,
size_t n);
79 obj::p<uint64_t> size_;
80 obj::p<uint64_t> capacity_;
100 template <typename CharT, typename Traits = std::char_traits<CharT>>
102 : public basic_inline_string_base<CharT, Traits> {
104 using traits_type = Traits;
105 using value_type = CharT;
106 using size_type = std::size_t;
107 using difference_type = std::ptrdiff_t;
108 using reference = value_type &;
109 using const_reference =
const value_type &;
110 using pointer = value_type *;
111 using const_pointer =
const value_type *;
112 using basic_inline_string_base<CharT, Traits>::operator=;
115 : basic_inline_string_base<CharT, Traits>(
v)
120 : basic_inline_string_base<CharT, Traits>(capacity)
124 : basic_inline_string_base<CharT, Traits>(rhs)
132 static_cast<const basic_inline_string_base<
133 CharT, Traits
> &>(rhs)));
153 template <
typename CharT,
typename Traits = std::
char_traits<CharT>>
156 using traits_type = Traits;
157 using value_type = CharT;
158 using size_type = std::size_t;
159 using difference_type = std::ptrdiff_t;
160 using reference = value_type &;
161 using const_reference =
const value_type &;
162 using pointer = value_type *;
163 using const_pointer =
const value_type *;
164 using basic_inline_string_base<CharT, Traits>::operator=;
170 : basic_inline_string_base<CharT, Traits>(check_forward(
v))
178 : basic_inline_string_base<CharT, Traits>(check_forward(capacity))
186 : basic_inline_string_base<CharT, Traits>(check_forward(rhs))
194 static_cast<const basic_inline_string_base<
195 CharT, Traits
> &>(rhs)));
199 template <
typename T>
203 if (
nullptr == pmemobj_pool_by_ptr(
this))
205 return std::forward<T>(t);
209 using dram_inline_string = basic_dram_inline_string<char>;
210 using dram_inline_wstring = basic_dram_inline_string<wchar_t>;
211 using dram_inline_u16string = basic_dram_inline_string<char16_t>;
212 using dram_inline_u32string = basic_dram_inline_string<char32_t>;
214 using inline_string = basic_inline_string<char>;
215 using inline_wstring = basic_inline_string<wchar_t>;
216 using inline_u16string = basic_inline_string<char16_t>;
217 using inline_u32string = basic_inline_string<char32_t>;
222 template <
typename CharT,
typename Traits>
223 basic_inline_string_base<CharT, Traits>::basic_inline_string_base(
224 basic_string_view<CharT, Traits> v)
225 : size_(v.size()), capacity_(v.size())
227 std::copy(v.data(), v.data() +
static_cast<ptrdiff_t
>(size_), data());
229 data()[
static_cast<ptrdiff_t
>(size_)] =
'\0';
235 template <
typename CharT,
typename Traits>
236 basic_inline_string_base<CharT, Traits>::basic_inline_string_base(
238 : size_(0), capacity_(capacity)
240 data()[
static_cast<ptrdiff_t
>(size_)] =
'\0';
246 template <
typename CharT,
typename Traits>
247 basic_inline_string_base<CharT, Traits>::basic_inline_string_base(
248 const basic_inline_string_base &rhs)
249 : size_(rhs.size()), capacity_(rhs.capacity())
251 std::copy(rhs.data(), rhs.data() +
static_cast<ptrdiff_t
>(size_),
254 data()[
static_cast<ptrdiff_t
>(size_)] =
'\0';
260 template <
typename CharT,
typename Traits>
261 basic_inline_string_base<CharT, Traits> &
262 basic_inline_string_base<CharT, Traits>::operator=(
263 const basic_inline_string_base &rhs)
274 template <
typename CharT,
typename Traits>
275 basic_inline_string_base<CharT, Traits> &
276 basic_inline_string_base<CharT, Traits>::operator=(
277 basic_string_view<CharT, Traits> rhs)
283 template <
typename CharT,
typename Traits>
284 basic_inline_string_base<CharT, Traits>::operator basic_string_view<
285 CharT, Traits>()
const
287 return {data(), size()};
291 template <
typename CharT,
typename Traits>
292 typename basic_inline_string_base<CharT, Traits>::size_type
293 basic_inline_string_base<CharT, Traits>::size() const noexcept
305 template <
typename CharT,
typename Traits>
306 typename basic_inline_string_base<CharT, Traits>::size_type
307 basic_inline_string_base<CharT, Traits>::capacity() const noexcept
321 template <
typename CharT,
typename Traits>
322 typename basic_inline_string_base<CharT, Traits>::pointer
323 basic_inline_string_base<CharT, Traits>::data()
325 return snapshotted_data(0, size_);
329 template <
typename CharT,
typename Traits>
330 typename basic_inline_string_base<CharT, Traits>::const_pointer
331 basic_inline_string_base<CharT, Traits>::data() const noexcept
343 template <
typename CharT,
typename Traits>
344 typename basic_inline_string_base<CharT, Traits>::const_pointer
345 basic_inline_string_base<CharT, Traits>::cdata() const noexcept
347 return reinterpret_cast<const CharT *
>(
this + 1);
358 template <
typename CharT,
typename Traits>
360 basic_inline_string_base<CharT, Traits>::compare(
361 basic_string_view<CharT, Traits> rhs)
const noexcept
363 return basic_string_view<CharT, Traits>(data(), size()).compare(rhs);
374 template <
typename CharT,
typename Traits>
375 typename basic_inline_string_base<CharT, Traits>::reference
376 basic_inline_string_base<CharT, Traits>::operator[](size_type p)
378 return snapshotted_data(p, 1)[0];
387 template <
typename CharT,
typename Traits>
388 typename basic_inline_string_base<CharT, Traits>::const_reference
389 basic_inline_string_base<CharT, Traits>::operator[](size_type p)
const
404 template <
typename CharT,
typename Traits>
405 typename basic_inline_string_base<CharT, Traits>::reference
406 basic_inline_string_base<CharT, Traits>::at(size_type p)
409 throw std::out_of_range(
"basic_inline_string_base::at");
411 return snapshotted_data(p, 1)[0];
422 template <
typename CharT,
typename Traits>
423 typename basic_inline_string_base<CharT, Traits>::const_reference
424 basic_inline_string_base<CharT, Traits>::at(size_type p)
const
427 throw std::out_of_range(
"basic_inline_string_base::at");
445 template <
typename CharT,
typename Traits>
446 slice<typename basic_inline_string_base<CharT, Traits>::pointer>
447 basic_inline_string_base<CharT, Traits>::range(size_type start, size_type n)
449 if (start + n > size())
450 throw std::out_of_range(
"basic_inline_string_base::range");
452 auto data = snapshotted_data(start, n);
454 return {data, data + n};
461 template <
typename CharT,
typename Traits>
462 typename basic_inline_string_base<CharT, Traits>::pointer
463 basic_inline_string_base<CharT, Traits>::snapshotted_data(
size_t p,
size_t n)
465 assert(p + n <= size());
467 detail::conditional_add_to_tx(
reinterpret_cast<CharT *
>(
this + 1) + p,
468 n, POBJ_XADD_ASSUME_INITIALIZED);
470 return reinterpret_cast<CharT *
>(
this + 1) + p;
479 template <
typename CharT,
typename Traits>
480 basic_inline_string_base<CharT, Traits> &
481 basic_inline_string_base<CharT, Traits>::assign(
482 basic_string_view<CharT, Traits> rhs)
484 auto cpop = pmemobj_pool_by_ptr(
this);
488 auto pop = pool_base(cpop);
490 if (rhs.size() > capacity())
491 throw std::out_of_range(
"inline_string capacity exceeded.");
493 auto initialized_mem =
494 (std::min)(rhs.size(), size()) + 1 ;
497 detail::conditional_add_to_tx(data(), initialized_mem);
499 if (rhs.size() > size())
500 detail::conditional_add_to_tx(
501 data() + initialized_mem,
502 rhs.size() - initialized_mem + 1,
503 POBJ_XADD_NO_SNAPSHOT);
505 std::copy(rhs.data(),
506 rhs.data() +
static_cast<ptrdiff_t
>(rhs.size()),
510 data()[
static_cast<ptrdiff_t
>(size_)] =
'\0';
522 template <
typename T>
524 template <
typename... Args>
526 value(
const Args &... args)
539 template <
typename CharT,
typename Traits>
544 return sizeof(basic_inline_string_base<CharT, Traits>) +
545 (s.
size() + 1 ) *
sizeof(CharT);
556 template <
typename CharT,
typename Traits>
562 (s.
size() + 1 ) *
sizeof(CharT);
573 struct is_inline_string : std::false_type {
576 template <
typename CharT,
typename Traits>
577 struct is_inline_string<obj::experimental::basic_inline_string<CharT, Traits>>
581 template <
typename CharT,
typename Traits>
582 struct is_inline_string<
583 obj::experimental::basic_dram_inline_string<CharT, Traits>>
constexpr size_type size() const noexcept
Returns count of characters stored in this pmem::obj::string_view data.
Definition: string_view.hpp:334
This class serves similar purpose to pmem::obj::string, but keeps the data within the same allocation...
Definition: inline_string.hpp:102
This class serves similar purpose to pmem::obj::string, but keeps the data within the same allocation...
Definition: inline_string.hpp:154
basic_inline_string(basic_string_view< CharT, Traits > v)
Definition: inline_string.hpp:169
basic_inline_string(const basic_inline_string &rhs)
Definition: inline_string.hpp:185
basic_inline_string(size_type capacity)
Definition: inline_string.hpp:177
Volatile residing on pmem class.
Definition: v.hpp:42
static void run(obj::pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:823
Custom pool error class.
Definition: pexceptions.hpp:45
Persistent_ptr transactional allocation functions for objects.
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Persistent smart pointer.
Interface to access sequence of objects.
Our partial std::string_view implementation.
A helper trait which calculates required memory capacity (in bytes) for a type.
Definition: inline_string.hpp:523
C++ pmemobj transactions.