1 /* 2 * Copyright 2021 Google LLC. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SKSL_DSL_BLOCK 9 #define SKSL_DSL_BLOCK 10 11 #include "include/private/SkSLDefines.h" 12 #include "include/sksl/DSLExpression.h" 13 #include "include/sksl/DSLStatement.h" 14 15 #include <memory> 16 17 namespace SkSL { 18 19 class Statement; 20 21 namespace dsl { 22 23 class DSLBlock { 24 public: 25 template<class... Statements> DSLBlock(Statements...statements)26 DSLBlock(Statements... statements) { 27 fStatements.reserve_back(sizeof...(statements)); 28 // in C++17, we could just do: 29 // (fStatements.push_back(DSLStatement(statements.release()).release()), ...); 30 int unused[] = 31 {0, 32 (static_cast<void>(fStatements.push_back(DSLStatement(statements.release()).release())), 33 0)...}; 34 static_cast<void>(unused); 35 } 36 37 DSLBlock(DSLBlock&& other) = default; 38 39 DSLBlock(SkSL::StatementArray statements, std::shared_ptr<SymbolTable> symbols = nullptr); 40 41 DSLBlock(SkTArray<DSLStatement> statements, std::shared_ptr<SymbolTable> symbols = nullptr); 42 43 ~DSLBlock(); 44 45 DSLBlock& operator=(DSLBlock&& other) { 46 fStatements = std::move(other.fStatements); 47 return *this; 48 } 49 50 void append(DSLStatement stmt); 51 52 std::unique_ptr<SkSL::Statement> release(); 53 54 private: 55 SkSL::StatementArray fStatements; 56 std::shared_ptr<SkSL::SymbolTable> fSymbols; 57 58 friend class DSLStatement; 59 friend class DSLFunction; 60 }; 61 62 } // namespace dsl 63 64 } // namespace SkSL 65 66 #endif 67