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; 27 static Registry ®istry() { 28 static Registry sRegistry; 29 return sRegistry; 30 } 31 32 HidlTypeAssertion::HidlTypeAssertion(const char *name, size_t size) 33 : mSize(size) { 34 registry().push_back(std::make_pair(name, size)); 35 } 36 37 size_t HidlTypeAssertion::size() const { 38 return mSize; 39 } 40 41 // static 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 (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