/*
 * Copyright(c) 2021 to 2022 ZettaScale Technology and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
 * v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
 */

#if defined(__IDLC__)

union test_union;

enum e { E1, E2 };

@nested
union union_nested1 switch (short) {
  case 1:
    long a1;
  case 2:
    @external test_union c1;
  case 3:
  case 4:
  case 5:
    e d1[2];
};

@nested
struct struct_nested1 {
  @external union_nested1 a2;
  @external union_nested1 a3;
};

@topic @appendable
union test_union switch (short) {
case 1:
   union_nested1 a;
default:
   struct_nested1 b;
};

#else

#include "dds/dds.h"
#include "test_union.h"
#include "common.h"

const dds_topic_descriptor_t *desc = &test_union_desc;

void init_sample (void *s)
{
  test_union *s1 = (test_union *) s;
  s1->_d = 2;

  A (s1->_u.b.a2);
  s1->_u.b.a2->_d = 2;
  A (s1->_u.b.a2->_u.c1);
  s1->_u.b.a2->_u.c1->_d = 1;
  s1->_u.b.a2->_u.c1->_u.a._d = 1;
  s1->_u.b.a2->_u.c1->_u.a._u.a1 = 123;

  A (s1->_u.b.a3);
  s1->_u.b.a3->_d = 5;
  s1->_u.b.a3->_u.d1[0] = E2;
  s1->_u.b.a3->_u.d1[1] = E1;
}

int cmp_sample (const void *sa, const void *sb)
{
  test_union *a = (test_union *) sa;
  test_union *b = (test_union *) sb;
  CMP(a, b, _d, 2)
  CMP(a, b, _u.b.a2->_d, 2)
  CMP(a, b, _u.b.a2->_u.c1->_d, 1)
  CMP(a, b, _u.b.a2->_u.c1->_u.a._d, 1)
  CMP(a, b, _u.b.a2->_u.c1->_u.a._u.a1, 123)

  CMP(a, b, _u.b.a3->_d, 5)
  CMP(a, b, _u.b.a3->_u.d1[0], E2)
  CMP(a, b, _u.b.a3->_u.d1[1], E1)
  return 0;
}

NO_KEY_CMP

#endif
