1 // Copyright 2020 The Dawn 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 COMMON_COREFOUNDATIONREF_H_ 16 #define COMMON_COREFOUNDATIONREF_H_ 17 18 #include "common/RefBase.h" 19 20 #include <CoreFoundation/CoreFoundation.h> 21 22 template <typename T> 23 struct CoreFoundationRefTraits { 24 static constexpr T kNullValue = nullptr; ReferenceCoreFoundationRefTraits25 static void Reference(T value) { 26 CFRetain(value); 27 } ReleaseCoreFoundationRefTraits28 static void Release(T value) { 29 CFRelease(value); 30 } 31 }; 32 33 template <typename T> 34 class CFRef : public RefBase<T, CoreFoundationRefTraits<T>> { 35 public: 36 using RefBase<T, CoreFoundationRefTraits<T>>::RefBase; 37 }; 38 39 template <typename T> AcquireCFRef(T pointee)40CFRef<T> AcquireCFRef(T pointee) { 41 CFRef<T> ref; 42 ref.Acquire(pointee); 43 return ref; 44 } 45 46 #endif // COMMON_COREFOUNDATIONREF_H_ 47