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

Initialize is_open value in ReadFileBuffer. #254

Merged
merged 2 commits into from
Jun 7, 2017

Conversation

markdewing
Copy link
Contributor

Address comments in issue #253.

  • initialize the value of is_open to false by default
  • Update the filename in the APP_ABORT messages
  • change get_file_length to be a member function.

Address comments in issue QMCPACK#253.
 - initialize the value of is_open to false by default
 - Update the filename in the APP_ABORT messages
 - change get_file_length to be a member function.
@ye-luo ye-luo self-requested a review June 7, 2017 17:06
Copy link
Contributor

@prckent prckent left a comment

Choose a reason for hiding this comment

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

+1 OK to merge after @ye-luo 's OK.

@ye-luo
Copy link
Contributor

ye-luo commented Jun 7, 2017

In open_file, set is_open to false if an open attempt fails.
Also check if fin has been allocated, free it first and new after. Otherwise, memory leaks.

@@ -86,10 +86,12 @@ class ReadFileBuffer
char *cbuffer;
std::ifstream *fin;
Communicate *myComm;
int get_file_length(std::ifstream *f);
Copy link
Contributor

Choose a reason for hiding this comment

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

Add const if this routine doesn't touch member variables.

Copy link
Contributor

Choose a reason for hiding this comment

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

How about remove the argument and directly operates on fin?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How to write get_file_length was mentioned in the bug report on OO vs. functional. Yes, it could operate directly on fin and write to length. However, I think that makes it harder to understand in isolation vs. using a more functional style.

@markdewing
Copy link
Contributor Author

The behavior for re-using this class is undefined, since it's currently not used that way.
Options

  1. Allow only once - immutable. Attempting to call open_file if another file is open will return false.
  2. Allow reopening a file. Then the open_file method close/delete fin and delete the buffer if allocated.

I would be inclined to go for (1), because it would simplify the usage.

@ye-luo
Copy link
Contributor

ye-luo commented Jun 7, 2017

The fact that open_file operates differently from other standard "open" provided by languages is a potential trap.

  1. if return false, the meaning of false is different from a failed false.
  2. Just a few more lines of code and it is consistent with common knowledge.

If a file has been opened using ReadFileBuffer, close it and clear the buffer
before opening the new file.
Add a unit test for the re-open case.
Make get_file_length const, as it does not modify the class.
@prckent prckent merged commit 259d67f into QMCPACK:develop Jun 7, 2017
@prckent prckent mentioned this pull request Jun 8, 2017
@markdewing markdewing deleted the ecp_read2 branch August 7, 2017 17:48
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.

3 participants