PMDK C++ bindings  1.12-git53.g67ba2be4
This is the C++ bindings documentation for PMDK's libpmemobj.
inline_string.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2020, Intel Corporation */
3 
9 #ifndef LIBPMEMOBJ_CPP_INLINE_STRING_HPP
10 #define LIBPMEMOBJ_CPP_INLINE_STRING_HPP
11 
14 #include <libpmemobj++/slice.hpp>
17 
18 #include <string>
19 
20 namespace pmem
21 {
22 
23 namespace obj
24 {
25 
26 namespace experimental
27 {
28 
43 template <typename CharT, typename Traits = std::char_traits<CharT>>
45 public:
46  using traits_type = Traits;
47  using value_type = CharT;
48  using size_type = std::size_t;
49  using difference_type = std::ptrdiff_t;
50  using reference = value_type &;
51  using const_reference = const value_type &;
52  using pointer = value_type *;
53  using const_pointer = const value_type *;
54 
56  basic_inline_string(size_type capacity);
58 
60 
62 
65  operator basic_string_view<CharT, Traits>() const;
66 
67  size_type size() const noexcept;
68  size_type capacity() const noexcept;
69 
70  pointer data();
71  const_pointer data() const noexcept;
72  const_pointer cdata() const noexcept;
73 
74  int compare(const basic_inline_string &rhs) const noexcept;
75 
76  reference operator[](size_type p);
77  const_reference operator[](size_type p) const noexcept;
78 
79  reference at(size_type p);
80  const_reference at(size_type p) const;
81 
82  slice<pointer> range(size_type p, size_type count);
83 
85 
86 private:
87  pointer snapshotted_data(size_t p, size_t n);
88 
89  obj::p<uint64_t> size_;
90  obj::p<uint64_t> capacity_;
91 };
92 
97 
103 template <typename CharT, typename Traits>
106  : size_(v.size()), capacity_(v.size())
107 {
108  if (nullptr == pmemobj_pool_by_ptr(this))
109  throw pmem::pool_error("Invalid pool handle.");
110 
111  std::copy(v.data(), v.data() + static_cast<ptrdiff_t>(size_), data());
112 
113  data()[static_cast<ptrdiff_t>(size_)] = '\0';
114 }
115 
121 template <typename CharT, typename Traits>
123  : size_(0), capacity_(capacity)
124 {
125  if (nullptr == pmemobj_pool_by_ptr(this))
126  throw pmem::pool_error("Invalid pool handle.");
127 
128  data()[static_cast<ptrdiff_t>(size_)] = '\0';
129 }
130 
136 template <typename CharT, typename Traits>
138  const basic_inline_string &rhs)
139  : size_(rhs.size()), capacity_(rhs.capacity())
140 {
141  if (nullptr == pmemobj_pool_by_ptr(this))
142  throw pmem::pool_error("Invalid pool handle.");
143 
144  std::copy(rhs.data(), rhs.data() + static_cast<ptrdiff_t>(size_),
145  data());
146 
147  data()[static_cast<ptrdiff_t>(size_)] = '\0';
148 }
149 
153 template <typename CharT, typename Traits>
156 {
157  if (this == &rhs)
158  return *this;
159 
160  return assign(rhs);
161 }
162 
166 template <typename CharT, typename Traits>
170 {
171  return assign(rhs);
172 }
173 
175 template <typename CharT, typename Traits>
177  const
178 {
179  return {data(), size()};
180 }
181 
183 template <typename CharT, typename Traits>
184 typename basic_inline_string<CharT, Traits>::size_type
186 {
187  return size_;
188 }
189 
197 template <typename CharT, typename Traits>
198 typename basic_inline_string<CharT, Traits>::size_type
200 {
201  return capacity_;
202 }
203 
213 template <typename CharT, typename Traits>
214 typename basic_inline_string<CharT, Traits>::pointer
216 {
217  return snapshotted_data(0, size_);
218 }
219 
221 template <typename CharT, typename Traits>
222 typename basic_inline_string<CharT, Traits>::const_pointer
224 {
225  return cdata();
226 }
227 
235 template <typename CharT, typename Traits>
236 typename basic_inline_string<CharT, Traits>::const_pointer
238 {
239  return reinterpret_cast<const CharT *>(this + 1);
240 }
241 
250 template <typename CharT, typename Traits>
251 int
253  const basic_inline_string &rhs) const noexcept
254 {
255  return basic_string_view<CharT, Traits>(data(), size()).compare(rhs);
256 }
257 
266 template <typename CharT, typename Traits>
267 typename basic_inline_string<CharT, Traits>::reference
269 {
270  return snapshotted_data(p, 1)[0];
271 }
272 
279 template <typename CharT, typename Traits>
280 typename basic_inline_string<CharT, Traits>::const_reference
282  noexcept
283 {
284  return cdata()[p];
285 }
286 
296 template <typename CharT, typename Traits>
297 typename basic_inline_string<CharT, Traits>::reference
299 {
300  if (p >= size())
301  throw std::out_of_range("basic_inline_string::at");
302 
303  return snapshotted_data(p, 1)[0];
304 }
305 
314 template <typename CharT, typename Traits>
315 typename basic_inline_string<CharT, Traits>::const_reference
317 {
318  if (p >= size())
319  throw std::out_of_range("basic_inline_string::at");
320 
321  return cdata()[p];
322 }
323 
337 template <typename CharT, typename Traits>
339 basic_inline_string<CharT, Traits>::range(size_type start, size_type n)
340 {
341  if (start + n > size())
342  throw std::out_of_range("basic_inline_string::range");
343 
344  auto data = snapshotted_data(start, n);
345 
346  return {data, data + n};
347 }
348 
353 template <typename CharT, typename Traits>
354 typename basic_inline_string<CharT, Traits>::pointer
356 {
357  assert(p + n <= size());
358 
359  detail::conditional_add_to_tx(reinterpret_cast<CharT *>(this + 1) + p,
360  n, POBJ_XADD_ASSUME_INITIALIZED);
361 
362  return reinterpret_cast<CharT *>(this + 1) + p;
363 }
364 
370 template <typename CharT, typename Traits>
373 {
374  auto pop = obj::pool_base(pmemobj_pool_by_ptr(this));
375 
376  if (rhs.size() > capacity())
377  throw std::out_of_range("inline_string capacity exceeded.");
378 
379  auto initialized_mem =
380  (std::min)(rhs.size(), size()) + 1 /* sizeof('\0') */;
381 
382  obj::flat_transaction::run(pop, [&] {
383  detail::conditional_add_to_tx(data(), initialized_mem);
384 
385  if (rhs.size() > size())
386  detail::conditional_add_to_tx(
387  data() + initialized_mem,
388  rhs.size() - initialized_mem + 1,
389  POBJ_XADD_NO_SNAPSHOT);
390 
391  std::copy(rhs.data(),
392  rhs.data() + static_cast<ptrdiff_t>(rhs.size()),
393  data());
394  size_ = rhs.size();
395 
396  data()[static_cast<ptrdiff_t>(size_)] = '\0';
397  });
398 
399  return *this;
400 }
401 
408 template <typename T>
409 struct total_sizeof {
410  template <typename... Args>
411  static size_t
412  value(const Args &... args)
413  {
414  return sizeof(T);
415  }
416 };
417 
425 template <typename CharT, typename Traits>
426 struct total_sizeof<basic_inline_string<CharT, Traits>> {
427  static size_t
428  value(const basic_string_view<CharT, Traits> &s)
429  {
430  return sizeof(basic_inline_string<CharT, Traits>) +
431  (s.size() + 1 /* '\0' */) * sizeof(CharT);
432  }
433 };
434 } /* namespace experimental */
435 } /* namespace obj */
436 
437 namespace detail
438 {
439 /* Check if type is pmem::obj::basic_inline_string */
440 template <typename>
441 struct is_inline_string : std::false_type {
442 };
443 
444 template <typename CharT, typename Traits>
445 struct is_inline_string<obj::experimental::basic_inline_string<CharT, Traits>>
446  : std::true_type {
447 };
448 } /* namespace detail */
449 
450 } /* namespace pmem */
451 
452 #endif /* LIBPMEMOBJ_CPP_INLINE_STRING_HPP */
pmem::obj::basic_string_view
Our partial std::string_view implementation.
Definition: string_view.hpp:46
pmem::pool_error
Custom pool error class.
Definition: pexceptions.hpp:45
pmem::obj::experimental::basic_inline_string::capacity
size_type capacity() const noexcept
Definition: inline_string.hpp:199
string_view.hpp
Our partial std::string_view implementation.
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:15
pmem::obj::experimental::basic_inline_string::compare
int compare(const basic_inline_string &rhs) const noexcept
Compares this inline_string with other.
Definition: inline_string.hpp:252
pmem::obj::experimental::basic_inline_string::operator=
basic_inline_string & operator=(const basic_inline_string &rhs)
Copy assignment operator.
Definition: inline_string.hpp:155
pmem::obj::basic_string_view::size
constexpr size_type size() const noexcept
Returns count of characters stored in this pmem::obj::string_view data.
Definition: string_view.hpp:334
pmem::obj::p
Resides on pmem class.
Definition: p.hpp:35
pmem::obj::experimental::total_sizeof
A helper trait which calculates required memory capacity (in bytes) for a type.
Definition: inline_string.hpp:409
pmem::obj::experimental::basic_inline_string
This class serves similar purpose to pmem::obj::string, but keeps the data within the same allocation...
Definition: inline_string.hpp:44
make_persistent.hpp
Persistent_ptr transactional allocation functions for objects.
slice.hpp
Interface to access sequence of objects.
pmem::obj::basic_string_view::compare
int compare(size_type pos1, size_type n1, basic_string_view sv) const
Compares two character sequences.
Definition: string_view.hpp:1018
transaction.hpp
C++ pmemobj transactions.
pmem::obj::experimental::basic_inline_string::data
pointer data()
Returns pointer to the underlying data and if there is an active transaction add entire data to a tra...
Definition: inline_string.hpp:215
pmem::obj::experimental::basic_inline_string::basic_inline_string
basic_inline_string(basic_string_view< CharT, Traits > v)
Constructs inline string from a string_view.
Definition: inline_string.hpp:104
pmem::obj::experimental::basic_inline_string::at
reference at(size_type p)
Returns reference to a character at position.
Definition: inline_string.hpp:298
pmem::obj::experimental::basic_inline_string::range
slice< pointer > range(size_type p, size_type count)
Returns slice and snapshots (if there is an active transaction) requested range.
Definition: inline_string.hpp:339
pmem::obj::slice
pmem::obj::slice - provides interface to access sequence of objects.
Definition: slice.hpp:50
pmem::obj::experimental::basic_inline_string::snapshotted_data
pointer snapshotted_data(size_t p, size_t n)
Return pointer to data at position p and if there is an active transaction snapshot elements from p t...
Definition: inline_string.hpp:355
pmem::obj::experimental::basic_inline_string::size
size_type size() const noexcept
Definition: inline_string.hpp:185
pmem::obj::experimental::basic_inline_string::assign
basic_inline_string & assign(basic_string_view< CharT, Traits > rhs)
Transactionally assign content of basic_string_view.
Definition: inline_string.hpp:372
pmem::obj::basic_string_view::data
constexpr const CharT * data() const noexcept
Returns pointer to data stored in this pmem::obj::string_view.
Definition: string_view.hpp:296
pmem::obj::experimental::v
Volatile residing on pmem class.
Definition: v.hpp:42
pmem::obj::experimental::basic_inline_string::cdata
const_pointer cdata() const noexcept
Returns const pointer to the underlying data.
Definition: inline_string.hpp:237
pmem::obj::experimental::basic_inline_string::operator[]
reference operator[](size_type p)
Returns reference to a character at position.
Definition: inline_string.hpp:268
pmem::obj::pool_base
The non-template pool base class.
Definition: pool.hpp:46
persistent_ptr.hpp
Persistent smart pointer.
pmem::obj::flat_transaction::run
static void run(obj::pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:817