1a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Protocol Buffers - Google's data interchange format 2a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Copyright 2008 Google Inc. All rights reserved. 3afb4b72037e3f13db208590fc782c4bc8e27f862Jeff Davidson// https://developers.google.com/protocol-buffers/ 4a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// 5a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Redistribution and use in source and binary forms, with or without 6a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// modification, are permitted provided that the following conditions are 7a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// met: 8a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// 9a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// * Redistributions of source code must retain the above copyright 10a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// notice, this list of conditions and the following disclaimer. 11a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// * Redistributions in binary form must reproduce the above 12a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// copyright notice, this list of conditions and the following disclaimer 13a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// in the documentation and/or other materials provided with the 14a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// distribution. 15a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// * Neither the name of Google Inc. nor the names of its 16a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// contributors may be used to endorse or promote products derived from 17a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// this software without specific prior written permission. 18a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// 19a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 31a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Author: anuraag@google.com (Anuraag Agrawal) 32a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Author: tibell@google.com (Johan Tibell) 33a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 34a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_SCALAR_CONTAINER_H__ 35a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson#define GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_SCALAR_CONTAINER_H__ 36a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 37a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson#include <Python.h> 38a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 39a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson#include <memory> 40a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson#ifndef _SHARED_PTR_H 41a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson#include <google/protobuf/stubs/shared_ptr.h> 42a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson#endif 43a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 44b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer#include <google/protobuf/descriptor.h> 45a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 46a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonnamespace google { 47a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonnamespace protobuf { 48a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 49a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonclass Message; 50a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 51b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer#ifdef _SHARED_PTR_H 52b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammerusing std::shared_ptr; 53b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer#else 54a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonusing internal::shared_ptr; 55b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer#endif 56a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 57a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonnamespace python { 58a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 59a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstruct CMessage; 60a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 61a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsontypedef struct RepeatedScalarContainer { 62a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson PyObject_HEAD; 63a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 64a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // This is the top-level C++ Message object that owns the whole 65a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // proto tree. Every Python RepeatedScalarContainer holds a 66a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // reference to it in order to keep it alive as long as there's a 67a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // Python object that references any part of the tree. 68a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson shared_ptr<Message> owner; 69a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 70a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // Pointer to the C++ Message that contains this container. The 71a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // RepeatedScalarContainer does not own this pointer. 72a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson Message* message; 73a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 74a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // Weak reference to a parent CMessage object (i.e. may be NULL.) 75a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // 76a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // Used to make sure all ancestors are also mutable when first 77a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // modifying the container. 78a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson CMessage* parent; 79a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 80b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Pointer to the parent's descriptor that describes this 81a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // field. Used together with the parent's message when making a 82a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // default message instance mutable. 83b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // The pointer is owned by the global DescriptorPool. 84b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer const FieldDescriptor* parent_field_descriptor; 85a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson} RepeatedScalarContainer; 86a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 87a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonextern PyTypeObject RepeatedScalarContainer_Type; 88a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 89a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonnamespace repeated_scalar_container { 90a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 91b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// Builds a RepeatedScalarContainer object, from a parent message and a 92b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// field descriptor. 93b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammerextern PyObject *NewContainer( 94b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer CMessage* parent, const FieldDescriptor* parent_field_descriptor); 95b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 96a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Appends the scalar 'item' to the end of the container 'self'. 97a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// 98a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Returns None if successful; returns NULL and sets an exception if 99a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// unsuccessful. 100a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff DavidsonPyObject* Append(RepeatedScalarContainer* self, PyObject* item); 101a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 102a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Releases the messages in the container to a new message. 103a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// 104a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Returns 0 on success, -1 on failure. 105a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonint Release(RepeatedScalarContainer* self); 106a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 107a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Appends all the elements in the input iterator to the container. 108a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// 109a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Returns None if successful; returns NULL and sets an exception if 110a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// unsuccessful. 111a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff DavidsonPyObject* Extend(RepeatedScalarContainer* self, PyObject* value); 112a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 113a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Set the owner field of self and any children of self. 114a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonvoid SetOwner(RepeatedScalarContainer* self, 115a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson const shared_ptr<Message>& new_owner); 116a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 117a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson} // namespace repeated_scalar_container 118a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson} // namespace python 119a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson} // namespace protobuf 120a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 121a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson} // namespace google 122a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson#endif // GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_SCALAR_CONTAINER_H__ 123