binarystring.hxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /** Representation for raw, binary data.
  2. *
  3. * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/binarystring instead.
  4. *
  5. * Copyright (c) 2000-2019, 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 mistake,
  9. * or contact the author.
  10. */
  11. #ifndef PQXX_H_BINARYSTRING
  12. #define PQXX_H_BINARYSTRING
  13. #include "pqxx/compiler-public.hxx"
  14. #include "pqxx/compiler-internal-pre.hxx"
  15. #include <memory>
  16. #include <string>
  17. #include "pqxx/result.hxx"
  18. namespace pqxx
  19. {
  20. /// Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
  21. /** @ingroup escaping-functions
  22. *
  23. * This class represents a binary string as stored in a field of type bytea.
  24. * The raw value returned by a bytea field contains escape sequences for certain
  25. * characters, which are filtered out by binarystring.
  26. *
  27. * Internally a binarystring is zero-terminated, but it may also contain zero
  28. * bytes, just like any other byte value. So don't assume that it can be
  29. * treated as a C-style string unless you've made sure of this yourself.
  30. *
  31. * The binarystring retains its value even if the result it was obtained from is
  32. * destroyed, but it cannot be copied or assigned.
  33. *
  34. * \relatesalso transaction_base::esc_raw
  35. *
  36. * To convert the other way, i.e. from a raw series of bytes to a string
  37. * suitable for inclusion as bytea values in your SQL, use the transaction's
  38. * esc_raw() functions.
  39. *
  40. * @warning This class is implemented as a reference-counting smart pointer.
  41. * Copying, swapping, and destroying binarystring objects that refer to the same
  42. * underlying data block is <em>not thread-safe</em>. If you wish to pass
  43. * binarystrings around between threads, make sure that each of these operations
  44. * is protected against concurrency with similar operations on the same object,
  45. * or other objects pointing to the same data block.
  46. */
  47. class PQXX_LIBEXPORT binarystring
  48. {
  49. public:
  50. using char_type = unsigned char;
  51. using value_type = std::char_traits<char_type>::char_type;
  52. using size_type = size_t;
  53. using difference_type = long;
  54. using const_reference = const value_type &;
  55. using const_pointer = const value_type *;
  56. using const_iterator = const_pointer;
  57. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  58. binarystring(const binarystring &) =default;
  59. /// Read and unescape bytea field
  60. /** The field will be zero-terminated, even if the original bytea field isn't.
  61. * @param F the field to read; must be a bytea field
  62. */
  63. explicit binarystring(const field &); //[t62]
  64. /// Copy binary data from std::string.
  65. explicit binarystring(const std::string &);
  66. /// Copy binary data of given length straight out of memory.
  67. binarystring(const void *, size_t);
  68. /// Size of converted string in bytes
  69. size_type size() const noexcept { return m_size; } //[t62]
  70. /// Size of converted string in bytes
  71. size_type length() const noexcept { return size(); } //[t62]
  72. bool empty() const noexcept { return size()==0; } //[t62]
  73. const_iterator begin() const noexcept { return data(); } //[t62]
  74. const_iterator cbegin() const noexcept { return begin(); }
  75. const_iterator end() const noexcept { return data()+m_size; } //[t62]
  76. const_iterator cend() const noexcept { return end(); }
  77. const_reference front() const noexcept { return *begin(); } //[t62]
  78. const_reference back() const noexcept //[t62]
  79. { return *(data()+m_size-1); }
  80. const_reverse_iterator rbegin() const //[t62]
  81. { return const_reverse_iterator{end()}; }
  82. const_reverse_iterator crbegin() const { return rbegin(); }
  83. const_reverse_iterator rend() const //[t62]
  84. { return const_reverse_iterator{begin()}; }
  85. const_reverse_iterator crend() const { return rend(); }
  86. /// Unescaped field contents
  87. const value_type *data() const noexcept {return m_buf.get();} //[t62]
  88. const_reference operator[](size_type i) const noexcept //[t62]
  89. { return data()[i]; }
  90. PQXX_PURE bool operator==(const binarystring &) const noexcept; //[t62]
  91. bool operator!=(const binarystring &rhs) const noexcept //[t62]
  92. { return not operator==(rhs); }
  93. binarystring &operator=(const binarystring &);
  94. /// Index contained string, checking for valid index
  95. const_reference at(size_type) const; //[t62]
  96. /// Swap contents with other binarystring
  97. void swap(binarystring &); //[t62]
  98. /// Raw character buffer (no terminating zero is added)
  99. /** @warning No terminating zero is added! If the binary data did not end in
  100. * a null character, you will not find one here.
  101. */
  102. const char *get() const noexcept //[t62]
  103. { return reinterpret_cast<const char *>(m_buf.get()); }
  104. /// Read as regular C++ string (may include null characters)
  105. /** @warning libpqxx releases before 3.1 stored the string and returned a
  106. * reference to it. This is no longer the case! It now creates and returns
  107. * a new string object. Avoid repeated use of this function; retrieve your
  108. * string once and keep it in a local variable. Also, do not expect to be
  109. * able to compare the string's address to that of an earlier invocation.
  110. */
  111. std::string str() const; //[t62]
  112. private:
  113. using smart_pointer_type = std::shared_ptr<value_type>;
  114. /// Shorthand: construct a smart_pointer_type.
  115. static smart_pointer_type make_smart_pointer(unsigned char *buf=nullptr)
  116. {
  117. #if !(defined(_MSC_VER) && defined(__clang__))
  118. return smart_pointer_type{
  119. buf,
  120. internal::freemallocmem_templated<unsigned char>};
  121. #else
  122. return smart_pointer_type{buf, internal::freemallocmem};
  123. #endif
  124. }
  125. smart_pointer_type m_buf;
  126. size_type m_size;
  127. };
  128. }
  129. #include "pqxx/compiler-internal-post.hxx"
  130. #endif