Google Cloud Storage C++ Client  1.32.1
A C++ Client Library for Google Cloud Storage
object_read_stream.cc
Go to the documentation of this file.
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/storage/object_read_stream.h"
16 #include "google/cloud/log.h"
17 #include "absl/memory/memory.h"
18 
19 namespace google {
20 namespace cloud {
21 namespace storage {
22 inline namespace STORAGE_CLIENT_NS {
23 namespace {
24 std::unique_ptr<internal::ObjectReadStreambuf> MakeErrorStreambuf() {
25  return absl::make_unique<internal::ObjectReadStreambuf>(
26  internal::ReadObjectRangeRequest("", ""),
28 }
29 } // namespace
30 
31 static_assert(std::is_move_assignable<ObjectReadStream>::value,
32  "storage::ObjectReadStream must be move assignable.");
33 static_assert(std::is_move_constructible<ObjectReadStream>::value,
34  "storage::ObjectReadStream must be move constructible.");
35 
37 
39  : std::basic_istream<char>(std::move(rhs)),
40  // The spec guarantees the base class move constructor only changes a few
41  // member variables in `std::basic_istream<>`, and there is no spooky
42  // action through virtual functions because there are no virtual
43  // functions. A good summary of the specification is "it calls the
44  // default constructor and then calls std::basic_ios<>::move":
45  // https://en.cppreference.com/w/cpp/io/basic_ios/move
46  // In fact, as that page indicates, the base classes are designed such
47  // that derived classes can define their own move constructor and move
48  // assignment.
49  buf_(std::move(rhs.buf_)) {
50  rhs.buf_ = MakeErrorStreambuf();
51  rhs.set_rdbuf(rhs.buf_.get());
52  set_rdbuf(buf_.get());
53 }
54 
56  if (!IsOpen()) {
57  return;
58  }
59 #if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
60  try {
61  Close();
62  } catch (std::exception const& ex) {
63  GCP_LOG(INFO) << "Ignored exception while trying to close stream: "
64  << ex.what();
65  } catch (...) {
66  GCP_LOG(INFO) << "Ignored unknown exception while trying to close stream";
67  }
68 #else
69  Close();
70 #endif // GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
71 }
72 
74  if (!IsOpen()) {
75  return;
76  }
77  buf_->Close();
78  if (!status().ok()) {
79  setstate(std::ios_base::badbit);
80  }
81 }
82 
83 } // namespace STORAGE_CLIENT_NS
84 } // namespace storage
85 } // namespace cloud
86 } // namespace google