kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/bazel_event_reader.h (about) 1 /* 2 * Copyright 2020 The Kythe Authors. All rights reserved. 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 KYTHE_CXX_EXTRACTOR_BAZEL_EVENT_READER_H_ 17 #define KYTHE_CXX_EXTRACTOR_BAZEL_EVENT_READER_H_ 18 19 #include "absl/log/log.h" 20 #include "absl/status/status.h" 21 #include "absl/types/variant.h" 22 #include "google/protobuf/io/zero_copy_stream.h" 23 #include "third_party/bazel/src/main/java/com/google/devtools/build/lib/buildeventstream/proto/build_event_stream.pb.h" 24 25 namespace kythe { 26 27 /// \brief Interface used for iterating through Bazel BuildEvent messages. 28 class BazelEventReaderInterface { 29 public: 30 using value_type = ::build_event_stream::BuildEvent; 31 using reference = value_type&; 32 using const_reference = const value_type&; 33 34 /// \brief Advances the reading to the next record. 35 virtual void Next() = 0; 36 /// \brief Returns false if there are no more records to read. 37 virtual bool Done() const = 0; 38 /// \brief Returns a reference to the current record. 39 /// Requires: !Done() 40 virtual reference Ref() = 0; 41 /// \brief Returns a const-reference to the current record. 42 /// Requires: !Done() 43 virtual const_reference Ref() const = 0; 44 /// \brief Returns the overall status of the iteration. 45 virtual absl::Status status() const = 0; 46 47 virtual ~BazelEventReaderInterface() = default; 48 49 protected: 50 /// \brief While BazelEventReaderInterface is not itself copyable, subclasses 51 /// choose to be. 52 BazelEventReaderInterface() = default; 53 BazelEventReaderInterface(const BazelEventReaderInterface&) = default; 54 BazelEventReaderInterface& operator=(const BazelEventReaderInterface&) = 55 default; 56 }; 57 58 /// \brief Reads BazelEvent messages from a binary Bazel event stream. 59 class BazelEventReader final : public BazelEventReaderInterface { 60 public: 61 /// \brief Constructs a new BazelEventReader 62 explicit BazelEventReader(google::protobuf::io::CodedInputStream* stream) 63 : stream_(stream) { 64 Next(); 65 } 66 67 /// \brief Constructs a new BazelEventReader 68 explicit BazelEventReader(google::protobuf::io::ZeroCopyInputStream* stream) 69 : stream_(stream) { 70 Next(); 71 } 72 73 void Next() override; 74 75 bool Done() const override { return value_.index() != 0; } 76 reference Ref() override { return absl::get<value_type>(value_); } 77 const_reference Ref() const override { return absl::get<value_type>(value_); } 78 absl::Status status() const override { 79 return absl::get<absl::Status>(value_); 80 } 81 82 private: 83 absl::variant<value_type, absl::Status> value_; 84 absl::variant<google::protobuf::io::CodedInputStream*, 85 google::protobuf::io::ZeroCopyInputStream*> 86 stream_; 87 }; 88 89 } // namespace kythe 90 91 #endif // KYTHE_CXX_EXTRACTOR_BAZEL_EVENT_READER_H_