9 #ifndef LIBPMEMOBJ_CPP_ARRAY_HPP
10 #define LIBPMEMOBJ_CPP_ARRAY_HPP
14 #include <initializer_list>
23 #include <libpmemobj/base.h>
55 template <
typename T, std::
size_t N>
58 template <
typename Y, std::
size_t M>
59 struct standard_array_traits {
65 struct standard_array_traits<Y, 0> {
66 struct _alignment_struct {
70 struct alignas(_alignment_struct) type {
71 char _data[
sizeof(_alignment_struct)];
77 using pointer = value_type *;
78 using const_pointer =
const value_type *;
79 using reference = value_type &;
80 using const_reference =
const value_type &;
82 using const_iterator = const_pointer;
83 using size_type = std::size_t;
84 using difference_type = std::ptrdiff_t;
85 using reverse_iterator = std::reverse_iterator<iterator>;
86 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
91 typename standard_array_traits<T, N>::type _data;
111 #if __cplusplus > 201703L
118 array(std::initializer_list<T> list)
120 if constexpr (N > 0) {
122 for (
auto &v : list) {
153 detail::conditional_add_to_tx(
154 this, 1, POBJ_XADD_ASSUME_INITIALIZED);
184 detail::conditional_add_to_tx(
185 this, 1, POBJ_XADD_ASSUME_INITIALIZED);
186 detail::conditional_add_to_tx(
187 &other, 1, POBJ_XADD_ASSUME_INITIALIZED);
188 std::move(other._get_data(), other._get_data() +
size(),
206 throw std::out_of_range(
"array::at");
208 detail::conditional_add_to_tx(
_get_data() + n, 1,
209 POBJ_XADD_ASSUME_INITIALIZED);
220 at(size_type n)
const
223 throw std::out_of_range(
"array::at");
237 throw std::out_of_range(
"array::const_at");
251 detail::conditional_add_to_tx(
_get_data() + n, 1,
252 POBJ_XADD_ASSUME_INITIALIZED);
276 detail::conditional_add_to_tx(
this, 1,
277 POBJ_XADD_ASSUME_INITIALIZED);
386 const_reverse_iterator
389 return const_reverse_iterator(
cend());
395 const_reverse_iterator
398 return const_reverse_iterator(
cend());
404 const_reverse_iterator
407 return const_reverse_iterator(
cbegin());
413 const_reverse_iterator
416 return const_reverse_iterator(
cbegin());
428 detail::conditional_add_to_tx(
_get_data(), 1,
429 POBJ_XADD_ASSUME_INITIALIZED);
443 POBJ_XADD_ASSUME_INITIALIZED);
498 throw std::out_of_range(
"array::range");
500 detail::conditional_add_to_tx(
_get_data() + start, n,
501 POBJ_XADD_ASSUME_INITIALIZED);
523 range(size_type start, size_type n, size_type snapshot_size)
526 throw std::out_of_range(
"array::range");
528 if (snapshot_size > n)
551 range(size_type start, size_type n)
const
554 throw std::out_of_range(
"array::range");
556 return {const_iterator(
_get_data() + start),
557 const_iterator(
_get_data() + start + n)};
572 crange(size_type start, size_type n)
const
575 throw std::out_of_range(
"array::crange");
577 return {const_iterator(
_get_data() + start),
578 const_iterator(
_get_data() + start + n)};
621 detail::conditional_add_to_tx(
622 this, 1, POBJ_XADD_ASSUME_INITIALIZED);
634 template <std::
size_t Size = N>
635 typename std::enable_if<Size != 0>::type
648 detail::conditional_add_to_tx(
649 this, 1, POBJ_XADD_ASSUME_INITIALIZED);
650 detail::conditional_add_to_tx(
651 &other, 1, POBJ_XADD_ASSUME_INITIALIZED);
661 template <std::
size_t Size = N>
662 typename std::enable_if<Size == 0>::type
665 static_assert(!std::is_const<T>::value,
666 "cannot swap zero-sized array of type 'const T'");
673 template <std::
size_t Size = N>
674 typename std::enable_if<Size != 0, T *>::type
683 template <std::
size_t Size = N>
684 typename std::enable_if<Size != 0, const T *>::type
694 template <std::
size_t Size = N>
695 typename std::enable_if<Size == 0, T *>::type
698 return reinterpret_cast<T *
>(&this->_data);
704 template <std::
size_t Size = N>
705 typename std::enable_if<Size == 0, const T *>::type
708 return reinterpret_cast<const T *
>(&this->_data);
726 template <
typename T, std::
size_t N>
736 template <
typename T, std::
size_t N>
740 return !(lhs == rhs);
746 template <
typename T, std::
size_t N>
750 return std::lexicographical_compare(lhs.
cbegin(), lhs.
cend(),
757 template <
typename T, std::
size_t N>
767 template <
typename T, std::
size_t N>
777 template <
typename T, std::
size_t N>
787 template <
typename T, std::
size_t N>
788 typename pmem::obj::array<T, N>::const_iterator
797 template <
typename T, std::
size_t N>
798 typename pmem::obj::array<T, N>::const_iterator
807 template <
typename T, std::
size_t N>
808 typename pmem::obj::array<T, N>::const_reverse_iterator
817 template <
typename T, std::
size_t N>
818 typename pmem::obj::array<T, N>::const_reverse_iterator
827 template <
typename T, std::
size_t N>
837 template <
typename T, std::
size_t N>
838 typename pmem::obj::array<T, N>::const_iterator
847 template <
typename T, std::
size_t N>
857 template <
typename T, std::
size_t N>
858 typename pmem::obj::array<T, N>::const_iterator
867 template <
typename T, std::
size_t N>
868 typename pmem::obj::array<T, N>::reverse_iterator
877 template <
typename T, std::
size_t N>
878 typename pmem::obj::array<T, N>::const_reverse_iterator
887 template <
typename T, std::
size_t N>
888 typename pmem::obj::array<T, N>::reverse_iterator
897 template <
typename T, std::
size_t N>
898 typename pmem::obj::array<T, N>::const_reverse_iterator
907 template <
typename T,
size_t N>
917 template <
size_t I,
typename T,
size_t N>
922 "Index out of bounds in std::get<> (pmem::obj::array)");
929 template <
size_t I,
typename T,
size_t N>
934 "Index out of bounds in std::get<> (pmem::obj::array)");
935 return std::move(a.at(I));
941 template <
size_t I,
typename T,
size_t N>
946 "Index out of bounds in std::get<> (pmem::obj::array)");
953 template <
size_t I,
typename T,
size_t N>
958 "Index out of bounds in std::get<> (pmem::obj::array)");
959 return std::move(a.at(I));
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
The non-template pool base class.
Definition: pool.hpp:50
pmem::obj::slice - provides interface to access sequence of objects.
Definition: slice.hpp:50
Commonly used functionality.
Iterators for contiguous persistent containers.
bool operator<=(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member less or equal operator.
Definition: array.hpp:779
bool operator<(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member less than operator.
Definition: array.hpp:748
bool operator>=(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member greater or equal operator.
Definition: array.hpp:769
bool operator!=(const allocator< T, P, Tr > &lhs, const OtherAllocator &rhs)
Determines if memory from another allocator can be deallocated from this one.
Definition: allocator.hpp:536
T & get(pmem::obj::array< T, N > &a)
Non-member get function.
Definition: array.hpp:919
pool_base pool_by_vptr(const T *that)
Retrieve pool handle for the given pointer.
Definition: utils.hpp:32
bool operator==(standard_alloc_policy< T > const &, standard_alloc_policy< T2 > const &)
Determines if memory from another allocator can be deallocated from this one.
Definition: allocator.hpp:420
bool operator>(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member greater than operator.
Definition: array.hpp:759
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Persistent smart pointer.
Convenience extensions for the resides on pmem property template.
Interface to access sequence of objects.
Default non-const iterator which adds element to a transaction on every access.
Definition: contiguous_iterator.hpp:331
Non-const iterator which adds elements to a transaction in a bulk.
Definition: contiguous_iterator.hpp:192
pmem::obj::array - persistent container with std::array compatible interface.
Definition: array.hpp:56
std::enable_if< Size==0, T * >::type _get_data()
Support for zero sized array.
Definition: array.hpp:696
reverse_iterator rbegin()
Returns a reverse iterator to the beginning.
Definition: array.hpp:366
reference at(size_type n)
Access element at specific index and add it to a transaction.
Definition: array.hpp:203
slice< range_snapshotting_iterator > range(size_type start, size_type n, size_type snapshot_size)
Returns slice.
Definition: array.hpp:523
constexpr size_type max_size() const noexcept
Returns the maximum size of the array.
Definition: array.hpp:594
void fill(const_reference value)
Fills array with specified value inside internal transaction.
Definition: array.hpp:616
slice< const_iterator > crange(size_type start, size_type n) const
Returns const slice.
Definition: array.hpp:572
const_reference front() const
Access the first element.
Definition: array.hpp:451
iterator begin()
Returns an iterator to the beginning.
Definition: array.hpp:306
iterator end()
Returns an iterator to the end.
Definition: array.hpp:318
const_reference at(size_type n) const
Access element at specific index.
Definition: array.hpp:220
array(array &&)=default
Defaulted move constructor.
const_reverse_iterator crend() const noexcept
Returns a const reverse iterator to the beginning.
Definition: array.hpp:414
const_iterator end() const noexcept
Returns a const iterator to the end.
Definition: array.hpp:345
array & operator=(array &&other)
Move assignment operator - perform move assignment from other pmem::obj::array.
Definition: array.hpp:172
std::enable_if< Size==0, const T * >::type _get_data() const
Support for zero sized array.
Definition: array.hpp:706
const_reverse_iterator rend() const noexcept
Returns a const reverse iterator to the end.
Definition: array.hpp:405
const T * data() const noexcept
Returns const raw pointer to the underlying data.
Definition: array.hpp:285
pool_base _get_pool() const
Check whether object is on pmem and return pool_base instance.
Definition: array.hpp:717
array(const array &)=default
Defaulted copy constructor.
reference operator[](size_type n)
Access element at specific index and add it to a transaction.
Definition: array.hpp:249
const_reverse_iterator rbegin() const noexcept
Returns a const reverse iterator to the beginning.
Definition: array.hpp:387
T * data()
Returns raw pointer to the underlying data and adds entire array to a transaction.
Definition: array.hpp:274
array & operator=(const array &other)
Copy assignment operator - perform assignment from other pmem::obj::array.
Definition: array.hpp:141
const_reverse_iterator crbegin() const noexcept
Returns a const reverse iterator to the beginning.
Definition: array.hpp:396
slice< pointer > range(size_type start, size_type n)
Returns slice and snapshots requested range.
Definition: array.hpp:495
const_reference const_at(size_type n) const
Access element at specific index.
Definition: array.hpp:234
std::enable_if< Size !=0, T * >::type _get_data()
Support for non-zero sized array.
Definition: array.hpp:675
const_reference back() const
Access the last element.
Definition: array.hpp:469
std::enable_if< Size !=0 >::type swap(array &other)
Swaps content with other array's content inside internal transaction.
Definition: array.hpp:636
const_reference cback() const
Access the last element.
Definition: array.hpp:478
reference back()
Access the last element and add this element to a transaction.
Definition: array.hpp:440
const_iterator cend() const noexcept
Returns a const iterator to the end.
Definition: array.hpp:354
const_reference cfront() const
Access the first element.
Definition: array.hpp:460
const_iterator cbegin() const noexcept
Returns const iterator to the beginning.
Definition: array.hpp:336
const_iterator begin() const noexcept
Returns const iterator to the beginning.
Definition: array.hpp:327
reference front()
Access the first element and add this element to a transaction.
Definition: array.hpp:426
constexpr bool empty() const noexcept
Checks whether array is empty.
Definition: array.hpp:603
std::enable_if< Size !=0, const T * >::type _get_data() const
Support for non-zero sized array.
Definition: array.hpp:685
const T * cdata() const noexcept
Returns const raw pointer to the underlying data.
Definition: array.hpp:294
array()=default
Defaulted constructor.
constexpr size_type size() const noexcept
Returns size of the array.
Definition: array.hpp:585
const_reference operator[](size_type n) const
Access element at specific index.
Definition: array.hpp:261
std::enable_if< Size==0 >::type swap(array &other)
Swap for zero-sized array.
Definition: array.hpp:663
slice< const_iterator > range(size_type start, size_type n) const
Returns const slice.
Definition: array.hpp:551
reverse_iterator rend()
Returns a reverse iterator to the end.
Definition: array.hpp:378
C++ pmemobj transactions.