| 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; | 41 namespace python { |
| 42 | 42 |
| 43 namespace python { | 43 struct PyMessageFactory; |
| 44 | 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 // DynamicMessageFactory used to create C++ instances of messages. | 72 // The preferred MessageFactory to be used by descriptors. |
| 73 // This object cache the descriptors that were used, so the DescriptorPool | 73 // TODO(amauryfa): Don't create the Factory from the DescriptorPool, but |
| 74 // needs to get rid of it before it can delete itself. | 74 // use the one passed while creating message classes. And remove this member. |
| 75 // | 75 PyMessageFactory* py_message_factory; |
| 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; | |
| 86 | 76 |
| 87 // Cache the options for any kind of descriptor. | 77 // Cache the options for any kind of descriptor. |
| 88 // Descriptor pointers are owned by the DescriptorPool above. | 78 // Descriptor pointers are owned by the DescriptorPool above. |
| 89 // Python objects are owned by the map. | 79 // Python objects are owned by the map. |
| 90 hash_map<const void*, PyObject*>* descriptor_options; | 80 hash_map<const void*, PyObject*>* descriptor_options; |
| 91 } PyDescriptorPool; | 81 } PyDescriptorPool; |
| 92 | 82 |
| 93 | 83 |
| 94 extern PyTypeObject PyDescriptorPool_Type; | 84 extern PyTypeObject PyDescriptorPool_Type; |
| 95 | 85 |
| 96 namespace cdescriptor_pool { | 86 namespace cdescriptor_pool { |
| 97 | 87 |
| 98 // Looks up a message by name. | 88 // Looks up a message by name. |
| 99 // Returns a message Descriptor, or NULL if not found. | 89 // Returns a message Descriptor, or NULL if not found. |
| 100 const Descriptor* FindMessageTypeByName(PyDescriptorPool* self, | 90 const Descriptor* FindMessageTypeByName(PyDescriptorPool* self, |
| 101 const string& name); | 91 const string& name); |
| 102 | 92 |
| 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 | |
| 116 // The functions below are also exposed as methods of the DescriptorPool type. | 93 // The functions below are also exposed as methods of the DescriptorPool type. |
| 117 | 94 |
| 118 // Looks up a message by name. Returns a PyMessageDescriptor corresponding to | 95 // Looks up a message by name. Returns a PyMessageDescriptor corresponding to |
| 119 // the field on success, or NULL on failure. | 96 // the field on success, or NULL on failure. |
| 120 // | 97 // |
| 121 // Returns a new reference. | 98 // Returns a new reference. |
| 122 PyObject* FindMessageByName(PyDescriptorPool* self, PyObject* name); | 99 PyObject* FindMessageByName(PyDescriptorPool* self, PyObject* name); |
| 123 | 100 |
| 124 // Looks up a field by name. Returns a PyFieldDescriptor corresponding to | 101 // Looks up a field by name. Returns a PyFieldDescriptor corresponding to |
| 125 // the field on success, or NULL on failure. | 102 // the field on success, or NULL on failure. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 PyDescriptorPool* GetDescriptorPool_FromPool(const DescriptorPool* pool); | 135 PyDescriptorPool* GetDescriptorPool_FromPool(const DescriptorPool* pool); |
| 159 | 136 |
| 160 // Initialize objects used by this module. | 137 // Initialize objects used by this module. |
| 161 bool InitDescriptorPool(); | 138 bool InitDescriptorPool(); |
| 162 | 139 |
| 163 } // namespace python | 140 } // namespace python |
| 164 } // namespace protobuf | 141 } // namespace protobuf |
| 165 | 142 |
| 166 } // namespace google | 143 } // namespace google |
| 167 #endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ | 144 #endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__ |
| OLD | NEW |