descriptor_containers.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2009-2021, Google LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Google LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef PYUPB_DESCRIPTOR_CONTAINERS_H__
  28. #define PYUPB_DESCRIPTOR_CONTAINERS_H__
  29. // This file defines immutable Python containiner types whose data comes from
  30. // an underlying descriptor (def).
  31. //
  32. // Because there are many instances of these types that vend different kinds of
  33. // data (fields, oneofs, enums, etc) these types accept a "vtable" of function
  34. // pointers. This saves us from having to define numerous distinct Python types
  35. // for each kind of data we want to vend.
  36. //
  37. // The underlying upb APIs follow a consistent pattern that allows us to use
  38. // those functions directly inside these vtables, greatly reducing the amount of
  39. // "adaptor" code we need to write.
  40. #include <stdbool.h>
  41. #include "protobuf.h"
  42. #include "upb/def.h"
  43. // -----------------------------------------------------------------------------
  44. // PyUpb_GenericSequence
  45. // -----------------------------------------------------------------------------
  46. // A Python object that vends a sequence of descriptors.
  47. typedef struct {
  48. // Returns the number of elements in the map.
  49. int (*get_elem_count)(const void* parent);
  50. // Returns an element by index.
  51. const void* (*index)(const void* parent, int idx);
  52. // Returns a Python object wrapping this element, caller owns a ref.
  53. PyObject* (*get_elem_wrapper)(const void* elem);
  54. } PyUpb_GenericSequence_Funcs;
  55. // Returns a new GenericSequence. The vtable `funcs` must outlive this object
  56. // (generally it should be static). The GenericSequence will take a ref on
  57. // `parent_obj`, which must be sufficient to keep `parent` alive. The object
  58. // `parent` will be passed as an argument to the functions in `funcs`.
  59. PyObject* PyUpb_GenericSequence_New(const PyUpb_GenericSequence_Funcs* funcs,
  60. const void* parent, PyObject* parent_obj);
  61. // -----------------------------------------------------------------------------
  62. // PyUpb_ByNameMap
  63. // -----------------------------------------------------------------------------
  64. // A Python object that vends a name->descriptor map.
  65. typedef struct {
  66. PyUpb_GenericSequence_Funcs base;
  67. // Looks up by name and returns either a pointer to the element or NULL.
  68. const void* (*lookup)(const void* parent, const char* key);
  69. // Returns the name associated with this element.
  70. const char* (*get_elem_name)(const void* elem);
  71. } PyUpb_ByNameMap_Funcs;
  72. // Returns a new ByNameMap. The vtable `funcs` must outlive this object
  73. // (generally it should be static). The ByNameMap will take a ref on
  74. // `parent_obj`, which must be sufficient to keep `parent` alive. The object
  75. // `parent` will be passed as an argument to the functions in `funcs`.
  76. PyObject* PyUpb_ByNameMap_New(const PyUpb_ByNameMap_Funcs* funcs,
  77. const void* parent, PyObject* parent_obj);
  78. // -----------------------------------------------------------------------------
  79. // PyUpb_ByNumberMap
  80. // -----------------------------------------------------------------------------
  81. // A Python object that vends a number->descriptor map.
  82. typedef struct {
  83. PyUpb_GenericSequence_Funcs base;
  84. // Looks up by name and returns either a pointer to the element or NULL.
  85. const void* (*lookup)(const void* parent, int num);
  86. // Returns the name associated with this element.
  87. int (*get_elem_num)(const void* elem);
  88. } PyUpb_ByNumberMap_Funcs;
  89. // Returns a new ByNumberMap. The vtable `funcs` must outlive this object
  90. // (generally it should be static). The ByNumberMap will take a ref on
  91. // `parent_obj`, which must be sufficient to keep `parent` alive. The object
  92. // `parent` will be passed as an argument to the functions in `funcs`.
  93. PyObject* PyUpb_ByNumberMap_New(const PyUpb_ByNumberMap_Funcs* funcs,
  94. const void* parent, PyObject* parent_obj);
  95. bool PyUpb_InitDescriptorContainers(PyObject* m);
  96. #endif // PYUPB_DESCRIPTOR_CONTAINERS_H__