QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 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_SPDY_PLATFORM_API_SPDY_MEM_SLICE_H_ |
| 6 | #define QUICHE_SPDY_PLATFORM_API_SPDY_MEM_SLICE_H_ |
| 7 | |
| 8 | #include <utility> |
| 9 | |
| 10 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_export.h" |
| 11 | #include "net/spdy/platform/impl/spdy_mem_slice_impl.h" |
| 12 | |
| 13 | namespace spdy { |
| 14 | |
| 15 | // SpdyMemSlice is an internally reference counted data buffer used as the |
| 16 | // source buffers for write operations. SpdyMemSlice implicitly maintains a |
| 17 | // reference count and will free the underlying data buffer when the reference |
| 18 | // count reaches zero. |
| 19 | class SPDY_EXPORT_PRIVATE SpdyMemSlice { |
| 20 | public: |
| 21 | // Constructs an empty SpdyMemSlice with no underlying data and 0 reference |
| 22 | // count. |
| 23 | SpdyMemSlice() = default; |
| 24 | |
| 25 | // Constructs a SpdyMemSlice with reference count 1 to a newly allocated data |
| 26 | // buffer of |length| bytes. |
| 27 | explicit SpdyMemSlice(size_t length) : impl_(length) {} |
| 28 | |
| 29 | // Constructs a SpdyMemSlice from |impl|. It takes the reference away from |
| 30 | // |impl|. |
| 31 | explicit SpdyMemSlice(SpdyMemSliceImpl impl) : impl_(std::move(impl)) {} |
| 32 | |
| 33 | SpdyMemSlice(const SpdyMemSlice& other) = delete; |
| 34 | SpdyMemSlice& operator=(const SpdyMemSlice& other) = delete; |
| 35 | |
| 36 | // Move constructors. |other| will not hold a reference to the data buffer |
| 37 | // after this call completes. |
| 38 | SpdyMemSlice(SpdyMemSlice&& other) = default; |
| 39 | SpdyMemSlice& operator=(SpdyMemSlice&& other) = default; |
| 40 | |
| 41 | ~SpdyMemSlice() = default; |
| 42 | |
| 43 | // Returns a char pointer to underlying data buffer. |
| 44 | const char* data() const { return impl_.data(); } |
| 45 | // Returns the length of underlying data buffer. |
| 46 | size_t length() const { return impl_.length(); } |
| 47 | |
| 48 | private: |
| 49 | SpdyMemSliceImpl impl_; |
| 50 | }; |
| 51 | |
| 52 | } // namespace spdy |
| 53 | |
| 54 | #endif // QUICHE_SPDY_PLATFORM_API_SPDY_MEM_SLICE_H_ |