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_READER_WGSL_PARSER_IMPL_DETAIL_H_ 16 #define SRC_READER_WGSL_PARSER_IMPL_DETAIL_H_ 17 18 #include <memory> 19 20 namespace tint { 21 namespace reader { 22 namespace wgsl { 23 namespace detail { 24 25 /// OperatorArrow is a traits helper for ParserImpl::Expect<T>::operator->() and 26 /// ParserImpl::Maybe<T>::operator->() so that pointer types are automatically 27 /// dereferenced. This simplifies usage by allowing 28 /// `result.value->field` 29 /// to be written as: 30 /// `result->field` 31 /// As well as reducing the amount of code, using the operator->() asserts that 32 /// the Expect<T> or Maybe<T> is not in an error state before dereferencing. 33 template <typename T> 34 struct OperatorArrow { 35 /// type resolves to the return type for the operator->() 36 using type = T*; 37 /// @param val the value held by `ParserImpl::Expect<T>` or 38 /// `ParserImpl::Maybe<T>`. 39 /// @return a pointer to `val` ptrOperatorArrow40 static inline T* ptr(T& val) { return &val; } 41 }; 42 43 /// OperatorArrow template specialization for std::unique_ptr<>. 44 template <typename T> 45 struct OperatorArrow<std::unique_ptr<T>> { 46 /// type resolves to the return type for the operator->() 47 using type = T*; 48 /// @param val the value held by `ParserImpl::Expect<T>` or 49 /// `ParserImpl::Maybe<T>`. 50 /// @return the raw pointer held by `val`. 51 static inline T* ptr(std::unique_ptr<T>& val) { return val.get(); } 52 }; 53 54 /// OperatorArrow template specialization for T*. 55 template <typename T> 56 struct OperatorArrow<T*> { 57 /// type resolves to the return type for the operator->() 58 using type = T*; 59 /// @param val the value held by `ParserImpl::Expect<T>` or 60 /// `ParserImpl::Maybe<T>`. 61 /// @return `val`. 62 static inline T* ptr(T* val) { return val; } 63 }; 64 65 } // namespace detail 66 } // namespace wgsl 67 } // namespace reader 68 } // namespace tint 69 70 #endif // SRC_READER_WGSL_PARSER_IMPL_DETAIL_H_ 71