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