1ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format 2ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc. All rights reserved. 3ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/ 4ffe3c632Sopenharmony_ci// 5ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 6ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are 7ffe3c632Sopenharmony_ci// met: 8ffe3c632Sopenharmony_ci// 9ffe3c632Sopenharmony_ci// * Redistributions of source code must retain the above copyright 10ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer. 11ffe3c632Sopenharmony_ci// * Redistributions in binary form must reproduce the above 12ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 13ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the 14ffe3c632Sopenharmony_ci// distribution. 15ffe3c632Sopenharmony_ci// * Neither the name of Google Inc. nor the names of its 16ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from 17ffe3c632Sopenharmony_ci// this software without specific prior written permission. 18ffe3c632Sopenharmony_ci// 19ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30ffe3c632Sopenharmony_ci 31ffe3c632Sopenharmony_ci#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ 32ffe3c632Sopenharmony_ci#define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ 33ffe3c632Sopenharmony_ci 34ffe3c632Sopenharmony_ci#include <Python.h> 35ffe3c632Sopenharmony_ci 36ffe3c632Sopenharmony_ci#include <unordered_map> 37ffe3c632Sopenharmony_ci#include <google/protobuf/descriptor.h> 38ffe3c632Sopenharmony_ci 39ffe3c632Sopenharmony_cinamespace google { 40ffe3c632Sopenharmony_cinamespace protobuf { 41ffe3c632Sopenharmony_cinamespace python { 42ffe3c632Sopenharmony_ci 43ffe3c632Sopenharmony_cistruct PyMessageFactory; 44ffe3c632Sopenharmony_ci 45ffe3c632Sopenharmony_ci// The (meta) type of all Messages classes. 46ffe3c632Sopenharmony_cistruct CMessageClass; 47ffe3c632Sopenharmony_ci 48ffe3c632Sopenharmony_ci// Wraps operations to the global DescriptorPool which contains information 49ffe3c632Sopenharmony_ci// about all messages and fields. 50ffe3c632Sopenharmony_ci// 51ffe3c632Sopenharmony_ci// There is normally one pool per process. We make it a Python object only 52ffe3c632Sopenharmony_ci// because it contains many Python references. 53ffe3c632Sopenharmony_ci// 54ffe3c632Sopenharmony_ci// "Methods" that interacts with this DescriptorPool are in the cdescriptor_pool 55ffe3c632Sopenharmony_ci// namespace. 56ffe3c632Sopenharmony_citypedef struct PyDescriptorPool { 57ffe3c632Sopenharmony_ci PyObject_HEAD 58ffe3c632Sopenharmony_ci 59ffe3c632Sopenharmony_ci // The C++ pool containing Descriptors. 60ffe3c632Sopenharmony_ci DescriptorPool* pool; 61ffe3c632Sopenharmony_ci 62ffe3c632Sopenharmony_ci // The error collector to store error info. Can be NULL. This pointer is 63ffe3c632Sopenharmony_ci // owned. 64ffe3c632Sopenharmony_ci DescriptorPool::ErrorCollector* error_collector; 65ffe3c632Sopenharmony_ci 66ffe3c632Sopenharmony_ci // The C++ pool acting as an underlay. Can be NULL. 67ffe3c632Sopenharmony_ci // This pointer is not owned and must stay alive. 68ffe3c632Sopenharmony_ci const DescriptorPool* underlay; 69ffe3c632Sopenharmony_ci 70ffe3c632Sopenharmony_ci // The C++ descriptor database used to fetch unknown protos. Can be NULL. 71ffe3c632Sopenharmony_ci // This pointer is owned. 72ffe3c632Sopenharmony_ci const DescriptorDatabase* database; 73ffe3c632Sopenharmony_ci 74ffe3c632Sopenharmony_ci // The preferred MessageFactory to be used by descriptors. 75ffe3c632Sopenharmony_ci // TODO(amauryfa): Don't create the Factory from the DescriptorPool, but 76ffe3c632Sopenharmony_ci // use the one passed while creating message classes. And remove this member. 77ffe3c632Sopenharmony_ci PyMessageFactory* py_message_factory; 78ffe3c632Sopenharmony_ci 79ffe3c632Sopenharmony_ci // Cache the options for any kind of descriptor. 80ffe3c632Sopenharmony_ci // Descriptor pointers are owned by the DescriptorPool above. 81ffe3c632Sopenharmony_ci // Python objects are owned by the map. 82ffe3c632Sopenharmony_ci std::unordered_map<const void*, PyObject*>* descriptor_options; 83ffe3c632Sopenharmony_ci} PyDescriptorPool; 84ffe3c632Sopenharmony_ci 85ffe3c632Sopenharmony_ci 86ffe3c632Sopenharmony_ciextern PyTypeObject PyDescriptorPool_Type; 87ffe3c632Sopenharmony_ci 88ffe3c632Sopenharmony_cinamespace cdescriptor_pool { 89ffe3c632Sopenharmony_ci 90ffe3c632Sopenharmony_ci 91ffe3c632Sopenharmony_ci// The functions below are also exposed as methods of the DescriptorPool type. 92ffe3c632Sopenharmony_ci 93ffe3c632Sopenharmony_ci// Looks up a field by name. Returns a PyFieldDescriptor corresponding to 94ffe3c632Sopenharmony_ci// the field on success, or NULL on failure. 95ffe3c632Sopenharmony_ci// 96ffe3c632Sopenharmony_ci// Returns a new reference. 97ffe3c632Sopenharmony_ciPyObject* FindFieldByName(PyDescriptorPool* self, PyObject* name); 98ffe3c632Sopenharmony_ci 99ffe3c632Sopenharmony_ci// Looks up an extension by name. Returns a PyFieldDescriptor corresponding 100ffe3c632Sopenharmony_ci// to the field on success, or NULL on failure. 101ffe3c632Sopenharmony_ci// 102ffe3c632Sopenharmony_ci// Returns a new reference. 103ffe3c632Sopenharmony_ciPyObject* FindExtensionByName(PyDescriptorPool* self, PyObject* arg); 104ffe3c632Sopenharmony_ci 105ffe3c632Sopenharmony_ci// Looks up an enum type by name. Returns a PyEnumDescriptor corresponding 106ffe3c632Sopenharmony_ci// to the field on success, or NULL on failure. 107ffe3c632Sopenharmony_ci// 108ffe3c632Sopenharmony_ci// Returns a new reference. 109ffe3c632Sopenharmony_ciPyObject* FindEnumTypeByName(PyDescriptorPool* self, PyObject* arg); 110ffe3c632Sopenharmony_ci 111ffe3c632Sopenharmony_ci// Looks up a oneof by name. Returns a COneofDescriptor corresponding 112ffe3c632Sopenharmony_ci// to the oneof on success, or NULL on failure. 113ffe3c632Sopenharmony_ci// 114ffe3c632Sopenharmony_ci// Returns a new reference. 115ffe3c632Sopenharmony_ciPyObject* FindOneofByName(PyDescriptorPool* self, PyObject* arg); 116ffe3c632Sopenharmony_ci 117ffe3c632Sopenharmony_ci} // namespace cdescriptor_pool 118ffe3c632Sopenharmony_ci 119ffe3c632Sopenharmony_ci// Retrieve the global descriptor pool owned by the _message module. 120ffe3c632Sopenharmony_ci// This is the one used by pb2.py generated modules. 121ffe3c632Sopenharmony_ci// Returns a *borrowed* reference. 122ffe3c632Sopenharmony_ci// "Default" pool used to register messages from _pb2.py modules. 123ffe3c632Sopenharmony_ciPyDescriptorPool* GetDefaultDescriptorPool(); 124ffe3c632Sopenharmony_ci 125ffe3c632Sopenharmony_ci// Retrieve the python descriptor pool owning a C++ descriptor pool. 126ffe3c632Sopenharmony_ci// Returns a *borrowed* reference. 127ffe3c632Sopenharmony_ciPyDescriptorPool* GetDescriptorPool_FromPool(const DescriptorPool* pool); 128ffe3c632Sopenharmony_ci 129ffe3c632Sopenharmony_ci// Initialize objects used by this module. 130ffe3c632Sopenharmony_cibool InitDescriptorPool(); 131ffe3c632Sopenharmony_ci 132ffe3c632Sopenharmony_ci} // namespace python 133ffe3c632Sopenharmony_ci} // namespace protobuf 134ffe3c632Sopenharmony_ci} // namespace google 135ffe3c632Sopenharmony_ci 136ffe3c632Sopenharmony_ci#endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ 137