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_EXTENSION_DICT_H__
35#define GOOGLE_PROTOBUF_PYTHON_CPP_EXTENSION_DICT_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
44
45namespace google {
46namespace protobuf {
47
48class Message;
49class FieldDescriptor;
50
51using internal::shared_ptr;
52
53namespace python {
54
55struct CMessage;
56struct CFieldDescriptor;
57
58typedef struct ExtensionDict {
59  PyObject_HEAD;
60  shared_ptr<Message> owner;
61  CMessage* parent;
62  Message* message;
63  PyObject* values;
64} ExtensionDict;
65
66extern PyTypeObject ExtensionDict_Type;
67
68namespace extension_dict {
69
70// Gets the _cdescriptor reference to a CFieldDescriptor object given a
71// python descriptor object.
72//
73// Returns a new reference.
74CFieldDescriptor* InternalGetCDescriptorFromExtension(PyObject* extension);
75
76// Gets the number of extension values in this ExtensionDict as a python object.
77//
78// Returns a new reference.
79PyObject* len(ExtensionDict* self);
80
81// Releases extensions referenced outside this dictionary to keep outside
82// references alive.
83//
84// Returns 0 on success, -1 on failure.
85int ReleaseExtension(ExtensionDict* self,
86                     PyObject* extension,
87                     const google::protobuf::FieldDescriptor* descriptor);
88
89// Gets an extension from the dict for the given extension descriptor.
90//
91// Returns a new reference.
92PyObject* subscript(ExtensionDict* self, PyObject* key);
93
94// Assigns a value to an extension in the dict. Can only be used for singular
95// simple types.
96//
97// Returns 0 on success, -1 on failure.
98int ass_subscript(ExtensionDict* self, PyObject* key, PyObject* value);
99
100// Clears an extension from the dict. Will release the extension if there
101// is still an external reference left to it.
102//
103// Returns None on success.
104PyObject* ClearExtension(ExtensionDict* self,
105                                       PyObject* extension);
106
107// Checks if the dict has an extension.
108//
109// Returns a new python boolean reference.
110PyObject* HasExtension(ExtensionDict* self, PyObject* extension);
111
112// Gets an extension from the dict given the extension name as opposed to
113// descriptor.
114//
115// Returns a new reference.
116PyObject* _FindExtensionByName(ExtensionDict* self, PyObject* name);
117
118}  // namespace extension_dict
119}  // namespace python
120}  // namespace protobuf
121
122}  // namespace google
123#endif  // GOOGLE_PROTOBUF_PYTHON_CPP_EXTENSION_DICT_H__
124