aboutsummaryrefslogtreecommitdiff
path: root/mojo/public/cpp/bindings/tests/struct_with_traits_impl_traits.cc
blob: 6b770b1a49bd30fddd57ee009c6fb8bcea0c5dc0 (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
// 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.

#include "mojo/public/cpp/bindings/tests/struct_with_traits_impl_traits.h"

namespace mojo {
namespace {

struct Context {
  int32_t value;
};

}  // namespace

// static
void* StructTraits<test::NestedStructWithTraitsDataView,
                   test::NestedStructWithTraitsImpl>::
    SetUpContext(const test::NestedStructWithTraitsImpl& input) {
  Context* context = new Context;
  context->value = input.value;
  return context;
}

// static
void StructTraits<test::NestedStructWithTraitsDataView,
                  test::NestedStructWithTraitsImpl>::
    TearDownContext(const test::NestedStructWithTraitsImpl& input,
                    void* context) {
  Context* context_obj = static_cast<Context*>(context);
  CHECK_EQ(context_obj->value, input.value);
  delete context_obj;
}

// static
int32_t StructTraits<test::NestedStructWithTraitsDataView,
                     test::NestedStructWithTraitsImpl>::
    value(const test::NestedStructWithTraitsImpl& input, void* context) {
  Context* context_obj = static_cast<Context*>(context);
  CHECK_EQ(context_obj->value, input.value);
  return input.value;
}

// static
bool StructTraits<test::NestedStructWithTraitsDataView,
                  test::NestedStructWithTraitsImpl>::
    Read(test::NestedStructWithTraits::DataView data,
         test::NestedStructWithTraitsImpl* output) {
  output->value = data.value();
  return true;
}

test::EnumWithTraits
EnumTraits<test::EnumWithTraits, test::EnumWithTraitsImpl>::ToMojom(
    test::EnumWithTraitsImpl input) {
  switch (input) {
    case test::EnumWithTraitsImpl::CUSTOM_VALUE_0:
      return test::EnumWithTraits::VALUE_0;
    case test::EnumWithTraitsImpl::CUSTOM_VALUE_1:
      return test::EnumWithTraits::VALUE_1;
  };

  NOTREACHED();
  return test::EnumWithTraits::VALUE_0;
}

bool EnumTraits<test::EnumWithTraits, test::EnumWithTraitsImpl>::FromMojom(
    test::EnumWithTraits input,
    test::EnumWithTraitsImpl* output) {
  switch (input) {
    case test::EnumWithTraits::VALUE_0:
      *output = test::EnumWithTraitsImpl::CUSTOM_VALUE_0;
      return true;
    case test::EnumWithTraits::VALUE_1:
      *output = test::EnumWithTraitsImpl::CUSTOM_VALUE_1;
      return true;
  };

  return false;
}

// static
bool StructTraits<test::StructWithTraitsDataView, test::StructWithTraitsImpl>::
    Read(test::StructWithTraits::DataView data,
         test::StructWithTraitsImpl* out) {
  test::EnumWithTraitsImpl f_enum;
  if (!data.ReadFEnum(&f_enum))
    return false;
  out->set_enum(f_enum);

  out->set_bool(data.f_bool());
  out->set_uint32(data.f_uint32());
  out->set_uint64(data.f_uint64());

  base::StringPiece f_string;
  std::string f_string2;
  if (!data.ReadFString(&f_string) || !data.ReadFString2(&f_string2) ||
      f_string != f_string2) {
    return false;
  }
  out->set_string(f_string2);

  if (!data.ReadFStringArray(&out->get_mutable_string_array()))
    return false;

  // We can't deserialize as a std::set, so we have to manually copy from the
  // data view.
  ArrayDataView<StringDataView> string_set_data_view;
  data.GetFStringSetDataView(&string_set_data_view);
  for (size_t i = 0; i < string_set_data_view.size(); ++i) {
    std::string value;
    string_set_data_view.Read(i, &value);
    out->get_mutable_string_set().insert(value);
  }

  if (!data.ReadFStruct(&out->get_mutable_struct()))
    return false;

  if (!data.ReadFStructArray(&out->get_mutable_struct_array()))
    return false;

  if (!data.ReadFStructMap(&out->get_mutable_struct_map()))
    return false;

  return true;
}

// static
bool StructTraits<test::MoveOnlyStructWithTraitsDataView,
                  test::MoveOnlyStructWithTraitsImpl>::
    Read(test::MoveOnlyStructWithTraits::DataView data,
         test::MoveOnlyStructWithTraitsImpl* out) {
  out->get_mutable_handle() = data.TakeFHandle();
  return true;
}

}  // namespace mojo