3
votes

i'm trying to serialize members in struct of type boost::chrono::time_point tp but give error serialize is not member of boost::chrono::time_point and below is my code :

 struct myTimestamp
      {
          boost::chrono::time_point<boost::chrono::high_resolution_clock>  begin1;
          boost::chrono::time_point<boost::chrono::high_resolution_clock>  end1;

      private:
         friend class boost::serialization::access;
         template<class Archive>
         void serialize(Archive &ar, const unsigned int version)
         {
            ar & begin1;
            ar & end1;   
         }        

      public:
          myTimestamp();    
          virtual  ~myTimestamp();
      };

I have included all the required header files still issue remains same.

I even tried this below option in serialize method .. still same

ar & boost::serialization::make_binary_object(&begin1, sizeof(begin1)); ar & boost::serialization::make_binary_object(&end1, sizeof(end1));

1
Isn't it boost::chrono::time_point? - eerorika
@user2079303 - yes it is boost::chrono::time_point - user3295725
@user2079303 Your answer is a good hint on how to solve this. Sadly I haven't found my other answer that described this. I'll write something up when I have the time - sehe
@sehe, a bit better (reusable) than what my answer did would be to define a free serialize function for time_point<high_resolution_clock> but I can't figure out how to access the lvalues of time_point internals to implement it. - eerorika
i'm also trying to look at time_point -- boost.org/doc/libs/1_55_0/doc/html/chrono/… - user3295725

1 Answers

0
votes

Okay, here's what the comments have been hinting at:

using hr_clock   = boost::chrono::high_resolution_clock;
using time_point = boost::chrono::time_point<hr_clock>;

namespace boost { namespace archive {

    template<class Archive, typename clock>
        void load(Archive& ar, boost::chrono::time_point<clock>& tp, unsigned)
        {
            using namespace boost::chrono;
            milliseconds::rep millis;

            ar & millis;
            tp = time_point<clock>(milliseconds(millis));
        }

    template<class Archive, typename clock>
        void save(Archive& ar, boost::chrono::time_point<clock> const& tp, unsigned)
        {
            using namespace boost::chrono;
            milliseconds::rep millis = duration_cast<milliseconds>(tp.time_since_epoch()).count();
            ar & millis;
        }
} }

Note

  • I made it work generically for other clocks as well.
  • It appears that time_point only considers milliseconds since start-of-day (this is probably what you expect, but I found the name since_epoch to be confusing in this respect)

Full Code

For reference

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/chrono.hpp>

using hr_clock   = boost::chrono::high_resolution_clock;
using time_point = boost::chrono::time_point<hr_clock>;

namespace boost { namespace archive {

    template<class Archive, typename clock>
        void load(Archive& ar, boost::chrono::time_point<clock>& tp, unsigned)
        {
            using namespace boost::chrono;
            milliseconds::rep millis;

            ar & millis;
            tp = time_point<clock>(milliseconds(millis));
        }

    template<class Archive, typename clock>
        void save(Archive& ar, boost::chrono::time_point<clock> const& tp, unsigned)
        {
            using namespace boost::chrono;
            milliseconds::rep millis = duration_cast<milliseconds>(tp.time_since_epoch()).count();
            ar & millis;
        }

    template<class Archive, typename clock>
        inline void serialize(Archive & ar, boost::chrono::time_point<clock>& tp, unsigned version)
        {
            boost::serialization::split_free(ar, tp, version);
        }
} }

struct myTimestamp
{
    time_point  begin1;
    time_point  end1;

  private:
    friend class boost::serialization::access;
    template<class Archive>
        void serialize(Archive &ar, unsigned)
        {
            ar & begin1;
            ar & end1;
        }

  public:
    myTimestamp()
        : begin1(hr_clock::now()),
            end1(hr_clock::now() + boost::chrono::hours(1))
    {  }
    virtual  ~myTimestamp() { }
};

#include <sstream>

int main()
{
    std::stringstream ss;
    {
        myTimestamp ts;
        boost::archive::text_oarchive oa(ss);
        oa << ts;
    }

    {
        myTimestamp ts;
        boost::archive::text_iarchive ia(ss);
        ia >> ts;

        boost::archive::text_oarchive oa(std::cout);
        oa << ts;
    }
}