blob: 6e4822ad6e415ab447e9af8a663db3eaa2ee37c4 [file] [log] [blame]
Andreas Huber6755e9d2017-04-06 11:09:07 -07001/*
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
24namespace android {
25
26typedef std::vector<std::pair<std::string, size_t>> Registry;
27static Registry &registry() {
28 static Registry sRegistry;
29 return sRegistry;
30}
31
32HidlTypeAssertion::HidlTypeAssertion(const char *name, size_t size)
33 : mSize(size) {
34 registry().push_back(std::make_pair(name, size));
35}
36
37size_t HidlTypeAssertion::size() const {
38 return mSize;
39}
40
Andreas Huber6755e9d2017-04-06 11:09:07 -070041void 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
Chih-Hung Hsieh8cfa3ed2018-12-12 14:54:11 -080049 for (const auto& entry : registry()) {
Andreas Huber6755e9d2017-04-06 11:09:07 -070050 out << "static_assert(sizeof(::android::hardware::"
51 << entry.first
52 << ") == "
53 << entry.second
54 << ", \"wrong size\");\n";
55 }
56}
57
58} // namespace android
59