1 // Copyright 2020 The Tint Authors. 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 SRC_TRANSFORM_MANAGER_H_ 16 #define SRC_TRANSFORM_MANAGER_H_ 17 18 #include <memory> 19 #include <utility> 20 #include <vector> 21 22 #include "src/transform/transform.h" 23 24 namespace tint { 25 namespace transform { 26 27 /// A collection of Transforms that act as a single Transform. 28 /// The inner transforms will execute in the appended order. 29 /// If any inner transform fails the manager will return immediately and 30 /// the error can be retrieved with the Output's diagnostics. 31 class Manager : public Castable<Manager, Transform> { 32 public: 33 /// Constructor 34 Manager(); 35 ~Manager() override; 36 37 /// Add pass to the manager 38 /// @param transform the transform to append append(std::unique_ptr<Transform> transform)39 void append(std::unique_ptr<Transform> transform) { 40 transforms_.push_back(std::move(transform)); 41 } 42 43 /// Add pass to the manager of type `T`, constructed with the provided 44 /// arguments. 45 /// @param args the arguments to forward to the `T` constructor 46 template <typename T, typename... ARGS> Add(ARGS &&...args)47 void Add(ARGS&&... args) { 48 transforms_.emplace_back(std::make_unique<T>(std::forward<ARGS>(args)...)); 49 } 50 51 /// Runs the transforms on `program`, returning the transformation result. 52 /// @param program the source program to transform 53 /// @param data optional extra transform-specific input data 54 /// @returns the transformed program and diagnostics 55 Output Run(const Program* program, const DataMap& data = {}) override; 56 57 private: 58 std::vector<std::unique_ptr<Transform>> transforms_; 59 }; 60 61 } // namespace transform 62 } // namespace tint 63 64 #endif // SRC_TRANSFORM_MANAGER_H_ 65