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