pr0073-add-document-destroy-hook.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. From aa19573fcfdc9344c217277c63fe6e1890e56e89 Mon Sep 17 00:00:00 2001
  2. From: Innokentii Mokin <innokentii@yandex-team.ru>
  3. Date: Sun, 5 Mar 2023 23:25:29 +0300
  4. Subject: [PATCH] add document destroy hook
  5. ---
  6. include/libfyaml.h | 70 ++++++++++++++++++++++++++++++++++++++++++++++
  7. src/lib/fy-doc.c | 37 ++++++++++++++++++++++++
  8. src/lib/fy-doc.h | 3 ++
  9. 3 files changed, 110 insertions(+)
  10. diff --git a/include/libfyaml.h b/include/libfyaml.h
  11. index 828f077..db85d54 100644
  12. --- a/include/libfyaml.h
  13. +++ b/include/libfyaml.h
  14. @@ -4758,6 +4758,76 @@ void
  15. fy_document_unregister_meta(struct fy_document *fyd)
  16. FY_EXPORT;
  17. +/**
  18. + * fy_document_get_userdata() - Get the userdata pointer of a document
  19. + *
  20. + * Return the userdata pointer of a document.
  21. + *
  22. + * @fyn: The document to get userdata from
  23. + *
  24. + * Returns:
  25. + * The stored userdata pointer
  26. + */
  27. +void *
  28. +fy_document_get_userdata(struct fy_document *fyd)
  29. + FY_EXPORT;
  30. +
  31. +/**
  32. + * fy_document_set_userdata() - Set the userdata pointer of a document
  33. + *
  34. + * Set the userdata pointer of a document. If @userdata is NULL
  35. + * then clear the userdata.
  36. + *
  37. + * @fyd: The document to set userdata
  38. + * @userdata: The userdata pointer
  39. + *
  40. + * Returns:
  41. + * 0 on success, -1 on error
  42. + */
  43. +int
  44. +fy_document_set_userdata(struct fy_document *fyd, void *userdata)
  45. + FY_EXPORT;
  46. +
  47. +/**
  48. + * typedef fy_document_on_destroy_fn - Userdata clear method
  49. + *
  50. + * This is the callback called just before document is destroyed.
  51. + *
  52. + * @fyd: The document which will be destroyed
  53. + * @userdata: The userdata pointer of a document
  54. + *
  55. + */
  56. +typedef void (*fy_document_on_destroy_fn)(struct fy_document *fyd, void *userdata);
  57. +
  58. +/**
  59. + * fy_document_register_on_destroy() - Register an on_destroy hook
  60. + *
  61. + * Register an on_destroy hook, to be called when
  62. + * the document is freed via a final call to fy_document_destroy().
  63. + *
  64. + * @fyd: The document which the hook is registered to
  65. + * @on_destroy_fn: The on_destroy hook method
  66. + *
  67. + * Returns:
  68. + * 0 on success, -1 if another hook is already registered.
  69. + */
  70. +int
  71. +fy_document_register_on_destroy(struct fy_document *fyd,
  72. + fy_document_on_destroy_fn on_destroy_fn)
  73. + FY_EXPORT;
  74. +
  75. +/**
  76. + * fy_document_unregister_on_destroy() - Unregister an on_destroy hook
  77. + *
  78. + * Unregister the currently active on_destroy hook.
  79. + *
  80. + * @fyd: The document to unregister it's on_destroy hook.
  81. + */
  82. +void
  83. +fy_document_unregister_on_destroy(struct fy_document *fyd)
  84. + FY_EXPORT;
  85. +
  86. +
  87. /**
  88. * fy_node_set_marker() - Set a marker of a node
  89. *
  90. diff --git a/src/lib/fy-doc.c b/src/lib/fy-doc.c
  91. index 602264b..c2f778c 100644
  92. --- a/src/lib/fy-doc.c
  93. +++ b/src/lib/fy-doc.c
  94. @@ -380,6 +380,9 @@ void fy_parse_document_destroy(struct fy_parser *fyp, struct fy_document *fyd)
  95. fy_diag_unref(fyd->diag);
  96. + if (fyd->on_destroy_fn)
  97. + fyd->on_destroy_fn(fyd, fyd->userdata);
  98. +
  99. free(fyd);
  100. }
  101. @@ -6285,6 +6288,40 @@ void fy_document_unregister_meta(struct fy_document *fyd)
  102. fyd->meta_user = NULL;
  103. }
  104. +int fy_document_set_userdata(struct fy_document *fyd, void *userdata)
  105. +{
  106. + if (!fyd || !userdata)
  107. + return -1;
  108. +
  109. + fyd->userdata = userdata;
  110. +
  111. + return 0;
  112. +}
  113. +
  114. +void* fy_document_get_userdata(struct fy_document *fyd)
  115. +{
  116. + return fyd->userdata;
  117. +}
  118. +
  119. +int fy_document_register_on_destroy(struct fy_document *fyd,
  120. + fy_document_on_destroy_fn on_destroy_fn)
  121. +{
  122. + if (!fyd || !on_destroy_fn)
  123. + return -1;
  124. +
  125. + fyd->on_destroy_fn = on_destroy_fn;
  126. +
  127. + return 0;
  128. +}
  129. +
  130. +void fy_document_unregister_on_destroy(struct fy_document *fyd)
  131. +{
  132. + if (!fyd)
  133. + return;
  134. +
  135. + fyd->on_destroy_fn = NULL;
  136. +}
  137. +
  138. bool fy_node_set_marker(struct fy_node *fyn, unsigned int marker)
  139. {
  140. unsigned int prev_marks;
  141. diff --git a/src/lib/fy-doc.h b/src/lib/fy-doc.h
  142. index 6c15024..b268edf 100644
  143. --- a/src/lib/fy-doc.h
  144. +++ b/src/lib/fy-doc.h
  145. @@ -118,6 +118,9 @@ struct fy_document {
  146. fy_node_meta_clear_fn meta_clear_fn;
  147. void *meta_user;
  148. + fy_document_on_destroy_fn on_destroy_fn;
  149. + void *userdata;
  150. +
  151. struct fy_path_expr_document_data *pxdd;
  152. };
  153. /* only the list declaration/methods */