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 <string>
14 
15 #if __cpp_lib_string_view
16 #include <string_view>
17 #endif
18 
19 namespace pmem
20 {
21 
22 namespace obj
23 {
24 
25 #if __cpp_lib_string_view
26 
27 template <typename CharT, typename Traits = std::char_traits<CharT>>
28 using basic_string_view = std::basic_string_view<CharT, Traits>;
29 using string_view = std::string_view;
30 using wstring_view = std::basic_string_view<wchar_t>;
31 using u16string_view = std::basic_string_view<char16_t>;
32 using u32string_view = std::basic_string_view<char32_t>;
33 
34 #else
35 
42 template <typename CharT, typename Traits = std::char_traits<CharT>>
43 class basic_string_view {
44 public:
45  /* Member types */
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 
55  basic_string_view() noexcept;
56  basic_string_view(const CharT *data, size_type size);
57  basic_string_view(const std::basic_string<CharT, Traits> &s);
58  basic_string_view(const CharT *data);
59 
60  basic_string_view(const basic_string_view &rhs) noexcept = default;
61  basic_string_view &
62  operator=(const basic_string_view &rhs) noexcept = default;
63 
64  const CharT *data() const noexcept;
65  size_type size() const noexcept;
66 
67  const CharT &operator[](size_type p) const noexcept;
68 
69  int compare(const basic_string_view &other) const noexcept;
70 
71 private:
72  const value_type *data_;
73  size_type size_;
74 };
75 
83 template <typename CharT, typename Traits>
85  : data_(nullptr), size_(0)
86 {
87 }
88 
96 template <typename CharT, typename Traits>
98  size_type size)
99  : data_(data), size_(size)
100 {
101 }
102 
108 template <typename CharT, typename Traits>
110  const std::basic_string<CharT, Traits> &s)
111  : data_(s.c_str()), size_(s.size())
112 {
113 }
114 
122 template <typename CharT, typename Traits>
124  : data_(data), size_(Traits::length(data))
125 {
126 }
127 
135 template <typename CharT, typename Traits>
136 inline const CharT *
138 {
139  return data_;
140 }
141 
148 template <typename CharT, typename Traits>
149 inline typename basic_string_view<CharT, Traits>::size_type
151 {
152  return size_;
153 }
154 
160 template <typename CharT, typename Traits>
161 inline const CharT &basic_string_view<CharT, Traits>::operator[](size_t p) const
162  noexcept
163 {
164  return data()[p];
165 }
166 
175 template <typename CharT, typename Traits>
176 inline int
177 basic_string_view<CharT, Traits>::compare(const basic_string_view &other) const
178  noexcept
179 {
180  int ret = Traits::compare(data(), other.data(),
181  (std::min)(size(), other.size()));
182  if (ret != 0)
183  return ret;
184  if (size() < other.size())
185  return -1;
186  if (size() > other.size())
187  return 1;
188  return 0;
189 }
190 
194 template <class CharT, class Traits>
195 bool
198 {
199  return lhs.compare(rhs) == 0;
200 }
201 #endif
202 
203 } /* namespace obj */
204 } /* namespace pmem */
205 
206 #endif /* LIBPMEMOBJ_CPP_STRING_VIEW */
int compare(const basic_string_view &other) const noexcept
Compares this string_view with other.
Definition: string_view.hpp:177
size_type size() const noexcept
Returns count of characters stored in this pmem::obj::string_view data.
Definition: string_view.hpp:150
basic_string_view() noexcept
Default constructor with empty data.
Definition: string_view.hpp:84
const CharT * data() const noexcept
Returns pointer to data stored in this pmem::obj::string_view.
Definition: string_view.hpp:137
const CharT & operator[](size_type p) const noexcept
Returns reference to a character at position.
Definition: string_view.hpp:161
Resides on pmem class.
Definition: p.hpp:35
Our partial std::string_view implementation.
Definition: string_view.hpp:43
Persistent memory namespace.
Definition: allocation_flag.hpp:14
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