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