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

Add readAll() to Resource and ResourceRetriever #875

Merged
merged 6 commits into from
Apr 18, 2017
Merged

Conversation

jslee02
Copy link
Member

@jslee02 jslee02 commented Apr 17, 2017

No description provided.

@jslee02 jslee02 added this to the DART 6.2.0 milestone Apr 17, 2017
// 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.");
Copy link
Collaborator

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.

Copy link
Member

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.

Copy link
Member Author

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.

@@ -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.
Copy link
Collaborator

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.

///
/// \return The string retrieved from the resource.
/// \throw std::runtime_error when failed to read sucessfully.
std::string readAll();
Copy link
Collaborator

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.

/// \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);
Copy link
Collaborator

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.

Copy link
Collaborator

@mkoval mkoval left a 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! :neckbeard:

@jslee02 jslee02 merged commit 52bbd15 into master Apr 18, 2017
@jslee02 jslee02 deleted the feature/read_all branch April 18, 2017 14:42
@psigen
Copy link
Collaborator

psigen commented Apr 18, 2017

@mxgrey: Just a side question: Does DART already make use of noexcept to distinguish code paths that never throw exceptions from ones that do?

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 std::exceptions instead.

I don't know what the pros/cons are here though, just curious.

@mxgrey
Copy link
Member

mxgrey commented Apr 18, 2017

I agree that we should consider using noexcept for the benefit clarity and potential performance improvements. The catch (no pun intended) is that we do allow exceptions (in the form of assert) everywhere when DART is compiled in debug mode. The logic is that unwinding the stack is likely to be problematic when running a critical control loop, but it can be very useful when debugging errors.

Perhaps even more problematic is that applying the noexcept specifier to a function which calls any potentially-throwing functions will result in std::terminate if the noexcept function does not handle the exception itself. So if anything used inside of a DART noexcept function does throw an exception, then that DART function must handle the exception or else the entire program will be terminated.

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 noexcept specifier anywhere.

Conclusion: I think if we have an opportunity to review the exception safety of DART in detail and can determine that using the noexcept specifier is safe, then we could use a macro which expands to noexcept in release mode while being blank in debug mode.

@psigen
Copy link
Collaborator

psigen commented Apr 18, 2017

Thanks @mxgrey. I moved the discussion to #878 so we can continue brainstorming about this.

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.

4 participants