message.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 PYPB_MESSAGE_H__
  28. #define PYPB_MESSAGE_H__
  29. #include <stdbool.h>
  30. #include "python/protobuf.h"
  31. #include "upb/reflection.h"
  32. // Removes the wrapper object for this field from the unset subobject cache.
  33. void PyUpb_CMessage_CacheDelete(PyObject* _self, const upb_FieldDef* f);
  34. // Sets the field value for `f` to `subobj`, evicting the wrapper object from
  35. // the "unset subobject" cache now that real data exists for it. The caller
  36. // must also update the wrapper associated with `f` to point to `subobj` also.
  37. void PyUpb_CMessage_SetConcreteSubobj(PyObject* _self, const upb_FieldDef* f,
  38. upb_MessageValue subobj);
  39. // Gets a Python wrapper object for message `u_msg` of type `m`, returning a
  40. // cached wrapper if one was previously created. If a new object is created,
  41. // it will reference `arena`, which must own `u_msg`.
  42. PyObject* PyUpb_CMessage_Get(upb_Message* u_msg, const upb_MessageDef* m,
  43. PyObject* arena);
  44. // Verifies that a Python object is a message. Sets a TypeError exception and
  45. // returns false on failure.
  46. bool PyUpb_CMessage_Verify(PyObject* self);
  47. // Gets the upb_Message* for this message object if the message is reified.
  48. // Otherwise returns NULL.
  49. upb_Message* PyUpb_CMessage_GetIfReified(PyObject* _self);
  50. // Returns the `upb_MessageDef` for a given CMessage.
  51. const upb_MessageDef* PyUpb_CMessage_GetMsgdef(PyObject* self);
  52. // Functions that match the corresponding methods on the message object.
  53. PyObject* PyUpb_CMessage_MergeFrom(PyObject* self, PyObject* arg);
  54. PyObject* PyUpb_CMessage_MergeFromString(PyObject* self, PyObject* arg);
  55. PyObject* PyUpb_CMessage_SerializeToString(PyObject* self, PyObject* args,
  56. PyObject* kwargs);
  57. // Sets fields of the message according to the attribuges in `kwargs`.
  58. int PyUpb_CMessage_InitAttributes(PyObject* _self, PyObject* args,
  59. PyObject* kwargs);
  60. // Checks that `key` is a field descriptor for an extension type, and that the
  61. // extendee is this message. Otherwise returns NULL and sets a KeyError.
  62. const upb_FieldDef* PyUpb_CMessage_GetExtensionDef(PyObject* _self,
  63. PyObject* key);
  64. // Clears the given field in this message.
  65. void PyUpb_CMessage_DoClearField(PyObject* _self, const upb_FieldDef* f);
  66. // Clears the ExtensionDict from the message. The message must have an
  67. // ExtensionDict set.
  68. void PyUpb_CMessage_ClearExtensionDict(PyObject* _self);
  69. // Implements the equivalent of getattr(msg, field), once `field` has
  70. // already been resolved to a `upb_FieldDef*`.
  71. PyObject* PyUpb_CMessage_GetFieldValue(PyObject* _self,
  72. const upb_FieldDef* field);
  73. // Implements the equivalent of setattr(msg, field, value), once `field` has
  74. // already been resolved to a `upb_FieldDef*`.
  75. int PyUpb_CMessage_SetFieldValue(PyObject* _self, const upb_FieldDef* field,
  76. PyObject* value, PyObject* exc);
  77. // Returns the version associated with this message. The version will be
  78. // incremented when the message changes.
  79. int PyUpb_CMessage_GetVersion(PyObject* _self);
  80. // Module-level init.
  81. bool PyUpb_InitMessage(PyObject* m);
  82. #endif // PYPB_MESSAGE_H__