Re: [Algorithms] Complexity of new hardware
Brought to you by:
vexxed72
|
From: Jon W. <jw...@gm...> - 2009-04-20 16:55:44
|
Will Vale wrote:
> On Sun, 19 Apr 2009 03:03:27 +1200, Jarkko Lempiainen <al...@gm...>
> wrote:
>
>> All I have is something like:
>> struct foo
>> { PFC_MONO(foo) {PFC_VAR3(x, y, z);}
>> int x, y, z;
>> };
>>
>
> That's admirably brief! I assume you're generating code rather than data
>
> // foo.h
> struct foo
> {
> COMPOUND(foo)
>
> int x, y, z;
> };
>
> // foo.cpp - essentially generates const member_t foo::members[] = { ... };
> RTTI(foo, MEMBER(x) MEMBER(y) MEMBER(z))
>
This is exactly what I want to avoid -- the members are listed twice, in
two different files! Version mismatch hell. (The Despair engine write-up
has this same problem).
You can actually use some template trickery to hoist the RTTI() /
MEMBER() parts into the struct itself.
This allows you to write something like:
struct foo {
int x, y, z;
RTTI(MEMBER(x) MEMBER(y) MEMBER(z))
};
That's all there's to it. No additional code, cpp files, or anything
like that needed (except for whatever runtime support you'll want, like
stream handling, custom type marshaling, etc).
members() gives you a list of the members; info() gives you information
about the struct itself. I put an implementation (including a program
you can compile and run) up for discussion at
http://www.enchantedage.com/cpp-reflection if you want to take a look.
Here's a main() program that prints the information about the type "foo":
int main() {
printf("type: %s\n", foo::info().name());
printf("size: %ld\n", foo::info().size());
for (size_t i = 0; i != foo::info().memberCount(); ++i) {
printf(" %s: offset %ld size %ld type %s\n",
foo::info().members()[i].name,
foo::info().members()[i].offset,
foo::info().members()[i].type->size(),
foo::info().members()[i].type->name());
}
return 0;
}
And here's the output:
type: foo
size: 12
x: offset 0 size 4 type i
y: offset 4 size 4 type i
z: offset 8 size 4 type i
You probably want to wrap those ::info().members()[i] accessors in some
nice readable macro, like TYPE_NTH_MEMBER() or suchlike, but I think
this provides the optimum implementation in the sense that it relies
only on static init (no dynamic memory allocations) and it doesn't
require member definition to be separate from declaration (it's all in
the .h file).
If you want editor information, you can easily push that into the
MEMBER() macro.
It's still repeating yourself, though.
Sincerely,
jw
|