c++ - Using meta programming to select member variables -


i trying create game save system using boost serialization, , want create easy way clients select member variables serialization.

basically want user input this:

class apple : public actor { public:      int a;      bool istasty;      float unimportantdata;        set_saved_members(a, istasty); }; 

and expanded like

class apple : public actor { public:    // ...     template<typename archive>    void serialize(archive& arch, const unsigned int version)    {         arch & boost_serialization_nvp(a);         arch & boost_serialization_nvp(istasty);  } 

of course, fine if requires few more macros this.

i'm fine template meta programming (preferably) or preprocessor meta programming.

c++11 fine, too.

thanks help!

previously had boost-focused answer wasn't able test. here's more comprehensible using combination of template meta-programming , macros:

#include <iostream>  // build base template save implementation template <typename... arg_types> struct save_impl;  // create doserialize function recursively serialize objects. template <typename archive, typename first_type, typename... other_types> struct save_impl<archive, first_type, other_types...> {   static void doserialize(archive& arch, first_type & arg1, other_types&... others) {     // instead of printing in next line, do:  arch & boost_serialization_nvp(a);     std::cout << arch << arg1 << std::endl;     save_impl<archive, other_types...>::doserialize(arch, others...);   } };  // base case end recursive call of struct-based doserialize template <typename archive> struct save_impl<archive> {   static void doserialize(archive& arch) { ; } };  // create doserialize function call struct-based implementation. template <typename archive, typename... arg_types> void doserialize(archive & arch, arg_types&... args) {   save_impl<archive, arg_types...>::doserialize(arch, args...); }  // create desired macro add serialize function class. #define set_saved_members(...)         \   template<typename archive>           \   void serialize(archive& arch) {      \     doserialize(arch, __va_args__);    \   } 

currently have printing test (but indicate above line need change). here test using apple example:

class apple { public:   int a;   bool istasty;   float unimportantdata;    set_saved_members(a, istasty); };   int main() {   apple = {7, false, 2.34};   a.istasty=true;   a.serialize("archive: "); } 

note i'm sending in string instead of archive object -- works fine fact it's using print.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -