Re: c++ reflection (was (no subject))
Sylvain Vignaud <[email protected]> Tue, 29 Jun 2010 22:49:44 -0400
| Newsgroups | gmane.games.devel.sweng |
|---|---|
| Message-ID | <[email protected]> |
Not sure anyone is still following this thread. I've just spent 10mn
to improve the interface so that the Member macro doesn't have such a
rigid number of arguments:
I think the following code is quite easily expandable, and it now looks
quite much like what Jarkko wanted: easy to have lots of parameters, no
code generation tool, no precompilation pass.
class Foo
{
public:
Foo() {}
virtual ~Foo() {}
private:
DefineClass(Foo);
Member(int, test, Default(0); Description("Some int") );
Member(char *, mName, Default("foo"); Description("Some static
text"); Serialized());
Member(int*, mBuffer, Default(NULL), Description("Some ptr to
nowhere"); Serialized());
Member(int, foo);
Member(int, foo2, Description("this one has a desc, unlike foo"));
Member(int, foo3, Default(0xDead));
EndClass();
};
Updated code is still in the same place:
http://tfpsly.free.fr/Files/Reflection.cpp
On 24/06/10 00:24, Sylvain G. Vignaud wrote:
> Actually you can get something quite similar if you're not too regarding on mixing metaprogramming and macros. The result looks like:
>
> class Foo
> {
> public:
> Foo() {}
> virtual ~Foo() {}
>
> private:
> DefineClass(Foo);
>
> Member(int, test, 0);
> Member(char *, mName, "foo");
>
> Member(int*, mBuffer, NULL);
>
> EndClass(Foo);
> };
>
> Foo foo;
> foo.InitializeMembersToDefaultValue();
>
> const Foo::ClassDescription&desc = Foo::GetClassDescription();
>
> printf("type: %s\n", desc.name());
> printf("size: %d\n", desc.size());
>
> for( int i=0; i<Foo::NbrMembers; ++i )
> {
> const member_t&member = desc.m_Members[i];
> printf(" %s: offset %d size %d type %s\n",
> member.name, member.offset,
> member.type->size(), member.type->name());
> }
>
> And it seems the "Member" macro doesn't prevent Intellisense and the likes to know about members :)
> Also it could be expended to take more parameters, like a flag to decide which members are serialized, a text description...
>
> Quick proof of concept with full code - hacked using Jon Watte's Reflexion.cpp as a basis:
> http://tfpsly.free.fr/Files/Reflection.cpp
>
>
>
> From: Jarkko Lempiainen [Profoundic]<jarkko
>
>> [...] But it would be nicer to have something like:
>> struct my_struct
>> {
>> int a {serialized, editable, desc("Car velocity"), color(0x123456)};
>> int b {serialized, editable, call(foobar())};
>> int c, d, e {serialized};
>> int f; // non-reflected variable
>> };
>>
>> Cheers, Jarkko
>>
>>
>>
>> From: sweng-gamedev-bounces<at> lists.midnightryder.com
>> [mailto:sweng-gamedev-bounces<at> lists.midnightryder.com] On Behalf Of Jon Watte
>> Sent: Thursday, June 17, 2010 1:13 AM
>> Am 17.06.2010 00:12, schrieb Jon Watte:
>>> Mail formatting still all messed up :-(
>>>
>>> Anyway, the example you had (PFC_MONO()) doesn't actually add all the
>>> annotations you want for editing and serialization in addition to the
>>> general reflection information.
>>>
>>> You can get rid of the ellipsis requirement by doing something like:
>>>
>>> #define MEMBERS(Type, x) \
>>> typedef mytype Type; \
>>> inline static Member *GetMembers() { \
>>> static Member<mytype>[] members = { \
>>> x \
>>> }; \
>>> return x; \
>>> }
>>>
>>> #define MEMBER(x, desc) Member<mytype>(&mytype::x, #x, desc),
>>>
>>> Use it like so:
>>>
>>> struct MyStruct {
>>> int x;
>>> float b;
>>> MEMBERS(MyStruct,
>>> MEMBER(x, "Some integer value")
>>> MEMBER(b, "The amount of float-ness"))
>>> };
>>>
>>> Note that the comma lives in the MEMBER() macro, and the members are
>>> just listed without comma within the MEMBERS() macro. The MEMBER()
>>> macro should take the union of all information you need about the
>>> member (editor information, networking, etc).
>>>
>>> Sincerely,
>>>
>>> jw
>
> _______________________________________________
> Sweng-Gamedev mailing list
> [email protected]
> http://lists.midnightryder.com/listinfo.cgi/sweng-gamedev-midnightryder.com
>
_______________________________________________
Sweng-Gamedev mailing list
[email protected]
http://lists.midnightryder.com/listinfo.cgi/sweng-gamedev-midnightryder.com