chromium/mojo/public/cpp/bindings/lib/array_serialization.h

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
#define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_

#include <stddef.h>
#include <string.h>  // For |memcpy()|.

#include <limits>
#include <type_traits>
#include <utility>
#include <vector>

#include "base/logging.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "mojo/public/cpp/bindings/array_data_view.h"
#include "mojo/public/cpp/bindings/lib/array_internal.h"
#include "mojo/public/cpp/bindings/lib/message_fragment.h"
#include "mojo/public/cpp/bindings/lib/serialization_forward.h"
#include "mojo/public/cpp/bindings/lib/template_util.h"
#include "mojo/public/cpp/bindings/lib/validation_errors.h"

namespace mojo {

class Message;

namespace internal {

template <typename Traits,
          typename MaybeConstUserType,
          bool HasGetBegin =
              HasGetBeginMethod<Traits, MaybeConstUserType>::value>
class ArrayIterator {};

// Used as the UserTypeIterator template parameter of ArraySerializer.
ArrayIterator<Traits, MaybeConstUserType, true>;

// Used as the UserTypeIterator template parameter of ArraySerializer.
ArrayIterator<Traits, MaybeConstUserType, false>;

// ArraySerializer is also used to serialize map keys and values. Therefore, it
// has a UserTypeIterator parameter which is an adaptor for reading to hide the
// difference between ArrayTraits and MapTraits.
template <typename MojomType,
          typename MaybeConstUserType,
          typename UserTypeIterator,
          typename EnableType = void>
struct ArraySerializer;

// Handles serialization and deserialization of arrays of pod types.
ArraySerializer<MojomType, MaybeConstUserType, UserTypeIterator, std::enable_if_t<BelongsTo<typename MojomType::Element, MojomTypeCategory::kPOD>::value>>;

// Handles serialization and deserialization of arrays of enum types.
ArraySerializer<MojomType, MaybeConstUserType, UserTypeIterator, typename std::enable_if<BelongsTo<typename MojomType::Element, MojomTypeCategory::kEnum>::value>::type>;

// Handles serialization and deserialization of arrays of optional enum types.
ArraySerializer<MojomType, MaybeConstUserType, UserTypeIterator, std::enable_if_t<BelongsTo<typename MojomType::Element, MojomTypeCategory::kEnum>::value>>;

// Serializes and deserializes arrays of bools.
ArraySerializer<MojomType, MaybeConstUserType, UserTypeIterator, typename std::enable_if<BelongsTo<typename MojomType::Element, MojomTypeCategory::kBoolean>::value>::type>;

// Serializes and deserializes arrays of handles or interfaces.
ArraySerializer<MojomType, MaybeConstUserType, UserTypeIterator, typename std::enable_if<BelongsTo<typename MojomType::Element, MojomTypeCategory::kAssociatedInterface | MojomTypeCategory::kAssociatedInterfaceRequest | MojomTypeCategory::kHandle | MojomTypeCategory::kInterface | MojomTypeCategory::kInterfaceRequest>::value>::type>;

// This template must only apply to pointer mojo entity (strings, structs,
// arrays and maps).
ArraySerializer<MojomType, MaybeConstUserType, UserTypeIterator, typename std::enable_if<BelongsTo<typename MojomType::Element, MojomTypeCategory::kArray | MojomTypeCategory::kMap | MojomTypeCategory::kString | MojomTypeCategory::kStruct>::value>::type>;

// Handles serialization and deserialization of arrays of unions.
ArraySerializer<MojomType, MaybeConstUserType, UserTypeIterator, typename std::enable_if<BelongsTo<typename MojomType::Element, MojomTypeCategory::kUnion>::value>::type>;

Serializer<ArrayDataView<Element>, MaybeConstUserType>;

}  // namespace internal
}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_