1 /*
2 * Copyright 2011 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 #pragma once
18 
19 template <class Container>
loadContainer(android::base::Stream * stream,Container & c)20 void loadContainer(android::base::Stream* stream, Container& c) {
21     size_t size = (size_t)stream->getBe32();
22     c.resize(size);
23     for (auto& ite : c) {
24         ite.onLoad(stream);
25     }
26 }
27 
28 template <class NameMap>
loadNameMap(android::base::Stream * stream,NameMap & namemap)29 void loadNameMap(android::base::Stream* stream, NameMap& namemap) {
30     assert(namemap.size() == 0);
31     size_t size = static_cast<size_t>(stream->getBe32());
32     for (size_t i = 0; i < size; i++) {
33         typename NameMap::key_type name = stream->getBe32();
34         namemap.emplace(name, stream);
35     }
36 }
37 
38 template <class Container>
saveContainer(android::base::Stream * stream,const Container & c)39 void saveContainer(android::base::Stream* stream, const Container& c) {
40     stream->putBe32(c.size());
41     for (const auto& ite : c) {
42         ite.onSave(stream);
43     }
44 }
45 
46 template <class NameMap>
saveNameMap(android::base::Stream * stream,const NameMap & nameMap)47 void saveNameMap(android::base::Stream* stream, const NameMap& nameMap) {
48     stream->putBe32(nameMap.size());
49     for (const auto& ite : nameMap) {
50         stream->putBe32(ite.first);
51         ite.second.onSave(stream);
52     }
53 }
54