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#ifndef CHRE_WIFI_OFFLOAD_WIFI_VECTOR_SERIALIZATION_H_
18#define CHRE_WIFI_OFFLOAD_WIFI_VECTOR_SERIALIZATION_H_
19
20#include "chre/apps/wifi_offload/wifi_offload.h"
21
22#include "chre/apps/wifi_offload/flatbuffers_types_generated.h"
23
24namespace wifi_offload {
25
26template <typename T>
27flatbuffers::Offset<
28    flatbuffers::Vector<flatbuffers::Offset<typename T::FbsType>>>
29SerializeVector(const Vector<T> &native_vector,
30                flatbuffers::FlatBufferBuilder *builder) {
31  wifi_offload::Vector<flatbuffers::Offset<typename T::FbsType>> offset_vector;
32  offset_vector.reserve(native_vector.size());
33  for (const auto &elem : native_vector) {
34    offset_vector.push_back(elem.Serialize(builder));
35  }
36  return builder->CreateVector(offset_vector);
37}
38
39template <typename T>
40bool DeserializeVector(
41    const flatbuffers::Vector<flatbuffers::Offset<typename T::FbsType>>
42        &flatbuffer_vector,
43    Vector<T> *native_vector) {
44  if (native_vector == nullptr) {
45    return false;
46  }
47
48  native_vector->clear();
49  native_vector->reserve(flatbuffer_vector.size());
50  for (const auto &flatbuffer_elem : flatbuffer_vector) {
51    T native_elem;
52    if (flatbuffer_elem && native_elem.Deserialize(*flatbuffer_elem)) {
53      native_vector->push_back(std::move(native_elem));
54    } else {
55      return false;
56    }
57  }
58  return true;
59}
60
61}  // namespace wifi_offload
62
63#endif  // CHRE_WIFI_OFFLOAD_WIFI_VECTOR_SERIALIZATION_H_
64