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
139
140
141
142
|
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
class Size {
public:
Size() {}
Size(int bits) {
is_valid_ = true;
bits_ = bits;
}
Size(std::string dynamic) {
is_valid_ = true;
dynamic_.push_back(dynamic);
}
Size(int bits, std::string dynamic) {
is_valid_ = true;
bits_ = bits;
dynamic_.push_back(dynamic);
}
Size(const Size& size) {
is_valid_ = size.is_valid_;
bits_ = size.bits_;
dynamic_ = size.dynamic_;
}
std::string dynamic_string() const {
if (dynamic_.empty()) return "0";
std::stringstream result;
// Print everything but the last element then append it manually to avoid
// the trailing "+" operator.
std::copy(dynamic_.begin(), dynamic_.end() - 1, std::ostream_iterator<std::string>(result, " + "));
result << dynamic_.back();
return result.str();
}
std::vector<std::string> dynamic_string_list() {
return dynamic_;
}
bool empty() const {
return !is_valid_;
}
bool has_bits() const {
return bits_ != 0;
}
bool has_dynamic() const {
return !dynamic_.empty();
}
int bits() const {
return bits_;
}
int bytes() const {
// Round up to the nearest byte
return (bits_ + 7) / 8;
}
Size operator+(int rhs) {
return Size(bits_ + rhs);
}
Size operator+(std::string rhs) {
auto ret = Size();
ret.is_valid_ = true;
ret.dynamic_.insert(ret.dynamic_.end(), dynamic_.begin(), dynamic_.end());
ret.dynamic_.push_back(rhs);
return ret;
}
Size operator+(const Size& rhs) {
auto ret = Size(bits_ + rhs.bits_);
ret.is_valid_ = is_valid_ && rhs.is_valid_;
ret.dynamic_.insert(ret.dynamic_.end(), dynamic_.begin(), dynamic_.end());
ret.dynamic_.insert(ret.dynamic_.end(), rhs.dynamic_.begin(), rhs.dynamic_.end());
return ret;
}
Size& operator+=(int rhs) {
is_valid_ = true;
bits_ += rhs;
return *this;
}
Size& operator+=(std::string rhs) {
is_valid_ = true;
dynamic_.push_back(rhs);
return *this;
}
Size& operator+=(const Size& rhs) {
is_valid_ = is_valid_ && rhs.is_valid_;
bits_ += rhs.bits_;
dynamic_.insert(dynamic_.end(), rhs.dynamic_.begin(), rhs.dynamic_.end());
return *this;
}
std::string ToString() const {
std::stringstream str;
str << "/* Bits: */ " << bits_ << " + /* Dynamic: */ " << dynamic_string();
if (!is_valid_) {
str << " (invalid) ";
}
return str.str();
}
friend std::ostream& operator<<(std::ostream& os, const Size& rhs) {
return os << rhs.ToString();
}
private:
bool is_valid_ = false;
int bits_ = 0;
std::vector<std::string> dynamic_;
};
|