any_lite.cc
Go to the documentation of this file.
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 #include <google/protobuf/any.h>
32 
37 
38 
39 namespace google {
40 namespace protobuf {
41 namespace internal {
42 
44  StringPiece type_url_prefix) {
45  if (!type_url_prefix.empty() &&
46  type_url_prefix[type_url_prefix.size() - 1] == '/') {
47  return StrCat(type_url_prefix, message_name);
48  } else {
49  return StrCat(type_url_prefix, "/", message_name);
50  }
51 }
52 
53 const char kAnyFullTypeName[] = "google.protobuf.Any";
54 const char kTypeGoogleApisComPrefix[] = "type.googleapis.com/";
55 const char kTypeGoogleProdComPrefix[] = "type.googleprod.com/";
56 
58  : type_url_(type_url), value_(value) {}
59 
61  StringPiece type_url_prefix,
62  StringPiece type_name) {
64  GetTypeUrl(type_name, type_url_prefix));
65  message.SerializeToString(value_->MutableNoArena(
67 }
68 
70  MessageLite* message) const {
71  if (!InternalIs(type_name)) {
72  return false;
73  }
74  return message->ParseFromString(value_->GetNoArena());
75 }
76 
77 namespace {
78 
79 // The type URL could be stored in either an ArenaStringPtr or a
80 // StringPieceField, so we provide these helpers to get a string_view from
81 // either type. We use a template function as a way to avoid depending on
82 // StringPieceField.
83 
84 template <typename T>
85 StringPiece Get(const T* ptr) {
86  return ptr->Get();
87 }
88 
89 template <>
90 // NOLINTNEXTLINE: clang-diagnostic-unused-function
91 StringPiece Get(const ArenaStringPtr* ptr) {
92  return ptr->GetNoArena();
93 }
94 
95 } // namespace
96 
97 bool AnyMetadata::InternalIs(StringPiece type_name) const {
99  return type_url.size() >= type_name.size() + 1 &&
100  type_url[type_url.size() - type_name.size() - 1] == '/' &&
101  HasSuffixString(type_url, type_name);
102 }
103 
105  std::string* full_type_name) {
106  size_t pos = type_url.find_last_of("/");
107  if (pos == std::string::npos || pos + 1 == type_url.size()) {
108  return false;
109  }
110  if (url_prefix) {
111  *url_prefix = type_url.substr(0, pos + 1);
112  }
113  *full_type_name = type_url.substr(pos + 1);
114  return true;
115 }
116 
117 bool ParseAnyTypeUrl(const std::string& type_url, std::string* full_type_name) {
118  return ParseAnyTypeUrl(type_url, nullptr, full_type_name);
119 }
120 
121 } // namespace internal
122 } // namespace protobuf
123 } // namespace google
arenastring.h
google::protobuf.internal::ArenaStringPtr::MutableNoArena
inline ::std::string * MutableNoArena(const ::std::string *default_value)
Definition: arenastring.h:287
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
google::protobuf.internal::Get
const T & Get(const void *ptr)
Definition: generated_message_util.cc:98
google::protobuf.internal::ParseAnyTypeUrl
bool ParseAnyTypeUrl(const std::string &type_url, std::string *full_type_name)
Definition: any_lite.cc:117
google::protobuf.internal::kAnyFullTypeName
const char kAnyFullTypeName[]
Definition: any_lite.cc:53
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::MessageLite
Definition: message_lite.h:183
google::protobuf.internal::AnyMetadata::type_url_
UrlType * type_url_
Definition: any.h:114
T
#define T(upbtypeconst, upbtype, ctype, default_value)
google::protobuf.internal::ArenaStringPtr::SetNoArena
void SetNoArena(const ::std::string *default_value, const ::std::string &value)
Definition: arenastring.h:264
google::protobuf.internal::ArenaStringPtr::GetNoArena
const ::std::string & GetNoArena() const
Definition: arenastring.h:285
google::protobuf.internal::AnyMetadata::value_
ValueType * value_
Definition: any.h:115
any.h
strutil.h
google::protobuf::StringPiece::empty
bool empty() const
Definition: stringpiece.h:250
google::protobuf.internal::kTypeGoogleApisComPrefix
const char kTypeGoogleApisComPrefix[]
Definition: any_lite.cc:54
google::protobuf::StringPiece
Definition: stringpiece.h:180
google::protobuf.internal::AnyMetadata::InternalIs
bool InternalIs(StringPiece type_name) const
Definition: any_lite.cc:97
google::protobuf.internal::AnyMetadata::InternalUnpackTo
bool InternalUnpackTo(StringPiece type_name, MessageLite *message) const
Definition: any_lite.cc:69
google::protobuf.internal::GetEmptyString
const PROTOBUF_EXPORT std::string & GetEmptyString()
Definition: generated_message_util.h:87
zero_copy_stream_impl_lite.h
value_
int value_
Definition: gmock-matchers_test.cc:571
google::protobuf.internal::kTypeGoogleProdComPrefix
const char kTypeGoogleProdComPrefix[]
Definition: any_lite.cc:55
google::protobuf.internal::GetEmptyStringAlreadyInited
const PROTOBUF_EXPORT std::string & GetEmptyStringAlreadyInited()
Definition: message_lite.h:152
google::protobuf.internal::GetTypeUrl
std::string GetTypeUrl(StringPiece message_name, StringPiece type_url_prefix)
Definition: any_lite.cc:43
generated_message_util.h
google::protobuf.internal::AnyMetadata::AnyMetadata
AnyMetadata(UrlType *type_url, ValueType *value)
Definition: any_lite.cc:57
internal
Definition: any.pb.h:40
google::protobuf::HasSuffixString
bool HasSuffixString(const string &str, const string &suffix)
Definition: strutil.h:137
type_url
string * type_url
Definition: conformance_cpp.cc:98
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf::StringPiece::size
stringpiece_ssize_type size() const
Definition: stringpiece.h:248
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf.internal::ArenaStringPtr
Definition: arenastring.h:68
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
google::protobuf.internal::AnyMetadata::InternalPackFrom
void InternalPackFrom(const MessageLite &message, StringPiece type_url_prefix, StringPiece type_name)
Definition: any_lite.cc:60


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:47