Shaka Player Embedded
buffer_writer.h
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_UTIL_BUFFER_WRITER_H_
16 #define SHAKA_EMBEDDED_UTIL_BUFFER_WRITER_H_
17 
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 #include <type_traits>
22 #include <vector>
23 
24 #include "src/util/buffer_reader.h"
25 
26 namespace shaka {
27 namespace util {
28 
35 class BufferWriter {
36  public:
37  BufferWriter(uint8_t* data, size_t data_size);
38 
39  bool empty() const {
40  return size_ == 0;
41  }
42 
44  size_t BytesRemaining() const {
45  return size_;
46  }
47 
49  void WriteByte(uint8_t byte) {
50  Write(&byte, 1);
51  }
52 
54  void WriteTag(const char (&tag)[5]) {
55  // Note this is char[5] since the null-terminator is included.
56  Write(tag, 4);
57  }
58 
60  template <typename T, typename = typename std::enable_if<
61  std::is_integral<T>::value>::type>
62  void Write(T value, Endianness endian = kBigEndian) {
63  if (endian == kHostOrder) {
64  Write(&value, sizeof(value));
65  } else {
66  for (size_t i = 0; i < sizeof(T); i++) {
67  if (endian == kBigEndian)
68  WriteByte((value >> ((sizeof(T) - i - 1) * 8)) & 0xff);
69  else
70  WriteByte((value >> (i * 8)) & 0xff);
71  }
72  }
73  }
74 
76  void Write(const std::vector<uint8_t>& data) {
77  Write(data.data(), data.size());
78  }
79 
85  void Write(const void* src, size_t src_size);
86 
87  private:
88  uint8_t* data_;
89  size_t size_;
90 };
91 
92 } // namespace util
93 } // namespace shaka
94 
95 #endif // SHAKA_EMBEDDED_UTIL_BUFFER_WRITER_H_
void Write(T value, Endianness endian=kBigEndian)
Definition: buffer_writer.h:62
ExceptionCode type
BufferWriter(uint8_t *data, size_t data_size)
void WriteTag(const char(&tag)[5])
Definition: buffer_writer.h:54
void Write(const std::vector< uint8_t > &data)
Definition: buffer_writer.h:76
void WriteByte(uint8_t byte)
Definition: buffer_writer.h:49
size_t BytesRemaining() const
Definition: buffer_writer.h:44