1 // Copyright (c) 2009-2017 The OTS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef OTS_GRAPHITE_H_
6 #define OTS_GRAPHITE_H_
7
8 #include <vector>
9 #include <type_traits>
10
11 namespace ots {
12
13 template<typename ParentType>
14 class TablePart {
15 public:
TablePart(ParentType * parent)16 TablePart(ParentType* parent) : parent(parent) { }
~TablePart()17 virtual ~TablePart() { }
18 virtual bool ParsePart(Buffer& table) = 0;
19 virtual bool SerializePart(OTSStream* out) const = 0;
20 protected:
21 ParentType* parent;
22 };
23
24 template<typename T>
SerializeParts(const std::vector<T> & vec,OTSStream * out)25 bool SerializeParts(const std::vector<T>& vec, OTSStream* out) {
26 for (const T& part : vec) {
27 if (!part.SerializePart(out)) {
28 return false;
29 }
30 }
31 return true;
32 }
33
34 template<typename T>
SerializeParts(const std::vector<std::vector<T>> & vec,OTSStream * out)35 bool SerializeParts(const std::vector<std::vector<T>>& vec, OTSStream* out) {
36 for (const std::vector<T>& part : vec) {
37 if (!SerializeParts(part, out)) {
38 return false;
39 }
40 }
41 return true;
42 }
43
SerializeParts(const std::vector<uint8_t> & vec,OTSStream * out)44 inline bool SerializeParts(const std::vector<uint8_t>& vec, OTSStream* out) {
45 for (uint8_t part : vec) {
46 if (!out->WriteU8(part)) {
47 return false;
48 }
49 }
50 return true;
51 }
52
SerializeParts(const std::vector<uint16_t> & vec,OTSStream * out)53 inline bool SerializeParts(const std::vector<uint16_t>& vec, OTSStream* out) {
54 for (uint16_t part : vec) {
55 if (!out->WriteU16(part)) {
56 return false;
57 }
58 }
59 return true;
60 }
61
SerializeParts(const std::vector<int16_t> & vec,OTSStream * out)62 inline bool SerializeParts(const std::vector<int16_t>& vec, OTSStream* out) {
63 for (int16_t part : vec) {
64 if (!out->WriteS16(part)) {
65 return false;
66 }
67 }
68 return true;
69 }
70
SerializeParts(const std::vector<uint32_t> & vec,OTSStream * out)71 inline bool SerializeParts(const std::vector<uint32_t>& vec, OTSStream* out) {
72 for (uint32_t part : vec) {
73 if (!out->WriteU32(part)) {
74 return false;
75 }
76 }
77 return true;
78 }
79
SerializeParts(const std::vector<int32_t> & vec,OTSStream * out)80 inline bool SerializeParts(const std::vector<int32_t>& vec, OTSStream* out) {
81 for (int32_t part : vec) {
82 if (!out->WriteS32(part)) {
83 return false;
84 }
85 }
86 return true;
87 }
88
89 template<typename T>
datasize(std::vector<T> vec)90 size_t datasize(std::vector<T> vec) {
91 return sizeof(T) * vec.size();
92 }
93
94 } // namespace ots
95
96 #endif // OTS_GRAPHITE_H_
97