1 /*
2  * Copyright (C) 2020 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 MINIKIN_TEST_BUFFER_UTILS_H
18 #define MINIKIN_TEST_BUFFER_UTILS_H
19 
20 #include <minikin/Buffer.h>
21 #include <vector>
22 
23 namespace minikin {
24 
25 template <class T>
allocateBuffer(const T & t)26 std::vector<uint8_t> allocateBuffer(const T& t) {
27     BufferWriter writer(nullptr);
28     t.writeTo(&writer);
29     // Fill with 0xFF for debugging.
30     return std::vector<uint8_t>(writer.size(), 0xFFu);
31 }
32 
33 template <class T, auto arg>
allocateBuffer(const T & t)34 std::vector<uint8_t> allocateBuffer(const T& t) {
35     BufferWriter writer(nullptr);
36     t.template writeTo<arg>(&writer);
37     // Fill with 0xFF for debugging.
38     return std::vector<uint8_t>(writer.size(), 0xFFu);
39 }
40 
41 template <class T>
writeToBuffer(const T & t)42 std::vector<uint8_t> writeToBuffer(const T& t) {
43     std::vector<uint8_t> buffer = allocateBuffer(t);
44     BufferWriter writer(buffer.data());
45     t.writeTo(&writer);
46     return buffer;
47 }
48 
49 template <class T, auto arg>
writeToBuffer(const T & t)50 std::vector<uint8_t> writeToBuffer(const T& t) {
51     std::vector<uint8_t> buffer = allocateBuffer<T, arg>(t);
52     BufferWriter writer(buffer.data());
53     t.template writeTo<arg>(&writer);
54     return buffer;
55 }
56 
57 }  // namespace minikin
58 
59 #endif  // MINIKIN_TEST_BUFFER_UTILS_H
60