File: buffer_reader.h

package info (click to toggle)
libwebm 1.0.0.32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,132 kB
  • sloc: cpp: 32,181; sh: 508; python: 128; makefile: 32
file content (138 lines) | stat: -rw-r--r-- 3,675 bytes parent folder | download | duplicates (28)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) 2016 The WebM project authors. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS.  All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
#ifndef INCLUDE_WEBM_BUFFER_READER_H_
#define INCLUDE_WEBM_BUFFER_READER_H_

#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <vector>

#include "./reader.h"
#include "./status.h"

/**
 \file
 A `Reader` implementation that reads from a `std::vector<std::uint8_t>`.
 */

namespace webm {

/**
 \addtogroup PUBLIC_API
 @{
 */

/**
 A simple reader that reads data from a buffer of bytes.
 */
class BufferReader : public Reader {
 public:
  /**
   Constructs a new, empty reader.
   */
  BufferReader() = default;

  /**
   Constructs a new reader by copying the provided reader into the new reader.

   \param other The source reader to copy.
   */
  BufferReader(const BufferReader& other) = default;

  /**
   Copies the provided reader into this reader.

   \param other The source reader to copy. May be equal to `*this`, in which
   case this is a no-op.
   \return `*this`.
   */
  BufferReader& operator=(const BufferReader& other) = default;

  /**
   Constructs a new reader by moving the provided reader into the new reader.

   \param other The source reader to move. After moving, it will be reset to an
   empty stream.
   */
  BufferReader(BufferReader&&);

  /**
   Moves the provided reader into this reader.

   \param other The source reader to move. After moving, it will be reset to an
   empty stream. May be equal to `*this`, in which case this is a no-op.
   \return `*this`.
   */
  BufferReader& operator=(BufferReader&&);

  /**
   Creates a new `BufferReader` populated with the provided bytes.

   \param bytes Bytes that are assigned to the internal buffer and used as the
   source which is read from.
   */
  BufferReader(std::initializer_list<std::uint8_t> bytes);

  /**
   Creates a new `BufferReader` populated with the provided data.

   \param vector A vector of bytes that is copied to the internal buffer and
   used as the source which is read from.
   */
  explicit BufferReader(const std::vector<std::uint8_t>& vector);

  /**
   Creates a new `BufferReader` populated with the provided data.

   \param vector A vector of bytes that is moved to the internal buffer and used
   as the source which is read from.
   */
  explicit BufferReader(std::vector<std::uint8_t>&& vector);

  /**
   Resets the reader to read from the given list of bytes, starting at the
   beginning.

   This makes `reader = {1, 2, 3};` effectively equivalent to `reader =
   BufferReader({1, 2, 3});`.

   \param bytes Bytes that are assigned to the internal buffer and used as the
   source which is read from.
   \return `*this`.
   */
  BufferReader& operator=(std::initializer_list<std::uint8_t> bytes);

  Status Read(std::size_t num_to_read, std::uint8_t* buffer,
              std::uint64_t* num_actually_read) override;

  Status Skip(std::uint64_t num_to_skip,
              std::uint64_t* num_actually_skipped) override;

  std::uint64_t Position() const override;

  /**
   Gets the total size of the buffer.
   */
  std::size_t size() const { return data_.size(); }

 private:
  // Stores the byte buffer from which data is read.
  std::vector<std::uint8_t> data_;

  // The position of the reader in the byte buffer.
  std::size_t pos_ = 0;
};

/**
 @}
 */

}  // namespace webm

#endif  // INCLUDE_WEBM_BUFFER_READER_H_