• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium 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 #include "serializable.h"
6 
7 #include <utility>
8 
9 namespace v8_crdtp {
10 // =============================================================================
11 // Serializable - An object to be emitted as a sequence of bytes.
12 // =============================================================================
13 
Serialize() const14 std::vector<uint8_t> Serializable::Serialize() const {
15   std::vector<uint8_t> out;
16   AppendSerialized(&out);
17   return out;
18 }
19 
20 namespace {
21 class PreSerialized : public Serializable {
22  public:
PreSerialized(std::vector<uint8_t> bytes)23   explicit PreSerialized(std::vector<uint8_t> bytes)
24       : bytes_(std::move(bytes)) {}
25 
AppendSerialized(std::vector<uint8_t> * out) const26   void AppendSerialized(std::vector<uint8_t>* out) const override {
27     out->insert(out->end(), bytes_.begin(), bytes_.end());
28   }
29 
30  private:
31   std::vector<uint8_t> bytes_;
32 };
33 }  // namespace
34 
35 // static
From(std::vector<uint8_t> bytes)36 std::unique_ptr<Serializable> Serializable::From(std::vector<uint8_t> bytes) {
37   return std::make_unique<PreSerialized>(std::move(bytes));
38 }
39 }  // namespace v8_crdtp
40