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 get()38 T& get() { 39 return fValue; 40 } 41 42 T& operator*() { 43 return fValue; 44 } 45 46 T* operator->() { 47 return &fValue; 48 } 49 get()50 const T& get() const { 51 return fValue; 52 } 53 54 const T& operator*() const { 55 return fValue; 56 } 57 58 const T* operator->() const { 59 return &fValue; 60 } 61 62 DSLWrapper& operator=(const DSLWrapper&) = delete; 63 64 DSLWrapper& operator=(DSLWrapper&& other) { 65 fValue.swap(other.fValue); 66 return *this; 67 } 68 69 private: 70 T fValue; 71 }; 72 73 } // namespace dsl 74 75 } // namespace SkSL 76 77 #endif 78