Google Cloud Spanner C++ Client 2.13.0
A C++ Client Library for Google Cloud Spanner
Loading...
Searching...
No Matches
bytes.h
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// https://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
26namespace google {
27namespace cloud {
28namespace spanner_internal {
29GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
30struct BytesInternals;
31GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
32} // namespace spanner_internal
33
34namespace spanner {
35GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
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 */
43class Bytes {
44 public:
45 /// An empty sequence.
46 Bytes() = default;
47
48 /// @name 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::BytesInternals;
86
87 std::string base64_rep_; // valid base64 representation
88};
89
90GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
91} // namespace spanner
92
93namespace spanner_internal {
94GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
95StatusOr<spanner::Bytes> BytesFromBase64(std::string input);
96std::string BytesToBase64(spanner::Bytes b);
97GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
98} // namespace spanner_internal
99
100} // namespace cloud
101} // namespace google
102
103#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_BYTES_H
A representation of the Spanner BYTES type: variable-length binary data.
Definition: bytes.h:43
Bytes()=default
An empty sequence.
friend bool operator==(Bytes const &a, Bytes const &b)
Definition: bytes.h:70
Bytes(Container const &c)
Definition: bytes.h:57
Bytes(InputIt first, InputIt last)
Definition: bytes.h:51
friend bool operator!=(Bytes const &a, Bytes const &b)
Definition: bytes.h:73
Container get() const
Conversion to a sequence of octets.
Definition: bytes.h:63
Contains all the Cloud Spanner C++ client types and functions.
Definition: backoff_policy.h:23