kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/bazel_artifact_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_ARTIFACT_READER_H_ 17 #define KYTHE_CXX_EXTRACTOR_BAZEL_ARTIFACT_READER_H_ 18 19 #include <functional> 20 #include <string> 21 #include <vector> 22 23 #include "absl/container/flat_hash_map.h" 24 #include "absl/container/flat_hash_set.h" 25 #include "absl/log/die_if_null.h" 26 #include "absl/status/status.h" 27 #include "absl/types/variant.h" 28 #include "kythe/cxx/extractor/bazel_artifact.h" 29 #include "kythe/cxx/extractor/bazel_artifact_selector.h" 30 #include "kythe/cxx/extractor/bazel_event_reader.h" 31 #include "third_party/bazel/src/main/java/com/google/devtools/build/lib/buildeventstream/proto/build_event_stream.pb.h" 32 33 namespace kythe { 34 35 /// \brief A cursor which selects and filters extraction artifacts from an 36 /// underlying Bazel BuildEvent stream. 37 class BazelArtifactReader { 38 public: 39 using value_type = BazelArtifact; 40 using reference = value_type&; 41 using const_reference = const value_type&; 42 43 /// \brief Returns a list of default BazelArtifactSelectors consisting of a 44 /// default-constructed ExtraActionSelector. 45 static std::vector<AnyArtifactSelector> DefaultSelectors(); 46 47 /// \brief Constructs a new BazelArtifactSelector over the provided event 48 /// stream. 49 explicit BazelArtifactReader( 50 BazelEventReaderInterface* event_reader, 51 std::vector<AnyArtifactSelector> selectors = DefaultSelectors()) 52 : event_reader_(ABSL_DIE_IF_NULL(event_reader)), 53 selectors_(std::move(selectors)) { 54 Select(); 55 } 56 57 /// \brief Advances the event stream until the next artifact is found. 58 /// Invokes all selectors on sequential events from the stream until at least 59 /// one selector signals success. 60 void Next(); 61 62 /// \brief Returns true if the underlying stream is exahusted and there are 63 /// no more artifacts to find. 64 bool Done() const { return value_.index() != 0; } 65 66 /// \brief Returns a reference to the most recently selected artifact. 67 /// Requires: Done() == false. 68 reference Ref() { return absl::get<value_type>(value_); } 69 const_reference Ref() const { return absl::get<value_type>(value_); } 70 71 /// \brief Returns the overall status of the stream. 72 /// Requires: Done() == true. 73 absl::Status status() const { return absl::get<absl::Status>(value_); } 74 75 private: 76 void Select(); 77 78 absl::variant<value_type, absl::Status> value_; 79 BazelEventReaderInterface* event_reader_; 80 std::vector<AnyArtifactSelector> selectors_; 81 }; 82 83 } // namespace kythe 84 85 #endif // KYTHE_CXX_EXTRACTOR_BAZEL_ARTIFACT_READER_H_