diff --git a/v2_0_cpp_visitor.md b/v2_0_cpp_visitor.md index b09a450..6e69d7e 100644 --- a/v2_0_cpp_visitor.md +++ b/v2_0_cpp_visitor.md @@ -91,6 +91,7 @@ struct null_visitor { } void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) { } + bool referenced() const; // Only when you want to use the visitor with a parser }; ``` @@ -208,7 +209,7 @@ They are similar to unpack APIs. When you call a parse function, then the parse function callback the visitor that you passed. If parsing process is successfully finished, then the parse function returns true, otherwise returns false. If the visitor's visit function returns false, then the parse function returns false. -You can use visitors with stream interface like an unpacker. +You can use visitors with stream interface like an `unpacker`. ```C++ template @@ -217,6 +218,16 @@ class parser `VisitorHolder` need to have a function named `visitor()` that returns a reference of the visitor object that satisfies the visitor concept. +`ReferencedBufferHook` need to have an `operator()(char *)`. The operator() is called with the current buffer pointer only when the all following conditions are satisfied: + +1. Called `parser::reserve_buffer(std::size_t)`. +2. The current buffer doesn't have sufficient size and then new buffer is allocated. +3. The current buffer is referenced by the visitor. In this case, `visitor::referenced()` should return true. + +If `visitor::referenced() returns true, the `parser` doesn't free the current buffer but call `ReferencedBufferHook`. So you can get a chance to set something cleanup function for the current buffer. + +See [unpacker's memory management mechanism](v2_0_cpp_unpacker#msgpack-controls-a-buffer). + `parser` has the same public member functions as unpacker except `bool next()`. `unpacker` has the following member functions named `next`: @@ -231,4 +242,4 @@ class parser bool next(); ``` -When you call `next()` then parse one MessagePack formatted data. \ No newline at end of file +When you call `next()` then parse one MessagePack formatted data. So visitor's visit functions are called. If the parsing process successfully finished, then returns true, otherwise returns false. \ No newline at end of file