1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef PYUPB_DESCRIPTOR_H__
29 #define PYUPB_DESCRIPTOR_H__
30 
31 #include <stdbool.h>
32 
33 #include "python/python_api.h"
34 #include "upb/reflection/def.h"
35 
36 typedef enum {
37   kPyUpb_Descriptor = 0,
38   kPyUpb_EnumDescriptor = 1,
39   kPyUpb_EnumValueDescriptor = 2,
40   kPyUpb_FieldDescriptor = 3,
41   kPyUpb_FileDescriptor = 4,
42   kPyUpb_MethodDescriptor = 5,
43   kPyUpb_OneofDescriptor = 6,
44   kPyUpb_ServiceDescriptor = 7,
45   kPyUpb_Descriptor_Count = 8,
46 } PyUpb_DescriptorType;
47 
48 // Given a descriptor object |desc|, returns a Python message class object for
49 // the msgdef |m|, which must be from the same pool.
50 PyObject* PyUpb_Descriptor_GetClass(const upb_MessageDef* m);
51 
52 // Returns a Python wrapper object for the given def. This will return an
53 // existing object if one already exists, otherwise a new object will be
54 // created.  The caller always owns a ref on the returned object.
55 PyObject* PyUpb_Descriptor_Get(const upb_MessageDef* msgdef);
56 PyObject* PyUpb_EnumDescriptor_Get(const upb_EnumDef* enumdef);
57 PyObject* PyUpb_FieldDescriptor_Get(const upb_FieldDef* field);
58 PyObject* PyUpb_FileDescriptor_Get(const upb_FileDef* file);
59 PyObject* PyUpb_OneofDescriptor_Get(const upb_OneofDef* oneof);
60 PyObject* PyUpb_EnumValueDescriptor_Get(const upb_EnumValueDef* enumval);
61 PyObject* PyUpb_Descriptor_GetOrCreateWrapper(const upb_MessageDef* msg);
62 PyObject* PyUpb_ServiceDescriptor_Get(const upb_ServiceDef* s);
63 PyObject* PyUpb_MethodDescriptor_Get(const upb_MethodDef* s);
64 
65 // Returns the underlying |def| for a given wrapper object. The caller must
66 // have already verified that the given Python object is of the expected type.
67 const upb_FileDef* PyUpb_FileDescriptor_GetDef(PyObject* file);
68 const upb_FieldDef* PyUpb_FieldDescriptor_GetDef(PyObject* file);
69 const upb_MessageDef* PyUpb_Descriptor_GetDef(PyObject* _self);
70 const void* PyUpb_AnyDescriptor_GetDef(PyObject* _self);
71 
72 // Returns the underlying |def| for a given wrapper object. The caller must
73 // have already verified that the given Python object is of the expected type.
74 const upb_FileDef* PyUpb_FileDescriptor_GetDef(PyObject* file);
75 
76 // Module-level init.
77 bool PyUpb_InitDescriptor(PyObject* m);
78 
79 #endif  // PYUPB_DESCRIPTOR_H__
80