-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Reuse memory in to_cbor and to_msgpack functions #476
Comments
I think if such functions are added, the should not clear the vector - it should be the user's responsibility. |
Yes, perhaps it would be better. |
Since we already are at C++11, a return value would be clearer. Move semantics already takes care of passing return values without additional memory handling, also it can be directly used in other functions. The void return value also forces the break of a call chain like |
It is not about copying on assignment. As an example of using such functions - some service exchange messages cbor or msgpack format and using a buffer to receive and transmit them. I do not mind to call these functions like - otherwise, but would like to have them available. |
This is not a universally accepted convention, however. It more depends on the context - what would make most sense. For example, Usually, to make it unambiguous that the function will not clear out a container, but will rather append to it, the convention is to pass |
You could also put |
I'm just wondering whether it would make sense to provide a reference to a vector when we do not know the size and hence cannot control whether the vector would use other memory anyways. |
This is useful if we use the same vector multiple times. When the "clear" method is executed, the vector does not free up memory and can reuse it. |
Alright, so I would propose a function like template<typename CharT, typename std::enable_if<
std::is_integral<CharT>::value and
sizeof(CharT) == 1, int>::type = 0>
static void to_cbor(const basic_json& j, std::vector<CharT>& vec) which writes to the vector As #477 would also introduce a function like static void to_cbor(const basic_json &j, std::ostream &o) I would propose to deprecate the static std::vector<uint8_t> to_cbor(const basic_json& j) function to unify the function signatures. What's your opinion on this? |
Looks good. I like. |
Opened #623 as place to discuss the structure of the parsing functions. |
- You can now pass a reference to a vector to the to_cbor and to_msgpack functions. The output will be written (appended) to the vector. #476 - You can now pass an output stream with uint8_t character type to the to_cbor and to_msgpack functions. #477 - You can now read from uint8_t */size in the to_cbor and to_msgpack functions. An input adapter will be created from this pair, so you need to use braces. #478
Function static void to_cbor(const basic_json& j, detail::output_adapter<uint8_t> o)
{
binary_writer(o).write_cbor(j);
} now allows to write CBOR (and similarly, MessagePack) to the following types from which an
Note the function does not clear passed vectors. |
Done. |
It would be good to add the implementation of functions for memory reuse purposes:
The text was updated successfully, but these errors were encountered: