1 // Copyright 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #pragma once
16
17 #include "android/base/containers/SmallVector.h"
18 #include "android/base/files/MemStream.h"
19 #include "android/base/files/Stream.h"
20 #include "android/base/TypeTraits.h"
21
22 #include <string>
23 #include <vector>
24
25 namespace android {
26 namespace base {
27
28 //
29 // Save/load operations for different types.
30 //
31
32 void saveStream(Stream* stream, const MemStream& memStream);
33 void loadStream(Stream* stream, MemStream* memStream);
34
35 void saveBufferRaw(Stream* stream, char* buffer, uint32_t len);
36 bool loadBufferRaw(Stream* stream, char* buffer);
37
38 template <class T, class = enable_if<std::is_standard_layout<T>>>
saveBuffer(Stream * stream,const std::vector<T> & buffer)39 void saveBuffer(Stream* stream, const std::vector<T>& buffer) {
40 stream->putBe32(buffer.size());
41 stream->write(buffer.data(), sizeof(T) * buffer.size());
42 }
43
44 template <class T, class = enable_if<std::is_standard_layout<T>>>
loadBuffer(Stream * stream,std::vector<T> * buffer)45 bool loadBuffer(Stream* stream, std::vector<T>* buffer) {
46 auto len = stream->getBe32();
47 buffer->resize(len);
48 int ret = (int)stream->read(buffer->data(), len * sizeof(T));
49 return ret == len * sizeof(T);
50 }
51
52 template <class T, class = enable_if<std::is_standard_layout<T>>>
saveBuffer(Stream * stream,const SmallVector<T> & buffer)53 void saveBuffer(Stream* stream, const SmallVector<T>& buffer) {
54 stream->putBe32(buffer.size());
55 stream->write(buffer.data(), sizeof(T) * buffer.size());
56 }
57
58 template <class T, class = enable_if<std::is_standard_layout<T>>>
loadBuffer(Stream * stream,SmallVector<T> * buffer)59 bool loadBuffer(Stream* stream, SmallVector<T>* buffer) {
60 auto len = stream->getBe32();
61 buffer->clear();
62 buffer->resize_noinit(len);
63 int ret = (int)stream->read(buffer->data(), len * sizeof(T));
64 return ret == len * sizeof(T);
65 }
66
67 template <class T, class SaveFunc>
saveBuffer(Stream * stream,const std::vector<T> & buffer,SaveFunc && saver)68 void saveBuffer(Stream* stream, const std::vector<T>& buffer, SaveFunc&& saver) {
69 stream->putBe32(buffer.size());
70 for (const auto& val : buffer) {
71 saver(stream, val);
72 }
73 }
74
75 template <class T>
saveBuffer(Stream * stream,const T * buffer,size_t numElts)76 void saveBuffer(Stream* stream, const T* buffer, size_t numElts) {
77 stream->putBe32(numElts);
78 stream->write(buffer, sizeof(T) * numElts);
79 }
80
81 template <class T>
loadBufferPtr(Stream * stream,T * out)82 void loadBufferPtr(Stream* stream, T* out) {
83 auto len = stream->getBe32();
84 stream->read(out, len * sizeof(T));
85 }
86
87 template <class T, class LoadFunc>
loadBuffer(Stream * stream,std::vector<T> * buffer,LoadFunc && loader)88 void loadBuffer(Stream* stream, std::vector<T>* buffer, LoadFunc&& loader) {
89 auto len = stream->getBe32();
90 buffer->clear();
91 buffer->reserve(len);
92 for (uint32_t i = 0; i < len; i++) {
93 buffer->emplace_back(loader(stream));
94 }
95 }
96
97 template <class Collection, class SaveFunc>
saveCollection(Stream * stream,const Collection & c,SaveFunc && saver)98 void saveCollection(Stream* stream, const Collection& c, SaveFunc&& saver) {
99 stream->putBe32(c.size());
100 for (const auto& val : c) {
101 saver(stream, val);
102 }
103 }
104
105 template <class Collection, class LoadFunc>
loadCollection(Stream * stream,Collection * c,LoadFunc && loader)106 void loadCollection(Stream* stream, Collection* c, LoadFunc&& loader) {
107 const int size = stream->getBe32();
108 for (int i = 0; i < size; ++i) {
109 c->emplace(loader(stream));
110 }
111 }
112
113 void saveStringArray(Stream* stream, const char* const* strings, uint32_t count);
114 std::vector<std::string> loadStringArray(Stream* stream);
115
116 } // namespace base
117 } // namespace android
118