repeated_composite_container.h 6.4 KB

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