any.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #ifndef GOOGLE_PROTOBUF_ANY_H__
  32. #define GOOGLE_PROTOBUF_ANY_H__
  33. #include <string>
  34. #include <google/protobuf/stubs/common.h>
  35. #include <google/protobuf/arenastring.h>
  36. #include <google/protobuf/message_lite.h>
  37. #include <google/protobuf/port_def.inc>
  38. namespace google {
  39. namespace protobuf {
  40. class FieldDescriptor;
  41. class Message;
  42. namespace internal {
  43. extern const char kAnyFullTypeName[]; // "google.protobuf.Any".
  44. extern const char kTypeGoogleApisComPrefix[]; // "type.googleapis.com/".
  45. extern const char kTypeGoogleProdComPrefix[]; // "type.googleprod.com/".
  46. std::string GetTypeUrl(StringPiece message_name,
  47. StringPiece type_url_prefix);
  48. // Helper class used to implement google::protobuf::Any.
  49. class PROTOBUF_EXPORT AnyMetadata {
  50. typedef ArenaStringPtr UrlType;
  51. typedef ArenaStringPtr ValueType;
  52. public:
  53. // AnyMetadata does not take ownership of "type_url" and "value".
  54. AnyMetadata(UrlType* type_url, ValueType* value);
  55. // Packs a message using the default type URL prefix: "type.googleapis.com".
  56. // The resulted type URL will be "type.googleapis.com/<message_full_name>".
  57. template <typename T>
  58. void PackFrom(const T& message) {
  59. InternalPackFrom(message, kTypeGoogleApisComPrefix, T::FullMessageName());
  60. }
  61. void PackFrom(const Message& message);
  62. // Packs a message using the given type URL prefix. The type URL will be
  63. // constructed by concatenating the message type's full name to the prefix
  64. // with an optional "/" separator if the prefix doesn't already end with "/".
  65. // For example, both PackFrom(message, "type.googleapis.com") and
  66. // PackFrom(message, "type.googleapis.com/") yield the same result type
  67. // URL: "type.googleapis.com/<message_full_name>".
  68. template <typename T>
  69. void PackFrom(const T& message, StringPiece type_url_prefix) {
  70. InternalPackFrom(message, type_url_prefix, T::FullMessageName());
  71. }
  72. void PackFrom(const Message& message, const std::string& type_url_prefix);
  73. // Unpacks the payload into the given message. Returns false if the message's
  74. // type doesn't match the type specified in the type URL (i.e., the full
  75. // name after the last "/" of the type URL doesn't match the message's actual
  76. // full name) or parsing the payload has failed.
  77. template <typename T>
  78. bool UnpackTo(T* message) const {
  79. return InternalUnpackTo(T::FullMessageName(), message);
  80. }
  81. bool UnpackTo(Message* message) const;
  82. // Checks whether the type specified in the type URL matches the given type.
  83. // A type is considered matching if its full name matches the full name after
  84. // the last "/" in the type URL.
  85. template <typename T>
  86. bool Is() const {
  87. return InternalIs(T::FullMessageName());
  88. }
  89. private:
  90. void InternalPackFrom(const MessageLite& message,
  91. StringPiece type_url_prefix,
  92. StringPiece type_name);
  93. bool InternalUnpackTo(StringPiece type_name,
  94. MessageLite* message) const;
  95. bool InternalIs(StringPiece type_name) const;
  96. UrlType* type_url_;
  97. ValueType* value_;
  98. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AnyMetadata);
  99. };
  100. // Get the proto type name from Any::type_url value. For example, passing
  101. // "type.googleapis.com/rpc.QueryOrigin" will return "rpc.QueryOrigin" in
  102. // *full_type_name. Returns false if the type_url does not have a "/"
  103. // in the type url separating the full type name.
  104. //
  105. // NOTE: this function is available publicly as:
  106. // google::protobuf::Any() // static method on the generated message type.
  107. bool ParseAnyTypeUrl(const std::string& type_url, std::string* full_type_name);
  108. // Get the proto type name and prefix from Any::type_url value. For example,
  109. // passing "type.googleapis.com/rpc.QueryOrigin" will return
  110. // "type.googleapis.com/" in *url_prefix and "rpc.QueryOrigin" in
  111. // *full_type_name. Returns false if the type_url does not have a "/" in the
  112. // type url separating the full type name.
  113. bool ParseAnyTypeUrl(const std::string& type_url, std::string* url_prefix,
  114. std::string* full_type_name);
  115. // See if message is of type google.protobuf.Any, if so, return the descriptors
  116. // for "type_url" and "value" fields.
  117. bool GetAnyFieldDescriptors(const Message& message,
  118. const FieldDescriptor** type_url_field,
  119. const FieldDescriptor** value_field);
  120. } // namespace internal
  121. } // namespace protobuf
  122. } // namespace google
  123. #include <google/protobuf/port_undef.inc>
  124. #endif // GOOGLE_PROTOBUF_ANY_H__
  125. #else
  126. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  127. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)