byte_stream.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FIRESTORE_CORE_SRC_UTIL_BYTE_STREAM_H_
  17. #define FIRESTORE_CORE_SRC_UTIL_BYTE_STREAM_H_
  18. #include <cstddef>
  19. #include <string>
  20. #include <utility>
  21. #include "Firestore/core/src/util/statusor.h"
  22. namespace firebase {
  23. namespace firestore {
  24. namespace util {
  25. /**
  26. * Represents results from read operations on a `ByteStream`.
  27. */
  28. class StreamReadResult {
  29. public:
  30. StreamReadResult(StatusOr<std::string> result, bool eof)
  31. : result_(std::move(result)), eof_(eof) {
  32. }
  33. /** Returns whether the `ByteStream` instance has reached to the end. */
  34. bool eof() const {
  35. return eof_;
  36. }
  37. /** Whether the read operation is successful. */
  38. bool ok() const {
  39. return result_.ok();
  40. }
  41. const Status& status() const {
  42. return result_.status();
  43. }
  44. const std::string& ValueOrDie() const& {
  45. return result_.ValueOrDie();
  46. }
  47. std::string& ValueOrDie() & {
  48. return result_.ValueOrDie();
  49. }
  50. const std::string&& ValueOrDie() const&& {
  51. return std::move(result_).ValueOrDie();
  52. }
  53. std::string&& ValueOrDie() && {
  54. return std::move(result_).ValueOrDie();
  55. }
  56. private:
  57. StatusOr<std::string> result_;
  58. bool eof_ = false;
  59. };
  60. /**
  61. * A class representing byte streams, providing interfaces for stream reading
  62. * on top of potentially different stream implementations: NSInputStream,
  63. * istream, etc.
  64. */
  65. class ByteStream {
  66. public:
  67. virtual ~ByteStream() = default;
  68. /**
  69. * Reads until either a specified (`delim`) character is encountered, or the
  70. * `max_length` number of bytes has been read, or the end of stream has been
  71. * reached.
  72. *
  73. * Note in the case when `delim` is encountered, the `delim` character is not
  74. * read after this operation, and will be the first character for the next
  75. * read operation.
  76. */
  77. virtual StreamReadResult ReadUntil(char delim, size_t max_length) = 0;
  78. /**
  79. * Reads until the `max_length` number of bytes has been read, or the end of
  80. * stream has been reached.
  81. */
  82. virtual StreamReadResult Read(size_t max_length) = 0;
  83. };
  84. } // namespace util
  85. } // namespace firestore
  86. } // namespace firebase
  87. #endif // FIRESTORE_CORE_SRC_UTIL_BYTE_STREAM_H_