// basic_union.test-mojom.h is auto generated by mojom_bindings_generator.py, do not edit

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

#ifndef BASIC_UNION_TEST_MOJOM_H_
#define BASIC_UNION_TEST_MOJOM_H_

#include <stdint.h>

#include <limits>
#include <optional>
#include <type_traits>
#include <utility>
#include "mojo/public/cpp/bindings/clone_traits.h"
#include "mojo/public/cpp/bindings/equals_traits.h"
#include "mojo/public/cpp/bindings/struct_ptr.h"
#include "mojo/public/cpp/bindings/struct_traits.h"
#include "mojo/public/cpp/bindings/union_traits.h"
#include "mojo/public/cpp/bindings/lib/serialization.h"

#include "third_party/perfetto/include/perfetto/tracing/traced_value_forward.h"

#include "basic_union.test-mojom-features.h"  // IWYU pragma: export
#include "basic_union.test-mojom-shared.h"  // IWYU pragma: export
#include "basic_union.test-mojom-forward.h"  // IWYU pragma: export
#include <string>
#include <vector>








namespace golden {





class  StructInUnion {
 public:
  template <typename T>
  using EnableIfSame = std::enable_if_t<std::is_same<StructInUnion, T>::value>;
  using DataView = StructInUnionDataView;
  using Data_ = internal::StructInUnion_Data;

  template <typename... Args>
  static StructInUnionPtr New(Args&&... args) {
    return StructInUnionPtr(
        std::in_place, std::forward<Args>(args)...);
  }

  template <typename U>
  static StructInUnionPtr From(const U& u) {
    return mojo::TypeConverter<StructInUnionPtr, U>::Convert(u);
  }

  template <typename U>
  U To() const {
    return mojo::TypeConverter<U, StructInUnion>::Convert(*this);
  }


  StructInUnion();

  StructInUnion(
      bool flag,
      const std::string& value);


  ~StructInUnion();

  // Clone() is a template so it is only instantiated if it is used. Thus, the
  // bindings generator does not need to know whether Clone() or copy
  // constructor/assignment are available for members.
  template <typename StructPtrType = StructInUnionPtr>
  StructInUnionPtr Clone() const;

  // Equals() is a template so it is only instantiated if it is used. Thus, the
  // bindings generator does not need to know whether Equals() or == operator
  // are available for members.
  template <typename T, StructInUnion::EnableIfSame<T>* = nullptr>
  bool Equals(const T& other) const;

  template <typename T, StructInUnion::EnableIfSame<T>* = nullptr>
  bool operator==(const T& rhs) const { return Equals(rhs); }

  template <typename T, StructInUnion::EnableIfSame<T>* = nullptr>
  bool operator!=(const T& rhs) const { return !operator==(rhs); }
  size_t Hash(size_t seed) const;

  template <mojo::internal::SendValidation send_validation, typename UserType>
  static std::vector<uint8_t> Serialize(UserType* input) {
    return mojo::internal::SerializeImpl<
        StructInUnion::DataView, std::vector<uint8_t>, send_validation>(input);
  }

  template <typename UserType>
  static std::vector<uint8_t> Serialize(UserType* input) {
    return mojo::internal::SerializeImpl<
        StructInUnion::DataView, std::vector<uint8_t>>(input);
  }

  template <typename UserType>
  static mojo::Message SerializeAsMessage(UserType* input) {
    return mojo::internal::SerializeAsMessageImpl<
        StructInUnion::DataView>(input);
  }

  // The returned Message is serialized only if the message is moved
  // cross-process or cross-language. Otherwise if the message is Deserialized
  // as the same UserType |input| will just be moved to |output| in
  // DeserializeFromMessage.
  template <typename UserType>
  static mojo::Message WrapAsMessage(UserType input) {
    return mojo::Message(std::make_unique<
        internal::StructInUnion_UnserializedMessageContext<
            UserType, StructInUnion::DataView>>(0, 0, std::move(input)),
        MOJO_CREATE_MESSAGE_FLAG_NONE);
  }

  template <typename UserType>
  static bool Deserialize(const void* data,
                          size_t data_num_bytes,
                          UserType* output) {
    mojo::Message message;
    return mojo::internal::DeserializeImpl<StructInUnion::DataView>(
        message, data, data_num_bytes, output, Validate);
  }

  template <typename UserType>
  static bool Deserialize(base::span<const uint8_t> input,
                          UserType* output) {
    return StructInUnion::Deserialize(
        input.empty() ? nullptr : input.data(), input.size(), output);
  }

  template <typename UserType>
  static bool DeserializeFromMessage(mojo::Message input,
                                     UserType* output) {
    auto context = input.TakeUnserializedContext<
        internal::StructInUnion_UnserializedMessageContext<
            UserType, StructInUnion::DataView>>();
    if (context) {
      *output = std::move(context->TakeData());
      return true;
    }
    input.SerializeIfNecessary();
    return mojo::internal::DeserializeImpl<StructInUnion::DataView>(
        input, input.payload(), input.payload_num_bytes(), output, Validate);
  }

  
  bool flag;
  
  std::string value;

  // Serialise this struct into a trace.
  void WriteIntoTrace(perfetto::TracedValue traced_context) const;

 private:
  static bool Validate(const void* data,
                       mojo::internal::ValidationContext* validation_context);
};

// The comparison operators are templates, so they are only instantiated if they
// are used. Thus, the bindings generator does not need to know whether
// comparison operators are available for members.
template <typename T, StructInUnion::EnableIfSame<T>* = nullptr>
bool operator<(const T& lhs, const T& rhs);

template <typename T, StructInUnion::EnableIfSame<T>* = nullptr>
bool operator<=(const T& lhs, const T& rhs) {
  return !(rhs < lhs);
}

template <typename T, StructInUnion::EnableIfSame<T>* = nullptr>
bool operator>(const T& lhs, const T& rhs) {
  return rhs < lhs;
}

template <typename T, StructInUnion::EnableIfSame<T>* = nullptr>
bool operator>=(const T& lhs, const T& rhs) {
  return !(lhs < rhs);
}





class  BasicUnion {
 public:
  using DataView = BasicUnionDataView;
  using Data_ = internal::BasicUnion_Data;
  using Tag = Data_::BasicUnion_Tag;

  template <typename... Args>
  static BasicUnionPtr New(Args&&... args) {
    static_assert(
        sizeof...(args) < 0,
        "Do not use Union::New(); to create a union of a given subtype, use "
        "New<SubType>(), not New() followed by set_<sub_type>(). To represent "
        "an empty union, mark the field or parameter as nullable in the mojom "
        "definition.");
    return nullptr;
  }

  // Construct an instance holding |numeric_value|.
  static BasicUnionPtr NewNumericValue(
      int32_t value);
  // Construct an instance holding |enum_value|.
  static BasicUnionPtr NewEnumValue(
      EnumInUnion value);
  // Construct an instance holding |struct_value|.
  static BasicUnionPtr NewStructValue(
      StructInUnionPtr value);

  template <typename U>
  static BasicUnionPtr From(const U& u) {
    return mojo::TypeConverter<BasicUnionPtr, U>::Convert(u);
  }

  template <typename U>
  U To() const {
    return mojo::TypeConverter<U, BasicUnion>::Convert(*this);
  }
  ~BasicUnion();

  // Delete the copy constructor and copy assignment operators because `data_`
  // contains raw pointers that must not be copied.
  BasicUnion(const BasicUnion& other) = delete;
  BasicUnion& operator=(const BasicUnion& other) = delete;

  // Clone() is a template so it is only instantiated if it is used. Thus, the
  // bindings generator does not need to know whether Clone() or copy
  // constructor/assignment are available for members.
  template <typename UnionPtrType = BasicUnionPtr>
  BasicUnionPtr Clone() const;

  // Equals() is a template so it is only instantiated if it is used. Thus, the
  // bindings generator does not need to know whether Equals() or == operator
  // are available for members.
  template <typename T,
            typename std::enable_if<std::is_same<
                T, BasicUnion>::value>::type* = nullptr>
  bool Equals(const T& other) const;

  template <typename T,
            typename std::enable_if<std::is_same<
                T, BasicUnion>::value>::type* = nullptr>
  bool operator==(const T& rhs) const { return Equals(rhs); }
  size_t Hash(size_t seed) const;

  Tag which() const {
    return tag_;
  }

  bool is_numeric_value() const { return tag_ == Tag::kNumericValue; }
  int32_t get_numeric_value() const {
    CHECK(tag_ == Tag::kNumericValue);
    return data_.numeric_value;
  }
  void set_numeric_value(int32_t numeric_value);

  bool is_enum_value() const { return tag_ == Tag::kEnumValue; }
  EnumInUnion get_enum_value() const {
    CHECK(tag_ == Tag::kEnumValue);
    return data_.enum_value;
  }
  void set_enum_value(EnumInUnion enum_value);

  bool is_struct_value() const { return tag_ == Tag::kStructValue; }
  const StructInUnionPtr& get_struct_value() const {
    CHECK(tag_ == Tag::kStructValue);
    return data_.struct_value;
  }
  StructInUnionPtr& get_struct_value() {
    CHECK(tag_ == Tag::kStructValue);
    return data_.struct_value;
  }
  void set_struct_value(StructInUnionPtr struct_value);

  template <typename UserType>
  static mojo::Message SerializeAsMessage(UserType* input) {
    return mojo::internal::SerializeAsMessageImpl<
        BasicUnion::DataView>(input);
  }

  template <typename UserType>
  static bool DeserializeFromMessage(mojo::Message input,
                                     UserType* output) {
    return mojo::internal::DeserializeImpl<BasicUnion::DataView>(
        input, input.payload(), input.payload_num_bytes(), output, Validate);
  }

 private:
  template <typename T>
  friend class ::mojo::InlinedStructPtr;
  template <typename T>
  friend class ::mojo::StructPtr;

  union Union_ {
    Union_(
        std::in_place_index_t<static_cast<size_t>(Tag::kNumericValue)>,
        int32_t value);
    Union_(
        std::in_place_index_t<static_cast<size_t>(Tag::kEnumValue)>,
        EnumInUnion value);
    Union_(
        std::in_place_index_t<static_cast<size_t>(Tag::kStructValue)>,
        StructInUnionPtr value);
    // The contents of the union are explicitly destroyed by `DestroyActive()`.
    ~Union_() {}

    int32_t numeric_value;
    EnumInUnion enum_value;
    StructInUnionPtr struct_value;
  };

  BasicUnion(
      std::in_place_index_t<static_cast<size_t>(Tag::kNumericValue)>,
      int32_t value);
  BasicUnion(
      std::in_place_index_t<static_cast<size_t>(Tag::kEnumValue)>,
      EnumInUnion value);
  BasicUnion(
      std::in_place_index_t<static_cast<size_t>(Tag::kStructValue)>,
      StructInUnionPtr value);

  static bool Validate(const void* data,
                       mojo::internal::ValidationContext* validation_context);

  void DestroyActive();
  Tag tag_;
  Union_ data_;
};


template <typename UnionPtrType>
BasicUnionPtr BasicUnion::Clone() const {
  switch (tag_) {
    case Tag::kNumericValue:
      return NewNumericValue(
          mojo::Clone(data_.numeric_value));
    case Tag::kEnumValue:
      return NewEnumValue(
          mojo::Clone(data_.enum_value));
    case Tag::kStructValue:
      return NewStructValue(
          mojo::Clone(data_.struct_value));
  }
  return nullptr;
}

template <typename T,
          typename std::enable_if<std::is_same<
              T, BasicUnion>::value>::type*>
bool BasicUnion::Equals(const T& other) const {
  if (tag_ != other.which())
    return false;

  switch (tag_) {
    case Tag::kNumericValue:
      return mojo::Equals(data_.numeric_value, other.data_.numeric_value);
    case Tag::kEnumValue:
      return mojo::Equals(data_.enum_value, other.data_.enum_value);
    case Tag::kStructValue:
      return mojo::Equals(data_.struct_value, other.data_.struct_value);
  }

  return false;
}
template <typename StructPtrType>
StructInUnionPtr StructInUnion::Clone() const {
  return New(
      mojo::Clone(flag),
      mojo::Clone(value)
  );
}

template <typename T, StructInUnion::EnableIfSame<T>*>
bool StructInUnion::Equals(const T& other_struct) const {
  if (!mojo::Equals(this->flag, other_struct.flag))
    return false;
  if (!mojo::Equals(this->value, other_struct.value))
    return false;
  return true;
}

template <typename T, StructInUnion::EnableIfSame<T>*>
bool operator<(const T& lhs, const T& rhs) {
  if (lhs.flag < rhs.flag)
    return true;
  if (rhs.flag < lhs.flag)
    return false;
  if (lhs.value < rhs.value)
    return true;
  if (rhs.value < lhs.value)
    return false;
  return false;
}


}  // golden

namespace mojo {


template <>
struct  StructTraits<::golden::StructInUnion::DataView,
                                         ::golden::StructInUnionPtr> {
  static bool IsNull(const ::golden::StructInUnionPtr& input) { return !input; }
  static void SetToNull(::golden::StructInUnionPtr* output) { output->reset(); }

  static decltype(::golden::StructInUnion::flag) flag(
      const ::golden::StructInUnionPtr& input) {
    return input->flag;
  }

  static const decltype(::golden::StructInUnion::value)& value(
      const ::golden::StructInUnionPtr& input) {
    return input->value;
  }

  static bool Read(::golden::StructInUnion::DataView input, ::golden::StructInUnionPtr* output);
};


template <>
struct  UnionTraits<::golden::BasicUnion::DataView,
                                        ::golden::BasicUnionPtr> {
  static bool IsNull(const ::golden::BasicUnionPtr& input) { return !input; }
  static void SetToNull(::golden::BasicUnionPtr* output) { output->reset(); }

  static ::golden::BasicUnion::Tag GetTag(const ::golden::BasicUnionPtr& input) {
    return input->which();
  }

  static  int32_t numeric_value(const ::golden::BasicUnionPtr& input) {
    return input->get_numeric_value();
  }

  static  ::golden::EnumInUnion enum_value(const ::golden::BasicUnionPtr& input) {
    return input->get_enum_value();
  }

  static const ::golden::StructInUnionPtr& struct_value(const ::golden::BasicUnionPtr& input) {
    return input->get_struct_value();
  }

  static bool Read(::golden::BasicUnion::DataView input, ::golden::BasicUnionPtr* output);
};

}  // namespace mojo

#endif  // BASIC_UNION_TEST_MOJOM_H_