visitor-impl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Core Definitions for QAPI Visitor implementations
  3. *
  4. * Copyright (C) 2012-2016 Red Hat, Inc.
  5. *
  6. * Author: Paolo Bonizni <pbonzini@redhat.com>
  7. *
  8. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  9. * See the COPYING.LIB file in the top-level directory.
  10. *
  11. */
  12. #ifndef QAPI_VISITOR_IMPL_H
  13. #define QAPI_VISITOR_IMPL_H
  14. #include "qapi/visitor.h"
  15. /*
  16. * This file describes the callback interface for implementing a QAPI
  17. * visitor. For the client interface, see visitor.h. When
  18. * implementing the callbacks, it is easiest to declare a struct with
  19. * 'Visitor visitor;' as the first member. A callback's contract
  20. * matches the corresponding public functions' contract unless stated
  21. * otherwise. In the comments below, some callbacks are marked "must
  22. * be set for $TYPE visits to work"; if a visitor implementation omits
  23. * that callback, it should also document that it is only useful for a
  24. * subset of QAPI.
  25. */
  26. /*
  27. * There are four classes of visitors; setting the class determines
  28. * how QAPI enums are visited, as well as what additional restrictions
  29. * can be asserted. The values are intentionally chosen so as to
  30. * permit some assertions based on whether a given bit is set (that
  31. * is, some assertions apply to input and clone visitors, some
  32. * assertions apply to output and clone visitors).
  33. */
  34. typedef enum VisitorType {
  35. VISITOR_INPUT = 1,
  36. VISITOR_OUTPUT = 2,
  37. VISITOR_CLONE = 3,
  38. VISITOR_DEALLOC = 4,
  39. } VisitorType;
  40. struct Visitor
  41. {
  42. /*
  43. * Only input visitors may fail!
  44. */
  45. /* Must be set to visit structs */
  46. bool (*start_struct)(Visitor *v, const char *name, void **obj,
  47. size_t size, Error **errp);
  48. /* Optional; intended for input visitors */
  49. bool (*check_struct)(Visitor *v, Error **errp);
  50. /* Must be set to visit structs */
  51. void (*end_struct)(Visitor *v, void **obj);
  52. /* Must be set; implementations may require @list to be non-null,
  53. * but must document it. */
  54. bool (*start_list)(Visitor *v, const char *name, GenericList **list,
  55. size_t size, Error **errp);
  56. /* Must be set */
  57. GenericList *(*next_list)(Visitor *v, GenericList *tail, size_t size);
  58. /* Optional; intended for input visitors */
  59. bool (*check_list)(Visitor *v, Error **errp);
  60. /* Must be set */
  61. void (*end_list)(Visitor *v, void **list);
  62. /* Must be set by input and clone visitors to visit alternates */
  63. bool (*start_alternate)(Visitor *v, const char *name,
  64. GenericAlternate **obj, size_t size,
  65. Error **errp);
  66. /* Optional */
  67. void (*end_alternate)(Visitor *v, void **obj);
  68. /* Must be set */
  69. bool (*type_int64)(Visitor *v, const char *name, int64_t *obj,
  70. Error **errp);
  71. /* Must be set */
  72. bool (*type_uint64)(Visitor *v, const char *name, uint64_t *obj,
  73. Error **errp);
  74. /* Optional; fallback is type_uint64() */
  75. bool (*type_size)(Visitor *v, const char *name, uint64_t *obj,
  76. Error **errp);
  77. /* Must be set */
  78. bool (*type_bool)(Visitor *v, const char *name, bool *obj, Error **errp);
  79. /* Must be set */
  80. bool (*type_str)(Visitor *v, const char *name, char **obj, Error **errp);
  81. /* Must be set to visit numbers */
  82. bool (*type_number)(Visitor *v, const char *name, double *obj,
  83. Error **errp);
  84. /* Must be set to visit arbitrary QTypes */
  85. bool (*type_any)(Visitor *v, const char *name, QObject **obj,
  86. Error **errp);
  87. /* Must be set to visit explicit null values. */
  88. bool (*type_null)(Visitor *v, const char *name, QNull **obj,
  89. Error **errp);
  90. /* Must be set for input visitors to visit structs, optional otherwise.
  91. The core takes care of the return type in the public interface. */
  92. void (*optional)(Visitor *v, const char *name, bool *present);
  93. /* Optional */
  94. bool (*policy_reject)(Visitor *v, const char *name,
  95. uint64_t features, Error **errp);
  96. /* Optional */
  97. bool (*policy_skip)(Visitor *v, const char *name,
  98. uint64_t features);
  99. /* Must be set */
  100. VisitorType type;
  101. /* Optional */
  102. struct CompatPolicy compat_policy;
  103. /* Must be set for output visitors, optional otherwise. */
  104. void (*complete)(Visitor *v, void *opaque);
  105. /* Must be set */
  106. void (*free)(Visitor *v);
  107. };
  108. #endif