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