Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:05

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef GOOGLE_PROTOBUF_ANY_H__
0009 #define GOOGLE_PROTOBUF_ANY_H__
0010 
0011 #include <string>
0012 
0013 #include "google/protobuf/port.h"
0014 #include "google/protobuf/arenastring.h"
0015 #include "google/protobuf/message_lite.h"
0016 
0017 // Must be included last.
0018 #include "google/protobuf/port_def.inc"
0019 
0020 namespace google {
0021 namespace protobuf {
0022 
0023 class FieldDescriptor;
0024 class Message;
0025 
0026 namespace internal {
0027 
0028 // "google.protobuf.Any".
0029 PROTOBUF_EXPORT extern const char kAnyFullTypeName[];
0030 // "type.googleapis.com/".
0031 PROTOBUF_EXPORT extern const char kTypeGoogleApisComPrefix[];
0032 // "type.googleprod.com/".
0033 PROTOBUF_EXPORT extern const char kTypeGoogleProdComPrefix[];
0034 
0035 std::string GetTypeUrl(absl::string_view message_name,
0036                        absl::string_view type_url_prefix);
0037 
0038 // Helper class used to implement google::protobuf::Any.
0039 class PROTOBUF_EXPORT AnyMetadata {
0040   typedef ArenaStringPtr UrlType;
0041   typedef ArenaStringPtr ValueType;
0042  public:
0043   // AnyMetadata does not take ownership of "type_url" and "value".
0044   constexpr AnyMetadata(UrlType* type_url, ValueType* value)
0045       : type_url_(type_url), value_(value) {}
0046   AnyMetadata(const AnyMetadata&) = delete;
0047   AnyMetadata& operator=(const AnyMetadata&) = delete;
0048 
0049   // Packs a message using the default type URL prefix: "type.googleapis.com".
0050   // The resulted type URL will be "type.googleapis.com/<message_full_name>".
0051   // Returns false if serializing the message failed.
0052   template <typename T>
0053   bool PackFrom(Arena* arena, const T& message) {
0054     return InternalPackFrom(arena, message, kTypeGoogleApisComPrefix,
0055                             T::FullMessageName());
0056   }
0057 
0058   bool PackFrom(Arena* arena, const Message& message);
0059 
0060   // Packs a message using the given type URL prefix. The type URL will be
0061   // constructed by concatenating the message type's full name to the prefix
0062   // with an optional "/" separator if the prefix doesn't already end with "/".
0063   // For example, both PackFrom(message, "type.googleapis.com") and
0064   // PackFrom(message, "type.googleapis.com/") yield the same result type
0065   // URL: "type.googleapis.com/<message_full_name>".
0066   // Returns false if serializing the message failed.
0067   template <typename T>
0068   bool PackFrom(Arena* arena, const T& message,
0069                 absl::string_view type_url_prefix) {
0070     return InternalPackFrom(arena, message, type_url_prefix,
0071                             T::FullMessageName());
0072   }
0073 
0074   bool PackFrom(Arena* arena, const Message& message,
0075                 absl::string_view type_url_prefix);
0076 
0077   // Unpacks the payload into the given message. Returns false if the message's
0078   // type doesn't match the type specified in the type URL (i.e., the full
0079   // name after the last "/" of the type URL doesn't match the message's actual
0080   // full name) or parsing the payload has failed.
0081   template <typename T>
0082   bool UnpackTo(T* message) const {
0083     return InternalUnpackTo(T::FullMessageName(), message);
0084   }
0085 
0086   bool UnpackTo(Message* message) const;
0087 
0088   // Checks whether the type specified in the type URL matches the given type.
0089   // A type is considered matching if its full name matches the full name after
0090   // the last "/" in the type URL.
0091   template <typename T>
0092   bool Is() const {
0093     return InternalIs(T::FullMessageName());
0094   }
0095 
0096  private:
0097   bool InternalPackFrom(Arena* arena, const MessageLite& message,
0098                         absl::string_view type_url_prefix,
0099                         absl::string_view type_name);
0100   bool InternalUnpackTo(absl::string_view type_name,
0101                         MessageLite* message) const;
0102   bool InternalIs(absl::string_view type_name) const;
0103 
0104   UrlType* type_url_;
0105   ValueType* value_;
0106 };
0107 
0108 // Get the proto type name from Any::type_url value. For example, passing
0109 // "type.googleapis.com/rpc.QueryOrigin" will return "rpc.QueryOrigin" in
0110 // *full_type_name. Returns false if the type_url does not have a "/"
0111 // in the type url separating the full type name.
0112 //
0113 // NOTE: this function is available publicly as a static method on the
0114 // generated message type: google::protobuf::Any::ParseAnyTypeUrl()
0115 bool ParseAnyTypeUrl(absl::string_view type_url, std::string* full_type_name);
0116 
0117 // Get the proto type name and prefix from Any::type_url value. For example,
0118 // passing "type.googleapis.com/rpc.QueryOrigin" will return
0119 // "type.googleapis.com/" in *url_prefix and "rpc.QueryOrigin" in
0120 // *full_type_name. Returns false if the type_url does not have a "/" in the
0121 // type url separating the full type name.
0122 bool ParseAnyTypeUrl(absl::string_view type_url, std::string* url_prefix,
0123                      std::string* full_type_name);
0124 
0125 // See if message is of type google.protobuf.Any, if so, return the descriptors
0126 // for "type_url" and "value" fields.
0127 bool GetAnyFieldDescriptors(const Message& message,
0128                             const FieldDescriptor** type_url_field,
0129                             const FieldDescriptor** value_field);
0130 
0131 }  // namespace internal
0132 }  // namespace protobuf
0133 }  // namespace google
0134 
0135 #include "google/protobuf/port_undef.inc"
0136 
0137 #endif  // GOOGLE_PROTOBUF_ANY_H__