Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use iostream::fail() to check for I/O error #522

Merged
merged 2 commits into from
Apr 12, 2023
Merged

Conversation

jhenin
Copy link
Member

@jhenin jhenin commented Apr 7, 2023

Fixes #521

Copy link
Member

@giacomofiorin giacomofiorin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking again at this change and the related issue, it is really not self-apparent why one should use fail() instead of bad(). Our use case has always been "test if there are any errors (no matter of what kind) and then read the stream".

So the most intuitive check is probably just using the bool cast operator, and testing the stream object directly (I believe this was also @HanatoK's suggestion earlier on).

Approving because it's a bugfix, but my preference would be for simpler semantics here, i.e. either just use the bool operator throughout or (as a less elegant option) set the badbit ourselves inside colvarproxy::input_stream() before returning it to the caller.

@jhenin
Copy link
Member Author

jhenin commented Apr 11, 2023

it is really not self-apparent why one should use fail() instead of bad(). Our use case has always been "test if there are any errors (no matter of what kind) and then read the stream".

The intended use of bad() is not very clear to me, that is, based on the name I would expect bad() to be the broadest way of catching errors. But I've accepted that it isn't :-)

I did consider using the bool cast, which is semantically equivalent to fail(), and thought that fail() was more explicit and slightly easier to read, but I'm not strongly opinionated on this. I'm ok with testing the stream objects themselves.

@giacomofiorin
Copy link
Member

I did consider using the bool cast, which is semantically equivalent to fail(), and thought that fail() was more explicit and slightly easier to read, but I'm not strongly opinionated on this. I'm ok with testing the stream objects themselves.

Indeed fail() is easier to read: what's not easy at all is distinguishing it from bad(), nor is it accepting the fact that (! good()) != bad().

Ok, can you please change to bool and then merge?

@jhenin
Copy link
Member Author

jhenin commented Apr 11, 2023

accepting the fact that (! good()) != bad().

Yup, that's f-ed up. Thank you C++.

@jhenin jhenin merged commit 1513218 into master Apr 12, 2023
29 checks passed
@jhenin jhenin deleted the check_stream_fail branch April 12, 2023 09:33
@giacomofiorin giacomofiorin restored the check_stream_fail branch April 12, 2023 15:28
@giacomofiorin giacomofiorin deleted the check_stream_fail branch April 12, 2023 15:28
giacomofiorin added a commit to Colvars/lammps that referenced this pull request May 17, 2023
This update consists exclusively of bugfixes or maintenance-related changes.

The following is a list of pull requests in the Colvars repository since the previous update to LAMMPS:

- 532 Add XYZ trajectory reading feature
  Colvars/colvars#532 (@jhenin, @giacomofiorin)

- 531 Delete objects quietly, unless explicitly requested via script (including VMD)
  Colvars/colvars#531 (@giacomofiorin)

- 530 Append newline to log and error messages if not already present
  Colvars/colvars#530 (@giacomofiorin)

- 528 Forward-declare OpenMP lock
  Colvars/colvars#528 (@giacomofiorin)

- 527 Remove unneeded STL container
  Colvars/colvars#527 (@giacomofiorin)

- 526 Allow collecting configuration files and strings before setting up interface
  Colvars/colvars#526 (@giacomofiorin, @jhenin)

- 523 Fallback to linearCombination when customFunction is missing in customColvar
  Colvars/colvars#523 (@HanatoK, @giacomofiorin)

- 522 Use iostream::fail() to check for I/O error
  Colvars/colvars#522 (@jhenin)

- 520 Fix ref count
  Colvars/colvars#520 (@giacomofiorin)

- 513 Set target temperature through a common code path
  Colvars/colvars#513 (@giacomofiorin, @jhenin)

- 509 Safer detection of Windows with recent Microsoft Visual Studio versions
  Colvars/colvars#509 (@akohlmey)

- 508 Update LAMMPS patching method to reflect Lepton availability
  Colvars/colvars#508 (@giacomofiorin)

- 497 Increase the precision of write_multicol
  Colvars/colvars#497 (@HanatoK)

- 496 Only perform MTS automatic enable/disable for timeStepFactor > 1
  Colvars/colvars#496 (@giacomofiorin)

- 493 Remove unused branch of quaternion input function
  Colvars/colvars#493 (@giacomofiorin)

- 489 Ensure there are spaces between the fields in the header
  Colvars/colvars#489 (@HanatoK)

- 487 Use map of output streams, and return references to its elements
  Colvars/colvars#487 (@giacomofiorin, @jhenin)

- 486 Remember first step of moving restraint
  Colvars/colvars#486 (@jhenin)

- 485 Add decoupling option for moving restraints
  Colvars/colvars#485 (@jhenin)

- 483 Update Lepton via patching procedure
  Colvars/colvars#483 (@giacomofiorin)

- 481 Make file-reading operations of input data abstractable
  Colvars/colvars#481 (@giacomofiorin)

Authors: @akohlmey, @giacomofiorin, @HanatoK, @jhenin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cv load with non-existent file fails silently
2 participants