Boost logo

Boost Users :

From: Niels Dekker - mail address until 2007-11-30 (nd_mail_address_valid_until_2007-11-30_at_[hidden])
Date: 2007-03-08 17:48:35


The state_saver class at www.boost.org/boost/state_saver.hpp appears to
be very useful for me. But I would like to propose two changes:

First of all, I would like to have an extra constructor that would allow
specifying a new value for the object (whose previous value will be
restored when state_saver is destructed). Such a constructor would be
very convenient, and it would also be conforming to io_state_saver's
classes, by Daryle Walker. www.boost.org/libs/io/doc/ios_state.html
A possible implementation would be:

 state_saver(
   T & object, const T & new_value
 ) :
   previous_value(object),
   previous_ref(object)
 {
   previous_ref = new_value;
 }

Secondly I would suggest using a swap instead of an assignment, when
restoring the previous value (when state_saver is destructed). A swap
is much more likely to be fail-safe and efficient than an assignment.
So I would propose the destructor to look as follows:

 ~state_saver() {
   // Doing "using std::swap", as suggested by Scott Meyers,
   // Effective C++ 3rd Edition, item 25, "Consider support
   // for a non-throwing swap"
   using std::swap;
   swap(previous_ref, previous_value);
 }

Will these changes generally be considered an improvement?

I already wrote Robert Ramey, the owner of state_saver.hpp. He also
recommanded posting a message on this subject. So please let me know
what you think!

-- 
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
C++ programmer at LKEB, Leiden University Medical Center

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