clone-visitor.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Clone Visitor
  3. *
  4. * Copyright (C) 2016 Red Hat, Inc.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. */
  10. #ifndef QAPI_CLONE_VISITOR_H
  11. #define QAPI_CLONE_VISITOR_H
  12. #include "qapi/error.h"
  13. #include "qapi/visitor.h"
  14. /*
  15. * The clone visitor is for direct use only by the QAPI_CLONE() macro;
  16. * it requires that the root visit occur on an object, list, or
  17. * alternate, and is not usable directly on built-in QAPI types.
  18. */
  19. typedef struct QapiCloneVisitor QapiCloneVisitor;
  20. Visitor *qapi_clone_visitor_new(void);
  21. Visitor *qapi_clone_members_visitor_new(void);
  22. /*
  23. * Deep-clone QAPI object @src of the given @type, and return the result.
  24. *
  25. * Not usable on QAPI scalars (integers, strings, enums), nor on a
  26. * QAPI object that references the 'any' type. Safe when @src is NULL.
  27. */
  28. #define QAPI_CLONE(type, src) \
  29. ({ \
  30. Visitor *v_; \
  31. type *dst_ = (type *) (src); /* Cast away const */ \
  32. \
  33. if (dst_) { \
  34. v_ = qapi_clone_visitor_new(); \
  35. visit_type_ ## type(v_, NULL, &dst_, &error_abort); \
  36. visit_free(v_); \
  37. } \
  38. dst_; \
  39. })
  40. /*
  41. * Copy deep clones of @type members from @src to @dst.
  42. *
  43. * Not usable on QAPI scalars (integers, strings, enums), nor on a
  44. * QAPI object that references the 'any' type.
  45. */
  46. #define QAPI_CLONE_MEMBERS(type, dst, src) \
  47. ({ \
  48. Visitor *v_; \
  49. \
  50. v_ = qapi_clone_members_visitor_new(); \
  51. *(type *)(dst) = *(src); \
  52. visit_type_ ## type ## _members(v_, (type *)(dst), &error_abort); \
  53. visit_free(v_); \
  54. })
  55. #endif