Google Cloud Spanner C++ Client  1.32.0
A C++ Client Library for Google Cloud Spanner
bytes.h
Go to the documentation of this file.
1 // Copyright 2019 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 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_BYTES_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_BYTES_H
17 
18 #include "google/cloud/spanner/version.h"
19 #include "google/cloud/internal/base64_transforms.h"
20 #include "google/cloud/status_or.h"
21 #include <array>
22 #include <cstddef>
23 #include <ostream>
24 #include <string>
25 
26 namespace google {
27 namespace cloud {
28 namespace spanner_internal {
29 inline namespace SPANNER_CLIENT_NS {
30 struct BytesInternals;
31 } // namespace SPANNER_CLIENT_NS
32 } // namespace spanner_internal
33 
34 namespace spanner {
35 inline namespace SPANNER_CLIENT_NS {
36 
37 /**
38  * A representation of the Spanner BYTES type: variable-length binary data.
39  *
40  * A `Bytes` value can be constructed from, and converted to any sequence of
41  * octets. `Bytes` values can be compared for equality.
42  */
43 class Bytes {
44  public:
45  /// An empty sequence.
46  Bytes() = default;
47 
48  /// Construction from a sequence of octets.
49  ///@{
50  template <typename InputIt>
51  Bytes(InputIt first, InputIt last) {
52  google::cloud::internal::Base64Encoder encoder;
53  while (first != last) encoder.PushBack(*first++);
54  base64_rep_ = std::move(encoder).FlushAndPad();
55  }
56  template <typename Container>
57  explicit Bytes(Container const& c) : Bytes(std::begin(c), std::end(c)) {}
58  ///@}
59 
60  /// Conversion to a sequence of octets. The `Container` must support
61  /// construction from a range specified as a pair of input iterators.
62  template <typename Container>
63  Container get() const {
64  google::cloud::internal::Base64Decoder decoder(base64_rep_);
65  return Container(decoder.begin(), decoder.end());
66  }
67 
68  /// @name Relational operators
69  ///@{
70  friend bool operator==(Bytes const& a, Bytes const& b) {
71  return a.base64_rep_ == b.base64_rep_;
72  }
73  friend bool operator!=(Bytes const& a, Bytes const& b) { return !(a == b); }
74  ///@}
75 
76  /**
77  * Outputs string representation of the Bytes to the provided stream.
78  *
79  * @warning This is intended for debugging and human consumption only, not
80  * machine consumption, as the output format may change without notice.
81  */
82  friend std::ostream& operator<<(std::ostream& os, Bytes const& bytes);
83 
84  private:
85  friend struct spanner_internal::SPANNER_CLIENT_NS::BytesInternals;
86 
87  std::string base64_rep_; // valid base64 representation
88 };
89 
90 } // namespace SPANNER_CLIENT_NS
91 } // namespace spanner
92 
93 namespace spanner_internal {
94 inline namespace SPANNER_CLIENT_NS {
95 StatusOr<spanner::Bytes> BytesFromBase64(std::string input);
96 std::string BytesToBase64(spanner::Bytes b);
97 } // namespace SPANNER_CLIENT_NS
98 } // namespace spanner_internal
99 
100 } // namespace cloud
101 } // namespace google
102 
103 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_BYTES_H