1/*******************************************************************************
2* Copyright 2021 Intel Corporation
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 COMMON_SERIALIZATION_STREAM_HPP
18#define COMMON_SERIALIZATION_STREAM_HPP
19
20#include <cstdint>
21#include <vector>
22#include <type_traits>
23
24namespace dnnl {
25namespace impl {
26
27struct serialization_stream_t {
28 serialization_stream_t() = default;
29
30 template <typename T>
31 status_t write(const T ptr, size_t nelems = 1) {
32 using non_pointer_type = typename std::remove_pointer<T>::type;
33
34 static_assert(std::is_pointer<T>::value,
35 "T is expected to be a pointer type.");
36 static_assert(!std::is_pointer<non_pointer_type>::value,
37 "T cannot be a pointer to pointer.");
38 static_assert(!std::is_class<non_pointer_type>::value,
39 "non-pointer type is expected to be a trivial type to avoid "
40 "padding issues.");
41 static_assert(!std::is_array<non_pointer_type>::value,
42 "non-pointer type cannot be an array.");
43
44 return write_impl((const void *)ptr, sizeof(non_pointer_type) * nelems);
45 }
46
47 bool empty() const { return data_.empty(); }
48
49 const std::vector<uint8_t> &get_data() const { return data_; }
50
51private:
52 status_t write_impl(const void *ptr, size_t size) {
53 const auto *p = reinterpret_cast<const uint8_t *>(ptr);
54 data_.insert(data_.end(), p, p + size);
55 return status::success;
56 }
57
58 std::vector<uint8_t> data_;
59};
60
61} // namespace impl
62} // namespace dnnl
63
64#endif
65