1 /* 2 * Copyright (c) Facebook, Inc. and its affiliates. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <exception> 20 21 namespace folly { 22 namespace detail { 23 24 // thunk 25 // 26 // A carefully curated collection of generic general-purpose thunk templates: 27 // * make: operator new with given arguments 28 // * ruin: operator delete 29 // * ctor: in-place constructor with given arguments 30 // * dtor: in-place destructor 31 // * noop: no-op function with the given arguments 32 // * fail: terminating function with the given return and arguments 33 struct thunk { 34 template <typename T, typename... A> makethunk35 static void* make(A... a) { 36 return new T(static_cast<A>(a)...); 37 } 38 template <typename T> ruinthunk39 static void ruin(void* const ptr) noexcept { 40 delete static_cast<T*>(ptr); 41 } 42 43 template <typename T, typename... A> ctorthunk44 static void ctor(void* const ptr, A... a) { 45 ::new (ptr) T(static_cast<A>(a)...); 46 } 47 template <typename T> dtorthunk48 static void dtor(void* const ptr) noexcept { 49 static_cast<T*>(ptr)->~T(); 50 } 51 52 template <typename... A> noopthunk53 static void noop(A...) noexcept {} 54 55 template <typename R, typename... A> failthunk56 static R fail(A...) noexcept { 57 std::terminate(); 58 } 59 }; 60 61 } // namespace detail 62 } // namespace folly 63