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 // Author: anuraag@google.com (Anuraag Agrawal) | |
32 // Author: tibell@google.com (Johan Tibell) | |
33 | |
34 #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_COMPOSITE_CONTAINER_H__ | |
35 #define GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_COMPOSITE_CONTAINER_H__ | |
36 | |
37 #include <Python.h> | |
38 | |
39 #include <memory> | |
40 #ifndef _SHARED_PTR_H | |
41 #include <google/protobuf/stubs/shared_ptr.h> | |
42 #endif | |
43 #include <string> | |
44 #include <vector> | |
45 | |
46 namespace google { | |
47 namespace protobuf { | |
48 | |
49 class FieldDescriptor; | |
50 class Message; | |
51 | |
52 using internal::shared_ptr; | |
53 | |
54 namespace python { | |
55 | |
56 struct CMessage; | |
57 | |
58 // A RepeatedCompositeContainer can be in one of two states: attached | |
59 // or released. | |
60 // | |
61 // When in the attached state all modifications to the container are | |
62 // done both on the 'message' and on the 'child_messages' | |
63 // list. In this state all Messages referred to by the children in | |
64 // 'child_messages' are owner by the 'owner'. | |
65 // | |
66 // When in the released state 'message', 'owner', 'parent', and | |
67 // 'parent_field_descriptor' are NULL. | |
68 typedef struct RepeatedCompositeContainer { | |
69 PyObject_HEAD; | |
70 | |
71 // This is the top-level C++ Message object that owns the whole | |
72 // proto tree. Every Python RepeatedCompositeContainer holds a | |
73 // reference to it in order to keep it alive as long as there's a | |
74 // Python object that references any part of the tree. | |
75 shared_ptr<Message> owner; | |
76 | |
77 // Weak reference to parent object. May be NULL. Used to make sure | |
78 // the parent is writable before modifying the | |
79 // RepeatedCompositeContainer. | |
80 CMessage* parent; | |
81 | |
82 // A descriptor used to modify the underlying 'message'. | |
83 // The pointer is owned by the global DescriptorPool. | |
84 const FieldDescriptor* parent_field_descriptor; | |
85 | |
86 // Pointer to the C++ Message that contains this container. The | |
87 // RepeatedCompositeContainer does not own this pointer. | |
88 // | |
89 // If NULL, this message has been released from its parent (by | |
90 // calling Clear() or ClearField() on the parent. | |
91 Message* message; | |
92 | |
93 // A callable that is used to create new child messages. | |
94 PyObject* subclass_init; | |
95 | |
96 // A list of child messages. | |
97 PyObject* child_messages; | |
98 } RepeatedCompositeContainer; | |
99 | |
100 extern PyTypeObject RepeatedCompositeContainer_Type; | |
101 | |
102 namespace repeated_composite_container { | |
103 | |
104 // Builds a RepeatedCompositeContainer object, from a parent message and a | |
105 // field descriptor. | |
106 PyObject *NewContainer( | |
107 CMessage* parent, | |
108 const FieldDescriptor* parent_field_descriptor, | |
109 PyObject *concrete_class); | |
110 | |
111 // Returns the number of items in this repeated composite container. | |
112 static Py_ssize_t Length(RepeatedCompositeContainer* self); | |
113 | |
114 // Appends a new CMessage to the container and returns it. The | |
115 // CMessage is initialized using the content of kwargs. | |
116 // | |
117 // Returns a new reference if successful; returns NULL and sets an | |
118 // exception if unsuccessful. | |
119 PyObject* Add(RepeatedCompositeContainer* self, | |
120 PyObject* args, | |
121 PyObject* kwargs); | |
122 | |
123 // Appends all the CMessages in the input iterator to the container. | |
124 // | |
125 // Returns None if successful; returns NULL and sets an exception if | |
126 // unsuccessful. | |
127 PyObject* Extend(RepeatedCompositeContainer* self, PyObject* value); | |
128 | |
129 // Appends a new message to the container for each message in the | |
130 // input iterator, merging each data element in. Equivalent to extend. | |
131 // | |
132 // Returns None if successful; returns NULL and sets an exception if | |
133 // unsuccessful. | |
134 PyObject* MergeFrom(RepeatedCompositeContainer* self, PyObject* other); | |
135 | |
136 // Accesses messages in the container. | |
137 // | |
138 // Returns a new reference to the message for an integer parameter. | |
139 // Returns a new reference to a list of messages for a slice. | |
140 PyObject* Subscript(RepeatedCompositeContainer* self, PyObject* slice); | |
141 | |
142 // Deletes items from the container (cannot be used for assignment). | |
143 // | |
144 // Returns 0 on success, -1 on failure. | |
145 int AssignSubscript(RepeatedCompositeContainer* self, | |
146 PyObject* slice, | |
147 PyObject* value); | |
148 | |
149 // Releases the messages in the container to the given message. | |
150 // | |
151 // Returns 0 on success, -1 on failure. | |
152 int ReleaseToMessage(RepeatedCompositeContainer* self, Message* new_message); | |
153 | |
154 // Releases the messages in the container to a new message. | |
155 // | |
156 // Returns 0 on success, -1 on failure. | |
157 int Release(RepeatedCompositeContainer* self); | |
158 | |
159 // Returns 0 on success, -1 on failure. | |
160 int SetOwner(RepeatedCompositeContainer* self, | |
161 const shared_ptr<Message>& new_owner); | |
162 | |
163 // Removes the last element of the repeated message field 'field' on | |
164 // the Message 'parent', and transfers the ownership of the released | |
165 // Message to 'target'. | |
166 // | |
167 // Corresponds to reflection api method ReleaseMessage. | |
168 void ReleaseLastTo(CMessage* parent, | |
169 const FieldDescriptor* field, | |
170 CMessage* target); | |
171 | |
172 } // namespace repeated_composite_container | |
173 } // namespace python | |
174 } // namespace protobuf | |
175 | |
176 } // namespace google | |
177 #endif // GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_COMPOSITE_CONTAINER_H__ | |
OLD | NEW |