PMDK C++ bindings  1.11
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 
16 
17 #include <string>
18 
19 namespace pmem
20 {
21 
22 namespace obj
23 {
24 
25 namespace experimental
26 {
27 
42 template <typename CharT, typename Traits = std::char_traits<CharT>>
44 public:
45  using traits_type = Traits;
46  using value_type = CharT;
47  using size_type = std::size_t;
48  using difference_type = std::ptrdiff_t;
49  using reference = value_type &;
50  using const_reference = const value_type &;
51  using pointer = value_type *;
52  using const_pointer = const value_type *;
53 
55  basic_inline_string(size_type capacity);
57 
59 
61 
64  operator basic_string_view<CharT, Traits>() const;
65 
66  size_type size() const noexcept;
67  size_type capacity() const noexcept;
68 
69  pointer data() noexcept;
70  const_pointer data() const noexcept;
71 
72  int compare(const basic_inline_string &rhs) const noexcept;
73 
74  CharT &operator[](size_type p) noexcept;
75 
77 
78 private:
79  obj::p<uint64_t> size_;
80  obj::p<uint64_t> capacity_;
81 };
82 
87 
93 template <typename CharT, typename Traits>
96  : size_(v.size()), capacity_(v.size())
97 {
98  if (nullptr == pmemobj_pool_by_ptr(this))
99  throw pmem::pool_error("Invalid pool handle.");
100 
101  std::copy(v.data(), v.data() + static_cast<ptrdiff_t>(size_), data());
102 
103  data()[static_cast<ptrdiff_t>(size_)] = '\0';
104 }
105 
111 template <typename CharT, typename Traits>
113  : size_(0), capacity_(capacity)
114 {
115  if (nullptr == pmemobj_pool_by_ptr(this))
116  throw pmem::pool_error("Invalid pool handle.");
117 
118  data()[static_cast<ptrdiff_t>(size_)] = '\0';
119 }
120 
126 template <typename CharT, typename Traits>
128  const basic_inline_string &rhs)
129  : size_(rhs.size()), capacity_(rhs.capacity())
130 {
131  if (nullptr == pmemobj_pool_by_ptr(this))
132  throw pmem::pool_error("Invalid pool handle.");
133 
134  std::copy(rhs.data(), rhs.data() + static_cast<ptrdiff_t>(size_),
135  data());
136 
137  data()[static_cast<ptrdiff_t>(size_)] = '\0';
138 }
139 
143 template <typename CharT, typename Traits>
146 {
147  if (this == &rhs)
148  return *this;
149 
150  return assign(rhs);
151 }
152 
156 template <typename CharT, typename Traits>
160 {
161  return assign(rhs);
162 }
163 
165 template <typename CharT, typename Traits>
167  const
168 {
169  return {data(), size()};
170 }
171 
173 template <typename CharT, typename Traits>
174 typename basic_inline_string<CharT, Traits>::size_type
176 {
177  return size_;
178 }
179 
187 template <typename CharT, typename Traits>
188 typename basic_inline_string<CharT, Traits>::size_type
190 {
191  return capacity_;
192 }
193 
195 template <typename CharT, typename Traits>
196 typename basic_inline_string<CharT, Traits>::pointer
198 {
199  return reinterpret_cast<CharT *>(this + 1);
200 }
201 
203 template <typename CharT, typename Traits>
204 typename basic_inline_string<CharT, Traits>::const_pointer
206 {
207  return reinterpret_cast<const CharT *>(this + 1);
208 }
209 
218 template <typename CharT, typename Traits>
219 int
221  const basic_inline_string &rhs) const noexcept
222 {
223  return basic_string_view<CharT, Traits>(data(), size()).compare(rhs);
224 }
225 
231 template <typename CharT, typename Traits>
233 {
234  return data()[p];
235 }
236 
242 template <typename CharT, typename Traits>
245 {
246  auto pop = obj::pool_base(pmemobj_pool_by_ptr(this));
247 
248  if (rhs.size() > capacity())
249  throw std::out_of_range("inline_string capacity exceeded.");
250 
251  auto initialized_mem =
252  (std::min)(rhs.size(), size()) + 1 /* sizeof('\0') */;
253 
254  obj::transaction::run(pop, [&] {
255  detail::conditional_add_to_tx(data(), initialized_mem);
256 
257  if (rhs.size() > size())
258  detail::conditional_add_to_tx(
259  data() + initialized_mem,
260  rhs.size() - initialized_mem + 1,
261  POBJ_XADD_NO_SNAPSHOT);
262 
263  std::copy(rhs.data(),
264  rhs.data() + static_cast<ptrdiff_t>(rhs.size()),
265  data());
266  size_ = rhs.size();
267 
268  data()[static_cast<ptrdiff_t>(size_)] = '\0';
269  });
270 
271  return *this;
272 }
273 
280 template <typename T>
281 struct total_sizeof {
282  template <typename... Args>
283  static size_t
284  value(const Args &... args)
285  {
286  return sizeof(T);
287  }
288 };
289 
297 template <typename CharT, typename Traits>
298 struct total_sizeof<basic_inline_string<CharT, Traits>> {
299  static size_t
300  value(const basic_string_view<CharT, Traits> &s)
301  {
302  return sizeof(basic_inline_string<CharT, Traits>) +
303  (s.size() + 1 /* '\0' */) * sizeof(CharT);
304  }
305 };
306 } /* namespace experimental */
307 } /* namespace obj */
308 
309 namespace detail
310 {
311 /* Check if type is pmem::obj::basic_inline_string */
312 template <typename>
313 struct is_inline_string : std::false_type {
314 };
315 
316 template <typename CharT, typename Traits>
317 struct is_inline_string<obj::experimental::basic_inline_string<CharT, Traits>>
318  : std::true_type {
319 };
320 } /* namespace detail */
321 
322 } /* namespace pmem */
323 
324 #endif /* LIBPMEMOBJ_CPP_INLINE_STRING_HPP */
pmem::obj::basic_string_view
Our partial std::string_view implementation.
Definition: string_view.hpp:44
pmem::obj::basic_string_view::compare
int compare(const basic_string_view &other) const noexcept
Compares this string_view with other.
Definition: string_view.hpp:179
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:189
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:220
pmem::obj::experimental::basic_inline_string::operator=
basic_inline_string & operator=(const basic_inline_string &rhs)
Copy assignment operator.
Definition: inline_string.hpp:145
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:281
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:43
make_persistent.hpp
Persistent_ptr transactional allocation functions for objects.
pmem::obj::transaction::run
static void run(pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:406
transaction.hpp
C++ pmemobj transactions.
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:94
pmem::obj::experimental::basic_inline_string::size
size_type size() const noexcept
Definition: inline_string.hpp:175
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:244
pmem::obj::basic_string_view::data
const CharT * data() const noexcept
Returns pointer to data stored in this pmem::obj::string_view.
Definition: string_view.hpp:138
pmem::obj::experimental::basic_inline_string::data
pointer data() noexcept
Definition: inline_string.hpp:197
pmem::obj::experimental::v
Volatile residing on pmem class.
Definition: v.hpp:42
pmem::obj::pool_base
The non-template pool base class.
Definition: pool.hpp:46
persistent_ptr.hpp
Persistent smart pointer.
pmem::obj::basic_string_view::size
size_type size() const noexcept
Returns count of characters stored in this pmem::obj::string_view data.
Definition: string_view.hpp:151
pmem::obj::experimental::basic_inline_string::operator[]
CharT & operator[](size_type p) noexcept
Returns reference to a character at position.
Definition: inline_string.hpp:232