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_IOKITREF_H_ 16 #define COMMON_IOKITREF_H_ 17 18 #include "common/RefBase.h" 19 20 #include <IOKit/IOKitLib.h> 21 22 template <typename T> 23 struct IOKitRefTraits { 24 static constexpr T kNullValue = IO_OBJECT_NULL; ReferenceIOKitRefTraits25 static void Reference(T value) { 26 IOObjectRetain(value); 27 } ReleaseIOKitRefTraits28 static void Release(T value) { 29 IOObjectRelease(value); 30 } 31 }; 32 33 template <typename T> 34 class IORef : public RefBase<T, IOKitRefTraits<T>> { 35 public: 36 using RefBase<T, IOKitRefTraits<T>>::RefBase; 37 }; 38 39 template <typename T> AcquireIORef(T pointee)40IORef<T> AcquireIORef(T pointee) { 41 IORef<T> ref; 42 ref.Acquire(pointee); 43 return ref; 44 } 45 46 #endif // COMMON_IOKITREF_H_ 47