You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.1 KiB

4 months ago
  1. /*
  2. * IXWebSocketSendData.h
  3. *
  4. * WebSocket (Binary/Text) send data buffer
  5. */
  6. #pragma once
  7. #include <string>
  8. #include <vector>
  9. #include <iterator>
  10. namespace ix
  11. {
  12. /*
  13. * IXWebSocketSendData implements a wrapper for std::string, std:vector<char/uint8_t> and char*.
  14. * It removes the necessarity to copy the data or string into a std::string
  15. */
  16. class IXWebSocketSendData {
  17. public:
  18. template<typename T>
  19. struct IXWebSocketSendData_const_iterator
  20. //: public std::iterator<std::forward_iterator_tag, T>
  21. {
  22. typedef IXWebSocketSendData_const_iterator<T> const_iterator;
  23. using iterator_category = std::forward_iterator_tag;
  24. using difference_type = std::ptrdiff_t;
  25. using value_type = T;
  26. using pointer = value_type*;
  27. using reference = const value_type&;
  28. pointer _ptr;
  29. public:
  30. IXWebSocketSendData_const_iterator() : _ptr(nullptr) {}
  31. IXWebSocketSendData_const_iterator(pointer ptr) : _ptr(ptr) {}
  32. ~IXWebSocketSendData_const_iterator() {}
  33. const_iterator operator++(int) { return const_iterator(_ptr++); }
  34. const_iterator& operator++() { ++_ptr; return *this; }
  35. reference operator* () const { return *_ptr; }
  36. pointer operator->() const { return _ptr; }
  37. const_iterator operator+ (const difference_type offset) const { return const_iterator(_ptr + offset); }
  38. const_iterator operator- (const difference_type offset) const { return const_iterator(_ptr - offset); }
  39. difference_type operator- (const const_iterator& rhs) const { return _ptr - rhs._ptr; }
  40. bool operator==(const const_iterator& rhs) const { return _ptr == rhs._ptr; }
  41. bool operator!=(const const_iterator& rhs) const { return _ptr != rhs._ptr; }
  42. const_iterator& operator+=(const difference_type offset) { _ptr += offset; return *this; }
  43. const_iterator& operator-=(const difference_type offset) { _ptr -= offset; return *this; }
  44. };
  45. using const_iterator = IXWebSocketSendData_const_iterator<char>;
  46. /* The assigned std::string must be kept alive for the lifetime of the input buffer */
  47. IXWebSocketSendData(const std::string& str)
  48. : _data(str.data())
  49. , _size(str.size())
  50. {
  51. }
  52. /* The assigned std::vector must be kept alive for the lifetime of the input buffer */
  53. IXWebSocketSendData(const std::vector<char>& v)
  54. : _data(v.data())
  55. , _size(v.size())
  56. {
  57. }
  58. /* The assigned std::vector must be kept alive for the lifetime of the input buffer */
  59. IXWebSocketSendData(const std::vector<uint8_t>& v)
  60. : _data(reinterpret_cast<const char*>(v.data()))
  61. , _size(v.size())
  62. {
  63. }
  64. /* The assigned memory must be kept alive for the lifetime of the input buffer */
  65. IXWebSocketSendData(const char* data, size_t size)
  66. : _data(data)
  67. , _size(data == nullptr ? 0 : size)
  68. {
  69. }
  70. bool empty() const
  71. {
  72. return _data == nullptr || _size == 0;
  73. }
  74. const char* c_str() const
  75. {
  76. return _data;
  77. }
  78. const char* data() const
  79. {
  80. return _data;
  81. }
  82. size_t size() const
  83. {
  84. return _size;
  85. }
  86. inline const_iterator begin() const
  87. {
  88. return const_iterator(const_cast<char*>(_data));
  89. }
  90. inline const_iterator end() const
  91. {
  92. return const_iterator(const_cast<char*>(_data) + _size);
  93. }
  94. inline const_iterator cbegin() const
  95. {
  96. return begin();
  97. }
  98. inline const_iterator cend() const
  99. {
  100. return end();
  101. }
  102. private:
  103. const char* _data;
  104. const size_t _size;
  105. };
  106. }