base_string_adapter_h.template 4 KB
Newer Older
1
// This file is generated by base_string_adapter_h.template.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef {{"_".join(config.protocol.namespace)}}_BASE_STRING_ADAPTER_H
#define {{"_".join(config.protocol.namespace)}}_BASE_STRING_ADAPTER_H

#include <memory>
#include <string>
#include <vector>

#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ref_counted_memory.h"
17
#include "{{config.crdtp.dir}}/serializable.h"
18
#include "{{config.crdtp.dir}}/protocol_core.h"
19

20 21 22 23 24 25 26 27
{% if config.lib.export_header %}
#include "{{config.lib.export_header}}"
{% endif %}

namespace base {
class Value;
}

28 29 30 31
namespace {{config.crdtp.namespace}} {
class DeserializerState;
}

32 33 34 35 36 37 38 39 40 41
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}

class Value;

using String = std::string;

class {{config.lib.export_macro}} StringUtil {
 public:
42 43 44
  static String fromUTF8(const uint8_t* data, size_t length) {
    return std::string(reinterpret_cast<const char*>(data), length);
  }
45

46
  static String fromUTF16LE(const uint16_t* data, size_t length);
47 48 49 50 51 52 53

  static const uint8_t* CharactersLatin1(const String& s) { return nullptr; }
  static const uint8_t* CharactersUTF8(const String& s) {
    return reinterpret_cast<const uint8_t*>(s.data());
  }
  static const uint16_t* CharactersUTF16(const String& s) { return nullptr; }
  static size_t CharacterCount(const String& s) { return s.size(); }
54 55 56

  static bool ReadString({{config.crdtp.namespace}}::DeserializerState* state, String* str);
  static void WriteString(const String& str, std::vector<uint8_t>* bytes);
57 58 59
};

// A read-only sequence of uninterpreted bytes with reference-counted storage.
60
class {{config.lib.export_macro}} Binary : public {{config.crdtp.namespace}}::Serializable {
61 62 63 64 65
 public:
  Binary(const Binary&);
  Binary();
  ~Binary();

66 67 68
  // Implements Serializable.
  void AppendSerialized(std::vector<uint8_t>* out) const override;

69 70 71 72 73 74 75 76 77 78
  const uint8_t* data() const { return bytes_->front(); }
  size_t size() const { return bytes_->size(); }
  scoped_refptr<base::RefCountedMemory> bytes() const { return bytes_; }

  String toBase64() const;

  static Binary fromBase64(const String& base64, bool* success);
  static Binary fromRefCounted(scoped_refptr<base::RefCountedMemory> memory);
  static Binary fromVector(std::vector<uint8_t> data);
  static Binary fromString(std::string data);
79
  static Binary fromSpan(const uint8_t* data, size_t size);
80 81 82 83 84 85 86 87 88 89 90 91

 private:
  explicit Binary(scoped_refptr<base::RefCountedMemory> bytes);
  scoped_refptr<base::RefCountedMemory> bytes_;
};

std::unique_ptr<Value> toProtocolValue(const base::Value* value, int depth);
std::unique_ptr<base::Value> toBaseValue(Value* value, int depth);
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
namespace {{config.crdtp.namespace}} {

template <>
struct ProtocolTypeTraits<{{"::".join(config.protocol.namespace)}}::String> {
  static bool Deserialize(DeserializerState* state, {{"::".join(config.protocol.namespace)}}::String* value) {
    return {{"::".join(config.protocol.namespace)}}::StringUtil::ReadString(state, value);
  }
  static void Serialize(const {{"::".join(config.protocol.namespace)}}::String& value, std::vector<uint8_t>* bytes) {
    {{"::".join(config.protocol.namespace)}}::StringUtil::WriteString(value, bytes);
  }
};

template <>
struct ProtocolTypeTraits<{{"::".join(config.protocol.namespace)}}::Binary> {
  static bool Deserialize(DeserializerState* state, {{"::".join(config.protocol.namespace)}}::Binary* value);
  static void Serialize(const {{"::".join(config.protocol.namespace)}}::Binary& value, std::vector<uint8_t>* bytes);
};

110 111 112 113 114
template <>
struct {{config.crdtp.namespace}}::detail::MaybeTypedef<{{"::".join(config.protocol.namespace)}}::Binary> {
  typedef ValueMaybe<{{"::".join(config.protocol.namespace)}}::Binary> type;
};

115 116
}  // {{config.crdtp.namespace}}

117
#endif // !defined({{"_".join(config.protocol.namespace)}}_BASE_STRING_ADAPTER_H)