message.h 4.5 KB

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