// Copyright 2017 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include "android/base/containers/SmallVector.h" #include "android/base/files/MemStream.h" #include "android/base/files/Stream.h" #include "android/base/TypeTraits.h" #include #include namespace android { namespace base { // // Save/load operations for different types. // void saveStream(Stream* stream, const MemStream& memStream); void loadStream(Stream* stream, MemStream* memStream); void saveBufferRaw(Stream* stream, char* buffer, uint32_t len); bool loadBufferRaw(Stream* stream, char* buffer); template >> void saveBuffer(Stream* stream, const std::vector& buffer) { stream->putBe32(buffer.size()); stream->write(buffer.data(), sizeof(T) * buffer.size()); } template >> bool loadBuffer(Stream* stream, std::vector* buffer) { auto len = stream->getBe32(); buffer->resize(len); auto ret = static_cast(stream->read(buffer->data(), len * sizeof(T))); return ret == len * sizeof(T); } template >> void saveBuffer(Stream* stream, const SmallVector& buffer) { stream->putBe32(buffer.size()); stream->write(buffer.data(), sizeof(T) * buffer.size()); } template >> bool loadBuffer(Stream* stream, SmallVector* buffer) { auto len = stream->getBe32(); buffer->clear(); buffer->resize_noinit(len); int ret = (int)stream->read(buffer->data(), len * sizeof(T)); return ret == len * sizeof(T); } template void saveBuffer(Stream* stream, const std::vector& buffer, SaveFunc&& saver) { stream->putBe32(buffer.size()); for (const auto& val : buffer) { saver(stream, val); } } template void saveBuffer(Stream* stream, const T* buffer, size_t numElts) { stream->putBe32(numElts); stream->write(buffer, sizeof(T) * numElts); } template void loadBufferPtr(Stream* stream, T* out) { auto len = stream->getBe32(); stream->read(out, len * sizeof(T)); } template void loadBuffer(Stream* stream, std::vector* buffer, LoadFunc&& loader) { auto len = stream->getBe32(); buffer->clear(); buffer->reserve(len); for (uint32_t i = 0; i < len; i++) { buffer->emplace_back(loader(stream)); } } template void saveCollection(Stream* stream, const Collection& c, SaveFunc&& saver) { stream->putBe32(c.size()); for (const auto& val : c) { saver(stream, val); } } template void loadCollection(Stream* stream, Collection* c, LoadFunc&& loader) { const int size = stream->getBe32(); for (int i = 0; i < size; ++i) { c->emplace(loader(stream)); } } void saveStringArray(Stream* stream, const char* const* strings, uint32_t count); std::vector loadStringArray(Stream* stream); } // namespace base } // namespace android