blob: ddf94be71b7c12a8eb74521d2eabb93d04c3f810 [file] [log] [blame]
Bo Yang5db21732015-05-21 14:28:59 -07001// 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
Feng Xiaoe841bac2015-12-11 17:09:20 -080031#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_MAP_CONTAINER_H__
32#define GOOGLE_PROTOBUF_PYTHON_CPP_MAP_CONTAINER_H__
Bo Yang5db21732015-05-21 14:28:59 -070033
34#include <Python.h>
35
36#include <memory>
37#ifndef _SHARED_PTR_H
38#include <google/protobuf/stubs/shared_ptr.h>
39#endif
40
41#include <google/protobuf/descriptor.h>
Feng Xiaoe841bac2015-12-11 17:09:20 -080042#include <google/protobuf/message.h>
Bo Yang5db21732015-05-21 14:28:59 -070043
44namespace google {
45namespace protobuf {
46
47class Message;
48
Manjunath Kudlur3f9b4f22015-12-07 14:15:29 -080049#ifdef _SHARED_PTR_H
Manjunath Kudlur3ff1dca2015-12-07 13:08:21 -080050using std::shared_ptr;
Manjunath Kudlur96537c42015-12-09 07:40:30 -080051#else
52using internal::shared_ptr;
Manjunath Kudlur3f9b4f22015-12-07 14:15:29 -080053#endif
Bo Yang5db21732015-05-21 14:28:59 -070054
55namespace python {
56
57struct CMessage;
58
Feng Xiaoe841bac2015-12-11 17:09:20 -080059// This struct is used directly for ScalarMap, and is the base class of
60// MessageMapContainer, which is used for MessageMap.
61struct MapContainer {
Bo Yang5db21732015-05-21 14:28:59 -070062 PyObject_HEAD;
63
64 // This is the top-level C++ Message object that owns the whole
Feng Xiaoe841bac2015-12-11 17:09:20 -080065 // proto tree. Every Python MapContainer holds a
Bo Yang5db21732015-05-21 14:28:59 -070066 // reference to it in order to keep it alive as long as there's a
67 // Python object that references any part of the tree.
68 shared_ptr<Message> owner;
69
70 // Pointer to the C++ Message that contains this container. The
Feng Xiaoe841bac2015-12-11 17:09:20 -080071 // MapContainer does not own this pointer.
72 const Message* message;
73
74 // Use to get a mutable message when necessary.
75 Message* GetMutableMessage();
Bo Yang5db21732015-05-21 14:28:59 -070076
77 // Weak reference to a parent CMessage object (i.e. may be NULL.)
78 //
79 // Used to make sure all ancestors are also mutable when first
80 // modifying the container.
81 CMessage* parent;
82
83 // Pointer to the parent's descriptor that describes this
84 // field. Used together with the parent's message when making a
85 // default message instance mutable.
86 // The pointer is owned by the global DescriptorPool.
87 const FieldDescriptor* parent_field_descriptor;
88 const FieldDescriptor* key_field_descriptor;
89 const FieldDescriptor* value_field_descriptor;
90
Feng Xiaoe841bac2015-12-11 17:09:20 -080091 // We bump this whenever we perform a mutation, to invalidate existing
92 // iterators.
93 uint64 version;
94
95 // Releases the messages in the container to a new message.
96 //
97 // Returns 0 on success, -1 on failure.
98 int Release();
99
100 // Set the owner field of self and any children of self.
101 void SetOwner(const shared_ptr<Message>& new_owner) {
102 owner = new_owner;
103 }
104};
105
106struct MessageMapContainer : public MapContainer {
Bo Yang5db21732015-05-21 14:28:59 -0700107 // A callable that is used to create new child messages.
108 PyObject* subclass_init;
109
110 // A dict mapping Message* -> CMessage.
111 PyObject* message_dict;
Bo Yang5db21732015-05-21 14:28:59 -0700112};
113
Josh Habermane1abdf22015-12-22 11:13:03 -0800114#if PY_MAJOR_VERSION >= 3
115 extern PyObject *MessageMapContainer_Type;
116 extern PyType_Spec MessageMapContainer_Type_spec;
117 extern PyObject *ScalarMapContainer_Type;
118 extern PyType_Spec ScalarMapContainer_Type_spec;
119#else
120 extern PyTypeObject MessageMapContainer_Type;
121 extern PyTypeObject ScalarMapContainer_Type;
122#endif
123
Feng Xiaoe841bac2015-12-11 17:09:20 -0800124extern PyTypeObject MapIterator_Type; // Both map types use the same iterator.
Bo Yang5db21732015-05-21 14:28:59 -0700125
Feng Xiaoe841bac2015-12-11 17:09:20 -0800126// Builds a MapContainer object, from a parent message and a
Bo Yang5db21732015-05-21 14:28:59 -0700127// field descriptor.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800128extern PyObject* NewScalarMapContainer(
129 CMessage* parent, const FieldDescriptor* parent_field_descriptor);
Bo Yang5db21732015-05-21 14:28:59 -0700130
Feng Xiaoe841bac2015-12-11 17:09:20 -0800131// Builds a MessageMap object, from a parent message and a
132// field descriptor.
133extern PyObject* NewMessageMapContainer(
134 CMessage* parent, const FieldDescriptor* parent_field_descriptor,
135 PyObject* concrete_class);
Bo Yang5db21732015-05-21 14:28:59 -0700136
Bo Yang5db21732015-05-21 14:28:59 -0700137} // namespace python
138} // namespace protobuf
139
140} // namespace google
Feng Xiaoe841bac2015-12-11 17:09:20 -0800141#endif // GOOGLE_PROTOBUF_PYTHON_CPP_MAP_CONTAINER_H__