ABY icon indicating copy to clipboard operation
ABY copied to clipboard

Keep changes to cout flags local

Open sebastianst opened this issue 5 years ago • 1 comments

It would be great if ABY kept changes to cout (and possibly other streams) flags local, i.e., save them before changing them and then restore them. An RAII solution that probably should go into ENCRYPTO_utils could be the following class

/**
 * RAII ostream flags saver
 * https://stackoverflow.com/a/18822888/1523730
 */
class ios_flags_saver {
  public:
    explicit ios_flags_saver(std::ostream& _ios):
      ios(_ios),
      f(_ios.flags()) {}
    ~ios_flags_saver() {
      ios.flags(f);
    }

    ios_flags_saver(const ios_flags_saver&) = delete;
    ios_flags_saver& operator=(const ios_flags_saver&) = delete;

  private:
    std::ostream& ios;
    std::ios::fmtflags f;
};

Then one could write

ios_flags_saver _flags_saver(std::cout);

at the beginning of each function that modifies cout.

This would be especially desirable for Sharing::EvaluatePrintValGate, where cout is currently reset to decimal printing with << std::dec which might not have been the state before the function call.

sebastianst avatar Aug 23 '18 15:08 sebastianst

Good idea. We could also use the ios_flag_saver that comes with boost, if we include boost with the network changes in https://github.com/encryptogroup/ENCRYPTO_utils/pull/14 anyways

dd23 avatar Nov 12 '18 18:11 dd23