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_WRAPPER 9 #define SKSL_DSL_WRAPPER 10 11 #include <memory> 12 13 namespace SkSL { 14 15 namespace dsl { 16 17 /** 18 * Several of the DSL classes override operator= in a non-standard fashion to allow for expressions 19 * like "x = 0" to compile into SkSL code. This makes it impossible to directly use these classes in 20 * C++ containers which expect standard behavior for operator=. 21 * 22 * Wrapper<T> contains a T, where T is a DSL class with non-standard operator=, and provides 23 * standard behavior for operator=, permitting it to be used in standard containers. 24 */ 25 template<typename T> 26 class DSLWrapper { 27 public: DSLWrapper(T value)28 DSLWrapper(T value) { 29 fValue.swap(value); 30 } 31 32 DSLWrapper(const DSLWrapper&) = delete; 33 DSLWrapper(DSLWrapper && other)34 DSLWrapper(DSLWrapper&& other) { 35 fValue.swap(other.fValue); 36 } 37 38 T& operator*() { 39 return fValue; 40 } 41 42 T* operator->() { 43 return &fValue; 44 } 45 46 DSLWrapper& operator=(const DSLWrapper&) = delete; 47 48 DSLWrapper& operator=(DSLWrapper&& other) { 49 fValue.swap(other.fValue); 50 return *this; 51 } 52 53 private: 54 T fValue; 55 }; 56 57 } // namespace dsl 58 59 } // namespace SkSL 60 61 #endif 62