From b70e04cab0f8bf5723861dc28c163e25d7964392 Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Sat, 28 Apr 2012 19:20:23 +0000 Subject: [PATCH] THRIFT-1586 Two small D issues Patch: David Nadlinger git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1331810 13f79535-47bb-0310-9956-ffa450edef68 --- lib/d/src/thrift/codegen/base.d | 41 +++++++++++++++------------------ tutorial/d/Makefile | 2 +- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/d/src/thrift/codegen/base.d b/lib/d/src/thrift/codegen/base.d index e7e3ead3..96240120 100644 --- a/lib/d/src/thrift/codegen/base.d +++ b/lib/d/src/thrift/codegen/base.d @@ -368,7 +368,7 @@ mixin template TStructHelpers(alias fieldMetaData = cast(TFieldMeta[])null) if ( static assert(is(This == struct) || is(This : Exception), "TStructHelpers can only be used inside a struct or an Exception class."); - static if (is(TIsSetFlags!(This, fieldMetaData))) { + static if (TIsSetFlags!(This, fieldMetaData).tupleof.length > 0) { // If we need to keep isSet flags around, create an instance of the // container struct. TIsSetFlags!(This, fieldMetaData) isSetFlags; @@ -530,38 +530,35 @@ unittest { assert(f.toString() == `Foo(a: a string, b: 0 (unset), c: 4)`); } + /** * Generates an eponymous struct with boolean flags for the non-required - * non-nullable fields of T, if any, or nothing otherwise (i.e. the template - * body is empty). + * non-nullable fields of T. * - * Nullable fields are just set to null to signal »not set«. + * Nullable fields are just set to null to signal »not set«, so no flag is + * emitted for them, even if they are optional. * * In most cases, you do not want to use this directly, but via TStructHelpers * instead. */ -// DMD @@BUG@@: Using getFieldMeta!T in here horribly breaks things to the point -// where getFieldMeta is *instantiated twice*, with different bodies. This is -// connected to the position of »enum fieldMeta« in TStructHelpers. template TIsSetFlags(T, alias fieldMetaData) { mixin({ - string boolDefinitions; - foreach (name; __traits(derivedMembers, T)) { - static if (!is(MemberType!(T, name)) || is(MemberType!(T, name) == void)) { - // We hit something strange like the TStructHelpers template itself, - // just ignore. - } else static if (isNullable!(MemberType!(T, name))) { - // If the field is nullable, we don't need an isSet flag as we can map - // unset to null. - } else static if (memberReq!(T, name, fieldMetaData) != TReq.REQUIRED) { - boolDefinitions ~= "bool " ~ name ~ ";\n"; + string code = "struct TIsSetFlags {\n"; + foreach (meta; fieldMetaData) { + code ~= "static if (!is(MemberType!(T, `" ~ meta.name ~ "`))) {\n"; + code ~= q{ + static assert(false, "Field '" ~ meta.name ~ + "' referenced in metadata not present in struct '" ~ T.stringof ~ "'."); + }; + code ~= "}"; + if (meta.req == TReq.OPTIONAL || meta.req == TReq.OPT_IN_REQ_OUT) { + code ~= "else static if (!isNullable!(MemberType!(T, `" ~ meta.name ~ "`))) {\n"; + code ~= " bool " ~ meta.name ~ ";\n"; + code ~= "}\n"; } } - if (!boolDefinitions.empty) { - return "struct TIsSetFlags {\n" ~ boolDefinitions ~ "}"; - } else { - return ""; - } + code ~= "}"; + return code; }()); } diff --git a/tutorial/d/Makefile b/tutorial/d/Makefile index efbb93e8..fcee9af8 100644 --- a/tutorial/d/Makefile +++ b/tutorial/d/Makefile @@ -31,7 +31,7 @@ client: client.d dmd -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd client.d ${GEN_SRC} async_client: async_client.d - dmd -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd -L-lthriftd-event -L-levent async_client.d ${GEN_SRC} + dmd -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd-event -L-lthriftd -L-levent async_client.d ${GEN_SRC} clean: $(RM) -f server client async_client -- 2.17.1