sammc | 94ef796 | 2016-12-21 17:55:41 +0900 | [diff] [blame] | 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_COMMON_VALUES_STRUCT_TRAITS_H_ |
| 6 | #define MOJO_COMMON_VALUES_STRUCT_TRAITS_H_ |
| 7 | |
| 8 | #include "base/values.h" |
| 9 | #include "mojo/common/values.mojom.h" |
| 10 | #include "mojo/public/cpp/bindings/array_traits.h" |
chfremer | 839b6bf | 2017-02-24 02:57:03 +0900 | [diff] [blame] | 11 | #include "mojo/public/cpp/bindings/clone_traits.h" |
sammc | 94ef796 | 2016-12-21 17:55:41 +0900 | [diff] [blame] | 12 | #include "mojo/public/cpp/bindings/map_traits.h" |
| 13 | #include "mojo/public/cpp/bindings/struct_traits.h" |
| 14 | #include "mojo/public/cpp/bindings/union_traits.h" |
| 15 | |
| 16 | namespace mojo { |
| 17 | |
| 18 | template <> |
| 19 | struct ArrayTraits<base::ListValue> { |
jdoerrie | 4518400 | 2017-04-12 03:09:14 +0900 | [diff] [blame^] | 20 | using Element = base::Value; |
sammc | 94ef796 | 2016-12-21 17:55:41 +0900 | [diff] [blame] | 21 | using ConstIterator = base::ListValue::const_iterator; |
| 22 | |
| 23 | static size_t GetSize(const base::ListValue& input) { |
| 24 | return input.GetSize(); |
| 25 | } |
| 26 | |
| 27 | static ConstIterator GetBegin(const base::ListValue& input) { |
| 28 | return input.begin(); |
| 29 | } |
| 30 | |
| 31 | static void AdvanceIterator(ConstIterator& iterator) { ++iterator; } |
| 32 | |
| 33 | static const Element& GetValue(ConstIterator& iterator) { return *iterator; } |
| 34 | }; |
| 35 | |
| 36 | template <> |
| 37 | struct StructTraits<common::mojom::ListValueDataView, base::ListValue> { |
| 38 | static const base::ListValue& values(const base::ListValue& value) { |
| 39 | return value; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | template <> |
| 44 | struct StructTraits<common::mojom::ListValueDataView, |
| 45 | std::unique_ptr<base::ListValue>> { |
| 46 | static bool IsNull(const std::unique_ptr<base::ListValue>& value) { |
| 47 | return !value; |
| 48 | } |
| 49 | |
| 50 | static void SetToNull(std::unique_ptr<base::ListValue>* value) { |
| 51 | value->reset(); |
| 52 | } |
| 53 | |
| 54 | static const base::ListValue& values( |
| 55 | const std::unique_ptr<base::ListValue>& value) { |
| 56 | return *value; |
| 57 | } |
| 58 | |
| 59 | static bool Read(common::mojom::ListValueDataView data, |
| 60 | std::unique_ptr<base::ListValue>* value); |
| 61 | }; |
| 62 | |
| 63 | template <> |
| 64 | struct MapTraits<base::DictionaryValue> { |
| 65 | using Key = std::string; |
| 66 | using Value = base::Value; |
| 67 | using Iterator = base::DictionaryValue::Iterator; |
| 68 | |
| 69 | static size_t GetSize(const base::DictionaryValue& input) { |
| 70 | return input.size(); |
| 71 | } |
| 72 | |
| 73 | static Iterator GetBegin(const base::DictionaryValue& input) { |
| 74 | return Iterator(input); |
| 75 | } |
| 76 | |
| 77 | static void AdvanceIterator(Iterator& iterator) { iterator.Advance(); } |
| 78 | |
| 79 | static const Key& GetKey(Iterator& iterator) { return iterator.key(); } |
| 80 | |
| 81 | static const Value& GetValue(Iterator& iterator) { return iterator.value(); } |
| 82 | }; |
| 83 | |
| 84 | template <> |
| 85 | struct StructTraits<common::mojom::DictionaryValueDataView, |
| 86 | base::DictionaryValue> { |
| 87 | static const base::DictionaryValue& values( |
| 88 | const base::DictionaryValue& value) { |
| 89 | return value; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | template <> |
| 94 | struct StructTraits<common::mojom::DictionaryValueDataView, |
| 95 | std::unique_ptr<base::DictionaryValue>> { |
| 96 | static bool IsNull(const std::unique_ptr<base::DictionaryValue>& value) { |
| 97 | return !value; |
| 98 | } |
| 99 | |
| 100 | static void SetToNull(std::unique_ptr<base::DictionaryValue>* value) { |
| 101 | value->reset(); |
| 102 | } |
| 103 | |
| 104 | static const base::DictionaryValue& values( |
| 105 | const std::unique_ptr<base::DictionaryValue>& value) { |
| 106 | return *value; |
| 107 | } |
| 108 | static bool Read(common::mojom::DictionaryValueDataView data, |
| 109 | std::unique_ptr<base::DictionaryValue>* value); |
| 110 | }; |
| 111 | |
| 112 | template <> |
chfremer | 839b6bf | 2017-02-24 02:57:03 +0900 | [diff] [blame] | 113 | struct CloneTraits<std::unique_ptr<base::DictionaryValue>, false> { |
| 114 | static std::unique_ptr<base::DictionaryValue> Clone( |
| 115 | const std::unique_ptr<base::DictionaryValue>& input); |
| 116 | }; |
| 117 | |
| 118 | template <> |
sammc | 94ef796 | 2016-12-21 17:55:41 +0900 | [diff] [blame] | 119 | struct UnionTraits<common::mojom::ValueDataView, base::Value> { |
| 120 | static common::mojom::ValueDataView::Tag GetTag(const base::Value& data) { |
| 121 | switch (data.GetType()) { |
| 122 | case base::Value::Type::NONE: |
| 123 | return common::mojom::ValueDataView::Tag::NULL_VALUE; |
| 124 | case base::Value::Type::BOOLEAN: |
| 125 | return common::mojom::ValueDataView::Tag::BOOL_VALUE; |
| 126 | case base::Value::Type::INTEGER: |
| 127 | return common::mojom::ValueDataView::Tag::INT_VALUE; |
| 128 | case base::Value::Type::DOUBLE: |
| 129 | return common::mojom::ValueDataView::Tag::DOUBLE_VALUE; |
| 130 | case base::Value::Type::STRING: |
| 131 | return common::mojom::ValueDataView::Tag::STRING_VALUE; |
| 132 | case base::Value::Type::BINARY: |
| 133 | return common::mojom::ValueDataView::Tag::BINARY_VALUE; |
| 134 | case base::Value::Type::DICTIONARY: |
| 135 | return common::mojom::ValueDataView::Tag::DICTIONARY_VALUE; |
| 136 | case base::Value::Type::LIST: |
| 137 | return common::mojom::ValueDataView::Tag::LIST_VALUE; |
| 138 | } |
| 139 | NOTREACHED(); |
| 140 | return common::mojom::ValueDataView::Tag::NULL_VALUE; |
| 141 | } |
| 142 | |
| 143 | static common::mojom::NullValuePtr null_value(const base::Value& value) { |
| 144 | return common::mojom::NullValuePtr(); |
| 145 | } |
| 146 | |
| 147 | static bool bool_value(const base::Value& value) { |
| 148 | bool bool_value{}; |
| 149 | if (!value.GetAsBoolean(&bool_value)) |
| 150 | NOTREACHED(); |
| 151 | return bool_value; |
| 152 | } |
| 153 | |
| 154 | static int32_t int_value(const base::Value& value) { |
| 155 | int int_value{}; |
| 156 | if (!value.GetAsInteger(&int_value)) |
| 157 | NOTREACHED(); |
| 158 | return int_value; |
| 159 | } |
| 160 | |
| 161 | static double double_value(const base::Value& value) { |
| 162 | double double_value{}; |
| 163 | if (!value.GetAsDouble(&double_value)) |
| 164 | NOTREACHED(); |
| 165 | return double_value; |
| 166 | } |
| 167 | |
| 168 | static base::StringPiece string_value(const base::Value& value) { |
| 169 | base::StringPiece string_piece; |
| 170 | if (!value.GetAsString(&string_piece)) |
| 171 | NOTREACHED(); |
| 172 | return string_piece; |
| 173 | } |
| 174 | |
| 175 | static mojo::ConstCArray<uint8_t> binary_value(const base::Value& value) { |
jdoerrie | c56cc7f | 2017-04-11 16:45:50 +0900 | [diff] [blame] | 176 | const base::Value* binary_value = nullptr; |
sammc | 94ef796 | 2016-12-21 17:55:41 +0900 | [diff] [blame] | 177 | if (!value.GetAsBinary(&binary_value)) |
| 178 | NOTREACHED(); |
| 179 | return mojo::ConstCArray<uint8_t>( |
| 180 | binary_value->GetSize(), |
| 181 | reinterpret_cast<const uint8_t*>(binary_value->GetBuffer())); |
| 182 | } |
| 183 | |
| 184 | static const base::ListValue& list_value(const base::Value& value) { |
| 185 | const base::ListValue* list_value = nullptr; |
| 186 | if (!value.GetAsList(&list_value)) |
| 187 | NOTREACHED(); |
| 188 | return *list_value; |
| 189 | } |
| 190 | static const base::DictionaryValue& dictionary_value( |
| 191 | const base::Value& value) { |
| 192 | const base::DictionaryValue* dictionary_value = nullptr; |
| 193 | if (!value.GetAsDictionary(&dictionary_value)) |
| 194 | NOTREACHED(); |
| 195 | return *dictionary_value; |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | template <> |
| 200 | struct UnionTraits<common::mojom::ValueDataView, std::unique_ptr<base::Value>> { |
| 201 | static bool IsNull(const std::unique_ptr<base::Value>& value) { |
| 202 | return !value; |
| 203 | } |
| 204 | |
| 205 | static void SetToNull(std::unique_ptr<base::Value>* value) { value->reset(); } |
| 206 | |
| 207 | static common::mojom::ValueDataView::Tag GetTag( |
| 208 | const std::unique_ptr<base::Value>& value) { |
| 209 | return UnionTraits<common::mojom::ValueDataView, base::Value>::GetTag( |
| 210 | *value); |
| 211 | } |
| 212 | |
| 213 | static common::mojom::NullValuePtr null_value( |
| 214 | const std::unique_ptr<base::Value>& value) { |
| 215 | return UnionTraits<common::mojom::ValueDataView, base::Value>::null_value( |
| 216 | *value); |
| 217 | } |
| 218 | static bool bool_value(const std::unique_ptr<base::Value>& value) { |
| 219 | return UnionTraits<common::mojom::ValueDataView, base::Value>::bool_value( |
| 220 | *value); |
| 221 | } |
| 222 | static int32_t int_value(const std::unique_ptr<base::Value>& value) { |
| 223 | return UnionTraits<common::mojom::ValueDataView, base::Value>::int_value( |
| 224 | *value); |
| 225 | } |
| 226 | static double double_value(const std::unique_ptr<base::Value>& value) { |
| 227 | return UnionTraits<common::mojom::ValueDataView, base::Value>::double_value( |
| 228 | *value); |
| 229 | } |
| 230 | static base::StringPiece string_value( |
| 231 | const std::unique_ptr<base::Value>& value) { |
| 232 | return UnionTraits<common::mojom::ValueDataView, base::Value>::string_value( |
| 233 | *value); |
| 234 | } |
| 235 | static mojo::ConstCArray<uint8_t> binary_value( |
| 236 | const std::unique_ptr<base::Value>& value) { |
| 237 | return UnionTraits<common::mojom::ValueDataView, base::Value>::binary_value( |
| 238 | *value); |
| 239 | } |
| 240 | static const base::ListValue& list_value( |
| 241 | const std::unique_ptr<base::Value>& value) { |
| 242 | return UnionTraits<common::mojom::ValueDataView, base::Value>::list_value( |
| 243 | *value); |
| 244 | } |
| 245 | static const base::DictionaryValue& dictionary_value( |
| 246 | const std::unique_ptr<base::Value>& value) { |
| 247 | return UnionTraits<common::mojom::ValueDataView, |
| 248 | base::Value>::dictionary_value(*value); |
| 249 | } |
| 250 | |
| 251 | static bool Read(common::mojom::ValueDataView data, |
| 252 | std::unique_ptr<base::Value>* value); |
| 253 | }; |
| 254 | |
| 255 | } // namespace mojo |
| 256 | |
| 257 | #endif // MOJO_COMMON_VALUES_STRUCT_TRAITS_H_ |