1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 #ifndef RUBY_PROTOBUF_MESSAGE_H_ 9 #define RUBY_PROTOBUF_MESSAGE_H_ 10 11 #include "protobuf.h" 12 #include "ruby-upb.h" 13 14 // Gets the underlying upb_Message* and upb_MessageDef for the given Ruby 15 // message wrapper. Requires that |value| is indeed a message object. 16 const upb_Message* Message_Get(VALUE value, const upb_MessageDef** m); 17 18 // Like Message_Get(), but checks that the object is not frozen and returns a 19 // mutable pointer. 20 upb_Message* Message_GetMutable(VALUE value, const upb_MessageDef** m); 21 22 // Returns the Arena object for this message. 23 VALUE Message_GetArena(VALUE value); 24 25 // Converts |value| into a upb_Message value of the expected upb_MessageDef 26 // type, raising an error if this is not possible. Used when assigning |value| 27 // to a field of another message, which means the message must be of a 28 // particular type. 29 // 30 // This will perform automatic conversions in some cases (for example, Time -> 31 // Google::Protobuf::Timestamp). If any new message is created, it will be 32 // created on |arena|, and any existing message will have its arena fused with 33 // |arena|. 34 const upb_Message* Message_GetUpbMessage(VALUE value, const upb_MessageDef* m, 35 const char* name, upb_Arena* arena); 36 37 // Gets or constructs a Ruby wrapper object for the given message. The wrapper 38 // object will reference |arena| and ensure that it outlives this object. 39 VALUE Message_GetRubyWrapper(const upb_Message* msg, const upb_MessageDef* m, 40 VALUE arena); 41 42 // Gets the given field from this message. 43 VALUE Message_getfield(VALUE _self, const upb_FieldDef* f); 44 45 // Implements #inspect for this message, printing the text to |b|. 46 void Message_PrintMessage(StringBuilder* b, const upb_Message* msg, 47 const upb_MessageDef* m); 48 49 // Returns a hash value for the given message. 50 uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m, 51 uint64_t seed); 52 53 // Returns a deep copy of the given message. 54 upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m, 55 upb_Arena* arena); 56 57 // Checks that this Ruby object is a message, and raises an exception if not. 58 void Message_CheckClass(VALUE klass); 59 60 // Returns a new Hash object containing the contents of this message. 61 VALUE Scalar_CreateHash(upb_MessageValue val, TypeInfo type_info); 62 63 // Creates a message class or enum module for this descriptor, respectively. 64 VALUE build_class_from_descriptor(VALUE descriptor); 65 VALUE build_module_from_enumdesc(VALUE _enumdesc); 66 67 // Returns the Descriptor/EnumDescriptor for the given message class or enum 68 // module, respectively. Returns nil if this is not a message class or enum 69 // module. 70 VALUE MessageOrEnum_GetDescriptor(VALUE klass); 71 72 // Decodes a Message from a byte sequence. 73 VALUE Message_decode_bytes(int size, const char* bytes, int options, 74 VALUE klass, bool freeze); 75 76 // Recursively freeze message 77 VALUE Message_freeze(VALUE _self); 78 79 // Call at startup to register all types in this module. 80 void Message_register(VALUE protobuf); 81 82 #endif // RUBY_PROTOBUF_MESSAGE_H_ 83