Re: c++ reflection (was (no subject))

"Sylvain G. Vignaud" <[email protected]> Thu, 24 Jun 2010 00:24:14 -0400
Newsgroups gmane.games.devel.sweng
Message-ID <[email protected]>
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