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_;
104 template <typename CharT, typename Traits = std::char_traits<CharT>>
106 : public basic_inline_string_base<CharT, Traits> {
108 using traits_type = Traits;
109 using value_type = CharT;
110 using size_type = std::size_t;
111 using difference_type = std::ptrdiff_t;
112 using reference = value_type &;
113 using const_reference =
const value_type &;
114 using pointer = value_type *;
115 using const_pointer =
const value_type *;
116 using basic_inline_string_base<CharT, Traits>::operator=;
119 : basic_inline_string_base<CharT, Traits>(
v)
124 : basic_inline_string_base<CharT, Traits>(capacity)
128 : basic_inline_string_base<CharT, Traits>(rhs)
136 static_cast<const basic_inline_string_base<
137 CharT, Traits
> &>(rhs)));
161 template <
typename CharT,
typename Traits = std::
char_traits<CharT>>
164 using traits_type = Traits;
165 using value_type = CharT;
166 using size_type = std::size_t;
167 using difference_type = std::ptrdiff_t;
168 using reference = value_type &;
169 using const_reference =
const value_type &;
170 using pointer = value_type *;
171 using const_pointer =
const value_type *;
172 using basic_inline_string_base<CharT, Traits>::operator=;
178 : basic_inline_string_base<CharT, Traits>(check_forward(
v))
186 : basic_inline_string_base<CharT, Traits>(check_forward(capacity))
194 : basic_inline_string_base<CharT, Traits>(check_forward(rhs))
202 static_cast<const basic_inline_string_base<
203 CharT, Traits
> &>(rhs)));
207 template <
typename T>
211 if (
nullptr == pmemobj_pool_by_ptr(
this))
213 return std::forward<T>(t);
264 template <
typename CharT,
typename Traits>
265 basic_inline_string_base<CharT, Traits>::basic_inline_string_base(
267 : size_(
v.size()), capacity_(
v.size())
269 std::copy(
v.data(),
v.data() +
static_cast<ptrdiff_t
>(size_), data());
271 data()[
static_cast<ptrdiff_t
>(size_)] =
'\0';
277 template <
typename CharT,
typename Traits>
278 basic_inline_string_base<CharT, Traits>::basic_inline_string_base(
280 : size_(0), capacity_(capacity)
282 data()[
static_cast<ptrdiff_t
>(size_)] =
'\0';
288 template <
typename CharT,
typename Traits>
289 basic_inline_string_base<CharT, Traits>::basic_inline_string_base(
290 const basic_inline_string_base &rhs)
291 : size_(rhs.size()), capacity_(rhs.capacity())
293 std::copy(rhs.data(), rhs.data() +
static_cast<ptrdiff_t
>(size_),
296 data()[
static_cast<ptrdiff_t
>(size_)] =
'\0';
302 template <
typename CharT,
typename Traits>
303 basic_inline_string_base<CharT, Traits> &
304 basic_inline_string_base<CharT, Traits>::operator=(
305 const basic_inline_string_base &rhs)
316 template <
typename CharT,
typename Traits>
317 basic_inline_string_base<CharT, Traits> &
318 basic_inline_string_base<CharT, Traits>::operator=(
319 basic_string_view<CharT, Traits> rhs)
325 template <
typename CharT,
typename Traits>
326 basic_inline_string_base<CharT, Traits>::operator basic_string_view<
327 CharT, Traits>()
const
329 return {data(), size()};
333 template <
typename CharT,
typename Traits>
334 typename basic_inline_string_base<CharT, Traits>::size_type
335 basic_inline_string_base<CharT, Traits>::size() const noexcept
347 template <
typename CharT,
typename Traits>
348 typename basic_inline_string_base<CharT, Traits>::size_type
349 basic_inline_string_base<CharT, Traits>::capacity() const noexcept
363 template <
typename CharT,
typename Traits>
364 typename basic_inline_string_base<CharT, Traits>::pointer
365 basic_inline_string_base<CharT, Traits>::data()
367 return snapshotted_data(0, size_);
371 template <
typename CharT,
typename Traits>
372 typename basic_inline_string_base<CharT, Traits>::const_pointer
373 basic_inline_string_base<CharT, Traits>::data() const noexcept
385 template <
typename CharT,
typename Traits>
386 typename basic_inline_string_base<CharT, Traits>::const_pointer
387 basic_inline_string_base<CharT, Traits>::cdata() const noexcept
389 return reinterpret_cast<const CharT *
>(
this + 1);
400 template <
typename CharT,
typename Traits>
402 basic_inline_string_base<CharT, Traits>::compare(
403 basic_string_view<CharT, Traits> rhs)
const noexcept
405 return basic_string_view<CharT, Traits>(data(), size()).compare(rhs);
416 template <
typename CharT,
typename Traits>
417 typename basic_inline_string_base<CharT, Traits>::reference
418 basic_inline_string_base<CharT, Traits>::operator[](size_type p)
420 return snapshotted_data(p, 1)[0];
429 template <
typename CharT,
typename Traits>
430 typename basic_inline_string_base<CharT, Traits>::const_reference
431 basic_inline_string_base<CharT, Traits>::operator[](size_type p)
const
446 template <
typename CharT,
typename Traits>
447 typename basic_inline_string_base<CharT, Traits>::reference
448 basic_inline_string_base<CharT, Traits>::at(size_type p)
451 throw std::out_of_range(
"basic_inline_string_base::at");
453 return snapshotted_data(p, 1)[0];
464 template <
typename CharT,
typename Traits>
465 typename basic_inline_string_base<CharT, Traits>::const_reference
466 basic_inline_string_base<CharT, Traits>::at(size_type p)
const
469 throw std::out_of_range(
"basic_inline_string_base::at");
487 template <
typename CharT,
typename Traits>
488 slice<typename basic_inline_string_base<CharT, Traits>::pointer>
489 basic_inline_string_base<CharT, Traits>::range(size_type start, size_type n)
491 if (start + n > size())
492 throw std::out_of_range(
"basic_inline_string_base::range");
494 auto data = snapshotted_data(start, n);
496 return {data, data + n};
503 template <
typename CharT,
typename Traits>
504 typename basic_inline_string_base<CharT, Traits>::pointer
505 basic_inline_string_base<CharT, Traits>::snapshotted_data(
size_t p,
size_t n)
507 assert(p + n <= size());
510 n, POBJ_XADD_ASSUME_INITIALIZED);
512 return reinterpret_cast<CharT *
>(
this + 1) + p;
521 template <
typename CharT,
typename Traits>
522 basic_inline_string_base<CharT, Traits> &
523 basic_inline_string_base<CharT, Traits>::assign(
524 basic_string_view<CharT, Traits> rhs)
526 auto cpop = pmemobj_pool_by_ptr(
this);
530 auto pop = pool_base(cpop);
532 if (rhs.size() > capacity())
533 throw std::out_of_range(
"inline_string capacity exceeded.");
535 auto initialized_mem =
536 (std::min)(rhs.size(), size()) + 1 ;
541 if (rhs.size() > size())
543 data() + initialized_mem,
544 rhs.size() - initialized_mem + 1,
545 POBJ_XADD_NO_SNAPSHOT);
547 std::copy(rhs.data(),
548 rhs.data() +
static_cast<ptrdiff_t
>(rhs.size()),
552 data()[
static_cast<ptrdiff_t
>(size_)] =
'\0';
564 template <
typename T>
566 template <
typename... Args>
568 value(
const Args &... args)
581 template <
typename CharT,
typename Traits>
586 return sizeof(basic_inline_string_base<CharT, Traits>) +
587 (s.
size() + 1 ) *
sizeof(CharT);
598 template <
typename CharT,
typename Traits>
604 (s.
size() + 1 ) *
sizeof(CharT);
615 struct is_inline_string : std::false_type {
618 template <
typename CharT,
typename Traits>
619 struct is_inline_string<obj::experimental::basic_inline_string<CharT, Traits>>
623 template <
typename CharT,
typename Traits>
624 struct is_inline_string<
625 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:397
Pmem/DRAM variation of pmem::obj::string, where data is kept right next to the inline_string structur...
Definition: inline_string.hpp:106
Pmem-only variation of pmem::obj::string, where data is kept right next to the inline_string structur...
Definition: inline_string.hpp:162
basic_inline_string(basic_string_view< CharT, Traits > v)
Definition: inline_string.hpp:177
basic_inline_string(const basic_inline_string &rhs)
Definition: inline_string.hpp:193
basic_inline_string(size_type capacity)
Definition: inline_string.hpp:185
Volatile residing on pmem class.
Definition: v.hpp:43
static void run(obj::pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:810
Custom pool error class.
Definition: pexceptions.hpp:84
persistent_ptr transactional allocation functions for objects.
void conditional_add_to_tx(const T *that, std::size_t count=1, uint64_t flags=0)
Conditionally add 'count' objects to a transaction.
Definition: common.hpp:176
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:565
C++ pmemobj transactions.