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 Block; 20 class SymbolTable; 21 22 namespace dsl { 23 24 class DSLBlock { 25 public: 26 template<class... Statements> DSLBlock(Statements...statements)27 DSLBlock(Statements... statements) { 28 fStatements.reserve_back(sizeof...(statements)); 29 // in C++17, we could just do: 30 // (fStatements.push_back(DSLStatement(statements.release()).release()), ...); 31 int unused[] = 32 {0, 33 (static_cast<void>(fStatements.push_back(DSLStatement(statements.release()).release())), 34 0)...}; 35 static_cast<void>(unused); 36 } 37 38 DSLBlock(DSLBlock&& other) = default; 39 40 DSLBlock(SkSL::StatementArray statements, std::shared_ptr<SymbolTable> symbols = nullptr); 41 42 DSLBlock(SkTArray<DSLStatement> statements, std::shared_ptr<SymbolTable> symbols = nullptr); 43 44 ~DSLBlock(); 45 46 DSLBlock& operator=(DSLBlock&& other) { 47 fStatements = std::move(other.fStatements); 48 return *this; 49 } 50 51 void append(DSLStatement stmt); 52 53 std::unique_ptr<SkSL::Block> release(); 54 55 private: 56 SkSL::StatementArray fStatements; 57 std::shared_ptr<SkSL::SymbolTable> fSymbols; 58 59 friend class DSLStatement; 60 friend class DSLFunction; 61 }; 62 63 } // namespace dsl 64 65 } // namespace SkSL 66 67 #endif 68