"Fossies" - the Fresh Open Source Software Archive

Member "protobuf-3.12.3/src/google/protobuf/any_lite.cc" (2 Jun 2020, 4614 Bytes) of package /linux/misc/protobuf-all-3.12.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "any_lite.cc" see the Fossies "Dox" file reference documentation.

    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 
   33 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
   34 #include <google/protobuf/arenastring.h>
   35 #include <google/protobuf/generated_message_util.h>
   36 #include <google/protobuf/stubs/strutil.h>
   37 
   38 namespace google {
   39 namespace protobuf {
   40 namespace internal {
   41 
   42 std::string GetTypeUrl(StringPiece message_name,
   43                        StringPiece type_url_prefix) {
   44   if (!type_url_prefix.empty() &&
   45       type_url_prefix[type_url_prefix.size() - 1] == '/') {
   46     return StrCat(type_url_prefix, message_name);
   47   } else {
   48     return StrCat(type_url_prefix, "/", message_name);
   49   }
   50 }
   51 
   52 const char kAnyFullTypeName[] = "google.protobuf.Any";
   53 const char kTypeGoogleApisComPrefix[] = "type.googleapis.com/";
   54 const char kTypeGoogleProdComPrefix[] = "type.googleprod.com/";
   55 
   56 AnyMetadata::AnyMetadata(UrlType* type_url, ValueType* value)
   57     : type_url_(type_url), value_(value) {}
   58 
   59 void AnyMetadata::InternalPackFrom(const MessageLite& message,
   60                                    StringPiece type_url_prefix,
   61                                    StringPiece type_name) {
   62   type_url_->SetNoArena(&::google::protobuf::internal::GetEmptyString(),
   63                         GetTypeUrl(type_name, type_url_prefix));
   64   message.SerializeToString(value_->MutableNoArena(
   65       &::google::protobuf::internal::GetEmptyStringAlreadyInited()));
   66 }
   67 
   68 bool AnyMetadata::InternalUnpackTo(StringPiece type_name,
   69                                    MessageLite* message) const {
   70   if (!InternalIs(type_name)) {
   71     return false;
   72   }
   73   return message->ParseFromString(value_->GetNoArena());
   74 }
   75 
   76 namespace {
   77 
   78 // The type URL could be stored in either an ArenaStringPtr or a
   79 // StringPieceField, so we provide these helpers to get a string_view from
   80 // either type. We use a template function as a way to avoid depending on
   81 // StringPieceField.
   82 
   83 template <typename T>
   84 StringPiece Get(const T* ptr) {
   85   return ptr->Get();
   86 }
   87 
   88 template <>
   89 // NOLINTNEXTLINE: clang-diagnostic-unused-function
   90 StringPiece Get(const ArenaStringPtr* ptr) {
   91   return ptr->GetNoArena();
   92 }
   93 
   94 }  // namespace
   95 
   96 bool AnyMetadata::InternalIs(StringPiece type_name) const {
   97   StringPiece type_url = Get(type_url_);
   98   return type_url.size() >= type_name.size() + 1 &&
   99          type_url[type_url.size() - type_name.size() - 1] == '/' &&
  100          HasSuffixString(type_url, type_name);
  101 }
  102 
  103 bool ParseAnyTypeUrl(const std::string& type_url, std::string* url_prefix,
  104                      std::string* full_type_name) {
  105   size_t pos = type_url.find_last_of("/");
  106   if (pos == std::string::npos || pos + 1 == type_url.size()) {
  107     return false;
  108   }
  109   if (url_prefix) {
  110     *url_prefix = type_url.substr(0, pos + 1);
  111   }
  112   *full_type_name = type_url.substr(pos + 1);
  113   return true;
  114 }
  115 
  116 bool ParseAnyTypeUrl(const std::string& type_url, std::string* full_type_name) {
  117   return ParseAnyTypeUrl(type_url, nullptr, full_type_name);
  118 }
  119 
  120 }  // namespace internal
  121 }  // namespace protobuf
  122 }  // namespace google