Boost logo

Boost Users :

Subject: [Boost-users] Developing a new boost archive
From: Roberto Fichera (kernel_at_[hidden])
Date: 2012-02-13 04:56:48


Hi All in the list,

I'm basically new to boost so I hope to find support here to integrate my own serialization
library within the boost archive logic. I would say as premise that I started to have a look
in all the boost archive implementation so binary, xml and text trying to understand the logic
but that increased my confusion about how to approach the solution of my problem.
Confusion that is increased by the lack of my boost knowledge and it's related template
programming, of course.

By the way I found really instructive the simple_log_archive example that cleanly shows
how to "walk" the full tree of the data representation in terms of serialization chain.

Furthermore, I would say that my serialization framework does already handle all the
representation formats like binary, xml, text, python and many others, so from the boost
archive point of view I need just to know very few things:

- how to walk the tree representing the serialization chain
- how to detect that a new type is being serialized
- how to get the type name as string
- how to define primitive types
- how to manage vectors/arrays
- be sure that everything is passed as nvp

About the two first, they seems related because the code below seems responsible
to walk the tree and call all the serialize functions

    template< class Archive >
    struct save_only
    {
      template< class T >
      static void invoke( Archive & ar, const T & t )
      {
        // make sure call is routed through the highest interface that might
        // be specialized by the user.
        boost::serialization::serialize_adl(
            ar,
            const_cast< T & >( t ),
            boost::serialization::version< T >::value
        );
      }
    };

So I've put my two functions that are used to defines types:

myserialize_beginType( myserialize &s, char *fieldName, char *type )

myserialize_endType( myserialize &s )

Such as the serialization walk chain looks like:

    template< class Archive >
    struct save_only
    {
      template< class T >
      static void invoke( Archive & ar, const T & t )
      {
        // make sure call is routed through the highest interface that might
        // be specialized by the user.
-->> myserialize_beginType( ar.serializer, ar.fieldName, ???type??? );

        boost::serialization::serialize_adl(
            ar,
            const_cast< T & >( t ),
            boost::serialization::version< T >::value
        );

-->> myserialize_endType( ar.serializer );

      }
    };

But actually I don't know how to determine the type name as string to pass to my beginType function.

About the place to define primitives, I'm not really sure if it's the right place where I have to put
my serialization primitives that maps all my basics types. The prototype of such overloaded
function is like

myserialize_serialize( "basetype" &t, char * fieldName, myserialize &s )

Actually I've defined something like:

#define BOOSTSERIALIZEARCHIVE_PRIMITIVE( __T ) \
      simple_log_archive & operator<<( const boost::serialization::nvp< __T > & t )\
      {\
        myserialize_serialize( t.value(), (char*)t.name(), this->serializer );\
        return *this;\
      }

      BOOSTSERIALIZEARCHIVE_PRIMITIVE( char )
      BOOSTSERIALIZEARCHIVE_PRIMITIVE( unsigned char )

which seems to work.

About managing vectors and array, actually I tried to define something like:

    template< class Archive >
    struct use_array_optimization
    {
      template < class T >
      static void invoke( Archive & ar, const T & t )
      {
        myserialize_serialize( (T*)t.address(), ar.fieldName , t.count() * sizeof( T ), ar.serializer );
      }
    };

    template< class T >
    void save( const T &t )
    {
      typedef
          BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<boost::is_enum< T >,
              boost::mpl::identity<save_enum_type<simple_log_archive> >,
          // else
-->> BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<boost::is_array< T >,
-->> boost::mpl::identity<use_array_optimization<simple_log_archive> >,
          //else
          BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
              // if it's primitive
                  boost::mpl::equal_to<
                      boost::serialization::implementation_level< T >,
                      boost::mpl::int_<boost::serialization::primitive_type>
>,
                  boost::mpl::identity<save_primitive<simple_log_archive> >,
          // else
              boost::mpl::identity<save_only<simple_log_archive> >
> > >::type typex;
      typex::invoke( *this, t );
    }

but this seems not working at all.

Finally the last items, be sure that everything is passed as nvp in order to catch
each given field name, I simply don't know how to do this check/constraint.

Hopefully to get some support.

Thanks in advance,
Roberto Fichera.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net