Line | Count | Source |
1 | // Copyright 2019 The Chromium Authors | |
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 CRDTP_SERIALIZABLE_H_ | |
6 | #define CRDTP_SERIALIZABLE_H_ | |
7 | ||
8 | #include <cstdint> | |
9 | #include <memory> | |
10 | #include <vector> | |
11 | #include "export.h" | |
12 | ||
13 | namespace crdtp { | |
14 | // ============================================================================= | |
15 | // Serializable - An object to be emitted as a sequence of bytes. | |
16 | // ============================================================================= | |
17 | class CRDTP_EXPORT Serializable { | |
18 | public: | |
19 | // Convenience: Invokes |AppendSerialized| on an empty vector. | |
20 | std::vector<uint8_t> Serialize() const; | |
21 | ||
22 | virtual void AppendSerialized(std::vector<uint8_t>* out) const = 0; | |
23 | ||
24 | 0 | virtual ~Serializable() = default; |
25 | ||
26 | // Wraps a vector of |bytes| into a Serializable for situations in which we | |
27 | // eagerly serialize a structure. | |
28 | static std::unique_ptr<Serializable> From(std::vector<uint8_t> bytes); | |
29 | }; | |
30 | } // namespace crdtp | |
31 | ||
32 | #endif // CRDTP_SERIALIZABLE_H_ |