1 /*
2 * Copyright (C) 2017 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 #include "HidlTypeAssertion.h"
18
19 #include <hidl-util/Formatter.h>
20
21 #include <string>
22 #include <vector>
23
24 namespace android {
25
26 typedef std::vector<std::pair<std::string, size_t>> Registry;
registry()27 static Registry ®istry() {
28 static Registry sRegistry;
29 return sRegistry;
30 }
31
HidlTypeAssertion(const char * name,size_t size)32 HidlTypeAssertion::HidlTypeAssertion(const char *name, size_t size)
33 : mSize(size) {
34 registry().push_back(std::make_pair(name, size));
35 }
36
size() const37 size_t HidlTypeAssertion::size() const {
38 return mSize;
39 }
40
EmitAll(Formatter & out)41 void HidlTypeAssertion::EmitAll(Formatter &out) {
42 std::sort(
43 registry().begin(),
44 registry().end(),
45 [](const auto &a, const auto &b) {
46 return a.first < b.first;
47 });
48
49 for (const auto& entry : registry()) {
50 out << "static_assert(sizeof(::android::hardware::"
51 << entry.first
52 << ") == "
53 << entry.second
54 << ", \"wrong size\");\n";
55 }
56 }
57
58 } // namespace android
59
60