Proposal: user defined attributes
Andrej Mitrovic
andrej.mitrovich at gmail.com
Mon Mar 19 14:51:45 PDT 2012
On 3/19/12, Jacob Carlborg <doob at me.com> wrote:
> Yeah, but that will complicate the retrieval of the information.
What is so complicated about extracting fields? Just iterate via .tupleof:
module test;
import std.stdio;
import std.conv;
struct Foo
{
int x;
string name;
mixin NonSerialized!name;
string lastName;
mixin NonSerialized!lastName;
string extra;
}
mixin template NonSerialized(alias field)
{
mixin("enum __attribute_nonSerialized_" ~ field.mangleof ~ ";");
}
string serialize(T)(T input)
{
string result;
foreach (i, field; input.tupleof)
{
static if (skipSerialize!(T, input.tupleof[i].mangleof))
result ~= to!string(typeof(field).init) ~ "\n";
else
result ~= to!string(field) ~ "\n";
}
return result;
}
template skipSerialize(T, string mangle)
{
enum bool skipSerialize = __traits(hasMember, T,
"__attribute_nonSerialized_" ~ mangle);
}
void main()
{
Foo foo = Foo(10, "Foo", "Bar", "Doo");
string bin = serialize(foo);
writeln(bin);
}
And by using .init instead of just skipping serialization altogether
you can unserialize at a later point even if you end up removing some
of the mixins, so you can have some binary-compatibility there.
More information about the Digitalmars-d
mailing list