Serenity Operating System
at hosted 112 lines 4.0 kB view raw
1/* 2 * Copyright (C) 2016 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#pragma once 27 28#include "Assertions.h" 29#include "OwnPtr.h" 30#include "StdLibExtras.h" 31 32namespace AK { 33 34template<typename> 35class Function; 36 37template<typename Out, typename... In> 38class Function<Out(In...)> { 39public: 40 Function() = default; 41 Function(std::nullptr_t) {} 42 43 template<typename CallableType, class = typename EnableIf<!(IsPointer<CallableType>::value && IsFunction<typename RemovePointer<CallableType>::Type>::value) && IsRvalueReference<CallableType&&>::value>::Type> 44 Function(CallableType&& callable) 45 : m_callable_wrapper(make<CallableWrapper<CallableType>>(move(callable))) 46 { 47 } 48 49 template<typename FunctionType, class = typename EnableIf<IsPointer<FunctionType>::value && IsFunction<typename RemovePointer<FunctionType>::Type>::value>::Type> 50 Function(FunctionType f) 51 : m_callable_wrapper(make<CallableWrapper<FunctionType>>(move(f))) 52 { 53 } 54 55 Out operator()(In... in) const 56 { 57 ASSERT(m_callable_wrapper); 58 return m_callable_wrapper->call(forward<In>(in)...); 59 } 60 61 explicit operator bool() const { return !!m_callable_wrapper; } 62 63 template<typename CallableType, class = typename EnableIf<!(IsPointer<CallableType>::value && IsFunction<typename RemovePointer<CallableType>::Type>::value) && IsRvalueReference<CallableType&&>::value>::Type> 64 Function& operator=(CallableType&& callable) 65 { 66 m_callable_wrapper = make<CallableWrapper<CallableType>>(move(callable)); 67 return *this; 68 } 69 70 template<typename FunctionType, class = typename EnableIf<IsPointer<FunctionType>::value && IsFunction<typename RemovePointer<FunctionType>::Type>::value>::Type> 71 Function& operator=(FunctionType f) 72 { 73 m_callable_wrapper = make<CallableWrapper<FunctionType>>(move(f)); 74 return *this; 75 } 76 77 Function& operator=(std::nullptr_t) 78 { 79 m_callable_wrapper = nullptr; 80 return *this; 81 } 82 83private: 84 class CallableWrapperBase { 85 public: 86 virtual ~CallableWrapperBase() {} 87 virtual Out call(In...) const = 0; 88 }; 89 90 template<typename CallableType> 91 class CallableWrapper final : public CallableWrapperBase { 92 public: 93 explicit CallableWrapper(CallableType&& callable) 94 : m_callable(move(callable)) 95 { 96 } 97 98 CallableWrapper(const CallableWrapper&) = delete; 99 CallableWrapper& operator=(const CallableWrapper&) = delete; 100 101 Out call(In... in) const final override { return m_callable(forward<In>(in)...); } 102 103 private: 104 CallableType m_callable; 105 }; 106 107 OwnPtr<CallableWrapperBase> m_callable_wrapper; 108}; 109 110} 111 112using AK::Function;