PMDK C++ bindings  1.11
This is the C++ bindings documentation for PMDK's libpmemobj.
string_view.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_STRING_VIEW
10 #define LIBPMEMOBJ_CPP_STRING_VIEW
11 
12 #include <algorithm>
13 #include <cassert>
14 #include <string>
15 
16 #if __cpp_lib_string_view
17 #include <string_view>
18 #endif
19 
20 namespace pmem
21 {
22 
23 namespace obj
24 {
25 
26 #if __cpp_lib_string_view
27 
28 template <typename CharT, typename Traits = std::char_traits<CharT>>
29 using basic_string_view = std::basic_string_view<CharT, Traits>;
30 using string_view = std::string_view;
31 using wstring_view = std::basic_string_view<wchar_t>;
32 using u16string_view = std::basic_string_view<char16_t>;
33 using u32string_view = std::basic_string_view<char32_t>;
34 
35 #else
36 
43 template <typename CharT, typename Traits = std::char_traits<CharT>>
45 public:
46  /* Member types */
47  using traits_type = Traits;
48  using value_type = CharT;
49  using size_type = std::size_t;
50  using difference_type = std::ptrdiff_t;
51  using reference = value_type &;
52  using const_reference = const value_type &;
53  using pointer = value_type *;
54  using const_pointer = const value_type *;
55 
56  basic_string_view() noexcept;
57  basic_string_view(const CharT *data, size_type size);
58  basic_string_view(const std::basic_string<CharT, Traits> &s);
59  basic_string_view(const CharT *data);
60 
61  basic_string_view(const basic_string_view &rhs) noexcept = default;
63  operator=(const basic_string_view &rhs) noexcept = default;
64 
65  const CharT *data() const noexcept;
66  size_type size() const noexcept;
67 
68  const CharT &operator[](size_type p) const noexcept;
69 
70  int compare(const basic_string_view &other) const noexcept;
71 
72 private:
73  const value_type *data_;
74  size_type size_;
75 };
76 
84 template <typename CharT, typename Traits>
86  : data_(nullptr), size_(0)
87 {
88 }
89 
97 template <typename CharT, typename Traits>
99  size_type size)
100  : data_(data), size_(size)
101 {
102 }
103 
109 template <typename CharT, typename Traits>
111  const std::basic_string<CharT, Traits> &s)
112  : data_(s.c_str()), size_(s.size())
113 {
114 }
115 
123 template <typename CharT, typename Traits>
125  : data_(data), size_(Traits::length(data))
126 {
127 }
128 
136 template <typename CharT, typename Traits>
137 inline const CharT *
139 {
140  return data_;
141 }
142 
149 template <typename CharT, typename Traits>
150 inline typename basic_string_view<CharT, Traits>::size_type
152 {
153  return size_;
154 }
155 
161 template <typename CharT, typename Traits>
162 inline const CharT &basic_string_view<CharT, Traits>::operator[](size_t p) const
163  noexcept
164 {
165  assert(p < size());
166  return data()[p];
167 }
168 
177 template <typename CharT, typename Traits>
178 inline int
180  noexcept
181 {
182  int ret = Traits::compare(data(), other.data(),
183  (std::min)(size(), other.size()));
184  if (ret != 0)
185  return ret;
186  if (size() < other.size())
187  return -1;
188  if (size() > other.size())
189  return 1;
190  return 0;
191 }
192 
196 template <class CharT, class Traits>
197 bool
200 {
201  return lhs.compare(rhs) == 0;
202 }
203 #endif
204 
205 } /* namespace obj */
206 } /* namespace pmem */
207 
208 #endif /* LIBPMEMOBJ_CPP_STRING_VIEW */
pmem::obj::basic_string_view
Our partial std::string_view implementation.
Definition: string_view.hpp:44
pmem::obj::basic_string_view::operator[]
const CharT & operator[](size_type p) const noexcept
Returns reference to a character at position.
Definition: string_view.hpp:162
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
Persistent memory namespace.
Definition: allocation_flag.hpp:15
pmem::obj::operator==
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
pmem::obj::p
Resides on pmem class.
Definition: p.hpp:35
pmem::obj::basic_string_view::basic_string_view
basic_string_view() noexcept
Default constructor with empty data.
Definition: string_view.hpp:85
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::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