Opened 8 years ago

Closed 8 years ago

#10992 closed Bugs (fixed)

Chrono IO state savers inconsistent with Boost.IO state savers

Reported by: rkawulak Owned by: viboes
Milestone: Boost 1.58.0 Component: chrono
Version: Boost Development Trunk Severity: Problem
Keywords: Cc:

Description

The constructors of Boost.IO state savers taking the second new_value argument set the state to the new_value immediately and restore the previous state upon destruction. OTOH, the Boost.Chrono IO state savers do something different (and arguably unintuitive): they don't alter the state upon construction at all and restore the state to new_value rather than the previous state upon destruction. The behaviour should be aligned with that of Boost.IO to avoid confusion and make it more useful.

Change History (10)

comment:1 by viboes, 8 years ago

Please, could you be more explicit?

Are you referring to

    struct duration_style_io_saver
    {

      //! the type of the state to restore
      typedef std::ios_base state_type;
      //! the type of aspect to save
      typedef duration_style aspect_type;

      /**
       * Explicit construction from an i/o stream.
       *
       * Store a reference to the i/o stream and the value of the associated @c duration_style.
       */
      explicit duration_style_io_saver(state_type &s) :
        s_save_(s)
      {
        a_save_ = get_duration_style(s_save_);
      }

      /**
       * Construction from an i/o stream and a @c duration_style to restore.
       *
       * Stores a reference to the i/o stream and the value @c duration_style to restore given as parameter.
       */
      duration_style_io_saver(state_type &s, aspect_type new_value) :
        s_save_(s), a_save_(new_value)
      {
      }

      /**
       * Destructor.
       *
       * Restores the i/o stream with the duration_style to be restored.
       */
      ~duration_style_io_saver()
      {
        this->restore();
      }

      /**
       * Restores the i/o stream with the duration_style to be restored.
       */
      void restore()
      {
        set_duration_style(s_save_, a_save_);
      }

    private:
      duration_style_io_saver& operator=(duration_style_io_saver const& rhs) ;

      state_type& s_save_;
      aspect_type a_save_;
    };

comment:2 by viboes, 8 years ago

Status: newassigned

comment:3 by rkawulak, 8 years ago

Yes. The second constructor should be implemented the following way to be consistent with IO state saver semantics (see for example basic_ios_iostate_saver):

duration_style_io_saver(state_type &s, aspect_type new_value) :
  s_save_(s), a_save_(get_duration_style(s))
{
  set_duration_style(s, new_value);
}

The same for all the remaining Chrono IO state savers.

comment:4 by viboes, 8 years ago

Thanks for catching this point. I missed it completly. I should stop adding code that I don't test.

comment:5 by viboes, 8 years ago

Have you find other cases?

comment:6 by rkawulak, 8 years ago

IIRC, all IO state savers in Chrono library are implemented this way.

comment:8 by rkawulak, 8 years ago

Seems that not all documentation comments have been updated accordingly.

comment:9 by viboes, 8 years ago

Ok, I will take a look.

Note: See TracTickets for help on using tickets.