TStructHelpers

Mixin template defining additional helper methods for using a struct with Thrift, and a member called isSetFlags if the struct contains any fields for which an »is set« flag is needed.

It can only be used inside structs or Exception classes.

For example, consider the following struct definition:

struct Foo {
  string a;
  int b;
  int c;

  mixin TStructHelpers!([
    TFieldMeta("a", 1), // Implicitly optional (nullable).
    TFieldMeta("b", 2), // Implicitly required (non-nullable).
    TFieldMeta("c", 3, TReq.REQUIRED, "4")
  ]);
}

TStructHelper adds the following methods to the struct:

1 /++
2  + Sets member fieldName to the given value and marks it as set.
3  +
4  + Examples:
5  + ---
6  + auto f = Foo();
7  + f.set!"b"(12345);
8  + assert(f.isSet!"b");
9  + ---
10  +/
11 void set(string fieldName)(MemberType!(This, fieldName) value);
12 
13 /++
14  + Resets member fieldName to the init property of its type and marks it as
15  + not set.
16  +
17  + Examples:
18  + ---
19  + // Set f.b to some value.
20  + auto f = Foo();
21  + f.set!"b"(12345);
22  +
23  + f.unset!b();
24  +
25  + // f.b is now unset again.
26  + assert(!f.isSet!"b");
27  + ---
28  +/
29 void unset(string fieldName)();
30 
31 /++
32  + Returns whether member fieldName is set.
33  +
34  + Examples:
35  + ---
36  + auto f = Foo();
37  + assert(!f.isSet!"b");
38  + f.set!"b"(12345);
39  + assert(f.isSet!"b");
40  + ---
41  +/
42 bool isSet(string fieldName)() const @property;
43 
44 /++
45  + Returns a string representation of the struct.
46  +
47  + Examples:
48  + ---
49  + auto f = Foo();
50  + f.a = "a string";
51  + assert(f.toString() == `Foo("a string", 0 (unset), 4)`);
52  + ---
53  +/
54 string toString() const;
55 
56 /++
57  + Deserializes the struct, setting its members to the values read from the
58  + protocol. Forwards to readStruct(this, proto);
59  +/
60 void read(Protocol)(Protocol proto) if (isTProtocol!Protocol);
61 
62 /++
63  + Serializes the struct to the target protocol. Forwards to
64  + writeStruct(this, proto);
65  +/
66 void write(Protocol)(Protocol proto) const if (isTProtocol!Protocol);

Additionally, an opEquals() implementation is provided which simply compares all fields, but disregards the is set struct, if any (the exact signature obviously differs between structs and exception classes). The metadata is stored in a manifest constant called fieldMeta.

Note: To set the default values for fields where one has been specified in the field metadata, a parameterless static opCall is generated, because D does not allow parameterless (default) constructors for structs. Thus, be always to use to initialize structs:

Foo foo; // Wrong!
auto foo = Foo(); // Correct.

Constructors

this
this()
Undocumented in source.

Members

Aliases

This
alias This = typeof(this)
Undocumented in source.

Functions

isSet
bool isSet()
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(Object other)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(This other)
Undocumented in source. Be warned that the author may not have intended to support it.
read
void read(Protocol proto)
Undocumented in source. Be warned that the author may not have intended to support it.
set
void set(MemberType!(This, fieldName) value)
Undocumented in source. Be warned that the author may not have intended to support it.
toHash
size_t toHash()
Undocumented in source. Be warned that the author may not have intended to support it.
toHash
size_t toHash()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.
unset
void unset()
Undocumented in source. Be warned that the author may not have intended to support it.
write
void write(Protocol proto)
Undocumented in source. Be warned that the author may not have intended to support it.

Manifest constants

fieldMeta
enum fieldMeta;
Undocumented in source.
fieldMeta
enum fieldMeta;
Undocumented in source.

Static functions

opCall
auto opCall()
Undocumented in source. Be warned that the author may not have intended to support it.

Variables

isSetFlags
TIsSetFlags!(This, fieldMetaData) isSetFlags;
Undocumented in source.

Meta