-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Add some unittest on get_buffer #889
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c61e4cb
Add some unittest on `get_buffer`
mgautierfr 9888399
Do not try to read out of reader in release mode.
mgautierfr 973d54b
Correctly add `override` on all methods of `*Reader`.
mgautierfr 860581c
Correctly throw an exception if we fail to read in file.
mgautierfr c540503
Don't use errno on Windows when reading fails.
mgautierfr 966a8d7
Fix reading of size >4GiB on Windows.
mgautierfr e4cbe41
Use explicit size in test when reading integers.
mgautierfr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
#define ZIM_READER_H_ | ||
|
||
#include <memory> | ||
#include <stdexcept> | ||
|
||
#include "zim_types.h" | ||
#include "endian_tools.h" | ||
|
@@ -31,13 +32,23 @@ | |
|
||
namespace zim { | ||
|
||
class Reader { | ||
class LIBZIM_PRIVATE_API Reader { | ||
public: | ||
Reader() {}; | ||
virtual zsize_t size() const = 0; | ||
virtual ~Reader() {}; | ||
|
||
virtual void read(char* dest, offset_t offset, zsize_t size) const = 0; | ||
void read(char* dest, offset_t offset, zsize_t size) const { | ||
if (can_read(offset, size)) { | ||
if (size) { | ||
// Do the actuall read only if we have a size to read | ||
readImpl(dest, offset, size); | ||
} | ||
return; | ||
} | ||
throw std::runtime_error("Cannot read after the end of the reader"); | ||
} | ||
Comment on lines
-40
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if the commit message is updated |
||
|
||
template<typename T> | ||
T read_uint(offset_t offset) const { | ||
ASSERT(offset.v, <, size().v); | ||
|
@@ -46,7 +57,13 @@ class Reader { | |
read(tmp_buf, offset, zsize_t(sizeof(T))); | ||
return fromLittleEndian<T>(tmp_buf); | ||
} | ||
virtual char read(offset_t offset) const = 0; | ||
|
||
char read(offset_t offset) const { | ||
if (can_read(offset, zsize_t(1))) { | ||
return readImpl(offset); | ||
} | ||
throw std::runtime_error("Cannot read after the end of the reader"); | ||
} | ||
|
||
virtual const Buffer get_buffer(offset_t offset, zsize_t size) const = 0; | ||
const Buffer get_buffer(offset_t offset) const { | ||
|
@@ -59,6 +76,15 @@ class Reader { | |
virtual offset_t offset() const = 0; | ||
|
||
bool can_read(offset_t offset, zsize_t size) const; | ||
|
||
private: | ||
// Implementation of the read method. | ||
// Check of the validity of the offset/size has already been done. | ||
virtual void readImpl(char* dest, offset_t offset, zsize_t size) const = 0; | ||
|
||
// Implementation of the read method. | ||
// Check of the validity of the offset has already been done. | ||
virtual char readImpl(offset_t offset) const = 0; | ||
}; | ||
|
||
}; | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit better be moved right after the one introducing
Reader::readImpl()