OLD | NEW |
| (Empty) |
1 // Protocol Buffers - Google's data interchange format | |
2 // Copyright 2008 Google Inc. All rights reserved. | |
3 // https://developers.google.com/protocol-buffers/ | |
4 // | |
5 // Redistribution and use in source and binary forms, with or without | |
6 // modification, are permitted provided that the following conditions are | |
7 // met: | |
8 // | |
9 // * Redistributions of source code must retain the above copyright | |
10 // notice, this list of conditions and the following disclaimer. | |
11 // * Redistributions in binary form must reproduce the above | |
12 // copyright notice, this list of conditions and the following disclaimer | |
13 // in the documentation and/or other materials provided with the | |
14 // distribution. | |
15 // * Neither the name of Google Inc. nor the names of its | |
16 // contributors may be used to endorse or promote products derived from | |
17 // this software without specific prior written permission. | |
18 // | |
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 | |
31 #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_FACTORY_H__ | |
32 #define GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_FACTORY_H__ | |
33 | |
34 #include <Python.h> | |
35 | |
36 #include <google/protobuf/stubs/hash.h> | |
37 #include <google/protobuf/descriptor.h> | |
38 #include <google/protobuf/pyext/descriptor_pool.h> | |
39 | |
40 namespace google { | |
41 namespace protobuf { | |
42 class MessageFactory; | |
43 | |
44 namespace python { | |
45 | |
46 // The (meta) type of all Messages classes. | |
47 struct CMessageClass; | |
48 | |
49 struct PyMessageFactory { | |
50 PyObject_HEAD | |
51 | |
52 // DynamicMessageFactory used to create C++ instances of messages. | |
53 // This object cache the descriptors that were used, so the DescriptorPool | |
54 // needs to get rid of it before it can delete itself. | |
55 // | |
56 // Note: A C++ MessageFactory is different from the PyMessageFactory. | |
57 // The C++ one creates messages, when the Python one creates classes. | |
58 MessageFactory* message_factory; | |
59 | |
60 // borrowed reference to a Python DescriptorPool. | |
61 // TODO(amauryfa): invert the dependency: the MessageFactory owns the | |
62 // DescriptorPool, not the opposite. | |
63 PyDescriptorPool* pool; | |
64 | |
65 // Make our own mapping to retrieve Python classes from C++ descriptors. | |
66 // | |
67 // Descriptor pointers stored here are owned by the DescriptorPool above. | |
68 // Python references to classes are owned by this PyDescriptorPool. | |
69 typedef hash_map<const Descriptor*, CMessageClass*> ClassesByMessageMap; | |
70 ClassesByMessageMap* classes_by_descriptor; | |
71 }; | |
72 | |
73 extern PyTypeObject PyMessageFactory_Type; | |
74 | |
75 namespace message_factory { | |
76 | |
77 // Creates a new MessageFactory instance. | |
78 PyMessageFactory* NewMessageFactory(PyTypeObject* type, PyDescriptorPool* pool); | |
79 | |
80 // Registers a new Python class for the given message descriptor. | |
81 // On error, returns -1 with a Python exception set. | |
82 int RegisterMessageClass(PyMessageFactory* self, | |
83 const Descriptor* message_descriptor, | |
84 CMessageClass* message_class); | |
85 // Retrieves the Python class registered with the given message descriptor, or | |
86 // fail with a TypeError. Returns a *borrowed* reference. | |
87 CMessageClass* GetMessageClass(PyMessageFactory* self, | |
88 const Descriptor* message_descriptor); | |
89 // Retrieves the Python class registered with the given message descriptor. | |
90 // The class is created if not done yet. Returns a *new* reference. | |
91 CMessageClass* GetOrCreateMessageClass(PyMessageFactory* self, | |
92 const Descriptor* message_descriptor); | |
93 } // namespace message_factory | |
94 | |
95 // Initialize objects used by this module. | |
96 // On error, returns false with a Python exception set. | |
97 bool InitMessageFactory(); | |
98 | |
99 } // namespace python | |
100 } // namespace protobuf | |
101 | |
102 } // namespace google | |
103 #endif // GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_FACTORY_H__ | |
OLD | NEW |