Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1227)

Unified Diff: mojo/public/cpp/bindings/lib/map_data_internal.h

Issue 611633002: mojom: Add associative arrays to the mojom language. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Suppress pylint warnings instead of making MapKind public. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/cpp/bindings/lib/map_data_internal.h
diff --git a/mojo/public/cpp/bindings/lib/map_data_internal.h b/mojo/public/cpp/bindings/lib/map_data_internal.h
new file mode 100644
index 0000000000000000000000000000000000000000..2754f11b3078aae265f78085fce979b6f6f22bb7
--- /dev/null
+++ b/mojo/public/cpp/bindings/lib/map_data_internal.h
@@ -0,0 +1,123 @@
+// Copyright 2014 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_LIB_MAP_DATA_INTERNAL_H_
+#define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_
+
+#include "mojo/public/cpp/bindings/lib/array_internal.h"
+#include "mojo/public/cpp/bindings/lib/validate_params.h"
+#include "mojo/public/cpp/bindings/lib/validation_errors.h"
+
+namespace mojo {
+namespace internal {
+
+template <typename MapKey>
+struct MapDataKeyValidateParams {
+ public:
+ typedef NoValidateParams ElementValidateParams;
+ static const uint32_t expected_num_elements = 0;
+ static const bool element_is_nullable = false;
+};
+
+template <>
+struct MapDataKeyValidateParams<mojo::internal::Array_Data<char>*> {
+ public:
+ typedef ArrayValidateParams<0, false, NoValidateParams> ElementValidateParams;
+ static const uint32_t expected_num_elements = 0;
+ static const bool element_is_nullable = false;
+};
+
+// Map serializes into a struct which has two arrays as struct fields, the keys
+// and the values.
+template <typename First, typename Second>
+class Map_Data {
+ public:
+ static Map_Data* New(mojo::internal::Buffer* buf) {
+ return new (buf->Allocate(sizeof(Map_Data))) Map_Data();
+ }
+
+ template <typename ValueParams>
+ static bool Validate(const void* data,
+ mojo::internal::BoundsChecker* bounds_checker) {
+ if (!data)
+ return true;
+
+ if (!ValidateStructHeader(data, sizeof(Map_Data), 2, bounds_checker))
+ return false;
+
+ const Map_Data* MOJO_ALLOW_UNUSED object =
+ static_cast<const Map_Data*>(data);
+ if (!mojo::internal::ValidateEncodedPointer(&object->first.offset)) {
+ ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER);
+ return false;
+ }
+ if (!object->first.offset) {
+ ReportValidationError(
+ mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
+ "null key array in map struct");
+ return false;
+ }
+ if (!internal::Array_Data<First>::template Validate<
+ MapDataKeyValidateParams<First>>(
+ mojo::internal::DecodePointerRaw(&object->first.offset),
+ bounds_checker)) {
+ return false;
+ }
+
+ if (!mojo::internal::ValidateEncodedPointer(&object->second.offset)) {
+ ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER);
+ return false;
+ }
+ if (!object->second.offset) {
+ ReportValidationError(
+ mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
+ "null value array in map struct");
+ return false;
+ }
+ if (!internal::Array_Data<Second>::template Validate<ValueParams>(
+ mojo::internal::DecodePointerRaw(&object->second.offset),
+ bounds_checker)) {
+ return false;
+ }
+
+ const ArrayHeader* first_header = static_cast<const ArrayHeader*>(
+ mojo::internal::DecodePointerRaw(&object->first.offset));
+ const ArrayHeader* second_header = static_cast<const ArrayHeader*>(
+ mojo::internal::DecodePointerRaw(&object->second.offset));
+ if (first_header->num_elements != second_header->num_elements) {
+ ReportValidationError(
+ mojo::internal::VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP);
+ return false;
+ }
+
+ return true;
+ }
+
+ mojo::internal::StructHeader header_;
+
+ mojo::internal::ArrayPointer<First> first;
+ mojo::internal::ArrayPointer<Second> second;
+
+ void EncodePointersAndHandles(std::vector<mojo::Handle>* handles) {
+ mojo::internal::Encode(&first, handles);
+ mojo::internal::Encode(&second, handles);
+ }
+
+ void DecodePointersAndHandles(std::vector<mojo::Handle>* handles) {
+ mojo::internal::Decode(&first, handles);
+ mojo::internal::Decode(&second, handles);
+ }
+
+ private:
+ Map_Data() {
+ header_.num_bytes = sizeof(*this);
+ header_.num_fields = 2;
+ }
+ ~Map_Data() = delete;
+};
+
+} // namespace internal
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_

Powered by Google App Engine
This is Rietveld 408576698