proto_api.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/message.h>
  47. namespace google {
  48. namespace protobuf {
  49. namespace python {
  50. // Note on the implementation:
  51. // This API is designed after
  52. // https://docs.python.org/3/extending/extending.html#providing-a-c-api-for-an-extension-module
  53. // The class below contains no mutable state, and all methods are "const";
  54. // we use a C++ class instead of a C struct with functions pointers just because
  55. // the code looks more readable.
  56. struct PyProto_API {
  57. // The API object is created at initialization time and never freed.
  58. // This destructor is never called.
  59. virtual ~PyProto_API() {}
  60. // Operations on Messages.
  61. // If the passed object is a Python Message, returns its internal pointer.
  62. // Otherwise, returns NULL with an exception set.
  63. virtual const Message* GetMessagePointer(PyObject* msg) const = 0;
  64. // If the passed object is a Python Message, returns a mutable pointer.
  65. // Otherwise, returns NULL with an exception set.
  66. // This function will succeed only if there are no other Python objects
  67. // pointing to the message, like submessages or repeated containers.
  68. // With the current implementation, only empty messages are in this case.
  69. virtual Message* GetMutableMessagePointer(PyObject* msg) const = 0;
  70. };
  71. inline const char* PyProtoAPICapsuleName() {
  72. static const char kCapsuleName[] =
  73. "google.protobuf.pyext._message.proto_API";
  74. return kCapsuleName;
  75. }
  76. } // namespace python
  77. } // namespace protobuf
  78. } // namespace google
  79. #endif // GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__