1 // Copyright 2016 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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_H_ 7 8 #include <stdint.h> 9 10 #include <map> 11 #include <set> 12 #include <string> 13 #include <vector> 14 15 #include "base/strings/string_piece.h" 16 #include "mojo/public/cpp/system/handle.h" 17 18 namespace mojo { 19 namespace test { 20 21 struct NestedStructWithTraitsImpl { 22 public: 23 NestedStructWithTraitsImpl(); 24 explicit NestedStructWithTraitsImpl(int32_t in_value); 25 26 bool operator==(const NestedStructWithTraitsImpl& other) const { 27 return value == other.value; 28 } 29 30 int32_t value = 0; 31 }; 32 33 enum class EnumWithTraitsImpl { CUSTOM_VALUE_0 = 10, CUSTOM_VALUE_1 = 11 }; 34 35 // A type which knows how to look like a mojo::test::StructWithTraits mojom type 36 // by way of mojo::StructTraits. 37 class StructWithTraitsImpl { 38 public: 39 StructWithTraitsImpl(); 40 ~StructWithTraitsImpl(); 41 42 StructWithTraitsImpl(const StructWithTraitsImpl& other); 43 44 void set_enum(EnumWithTraitsImpl value) { enum_ = value; } 45 EnumWithTraitsImpl get_enum() const { return enum_; } 46 47 void set_bool(bool value) { bool_ = value; } 48 bool get_bool() const { return bool_; } 49 50 void set_uint32(uint32_t value) { uint32_ = value; } 51 uint32_t get_uint32() const { return uint32_; } 52 53 void set_uint64(uint64_t value) { uint64_ = value; } 54 uint64_t get_uint64() const { return uint64_; } 55 56 void set_string(std::string value) { string_ = value; } 57 base::StringPiece get_string_as_string_piece() const { return string_; } 58 const std::string& get_string() const { return string_; } 59 60 const std::vector<std::string>& get_string_array() const { 61 return string_array_; 62 } 63 std::vector<std::string>& get_mutable_string_array() { return string_array_; } 64 65 const std::set<std::string>& get_string_set() const { 66 return string_set_; 67 } 68 std::set<std::string>& get_mutable_string_set() { return string_set_; } 69 70 const NestedStructWithTraitsImpl& get_struct() const { return struct_; } 71 NestedStructWithTraitsImpl& get_mutable_struct() { return struct_; } 72 73 const std::vector<NestedStructWithTraitsImpl>& get_struct_array() const { 74 return struct_array_; 75 } 76 std::vector<NestedStructWithTraitsImpl>& get_mutable_struct_array() { 77 return struct_array_; 78 } 79 80 const std::map<std::string, NestedStructWithTraitsImpl>& get_struct_map() 81 const { 82 return struct_map_; 83 } 84 std::map<std::string, NestedStructWithTraitsImpl>& get_mutable_struct_map() { 85 return struct_map_; 86 } 87 88 private: 89 EnumWithTraitsImpl enum_ = EnumWithTraitsImpl::CUSTOM_VALUE_0; 90 bool bool_ = false; 91 uint32_t uint32_ = 0; 92 uint64_t uint64_ = 0; 93 std::string string_; 94 std::vector<std::string> string_array_; 95 std::set<std::string> string_set_; 96 NestedStructWithTraitsImpl struct_; 97 std::vector<NestedStructWithTraitsImpl> struct_array_; 98 std::map<std::string, NestedStructWithTraitsImpl> struct_map_; 99 }; 100 101 // A type which corresponds nominally to the 102 // mojo::test::StructWithUnreachableTraits mojom type. Used to test that said 103 // type is never serialized, i.e. objects of this type are simply copied into 104 // a message as-is when written to an intra-process interface. 105 struct StructWithUnreachableTraitsImpl { 106 int32_t magic_number = 0; 107 }; 108 109 // A type which knows how to look like a mojo::test::TrivialStructWithTraits 110 // mojom type by way of mojo::StructTraits. 111 struct TrivialStructWithTraitsImpl { 112 int32_t value; 113 }; 114 115 // A type which knows how to look like a mojo::test::MoveOnlyStructWithTraits 116 // mojom type by way of mojo::StructTraits. 117 class MoveOnlyStructWithTraitsImpl { 118 public: 119 MoveOnlyStructWithTraitsImpl(); 120 MoveOnlyStructWithTraitsImpl(MoveOnlyStructWithTraitsImpl&& other); 121 ~MoveOnlyStructWithTraitsImpl(); 122 123 ScopedHandle& get_mutable_handle() { return handle_; } 124 125 MoveOnlyStructWithTraitsImpl& operator=(MoveOnlyStructWithTraitsImpl&& other); 126 127 private: 128 ScopedHandle handle_; 129 DISALLOW_COPY_AND_ASSIGN(MoveOnlyStructWithTraitsImpl); 130 }; 131 132 class UnionWithTraitsBase { 133 public: 134 enum class Type { INT32, STRUCT }; 135 136 virtual ~UnionWithTraitsBase() {} 137 138 Type type() const { return type_; } 139 140 protected: 141 Type type_ = Type::INT32; 142 }; 143 144 class UnionWithTraitsInt32 : public UnionWithTraitsBase { 145 public: 146 UnionWithTraitsInt32() {} 147 explicit UnionWithTraitsInt32(int32_t value) : value_(value) {} 148 149 ~UnionWithTraitsInt32() override; 150 151 int32_t value() const { return value_; } 152 void set_value(int32_t value) { value_ = value; } 153 154 private: 155 int32_t value_ = 0; 156 }; 157 158 class UnionWithTraitsStruct : public UnionWithTraitsBase { 159 public: 160 UnionWithTraitsStruct() { type_ = Type::STRUCT; } 161 explicit UnionWithTraitsStruct(int32_t value) : struct_(value) { 162 type_ = Type::STRUCT; 163 } 164 ~UnionWithTraitsStruct() override; 165 166 NestedStructWithTraitsImpl& get_mutable_struct() { return struct_; } 167 const NestedStructWithTraitsImpl& get_struct() const { return struct_; } 168 169 private: 170 NestedStructWithTraitsImpl struct_; 171 }; 172 173 class StructForceSerializeImpl { 174 public: 175 StructForceSerializeImpl(); 176 ~StructForceSerializeImpl(); 177 178 void set_value(int32_t value) { value_ = value; } 179 int32_t value() const { return value_; } 180 181 void set_was_serialized() const { was_serialized_ = true; } 182 bool was_serialized() const { return was_serialized_; } 183 184 void set_was_deserialized() { was_deserialized_ = true; } 185 bool was_deserialized() const { return was_deserialized_; } 186 187 private: 188 int32_t value_ = 0; 189 mutable bool was_serialized_ = false; 190 bool was_deserialized_ = false; 191 }; 192 193 class StructNestedForceSerializeImpl { 194 public: 195 StructNestedForceSerializeImpl(); 196 ~StructNestedForceSerializeImpl(); 197 198 StructForceSerializeImpl& force() { return force_; } 199 const StructForceSerializeImpl& force() const { return force_; } 200 201 void set_was_serialized() const { was_serialized_ = true; } 202 bool was_serialized() const { return was_serialized_; } 203 204 void set_was_deserialized() { was_deserialized_ = true; } 205 bool was_deserialized() const { return was_deserialized_; } 206 207 private: 208 StructForceSerializeImpl force_; 209 mutable bool was_serialized_ = false; 210 bool was_deserialized_ = false; 211 }; 212 213 } // namespace test 214 } // namespace mojo 215 216 #endif // MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_H_ 217