-
Notifications
You must be signed in to change notification settings - Fork 77
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 new file access methods to support more use cases conveniently #49
base: master
Are you sure you want to change the base?
Add new file access methods to support more use cases conveniently #49
Conversation
fc3b502
to
3e1f1bb
Compare
Maybe std::span? Or std::string_view? |
I will send an updated PR adding |
3e1f1bb
to
c83f019
Compare
I added a std::string_view accessor, updated the docs (did a drive-by fix on an indentation problem), added a standalone example and added more tests (drive-by fix of a potential undefined return value). Separated this into five separate commits to make it easier to review. Let me know what you think when you have time @vector-of-bool. |
To make the resource access convenient in more cases the following access functions have been added: get_as_string() get_as_string_view() - requires C++17 get_as_raw_ptr() get_size()
c83f019
to
e2b1bb8
Compare
Fixed a problem with reference assignment (noticed it with MSVC with C++20 standard mode). |
Sorry that I missed this. It got buried in notifications and I just saw it after your comment yesterday. This project has unfortunately languished a bit while I've been busy with other things. I think, IIUC, what this PR and #24 are looking for is an easy As for the convenience interfaces of // BEWARE: Untested code!
template <
typename To,
typename ConstPointer = typename To::const_pointer,
typename = std::enable_if_t<
// Can construct with a size-pointer pair
std::is_constructible_v<To, ConstPointer, std::size_t>
// Make sure the elements are byte-sized
and sizeof(std::remove_pointer_t<ConstPointer>) == 1
>>
explicit operator T() const noexcept {
return To(reinterpret_cast<ConstPointer>(data()), size());
} with this, any class which satisfies the constraints can be used in an explicit conversion: cmrc::file get_some_file();
// ...
auto sv = std::string_view(get_some_file());
How does that sound? |
No worries at all - mailboxes get flooded and life is busy for us all. Thank you for creating the little gem that CMRC is. Really helps creating self-contained portable apps smoothly! That said, that is some strong C++ Kung Fu you have there! Learned a lot just looking up what those template constructs do. From a library users perspective your solution looks clean and efficient. Does this mean that as a consumer of the resources one could do this: // No copy of data, direct access to read-only (const) data
const char* raw = std::string_view(fs.open("file.txt")).data(); If so, I'd happily take your solution over this MR. |
Gently bumping this one a little bit @vector-of-bool :-) |
This change adds support for directly fetching resource data as std::string as well as raw "const char*" combined with std::size_t. This allows cleaner code in use cases where the existing file interface is overkill.
Complete with docs and tests. Also added small doc about the NULL byte that is appended to all resources.
Not super happy about the get_as_raw_ptr() returning size through an output argument, but I can't think of a cleaner way.
Thoughts?