-
Notifications
You must be signed in to change notification settings - Fork 287
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 readAll() to Resource and ResourceRetriever #875
Conversation
// Safe because std::string is guaranteed to be contiguous in C++11. | ||
|
||
if (result != 1) | ||
throw std::runtime_error("Failed reading data from a resource."); |
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.
I am personally fine with this. But I thought DART tried explicitly avoided using exceptions. For consistency, it might be better to make the std::string
an output parameter and return a bool
success flag.
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.
I think there's some leniency with exceptions used during parsing, since someone would have to be pretty self-destructive to be doing any parsing during a critical control loop.
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.
But I thought DART tried explicitly avoided using exceptions.
That is true, but I thought we would like to use exceptions when we handle external resources such as files. For example, we have already used exceptions in the parsers.
dart/common/Resource.hpp
Outdated
@@ -75,6 +75,12 @@ class Resource | |||
/// \param[in] _count Number of elements, each of _size bytes. | |||
/// \note This method has the same API as the standard fread function. | |||
virtual std::size_t read(void *_buffer, std::size_t _size, std::size_t _count) = 0; | |||
|
|||
/// Reads all remaining data from this resource, and returns it as a string. |
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.
I don't think "read remaining data" is true. I suspect that this function will fail if it is called on a Resource
that has been partially consumed because getSize()
returns the size of the entire resource and read
starts reading from the current cursor position.
dart/common/Resource.hpp
Outdated
/// | ||
/// \return The string retrieved from the resource. | ||
/// \throw std::runtime_error when failed to read sucessfully. | ||
std::string readAll(); |
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.
I suggest marking this virtual
. Some classes may be able to implement this more efficiently than the generic implementation used in the base class.
dart/common/ResourceRetriever.hpp
Outdated
/// \param[in] uri URI to the resource to be retrieved. | ||
/// \return The string retrieved from the resource. | ||
/// \throw std::runtime_error when failed to read sucessfully. | ||
std::string readAll(const Uri& uri); |
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.
I suggest also making this virtual, for the same reason as I explained above.
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.
LGTM. If exceptions are good enough for @mxgrey, then they're good enough for me!
@mxgrey: Just a side question: Does DART already make use of It seems like that could make it clearer to end-users which DART functions have this guarantee and which don't, since you are right that many loading/visualization/debugging functions don't need it and could have very nice I don't know what the pros/cons are here though, just curious. |
I agree that we should consider using Perhaps even more problematic is that applying the I would like to be able to offer a guarantee to the user that DART functions will handle all possible internal exceptions in a reasonable way, but I think we would need to take some time to examine where all the potential exceptions could pop up inside of DART before we commit to applying the Conclusion: I think if we have an opportunity to review the exception safety of DART in detail and can determine that using the |
No description provided.