1 /* 2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. 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 16 #ifndef HISTREAMER_FOUNDATION_MEMORY_HELPER_H 17 #define HISTREAMER_FOUNDATION_MEMORY_HELPER_H 18 19 #include <memory> 20 21 namespace OHOS { 22 namespace Media { 23 namespace MemoryHelper { 24 template<class T> struct _unique_ptr_if { 25 typedef std::unique_ptr<T> _single_object; 26 }; 27 28 template<class T> struct _unique_ptr_if<T[]> { 29 typedef std::unique_ptr<T[]> _unknown_bound_array; 30 }; 31 32 template<class T, size_t N> struct _unique_ptr_if<T[N]> { 33 typedef void _known_bound_array; // not supported 34 }; 35 36 template<class T, class... U> 37 typename _unique_ptr_if<T>::_single_object make_unique(U&&... params) 38 { 39 return std::unique_ptr<T>(new T(std::forward<U>(params)...)); 40 } 41 42 template<class T> 43 typename _unique_ptr_if<T>::_unknown_bound_array make_unique(size_t n) 44 { 45 typedef typename std::remove_extent<T>::type U; 46 return std::unique_ptr<T>(new U[n]()); 47 } 48 49 template<class T, class... Args> 50 typename _unique_ptr_if<T>::_known_bound_array make_unique(Args&&...) = delete; 51 } 52 } 53 } 54 #endif // HISTREAMER_FOUNDATION_MEMORY_HELPER_H 55