aboutsummaryrefslogtreecommitdiff
path: root/mojo/public/cpp/bindings/tests/struct_with_traits_impl_traits.h
blob: adcad8aa9e42442c19ee98775c1484b3b34e53b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2016 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 MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_TRAITS_H_
#define MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_TRAITS_H_

#include <stdint.h>

#include <string>
#include <vector>

#include "base/strings/string_piece.h"
#include "mojo/public/cpp/bindings/struct_traits.h"
#include "mojo/public/cpp/bindings/tests/struct_with_traits_impl.h"
#include "mojo/public/interfaces/bindings/tests/struct_with_traits.mojom.h"

namespace mojo {

template <>
struct StructTraits<test::NestedStructWithTraitsDataView,
                    test::NestedStructWithTraitsImpl> {
  static void* SetUpContext(const test::NestedStructWithTraitsImpl& input);
  static void TearDownContext(const test::NestedStructWithTraitsImpl& input,
                              void* context);

  static int32_t value(const test::NestedStructWithTraitsImpl& input,
                       void* context);

  static bool Read(test::NestedStructWithTraitsDataView data,
                   test::NestedStructWithTraitsImpl* output);
};

template <>
struct EnumTraits<test::EnumWithTraits, test::EnumWithTraitsImpl> {
  static test::EnumWithTraits ToMojom(test::EnumWithTraitsImpl input);
  static bool FromMojom(test::EnumWithTraits input,
                        test::EnumWithTraitsImpl* output);
};

template <>
struct StructTraits<test::StructWithTraitsDataView,
                    test::StructWithTraitsImpl> {
  // Deserialization to test::StructTraitsImpl.
  static bool Read(test::StructWithTraitsDataView data,
                   test::StructWithTraitsImpl* out);

  // Fields in test::StructWithTraits.
  // See src/mojo/public/interfaces/bindings/tests/struct_with_traits.mojom.
  static test::EnumWithTraitsImpl f_enum(
      const test::StructWithTraitsImpl& value) {
    return value.get_enum();
  }

  static bool f_bool(const test::StructWithTraitsImpl& value) {
    return value.get_bool();
  }

  static uint32_t f_uint32(const test::StructWithTraitsImpl& value) {
    return value.get_uint32();
  }

  static uint64_t f_uint64(const test::StructWithTraitsImpl& value) {
    return value.get_uint64();
  }

  static base::StringPiece f_string(const test::StructWithTraitsImpl& value) {
    return value.get_string_as_string_piece();
  }

  static const std::string& f_string2(const test::StructWithTraitsImpl& value) {
    return value.get_string();
  }

  static const std::vector<std::string>& f_string_array(
      const test::StructWithTraitsImpl& value) {
    return value.get_string_array();
  }

  static const std::set<std::string>& f_string_set(
      const test::StructWithTraitsImpl& value) {
    return value.get_string_set();
  }

  static const test::NestedStructWithTraitsImpl& f_struct(
      const test::StructWithTraitsImpl& value) {
    return value.get_struct();
  }

  static const std::vector<test::NestedStructWithTraitsImpl>& f_struct_array(
      const test::StructWithTraitsImpl& value) {
    return value.get_struct_array();
  }

  static const std::map<std::string, test::NestedStructWithTraitsImpl>&
  f_struct_map(const test::StructWithTraitsImpl& value) {
    return value.get_struct_map();
  }
};

template <>
struct StructTraits<test::TrivialStructWithTraitsDataView,
                    test::TrivialStructWithTraitsImpl> {
  // Deserialization to test::TrivialStructTraitsImpl.
  static bool Read(test::TrivialStructWithTraitsDataView data,
                   test::TrivialStructWithTraitsImpl* out) {
    out->value = data.value();
    return true;
  }

  // Fields in test::TrivialStructWithTraits.
  // See src/mojo/public/interfaces/bindings/tests/struct_with_traits.mojom.
  static int32_t value(test::TrivialStructWithTraitsImpl& input) {
    return input.value;
  }
};

template <>
struct StructTraits<test::MoveOnlyStructWithTraitsDataView,
                    test::MoveOnlyStructWithTraitsImpl> {
  // Deserialization to test::MoveOnlyStructTraitsImpl.
  static bool Read(test::MoveOnlyStructWithTraitsDataView data,
                   test::MoveOnlyStructWithTraitsImpl* out);

  // Fields in test::MoveOnlyStructWithTraits.
  // See src/mojo/public/interfaces/bindings/tests/struct_with_traits.mojom.
  static ScopedHandle f_handle(test::MoveOnlyStructWithTraitsImpl& value) {
    return std::move(value.get_mutable_handle());
  }
};

template <>
struct StructTraits<test::StructWithTraitsForUniquePtrDataView,
                    std::unique_ptr<int>> {
  static bool IsNull(const std::unique_ptr<int>& data) { return !data; }
  static void SetToNull(std::unique_ptr<int>* data) { data->reset(); }

  static int f_int32(const std::unique_ptr<int>& data) { return *data; }

  static bool Read(test::StructWithTraitsForUniquePtrDataView data,
                   std::unique_ptr<int>* out) {
    out->reset(new int(data.f_int32()));
    return true;
  }
};

template <>
struct UnionTraits<test::UnionWithTraitsDataView,
                   std::unique_ptr<test::UnionWithTraitsBase>> {
  static bool IsNull(const std::unique_ptr<test::UnionWithTraitsBase>& data) {
    return !data;
  }
  static void SetToNull(std::unique_ptr<test::UnionWithTraitsBase>* data) {
    data->reset();
  }

  static test::UnionWithTraitsDataView::Tag GetTag(
      const std::unique_ptr<test::UnionWithTraitsBase>& data) {
    if (data->type() == test::UnionWithTraitsBase::Type::INT32)
      return test::UnionWithTraitsDataView::Tag::F_INT32;

    return test::UnionWithTraitsDataView::Tag::F_STRUCT;
  }

  static int32_t f_int32(
      const std::unique_ptr<test::UnionWithTraitsBase>& data) {
    return static_cast<test::UnionWithTraitsInt32*>(data.get())->value();
  }

  static const test::NestedStructWithTraitsImpl& f_struct(
      const std::unique_ptr<test::UnionWithTraitsBase>& data) {
    return static_cast<test::UnionWithTraitsStruct*>(data.get())->get_struct();
  }

  static bool Read(test::UnionWithTraitsDataView data,
                   std::unique_ptr<test::UnionWithTraitsBase>* out) {
    switch (data.tag()) {
      case test::UnionWithTraitsDataView::Tag::F_INT32: {
        out->reset(new test::UnionWithTraitsInt32(data.f_int32()));
        return true;
      }
      case test::UnionWithTraitsDataView::Tag::F_STRUCT: {
        auto* struct_object = new test::UnionWithTraitsStruct();
        out->reset(struct_object);
        return data.ReadFStruct(&struct_object->get_mutable_struct());
      }
    }

    NOTREACHED();
    return false;
  }
};

}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_TRAITS_H_