proto_api.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // This file can be included by other C++ libraries, typically extension modules
  31. // which want to interact with the Python Messages coming from the "cpp"
  32. // implementation of protocol buffers.
  33. //
  34. // Usage:
  35. // Declare a (probably static) variable to hold the API:
  36. // const PyProto_API* py_proto_api;
  37. // In some initialization function, write:
  38. // py_proto_api = static_cast<const PyProto_API*>(PyCapsule_Import(
  39. // PyProtoAPICapsuleName(), 0));
  40. // if (!py_proto_api) { ...handle ImportError... }
  41. // Then use the methods of the returned class:
  42. // py_proto_api->GetMessagePointer(...);
  43. #ifndef GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
  44. #define GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
  45. #include <Python.h>
  46. #include <google/protobuf/descriptor_database.h>
  47. #include <google/protobuf/message.h>
  48. namespace google {
  49. namespace protobuf {
  50. namespace python {
  51. // Note on the implementation:
  52. // This API is designed after
  53. // https://docs.python.org/3/extending/extending.html#providing-a-c-api-for-an-extension-module
  54. // The class below contains no mutable state, and all methods are "const";
  55. // we use a C++ class instead of a C struct with functions pointers just because
  56. // the code looks more readable.
  57. struct PyProto_API {
  58. // The API object is created at initialization time and never freed.
  59. // This destructor is never called.
  60. virtual ~PyProto_API() {}
  61. // Operations on Messages.
  62. // If the passed object is a Python Message, returns its internal pointer.
  63. // Otherwise, returns NULL with an exception set.
  64. virtual const Message* GetMessagePointer(PyObject* msg) const = 0;
  65. // If the passed object is a Python Message, returns a mutable pointer.
  66. // Otherwise, returns NULL with an exception set.
  67. // This function will succeed only if there are no other Python objects
  68. // pointing to the message, like submessages or repeated containers.
  69. // With the current implementation, only empty messages are in this case.
  70. virtual Message* GetMutableMessagePointer(PyObject* msg) const = 0;
  71. // Expose the underlying DescriptorPool and MessageFactory to enable C++ code
  72. // to create Python-compatible message.
  73. virtual const DescriptorPool* GetDefaultDescriptorPool() const = 0;
  74. virtual MessageFactory* GetDefaultMessageFactory() const = 0;
  75. };
  76. inline const char* PyProtoAPICapsuleName() {
  77. static const char kCapsuleName[] =
  78. "google.protobuf.pyext._message.proto_API";
  79. return kCapsuleName;
  80. }
  81. } // namespace python
  82. } // namespace protobuf
  83. } // namespace google
  84. #endif // GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__