Boost logo

Boost Users :

From: Larry I Smith (larry.i.smith_at_[hidden])
Date: 2006-11-25 00:01:31


Robert Ramey wrote:
> Larry Smith wrote:
>
> I can not get these struct members to
>> serialize; I get compile errors, e.g. this fails to compile:
>>
>> ar & fld; // where fld is: char[200]
>
> Hmmm - works for me. The following example compiles just fine.
>
>
>
> #include <sstream>
>
> #include <boost/archive/text_oarchive.hpp>
>
> struct x {
>
> char y[200];
>
> template<class Archive>
>
> void serialize(Archive & ar, const unsigned int version){
>
> ar & y;
>
> }
>
> };
>
> int main(int argc, char *argv[]){
>
> std::stringstream os;
>
> boost::archive::text_oarchive oa(os);
>
> const x x1;
>
> oa & x1;
>
> }
>
>
>
> Robert Ramey
>
>
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

The structs we have to deal with are 'C' structs, not
C++. They are included in approx 9000 source files;
90% of those source files are 'C', only 10% are C++.

All of the source is compiled with 'pack 1' ('/Zp1 on Windows,
__attribute__ ((packed)) on Linux gcc/g++) to eliminate
'pad fields' within the structs; all of the source files
depend on this packed layout, so it can not be changed - things
would break. Once we move to non-Intel servers, 'packed'
will no longer be used on the server side; this is another reason
to serialize-xfer-deserialize the data - the struct layouts/sizes
will be very different between the workstation and server.

Here's a highly reduced example:

-----------------------

/* cmdef.h */
/* NOTE: on Linux/Intel with gcc/g++ 'ADPACKED' is
 * defined as '__attribute__ ((packed))'.
 * on Windows the '/Zp1' compile option is
 * used to pack the structs and 'ADPACKED'
 * is defined as empty.
 */
#ifdef __cplusplus
extern "C" {
#endif

typedef struct ADPACKED NODE_MGR
{
   char ApplName[8];
   char CompName[8];
} NODE_MGR;

#ifdef __cplusplus
}
#endif

---------------------

/* arch.hpp */

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

#include "cmdef.h"

namespace boost {
namespace serialization {

template<class Archive>
void serialize(Archive & ar, struct NODE_MGR & st,
 const unsigned int version)
{
  ar & st.ApplName; // line 19 in err msg
  ar & st.CompName; // line 20 in err msg
}

} // end namespace serialization */
} // end of namespace boost */

--------------------

/* arch.cpp */

#include <sstream>

#include "arch.hpp"

int main()
{
  struct NODE_MGR nm;
  std::ostringstream ofs;
  boost::archive::text_oarchive oa(ofs);

  oa << nm; // line 15 in err msg
        
  return 0;
}

-------------------

Compiling arch.cpp produces these errors:

  arch.hpp:19: error: cannot bind packed field
    ‘st->NODE_MGR::ApplName’ to ‘char (&)[8]

  arch.hpp:20: error: cannot bind packed field
    ‘st->NODE_MGR::CompName’ to ‘char (&)[8]

Removing the 'packed' attribute produces a
different set of errors:

/usr/include/boost/archive/detail/oserializer.hpp:
  In function ‘void boost::archive::save(Archive&, T&)
  [with Archive = boost::archive::text_oarchive, T = NODE_MGR]’:
/usr/include/boost/archive/basic_text_oarchive.hpp:78:
  instantiated from ‘void
  boost::archive::basic_text_oarchive<Archive>::save_override(T&, int)
  [with T = NODE_MGR, Archive = boost::archive::text_oarchive]’
/usr/include/boost/archive/detail/interface_oarchive.hpp:85:
  instantiated from ‘Archive&
  boost::archive::detail::interface_oarchive<Archive>::operator<<(T&)
  [with T = NODE_MGR, Archive = boost::archive::text_oarchive]’
arch.cpp:15: instantiated from here
 /usr/include/boost/archive/detail/oserializer.hpp:566:
 error: invalid application of ‘sizeof’ to incomplete type
 ‘boost::STATIC_ASSERTION_FAILURE<false>’

However, on the workstation side the structs MUST remain
'packed'; otherwise thousands of source files will have to
be modified.

Is there any way to make this work with boost archives,
or should I explore other options?

Thanks,
Larry


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