Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. serializing an std::chrono::duration object with Boost

serializing an std::chrono::duration object with Boost

Scheduled Pinned Locked Moved C / C++ / MFC
jsonquestionannouncement
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alexander Kindel
    wrote on last edited by
    #1

    Why is it that in the following program, the value of newCount that prints at the end is 10, as I would expect, while the value of newTime.count() that prints is -3689348814741910324?

    #include "stdafx.h"
    #include
    #include
    #include
    #include
    #include

    namespace boost
    {
    namespace serialization
    {
    template
    void serialize(Archive&ar, std::chrono::nanoseconds&time, const unsigned int version)
    {
    long long count{ time.count() };
    ar&count;
    }
    }
    }

    int main()
    {
    std::chrono::nanoseconds time(10);
    long long count = time.count();
    {
    std::ofstream saveFile("filename");
    boost::archive::text_oarchive boostOutputArchieve(saveFile);
    boostOutputArchieve << time;
    boostOutputArchieve << count;
    }
    std::chrono::nanoseconds newTime;
    long long newCount;
    {
    std::ifstream loadFile("filename");
    boost::archive::text_iarchive boostInputArchieve(loadFile);
    boostInputArchieve >> newTime;
    boostInputArchieve >> newCount;
    }
    std::cout << newTime.count() << '\n' << newCount << '\n';
    return 0;
    }

    I would have thought setting up the serialize() overload for nanoseconds to serialize the tick count and using that would have the same result as serializing the tick count directly.

    A 1 Reply Last reply
    0
    • A Alexander Kindel

      Why is it that in the following program, the value of newCount that prints at the end is 10, as I would expect, while the value of newTime.count() that prints is -3689348814741910324?

      #include "stdafx.h"
      #include
      #include
      #include
      #include
      #include

      namespace boost
      {
      namespace serialization
      {
      template
      void serialize(Archive&ar, std::chrono::nanoseconds&time, const unsigned int version)
      {
      long long count{ time.count() };
      ar&count;
      }
      }
      }

      int main()
      {
      std::chrono::nanoseconds time(10);
      long long count = time.count();
      {
      std::ofstream saveFile("filename");
      boost::archive::text_oarchive boostOutputArchieve(saveFile);
      boostOutputArchieve << time;
      boostOutputArchieve << count;
      }
      std::chrono::nanoseconds newTime;
      long long newCount;
      {
      std::ifstream loadFile("filename");
      boost::archive::text_iarchive boostInputArchieve(loadFile);
      boostInputArchieve >> newTime;
      boostInputArchieve >> newCount;
      }
      std::cout << newTime.count() << '\n' << newCount << '\n';
      return 0;
      }

      I would have thought setting up the serialize() overload for nanoseconds to serialize the tick count and using that would have the same result as serializing the tick count directly.

      A Offline
      A Offline
      Alexander Kindel
      wrote on last edited by
      #2

      I've realized that -3689348814741910324 is the value uninitialized variables get set to when debugging, so...does that mean that the line

      boostInputArchieve >> newTime;

      is currently doing nothing?

      V A 2 Replies Last reply
      0
      • A Alexander Kindel

        I've realized that -3689348814741910324 is the value uninitialized variables get set to when debugging, so...does that mean that the line

        boostInputArchieve >> newTime;

        is currently doing nothing?

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        Can't you debug this code? :confused:

        A 1 Reply Last reply
        0
        • A Alexander Kindel

          I've realized that -3689348814741910324 is the value uninitialized variables get set to when debugging, so...does that mean that the line

          boostInputArchieve >> newTime;

          is currently doing nothing?

          A Offline
          A Offline
          Alexander Kindel
          wrote on last edited by
          #4

          Apparently the fact that the serialization requires (as far as I can tell) the extra step of reading the count into a variable breaks the symmetry between save and load operations, so I have to overload them individually rather than handling both in a serialize() overload. This is what I've tried:

          namespace boost
          {
          namespace serialization
          {
          template
          void save(Archive&ar, const std::chrono::nanoseconds&time, const unsigned int version)
          {
          long long count{ time.count() };
          ar&count;
          }
          template
          void load(Archive&ar, std::chrono::nanoseconds&time, const unsigned int version)
          {
          long long count;
          ar&count;
          time = std::chrono::nanoseconds(count);
          }
          }
          }

          BOOST_SERIALIZATION_SPLIT_FREE(std::chrono::nanoseconds)

          It seems to work, though I'm not confident that this is the way it's supposed to be done.

          1 Reply Last reply
          0
          • V Victor Nijegorodov

            Can't you debug this code? :confused:

            A Offline
            A Offline
            Alexander Kindel
            wrote on last edited by
            #5

            If I knew what I was looking for. It's entirely possible that my debugging skills are lacking.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups