| 886 | } |
| 887 | |
| 888 | bool idl_mapping_java::gen_union(UTL_ScopedName *name, |
| 889 | const std::vector<AST_UnionBranch *> &branches, AST_Type *discriminator, |
| 890 | AST_Expression::ExprType udisc_type, |
| 891 | const AST_Union::DefaultValue &default_value, const char *repoid) |
| 892 | { |
| 893 | string body_branches; |
| 894 | string disc_ty = type(discriminator); |
| 895 | string disc_val = isEnum(discriminator) ? ".value()" : ""; |
| 896 | string pre_disc_set = isEnum(discriminator) ? (disc_ty + ".from_int(") : ""; |
| 897 | string post_disc_set = isEnum(discriminator) ? ")" : ""; |
| 898 | string default_disc_check; |
| 899 | bool hasDefault(false); |
| 900 | |
| 901 | for (size_t i = 0; i < branches.size(); ++i) { |
| 902 | string disc_check, first_label_value; |
| 903 | unsigned long n_labels = branches[i]->label_list_length(); |
| 904 | |
| 905 | for (unsigned long j = 0; j < n_labels; ++j) { |
| 906 | AST_UnionLabel *ul = branches[i]->label(j); |
| 907 | ostringstream oss; |
| 908 | |
| 909 | if (ul->label_kind() == AST_UnionLabel::UL_default) { |
| 910 | hasDefault = true; |
| 911 | writeUnionDefaultValue(oss, udisc_type, default_value); |
| 912 | //disc_check is an expression to make sure the discriminant isn't |
| 913 | //one of the other branch labels, but we don't know all the other |
| 914 | //values yet, so this special string will be replaced later |
| 915 | disc_check = "<%default_disc_check%>"; |
| 916 | |
| 917 | } else { |
| 918 | oss << *ul->label_val()->ev(); |
| 919 | disc_check += "_discriminator" + disc_val + " != " + oss.str() |
| 920 | + ((j == n_labels - 1) ? "" : " && "); |
| 921 | default_disc_check += (default_disc_check.size() ? " || " : "") |
| 922 | + string("_discriminator") + disc_val + " == " + oss.str(); |
| 923 | } |
| 924 | |
| 925 | if (j == 0) first_label_value = oss.str(); |
| 926 | } |
| 927 | |
| 928 | string ty = type(branches[i]->field_type()); |
| 929 | const char *brname = branches[i]->local_name()->get_string(); |
| 930 | body_branches += |
| 931 | " private " + ty + ' ' + brname + ";\n\n" |
| 932 | " public " + ty + ' ' + brname + "() {\n" |
| 933 | " if (" + disc_check + ") throw new org.omg.CORBA.BAD_OPERATION();\n" |
| 934 | " return " + brname + ";\n" |
| 935 | " }\n\n" |
| 936 | " public void " + brname + '(' + ty + " _value) {\n" |
| 937 | " _discriminator = " + pre_disc_set + first_label_value |
| 938 | + post_disc_set + ";\n" |
| 939 | " " + brname + " = _value;\n" |
| 940 | " }\n\n"; |
| 941 | |
| 942 | if (n_labels > 1 || default_disc_check.size() > 0) { |
| 943 | body_branches += |
| 944 | " public void " + string(brname) + '(' + disc_ty |
| 945 | + " _discriminator, " + ty + " _value) {\n" |
nothing calls this directly
no test coverage detected