
On Tue, Feb 26, 2019 at 11:29:33 -0600, Eric Blake wrote:
On 2/26/19 11:08 AM, Peter Krempa wrote:
Quite a few parts modify the XPath context current node to shif the
shift
scope and allow easier queries. This also means that the node needs to be restored afterwards.
Introduce a macro based on 'VIR_AUTOCLEAN' which adds a local structure on the stack remembering the original node along with a function which will make sure that the node is reset when the local structure leaves scope.
Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virxml.c | 10 ++++++++++ src/util/virxml.h | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+)
Nice idea.
+/** + * VIR_XPATH_NODE_AUTORESTORE: + * @ctxt: XML XPath context pointer + * + * This macro ensures that when the scope where it's used ends @ctxt's current
Reads better with s/ends/ends,/
+ * node pointer is reset to the original value when this macro was used. + */ +# define VIR_XPATH_NODE_AUTORESTORE(ctxt) \ + VIR_AUTOCLEAN(virXPathContextNodeSave) ctxt ## CtxtSave = {(ctxt), (ctxt)->node}
Worth using C99 syntax as in { .ctxt = (ctxt), .node = (ctxt)->node, } ?
It would require renaming the argument of the macro as it would be replaced otherwise. Interrestingly in most cases it would actually work as in most cases the macro is used with 'ctxt'.