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 #ifndef RUBY_PROTOBUF_MESSAGE_H_ 32 #define RUBY_PROTOBUF_MESSAGE_H_ 33 34 #include <ruby/ruby.h> 35 36 #include "protobuf.h" 37 #include "ruby-upb.h" 38 39 // Gets the underlying upb_Message* and upb_MessageDef for the given Ruby 40 // message wrapper. Requires that |value| is indeed a message object. 41 const upb_Message* Message_Get(VALUE value, const upb_MessageDef** m); 42 43 // Like Message_Get(), but checks that the object is not frozen and returns a 44 // mutable pointer. 45 upb_Message* Message_GetMutable(VALUE value, const upb_MessageDef** m); 46 47 // Returns the Arena object for this message. 48 VALUE Message_GetArena(VALUE value); 49 50 // Converts |value| into a upb_Message value of the expected upb_MessageDef 51 // type, raising an error if this is not possible. Used when assigning |value| 52 // to a field of another message, which means the message must be of a 53 // particular type. 54 // 55 // This will perform automatic conversions in some cases (for example, Time -> 56 // Google::Protobuf::Timestamp). If any new message is created, it will be 57 // created on |arena|, and any existing message will have its arena fused with 58 // |arena|. 59 const upb_Message* Message_GetUpbMessage(VALUE value, const upb_MessageDef* m, 60 const char* name, upb_Arena* arena); 61 62 // Gets or constructs a Ruby wrapper object for the given message. The wrapper 63 // object will reference |arena| and ensure that it outlives this object. 64 VALUE Message_GetRubyWrapper(upb_Message* msg, const upb_MessageDef* m, 65 VALUE arena); 66 67 // Gets the given field from this message. 68 VALUE Message_getfield(VALUE _self, const upb_FieldDef* f); 69 70 // Implements #inspect for this message, printing the text to |b|. 71 void Message_PrintMessage(StringBuilder* b, const upb_Message* msg, 72 const upb_MessageDef* m); 73 74 // Returns a hash value for the given message. 75 uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m, 76 uint64_t seed); 77 78 // Returns a deep copy of the given message. 79 upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m, 80 upb_Arena* arena); 81 82 // Returns true if these two messages are equal. 83 bool Message_Equal(const upb_Message* m1, const upb_Message* m2, 84 const upb_MessageDef* m); 85 86 // Checks that this Ruby object is a message, and raises an exception if not. 87 void Message_CheckClass(VALUE klass); 88 89 // Returns a new Hash object containing the contents of this message. 90 VALUE Scalar_CreateHash(upb_MessageValue val, TypeInfo type_info); 91 92 // Creates a message class or enum module for this descriptor, respectively. 93 VALUE build_class_from_descriptor(VALUE descriptor); 94 VALUE build_module_from_enumdesc(VALUE _enumdesc); 95 96 // Returns the Descriptor/EnumDescriptor for the given message class or enum 97 // module, respectively. Returns nil if this is not a message class or enum 98 // module. 99 VALUE MessageOrEnum_GetDescriptor(VALUE klass); 100 101 // Call at startup to register all types in this module. 102 void Message_register(VALUE protobuf); 103 104 #endif // RUBY_PROTOBUF_MESSAGE_H_ 105