OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 20 matching lines...) Expand all Loading... |
31 #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ | 31 #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ |
32 #define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ | 32 #define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ |
33 | 33 |
34 #include <Python.h> | 34 #include <Python.h> |
35 | 35 |
36 #include <google/protobuf/stubs/hash.h> | 36 #include <google/protobuf/stubs/hash.h> |
37 #include <google/protobuf/descriptor.h> | 37 #include <google/protobuf/descriptor.h> |
38 | 38 |
39 namespace google { | 39 namespace google { |
40 namespace protobuf { | 40 namespace protobuf { |
| 41 class MessageFactory; |
| 42 |
41 namespace python { | 43 namespace python { |
42 | 44 |
43 struct PyMessageFactory; | |
44 | |
45 // The (meta) type of all Messages classes. | 45 // The (meta) type of all Messages classes. |
46 struct CMessageClass; | 46 struct CMessageClass; |
47 | 47 |
48 // Wraps operations to the global DescriptorPool which contains information | 48 // Wraps operations to the global DescriptorPool which contains information |
49 // about all messages and fields. | 49 // about all messages and fields. |
50 // | 50 // |
51 // There is normally one pool per process. We make it a Python object only | 51 // There is normally one pool per process. We make it a Python object only |
52 // because it contains many Python references. | 52 // because it contains many Python references. |
53 // TODO(amauryfa): See whether such objects can appear in reference cycles, and | 53 // TODO(amauryfa): See whether such objects can appear in reference cycles, and |
54 // consider adding support for the cyclic GC. | 54 // consider adding support for the cyclic GC. |
55 // | 55 // |
56 // "Methods" that interacts with this DescriptorPool are in the cdescriptor_pool | 56 // "Methods" that interacts with this DescriptorPool are in the cdescriptor_pool |
57 // namespace. | 57 // namespace. |
58 typedef struct PyDescriptorPool { | 58 typedef struct PyDescriptorPool { |
59 PyObject_HEAD | 59 PyObject_HEAD |
60 | 60 |
61 // The C++ pool containing Descriptors. | 61 // The C++ pool containing Descriptors. |
62 DescriptorPool* pool; | 62 DescriptorPool* pool; |
63 | 63 |
64 // The C++ pool acting as an underlay. Can be NULL. | 64 // The C++ pool acting as an underlay. Can be NULL. |
65 // This pointer is not owned and must stay alive. | 65 // This pointer is not owned and must stay alive. |
66 const DescriptorPool* underlay; | 66 const DescriptorPool* underlay; |
67 | 67 |
68 // The C++ descriptor database used to fetch unknown protos. Can be NULL. | 68 // The C++ descriptor database used to fetch unknown protos. Can be NULL. |
69 // This pointer is owned. | 69 // This pointer is owned. |
70 const DescriptorDatabase* database; | 70 const DescriptorDatabase* database; |
71 | 71 |
72 // The preferred MessageFactory to be used by descriptors. | 72 // DynamicMessageFactory used to create C++ instances of messages. |
73 // TODO(amauryfa): Don't create the Factory from the DescriptorPool, but | 73 // This object cache the descriptors that were used, so the DescriptorPool |
74 // use the one passed while creating message classes. And remove this member. | 74 // needs to get rid of it before it can delete itself. |
75 PyMessageFactory* py_message_factory; | 75 // |
| 76 // Note: A C++ MessageFactory is different from the Python MessageFactory. |
| 77 // The C++ one creates messages, when the Python one creates classes. |
| 78 MessageFactory* message_factory; |
| 79 |
| 80 // Make our own mapping to retrieve Python classes from C++ descriptors. |
| 81 // |
| 82 // Descriptor pointers stored here are owned by the DescriptorPool above. |
| 83 // Python references to classes are owned by this PyDescriptorPool. |
| 84 typedef hash_map<const Descriptor*, CMessageClass*> ClassesByMessageMap; |
| 85 ClassesByMessageMap* classes_by_descriptor; |
76 | 86 |
77 // Cache the options for any kind of descriptor. | 87 // Cache the options for any kind of descriptor. |
78 // Descriptor pointers are owned by the DescriptorPool above. | 88 // Descriptor pointers are owned by the DescriptorPool above. |
79 // Python objects are owned by the map. | 89 // Python objects are owned by the map. |
80 hash_map<const void*, PyObject*>* descriptor_options; | 90 hash_map<const void*, PyObject*>* descriptor_options; |
81 } PyDescriptorPool; | 91 } PyDescriptorPool; |
82 | 92 |
83 | 93 |
84 extern PyTypeObject PyDescriptorPool_Type; | 94 extern PyTypeObject PyDescriptorPool_Type; |
85 | 95 |
86 namespace cdescriptor_pool { | 96 namespace cdescriptor_pool { |
87 | 97 |
88 // Looks up a message by name. | 98 // Looks up a message by name. |
89 // Returns a message Descriptor, or NULL if not found. | 99 // Returns a message Descriptor, or NULL if not found. |
90 const Descriptor* FindMessageTypeByName(PyDescriptorPool* self, | 100 const Descriptor* FindMessageTypeByName(PyDescriptorPool* self, |
91 const string& name); | 101 const string& name); |
92 | 102 |
| 103 // Registers a new Python class for the given message descriptor. |
| 104 // On error, returns -1 with a Python exception set. |
| 105 int RegisterMessageClass(PyDescriptorPool* self, |
| 106 const Descriptor* message_descriptor, |
| 107 CMessageClass* message_class); |
| 108 |
| 109 // Retrieves the Python class registered with the given message descriptor. |
| 110 // |
| 111 // Returns a *borrowed* reference if found, otherwise returns NULL with an |
| 112 // exception set. |
| 113 CMessageClass* GetMessageClass(PyDescriptorPool* self, |
| 114 const Descriptor* message_descriptor); |
| 115 |
93 // The functions below are also exposed as methods of the DescriptorPool type. | 116 // The functions below are also exposed as methods of the DescriptorPool type. |
94 | 117 |
95 // Looks up a message by name. Returns a PyMessageDescriptor corresponding to | 118 // Looks up a message by name. Returns a PyMessageDescriptor corresponding to |
96 // the field on success, or NULL on failure. | 119 // the field on success, or NULL on failure. |
97 // | 120 // |
98 // Returns a new reference. | 121 // Returns a new reference. |
99 PyObject* FindMessageByName(PyDescriptorPool* self, PyObject* name); | 122 PyObject* FindMessageByName(PyDescriptorPool* self, PyObject* name); |
100 | 123 |
101 // Looks up a field by name. Returns a PyFieldDescriptor corresponding to | 124 // Looks up a field by name. Returns a PyFieldDescriptor corresponding to |
102 // the field on success, or NULL on failure. | 125 // the field on success, or NULL on failure. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 PyDescriptorPool* GetDescriptorPool_FromPool(const DescriptorPool* pool); | 158 PyDescriptorPool* GetDescriptorPool_FromPool(const DescriptorPool* pool); |
136 | 159 |
137 // Initialize objects used by this module. | 160 // Initialize objects used by this module. |
138 bool InitDescriptorPool(); | 161 bool InitDescriptorPool(); |
139 | 162 |
140 } // namespace python | 163 } // namespace python |
141 } // namespace protobuf | 164 } // namespace protobuf |
142 | 165 |
143 } // namespace google | 166 } // namespace google |
144 #endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ | 167 #endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ |
OLD | NEW |