libpqxx  7.0.7
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 = 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 *, 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
pqxx::binarystring::crend
const_reverse_iterator crend() const
Definition: binarystring.hxx:106
pqxx::binarystring::front
const_reference front() const noexcept
Definition: binarystring.hxx:91
pqxx::binarystring::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: binarystring.hxx:63
pqxx::binarystring::const_pointer
value_type const * const_pointer
Definition: binarystring.hxx:61
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
pqxx::binarystring::operator==
PQXX_PURE bool operator==(binarystring const &) const noexcept
Definition: binarystring.cxx:66
pqxx::binarystring::swap
void swap(binarystring &)
Swap contents with other binarystring.
Definition: binarystring.cxx:90
pqxx::to_string
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:478
pqxx::binarystring::difference_type
long difference_type
Definition: binarystring.hxx:59
pqxx::binarystring::cbegin
const_iterator cbegin() const noexcept
Definition: binarystring.hxx:87
pqxx::binarystring::char_type
unsigned char char_type
Definition: binarystring.hxx:56
pqxx::binarystring::const_reference
value_type const & const_reference
Definition: binarystring.hxx:60
pqxx::binarystring::operator=
binarystring & operator=(binarystring const &)
pqxx::binarystring::empty
bool empty() const noexcept
Definition: binarystring.hxx:84
pqxx::binarystring::rend
const_reverse_iterator rend() const
Definition: binarystring.hxx:102
pqxx::binarystring::back
const_reference back() const noexcept
Definition: binarystring.hxx:92
pqxx::binarystring::str
std::string str() const
Read as regular C++ string (may include null characters).
Definition: binarystring.cxx:101
pqxx::binarystring::at
const_reference at(size_type) const
Index contained string, checking for valid index.
Definition: binarystring.cxx:76
pqxx::binarystring::binarystring
binarystring(binarystring const &)=default
pqxx::binarystring::rbegin
const_reverse_iterator rbegin() const
Definition: binarystring.hxx:97
pqxx::binarystring::end
const_iterator end() const noexcept
Definition: binarystring.hxx:88
pqxx::binarystring::const_iterator
const_pointer const_iterator
Definition: binarystring.hxx:62
pqxx::binarystring::length
size_type length() const noexcept
Size of converted string in bytes.
Definition: binarystring.hxx:83
pqxx::binarystring::size
size_type size() const noexcept
Size of converted string in bytes.
Definition: binarystring.hxx:81
pqxx::binarystring::data
const value_type * data() const noexcept
Unescaped field contents.
Definition: binarystring.hxx:109
pqxx::binarystring::cend
const_iterator cend() const noexcept
Definition: binarystring.hxx:89
pqxx::binarystring::crbegin
const_reverse_iterator crbegin() const
Definition: binarystring.hxx:101
pqxx::binarystring::begin
const_iterator begin() const noexcept
Definition: binarystring.hxx:86
pqxx::field::c_str
const char * c_str() const
Read as plain C string.
Definition: field.cxx:62
pqxx::field
Reference to a field in a result set.
Definition: field.hxx:32
pqxx::binarystring::size_type
size_t size_type
Definition: binarystring.hxx:58
pqxx::binarystring::get
const char * get() const noexcept
Raw character buffer (no terminating zero is added).
Definition: binarystring.hxx:134
pqxx::binarystring::view
std::string_view view() const noexcept
Read contents as a std::string_view.
Definition: binarystring.hxx:140
pqxx::binarystring::operator!=
bool operator!=(binarystring const &rhs) const noexcept
Definition: binarystring.hxx:117
pqxx::binarystring::value_type
std::char_traits< char_type >::char_type value_type
Definition: binarystring.hxx:57
pqxx::binarystring
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition: binarystring.hxx:53
pqxx::binarystring::operator[]
const_reference operator[](size_type i) const noexcept
Definition: binarystring.hxx:111