libpqxx  7.1.0
binarystring.hxx
1 /* Representation for raw, binary data.
2  *
3  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/binarystring instead.
4  *
5  * Copyright (c) 2000-2020, Jeroen T. Vermeulen.
6  *
7  * See COPYING for copyright license. If you did not receive a file called
8  * COPYING with this source code, please notify the distributor of this
9  * mistake, or contact the author.
10  */
11 #ifndef PQXX_H_BINARYSTRING
12 #define PQXX_H_BINARYSTRING
13 
14 #include "pqxx/compiler-public.hxx"
15 #include "pqxx/internal/compiler-internal-pre.hxx"
16 
17 #include <memory>
18 #include <string>
19 #include <string_view>
20 
21 #include "pqxx/result.hxx"
22 
23 namespace pqxx
24 {
26 
53 class PQXX_LIBEXPORT binarystring
54 {
55 public:
56  using char_type = unsigned char;
57  using value_type = std::char_traits<char_type>::char_type;
58  using size_type = std::size_t;
59  using difference_type = long;
60  using const_reference = value_type const &;
61  using const_pointer = value_type const *;
63  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
64 
65  binarystring(binarystring const &) = default;
66 
68 
72  explicit binarystring(field const &);
73 
75  explicit binarystring(std::string_view);
76 
78  binarystring(void const *, std::size_t);
79 
81  [[nodiscard]] size_type size() const noexcept { return m_size; }
83  [[nodiscard]] size_type length() const noexcept { return size(); }
84  [[nodiscard]] bool empty() const noexcept { return size() == 0; }
85 
86  [[nodiscard]] const_iterator begin() const noexcept { return data(); }
87  [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); }
88  [[nodiscard]] const_iterator end() const noexcept { return data() + m_size; }
89  [[nodiscard]] const_iterator cend() const noexcept { return end(); }
90 
91  [[nodiscard]] const_reference front() const noexcept { return *begin(); }
92  [[nodiscard]] const_reference back() const noexcept
93  {
94  return *(data() + m_size - 1);
95  }
96 
97  [[nodiscard]] const_reverse_iterator rbegin() const
98  {
99  return const_reverse_iterator{end()};
100  }
101  [[nodiscard]] const_reverse_iterator crbegin() const { return rbegin(); }
102  [[nodiscard]] const_reverse_iterator rend() const
103  {
104  return const_reverse_iterator{begin()};
105  }
106  [[nodiscard]] const_reverse_iterator crend() const { return rend(); }
107 
109  [[nodiscard]] value_type const *data() const noexcept { return m_buf.get(); }
110 
111  [[nodiscard]] const_reference operator[](size_type i) const noexcept
112  {
113  return data()[i];
114  }
115 
116  [[nodiscard]] PQXX_PURE bool operator==(binarystring const &) const noexcept;
117  [[nodiscard]] bool operator!=(binarystring const &rhs) const noexcept
118  {
119  return not operator==(rhs);
120  }
121 
122  binarystring &operator=(binarystring const &);
123 
125  const_reference at(size_type) const;
126 
128  void swap(binarystring &);
129 
131 
134  [[nodiscard]] char const *get() const noexcept
135  {
136  return reinterpret_cast<char const *>(m_buf.get());
137  }
138 
140  [[nodiscard]] std::string_view view() const noexcept
141  {
142  return std::string_view(get(), size());
143  }
144 
146 
151  [[nodiscard]] std::string str() const;
152 
153 private:
154  std::shared_ptr<value_type> m_buf;
155  size_type m_size{0};
156 };
157 } // namespace pqxx
158 
159 #include "pqxx/internal/compiler-internal-post.hxx"
160 #endif
size_type size() const noexcept
Size of converted string in bytes.
Definition: binarystring.hxx:81
bool operator!=(binarystring const &rhs) const noexcept
Definition: binarystring.hxx:117
std::char_traits< char_type >::char_type value_type
Definition: binarystring.hxx:57
value_type const * data() const noexcept
Unescaped field contents.
Definition: binarystring.hxx:109
std::string_view view() const noexcept
Read contents as a std::string_view.
Definition: binarystring.hxx:140
bool empty() const noexcept
Definition: binarystring.hxx:84
long difference_type
Definition: binarystring.hxx:59
value_type const & const_reference
Definition: binarystring.hxx:60
const_reference operator[](size_type i) const noexcept
Definition: binarystring.hxx:111
const_iterator cbegin() const noexcept
Definition: binarystring.hxx:87
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
std::size_t size_type
Definition: binarystring.hxx:58
Binary data corresponding to PostgreSQL&#39;s "BYTEA" binary-string type.
Definition: binarystring.hxx:53
const_reverse_iterator rend() const
Definition: binarystring.hxx:102
const_pointer const_iterator
Definition: binarystring.hxx:62
size_type length() const noexcept
Size of converted string in bytes.
Definition: binarystring.hxx:83
const_iterator end() const noexcept
Definition: binarystring.hxx:88
const_iterator cend() const noexcept
Definition: binarystring.hxx:89
const_iterator begin() const noexcept
Definition: binarystring.hxx:86
const_reverse_iterator rbegin() const
Definition: binarystring.hxx:97
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: binarystring.hxx:63
const_reference back() const noexcept
Definition: binarystring.hxx:92
const_reverse_iterator crend() const
Definition: binarystring.hxx:106
unsigned char char_type
Definition: binarystring.hxx:56
Reference to a field in a result set.
Definition: field.hxx:32
const_reference front() const noexcept
Definition: binarystring.hxx:91
const_reverse_iterator crbegin() const
Definition: binarystring.hxx:101
value_type const * const_pointer
Definition: binarystring.hxx:61