blob: e9a1c32a33f8b3bcb351d30fab9d8f748083037b [file] [log] [blame]
Ian Rogers96faf5b2013-08-09 22:05:32 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_LEB128_ENCODER_H_
18#define ART_COMPILER_LEB128_ENCODER_H_
19
20#include "base/macros.h"
21
22namespace art {
23
24// An encoder with an API similar to vector<uint32_t> where the data is captured in ULEB128 format.
25class UnsignedLeb128EncodingVector {
26 public:
27 UnsignedLeb128EncodingVector() {
28 }
29
30 void PushBack(uint32_t value) {
31 bool done = false;
32 do {
33 uint8_t out = value & 0x7f;
34 if (out != value) {
35 data_.push_back(out | 0x80);
36 value >>= 7;
37 } else {
38 data_.push_back(out);
39 done = true;
40 }
41 } while (!done);
42 }
43
44 template<typename It>
45 void InsertBack(It cur, It end) {
46 for (; cur != end; ++cur) {
47 PushBack(*cur);
48 }
49 }
50
51 const std::vector<uint8_t>& GetData() const {
52 return data_;
53 }
54
55 private:
56 std::vector<uint8_t> data_;
57
58 DISALLOW_COPY_AND_ASSIGN(UnsignedLeb128EncodingVector);
59};
60
61} // namespace art
62
63#endif // ART_COMPILER_LEB128_ENCODER_H_