serializable.h 1.0 KB

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