4 ms·
I am writing a game engine and C++'s lack of proper reflection is a real hindrance for a variety of serialisation/deserialisation interfaces, be it networking o
by Kenji 10y ago
I am writing a game engine and C++'s lack of proper reflection is a real hindrance for a variety of serialisation/deserialisation interfaces, be it networking or saving things to disk and loading them. You just end up writing redundant and more brittle code for no reason other than that a language feature is missing. I think it is great that this problem is given attention.
Edit: After thinking on it for a few minutes. All I want is a generic function that takes a class as the generic type T, and a string as an argument, and returns me a completely filled in object of type T, or throws an error on malformed input. I really don't think I am asking for too much here.
- TillE 10y ago> All I want is a generic function that takes a class as the generic type T, and a string as an argument, and returns me a completely filled in object of type T, or throws an error on malformed input. Well, what you need is a way to enumerate the members of a class. Everything else is already done by something like Cereal or Boost.Serialization.
- kuschku 10y agoUsually you might want to instead use code generation at compile time to do these things, even in Java. Reflection is always magnitudes slower.
- caseymarquis 10y ago.NET allows you to use reflection to emit bytecode which does the same work as reflection. More complicated to setup, but the speed-up is huge. Not sure if Java has an equivalent? I'd be curious to know.
- barrkel 10y agoYes. You generate bytecode for classes that are loaded by a class loader, and thereby enter the JIT world etc. The tech predates .NET, but it isn't as developer friendly as DynamicMethod etc.
- bsaul 10y agoi'm not sure about your particular issue, but every time i had to choose between being smart and generic with serialization over being straightforward and boring ( at the cost of writing one small line of code for each and every property), i always choose to stay boring. Not only is it nicer to the person that's going to maintain your code and may not know about your smart trick, but with generic code there is always an exception that makes you need to write some code somewhere that breaks the abstraction.
- maxxxxx 10y agoSame here. Generic serialization often breaks when there are some changes to the data and you have to be compatible with different versions.
- maccard 10y ago> and returns me a completely filled in object of type T What if type T contains a pointer to type U ? Or a list of pointers to a type U, with some duplicates in the list? What if T contains a floating point number, and the application is compiled with ffast-math? What if the object contains a reference to some unknown type (a file, or socket)? There are lots of ways of solving these problems, but each of the solutions has trade offs which may be acceptable to you, but aren't acceptable to me and vice versa.