1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ 9 #define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ 10 11 #define PY_SSIZE_T_CLEAN 12 #include <Python.h> 13 14 #include "absl/container/flat_hash_map.h" 15 #include "google/protobuf/descriptor.h" 16 17 namespace google { 18 namespace protobuf { 19 namespace python { 20 21 struct PyMessageFactory; 22 23 // The (meta) type of all Messages classes. 24 struct CMessageClass; 25 26 // Wraps operations to the global DescriptorPool which contains information 27 // about all messages and fields. 28 // 29 // There is normally one pool per process. We make it a Python object only 30 // because it contains many Python references. 31 // 32 // "Methods" that interacts with this DescriptorPool are in the cdescriptor_pool 33 // namespace. 34 typedef struct PyDescriptorPool { 35 PyObject_HEAD; 36 37 // The C++ pool containing Descriptors. 38 const DescriptorPool* pool; 39 40 // True if we should free the pointer above. 41 bool is_owned; 42 43 // True if this pool accepts new proto definitions. 44 // In this case it is allowed to const_cast<DescriptorPool*>(pool). 45 bool is_mutable; 46 47 48 // The error collector to store error info. Can be NULL. This pointer is 49 // owned. 50 DescriptorPool::ErrorCollector* error_collector; 51 52 // The C++ pool acting as an underlay. Can be NULL. 53 // This pointer is not owned and must stay alive. 54 const DescriptorPool* underlay; 55 56 // The C++ descriptor database used to fetch unknown protos. Can be NULL. 57 // This pointer is owned. 58 const DescriptorDatabase* database; 59 60 // The preferred MessageFactory to be used by descriptors. 61 // TODO: Don't create the Factory from the DescriptorPool, but 62 // use the one passed while creating message classes. And remove this member. 63 PyMessageFactory* py_message_factory; 64 65 // Cache the options for any kind of descriptor. 66 // Descriptor pointers are owned by the DescriptorPool above. 67 // Python objects are owned by the map. 68 absl::flat_hash_map<const void*, PyObject*>* descriptor_options; 69 // Similar cache for features. 70 absl::flat_hash_map<const void*, PyObject*>* descriptor_features; 71 } PyDescriptorPool; 72 73 74 extern PyTypeObject PyDescriptorPool_Type; 75 76 namespace cdescriptor_pool { 77 78 79 // The functions below are also exposed as methods of the DescriptorPool type. 80 81 // Looks up a field by name. Returns a PyFieldDescriptor corresponding to 82 // the field on success, or NULL on failure. 83 // 84 // Returns a new reference. 85 PyObject* FindFieldByName(PyDescriptorPool* self, PyObject* name); 86 87 // Looks up an extension by name. Returns a PyFieldDescriptor corresponding 88 // to the field on success, or NULL on failure. 89 // 90 // Returns a new reference. 91 PyObject* FindExtensionByName(PyDescriptorPool* self, PyObject* arg); 92 93 // Looks up an enum type by name. Returns a PyEnumDescriptor corresponding 94 // to the field on success, or NULL on failure. 95 // 96 // Returns a new reference. 97 PyObject* FindEnumTypeByName(PyDescriptorPool* self, PyObject* arg); 98 99 // Looks up a oneof by name. Returns a COneofDescriptor corresponding 100 // to the oneof on success, or NULL on failure. 101 // 102 // Returns a new reference. 103 PyObject* FindOneofByName(PyDescriptorPool* self, PyObject* arg); 104 105 } // namespace cdescriptor_pool 106 107 // Retrieves the global descriptor pool owned by the _message module. 108 // This is the one used by pb2.py generated modules. 109 // Returns a *borrowed* reference. 110 // "Default" pool used to register messages from _pb2.py modules. 111 PyDescriptorPool* GetDefaultDescriptorPool(); 112 113 // Retrieves an existing python descriptor pool owning the C++ descriptor pool. 114 // Returns a *borrowed* reference. 115 PyDescriptorPool* GetDescriptorPool_FromPool(const DescriptorPool* pool); 116 117 // Wraps a C++ descriptor pool in a Python object, creates it if necessary. 118 // Returns a new reference. 119 PyObject* PyDescriptorPool_FromPool(const DescriptorPool* pool); 120 121 // Initialize objects used by this module. 122 bool InitDescriptorPool(); 123 124 } // namespace python 125 } // namespace protobuf 126 } // namespace google 127 128 #endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ 129