proto_api.h
Go to the documentation of this file.
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 
31 // This file can be included by other C++ libraries, typically extension modules
32 // which want to interact with the Python Messages coming from the "cpp"
33 // implementation of protocol buffers.
34 //
35 // Usage:
36 // Declare a (probably static) variable to hold the API:
37 // const PyProto_API* py_proto_api;
38 // In some initialization function, write:
39 // py_proto_api = static_cast<const PyProto_API*>(PyCapsule_Import(
40 // PyProtoAPICapsuleName(), 0));
41 // if (!py_proto_api) { ...handle ImportError... }
42 // Then use the methods of the returned class:
43 // py_proto_api->GetMessagePointer(...);
44 
45 #ifndef GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
46 #define GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
47 
48 #include <Python.h>
49 
52 
53 namespace google {
54 namespace protobuf {
55 namespace python {
56 
57 // Note on the implementation:
58 // This API is designed after
59 // https://docs.python.org/3/extending/extending.html#providing-a-c-api-for-an-extension-module
60 // The class below contains no mutable state, and all methods are "const";
61 // we use a C++ class instead of a C struct with functions pointers just because
62 // the code looks more readable.
63 struct PyProto_API {
64  // The API object is created at initialization time and never freed.
65  // This destructor is never called.
66  virtual ~PyProto_API() {}
67 
68  // Operations on Messages.
69 
70  // If the passed object is a Python Message, returns its internal pointer.
71  // Otherwise, returns NULL with an exception set.
72  virtual const Message* GetMessagePointer(PyObject* msg) const = 0;
73 
74  // If the passed object is a Python Message, returns a mutable pointer.
75  // Otherwise, returns NULL with an exception set.
76  // This function will succeed only if there are no other Python objects
77  // pointing to the message, like submessages or repeated containers.
78  // With the current implementation, only empty messages are in this case.
79  virtual Message* GetMutableMessagePointer(PyObject* msg) const = 0;
80 
81  // Expose the underlying DescriptorPool and MessageFactory to enable C++ code
82  // to create Python-compatible message.
83  virtual const DescriptorPool* GetDefaultDescriptorPool() const = 0;
84  virtual MessageFactory* GetDefaultMessageFactory() const = 0;
85 };
86 
87 inline const char* PyProtoAPICapsuleName() {
88  static const char kCapsuleName[] =
89  "google.protobuf.pyext._message.proto_API";
90  return kCapsuleName;
91 }
92 
93 } // namespace python
94 } // namespace protobuf
95 } // namespace google
96 
97 #endif // GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
google::protobuf::python::PyProto_API::GetMessagePointer
virtual const Message * GetMessagePointer(PyObject *msg) const =0
descriptor_database.h
google::protobuf::MessageFactory
Definition: src/google/protobuf/message.h:1069
google::protobuf::python::PyProtoAPICapsuleName
const char * PyProtoAPICapsuleName()
Definition: proto_api.h:87
google::protobuf::python::PyProto_API
Definition: proto_api.h:63
google::protobuf::python::PyProto_API::~PyProto_API
virtual ~PyProto_API()
Definition: proto_api.h:66
google::protobuf::DescriptorPool
Definition: src/google/protobuf/descriptor.h:1539
message.h
google::protobuf::python::PyProto_API::GetMutableMessagePointer
virtual Message * GetMutableMessagePointer(PyObject *msg) const =0
google::protobuf::python::PyProto_API::GetDefaultDescriptorPool
virtual const DescriptorPool * GetDefaultDescriptorPool() const =0
google::protobuf::Message
Definition: src/google/protobuf/message.h:205
google::protobuf::python::PyProto_API::GetDefaultMessageFactory
virtual MessageFactory * GetDefaultMessageFactory() const =0
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57