map_container.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_MAP_CONTAINER_H__
  31. #define GOOGLE_PROTOBUF_PYTHON_CPP_MAP_CONTAINER_H__
  32. #include <Python.h>
  33. #include <memory>
  34. #ifndef _SHARED_PTR_H
  35. #include <google/protobuf/stubs/shared_ptr.h>
  36. #endif
  37. #include <google/protobuf/descriptor.h>
  38. #include <google/protobuf/message.h>
  39. namespace google {
  40. namespace protobuf {
  41. class Message;
  42. #ifdef _SHARED_PTR_H
  43. using shared_ptr;
  44. #else
  45. using internal::shared_ptr;
  46. #endif
  47. namespace python {
  48. struct CMessage;
  49. // This struct is used directly for ScalarMap, and is the base class of
  50. // MessageMapContainer, which is used for MessageMap.
  51. struct MapContainer {
  52. PyObject_HEAD;
  53. // This is the top-level C++ Message object that owns the whole
  54. // proto tree. Every Python MapContainer holds a
  55. // reference to it in order to keep it alive as long as there's a
  56. // Python object that references any part of the tree.
  57. shared_ptr<Message> owner;
  58. // Pointer to the C++ Message that contains this container. The
  59. // MapContainer does not own this pointer.
  60. const Message* message;
  61. // Use to get a mutable message when necessary.
  62. Message* GetMutableMessage();
  63. // Weak reference to a parent CMessage object (i.e. may be NULL.)
  64. //
  65. // Used to make sure all ancestors are also mutable when first
  66. // modifying the container.
  67. CMessage* parent;
  68. // Pointer to the parent's descriptor that describes this
  69. // field. Used together with the parent's message when making a
  70. // default message instance mutable.
  71. // The pointer is owned by the global DescriptorPool.
  72. const FieldDescriptor* parent_field_descriptor;
  73. const FieldDescriptor* key_field_descriptor;
  74. const FieldDescriptor* value_field_descriptor;
  75. // We bump this whenever we perform a mutation, to invalidate existing
  76. // iterators.
  77. uint64 version;
  78. // Releases the messages in the container to a new message.
  79. //
  80. // Returns 0 on success, -1 on failure.
  81. int Release();
  82. // Set the owner field of self and any children of self.
  83. void SetOwner(const shared_ptr<Message>& new_owner) {
  84. owner = new_owner;
  85. }
  86. };
  87. struct MessageMapContainer : public MapContainer {
  88. // A callable that is used to create new child messages.
  89. PyObject* subclass_init;
  90. // A dict mapping Message* -> CMessage.
  91. PyObject* message_dict;
  92. };
  93. #if PY_MAJOR_VERSION >= 3
  94. extern PyObject *MessageMapContainer_Type;
  95. extern PyType_Spec MessageMapContainer_Type_spec;
  96. extern PyObject *ScalarMapContainer_Type;
  97. extern PyType_Spec ScalarMapContainer_Type_spec;
  98. #else
  99. extern PyTypeObject MessageMapContainer_Type;
  100. extern PyTypeObject ScalarMapContainer_Type;
  101. #endif
  102. extern PyTypeObject MapIterator_Type; // Both map types use the same iterator.
  103. // Builds a MapContainer object, from a parent message and a
  104. // field descriptor.
  105. extern PyObject* NewScalarMapContainer(
  106. CMessage* parent, const FieldDescriptor* parent_field_descriptor);
  107. // Builds a MessageMap object, from a parent message and a
  108. // field descriptor.
  109. extern PyObject* NewMessageMapContainer(
  110. CMessage* parent, const FieldDescriptor* parent_field_descriptor,
  111. PyObject* concrete_class);
  112. } // namespace python
  113. } // namespace protobuf
  114. } // namespace google
  115. #endif // GOOGLE_PROTOBUF_PYTHON_CPP_MAP_CONTAINER_H__