encode/decode functions std::string_view instead of vector

Issue #11 new
WorkerH created an issue

If the encode and decode function of Signal would use std::string_view instead of std::vector it would be possible to pass the data from the stack and the caller doesn't need to create a vector, allocate memory and copy the data into the new allocated memory location:

Instead of uint64_t decode(std::vector<uint8_t> & data); this uint64_t decode(std::string_view<uint8_t> data);

And invoke it like:

can_frame frame = receive_frame();
signal.decode({frame.data, frame.dlc});

Comments (7)

  1. Tobias Lorenz repo owner

    Hi Julian,

    also, looks great. Same here, I need to change the unit tests also, and then release it.

    Bye Tobias

  2. Tobias Lorenz repo owner

    At least here on Linux and gcc, string_view is not defined for uint8_t, only for char-types. With the "& data" there should also be no copy necessary. At least this was my intention with it. What do you say?

  3. WorkerH reporter

    You can use the std::u8string_view typedef for a std::string_view, this should be defined in <string_view> (it's C++17). I'm not 100% familiar how you should use a std::string_view, but I guess it's okay to copy them, because they are really lightweight (theoretically you only need 16 Bytes on a x64 maschine), so the compiler respectively the CPU doesn't need to dereference the std::sting_view.

    I'm thinking like passing a std::string_view& to a function would be the same like passing a char*& to a function.

  4. Log in to comment