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 support for constructing Buffer from std::array #138

Merged
merged 3 commits into from
Nov 20, 2018

Conversation

tgaldes
Copy link

@tgaldes tgaldes commented Nov 17, 2018

No description provided.

Copy link
Owner

@mfontanini mfontanini left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Minor comment.

*/
template <typename T, size_t N>
Buffer(const std::array<T, N>& data)
: data_(reinterpret_cast<const DataType*>(&data[0])), size_(N * sizeof(T)) {
Copy link
Owner

Choose a reason for hiding this comment

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

Minor: can you use data.data() and data.size() rather than &data[0] and N * sizeof(T) here?

Copy link
Author

Choose a reason for hiding this comment

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

will do! first time working with std::arrays

Copy link
Owner

Choose a reason for hiding this comment

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

Perfect, that works! One more thing: can you add a deleted constructor taking a std::array<T, N>&& just like there is for vector and string? e.g. this one. This will make sure people can't misuse Buffer and construct it from a temporary array, which will end up making the buffer contain garbage once it goes out of scope.

Copy link
Author

Choose a reason for hiding this comment

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

should be fixed

@accelerated
Copy link
Contributor

Maybe we can also have an overload for raw arrays Buffer(T(&arr)[N]) ?

@mfontanini
Copy link
Owner

Yeah, that's a good idea!

@tgaldes
Copy link
Author

tgaldes commented Nov 19, 2018

@accelerated is there a difference between construction with the raw array and the constructor Buffer(const T*, size_t size) here:
https://github.com/mfontanini/cppkafka/blob/master/include/cppkafka/buffer.h#L76

None of the code in message_builder.h uses that template from line 76, so maybe the solution is to overload payload() of BasicMessageBuilder with payload(const T* value, size_t size), and also overload the implicit conversion between ConcreteMB and MBuilder with something that will construct the buffer of the MBuilder with that same pointer and size to that no data needs to be copied.

@accelerated
Copy link
Contributor

@tgaldes, the difference is simplicity and easy of use. Just like you can skip the std::vector and std::array constructors because eventually everything is convertible to a T* and a size, but they're nice to have. BasicMessageBuilder takes BufferType in all its methods and those can be a Buffer, so no need to overload with (const T*, size_t). It would make the message builder class unnecessarily complicated.

@mfontanini mfontanini merged commit 4ad2685 into mfontanini:master Nov 20, 2018
@mfontanini
Copy link
Owner

Thanks! The PR build is flapping but it's unrelated.

@tgaldes
Copy link
Author

tgaldes commented Nov 20, 2018

@accelerated Here's the addition you requested: #140

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