1 // Copyright 2017 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef QUICHE_COMMON_PLATFORM_API_QUICHE_MEM_SLICE_H_ 6 #define QUICHE_COMMON_PLATFORM_API_QUICHE_MEM_SLICE_H_ 7 8 #include <cstddef> 9 #include <memory> 10 #include <utility> 11 12 #include "quiche_platform_impl/quiche_mem_slice_impl.h" 13 14 #include "absl/strings/string_view.h" 15 #include "quiche/common/platform/api/quiche_export.h" 16 #include "quiche/common/quiche_buffer_allocator.h" 17 #include "quiche/common/quiche_callbacks.h" 18 19 namespace quiche { 20 21 // QuicheMemSlice is a wrapper around a platform-specific I/O buffer type. It 22 // may be reference counted, though QUICHE itself does not rely on that. 23 class QUICHE_EXPORT QuicheMemSlice { 24 public: 25 // Constructs a empty QuicheMemSlice with no underlying data. 26 QuicheMemSlice() = default; 27 28 // Constructs a QuicheMemSlice that takes ownership of |buffer|. The length 29 // of the |buffer| must not be zero. To construct an empty QuicheMemSlice, 30 // use the zero-argument constructor instead. QuicheMemSlice(QuicheBuffer buffer)31 explicit QuicheMemSlice(QuicheBuffer buffer) : impl_(std::move(buffer)) {} 32 33 // Constructs a QuicheMemSlice that takes ownership of |buffer| allocated on 34 // heap. |length| must not be zero. QuicheMemSlice(std::unique_ptr<char[]> buffer,size_t length)35 QuicheMemSlice(std::unique_ptr<char[]> buffer, size_t length) 36 : impl_(std::move(buffer), length) {} 37 QuicheMemSlice(const char * buffer,size_t length,quiche::SingleUseCallback<void (const char *)> done_callback)38 QuicheMemSlice(const char* buffer, size_t length, 39 quiche::SingleUseCallback<void(const char*)> done_callback) 40 : impl_(buffer, length, std::move(done_callback)) {} 41 42 // Ensures the use of the in-place constructor (below) is intentional. 43 struct InPlace {}; 44 45 // Constructs a QuicheMemSlice by constructing |impl_| in-place. 46 template <typename... Args> QuicheMemSlice(InPlace,Args &&...args)47 explicit QuicheMemSlice(InPlace, Args&&... args) 48 : impl_{std::forward<Args>(args)...} {} 49 50 QuicheMemSlice(const QuicheMemSlice& other) = delete; 51 QuicheMemSlice& operator=(const QuicheMemSlice& other) = delete; 52 53 // Move constructors. |other| will not hold a reference to the data buffer 54 // after this call completes. 55 QuicheMemSlice(QuicheMemSlice&& other) = default; 56 QuicheMemSlice& operator=(QuicheMemSlice&& other) = default; 57 58 ~QuicheMemSlice() = default; 59 60 // Release the underlying reference. Further access the memory will result in 61 // undefined behavior. Reset()62 void Reset() { impl_.Reset(); } 63 64 // Returns a const char pointer to underlying data buffer. data()65 const char* data() const { return impl_.data(); } 66 // Returns the length of underlying data buffer. length()67 size_t length() const { return impl_.length(); } 68 // Returns the representation of the underlying data as a string view. AsStringView()69 absl::string_view AsStringView() const { 70 return absl::string_view(data(), length()); 71 } 72 empty()73 bool empty() const { return impl_.empty(); } 74 75 private: 76 QuicheMemSliceImpl impl_; 77 }; 78 79 } // namespace quiche 80 81 #endif // QUICHE_COMMON_PLATFORM_API_QUICHE_MEM_SLICE_H_ 82