Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
Buffer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 */
9#pragma once
10
11#include "utils/Range.h"
12
13#include <array>
14#include <cstddef>
15#include <memory>
16
17namespace pzstd {
18
27class Buffer {
28 std::shared_ptr<unsigned char> buffer_;
29 MutableByteRange range_;
30
31 static void delete_buffer(unsigned char* buffer) {
32 delete[] buffer;
33 }
34
35 public:
37 explicit Buffer() {}
38
40 explicit Buffer(std::size_t size)
41 : buffer_(new unsigned char[size], delete_buffer),
42 range_(buffer_.get(), buffer_.get() + size) {}
43
44 explicit Buffer(std::shared_ptr<unsigned char> buffer, MutableByteRange data)
45 : buffer_(buffer), range_(data) {}
46
47 Buffer(Buffer&&) = default;
48 Buffer& operator=(Buffer&&) = default;
49
58 Buffer splitAt(std::size_t n) {
59 auto firstPiece = range_.subpiece(0, n);
60 range_.advance(n);
61 return Buffer(buffer_, firstPiece);
62 }
63
65 void advance(std::size_t n) {
66 range_.advance(n);
67 }
68
70 void subtract(std::size_t n) {
71 range_.subtract(n);
72 }
73
75 ByteRange range() const {
76 return range_;
77 }
80 return range_;
81 }
82
83 const unsigned char* data() const {
84 return range_.data();
85 }
86
87 unsigned char* data() {
88 return range_.data();
89 }
90
91 std::size_t size() const {
92 return range_.size();
93 }
94
95 bool empty() const {
96 return range_.empty();
97 }
98};
99}
Definition Buffer.h:27
const unsigned char * data() const
Definition Buffer.h:83
unsigned char * data()
Definition Buffer.h:87
void advance(std::size_t n)
Modifies the buffer to point to the range [begin + n, end).
Definition Buffer.h:65
Buffer()
Construct an empty buffer that owns no data.
Definition Buffer.h:37
Buffer splitAt(std::size_t n)
Definition Buffer.h:58
bool empty() const
Definition Buffer.h:95
Buffer(Buffer &&)=default
MutableByteRange range()
Returns a mutable Range pointing to the Buffers data.
Definition Buffer.h:79
Buffer(std::shared_ptr< unsigned char > buffer, MutableByteRange data)
Definition Buffer.h:44
Buffer(std::size_t size)
Construct a Buffer that owns a new underlying buffer of size size.
Definition Buffer.h:40
void subtract(std::size_t n)
Modifies the buffer to point to the range [begin, end - n).
Definition Buffer.h:70
std::size_t size() const
Definition Buffer.h:91
ByteRange range() const
Returns a read only Range pointing to the Buffers data.
Definition Buffer.h:75
Buffer & operator=(Buffer &&)=default
Definition ErrorHolder.h:16
Range< unsigned char * > MutableByteRange
Definition Range.h:129
Definition poolTests.c:28