From 85575856cb16733e8a511c5e7bcfe8f013e1de5a Mon Sep 17 00:00:00 2001 From: =?utf8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=91=D0=B0=D1=80?= =?utf8?q?=D0=B0=D0=BD=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2/AI=20Tools=20Lab=20/S?= =?utf8?q?RR/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 27 May 2019 07:05:43 +0300 Subject: [PATCH] Overload cpp14::make_unique to support arrays (#5268) * Overload cpp14::make_unique to support arrays Extend own implementation of make_unique to support array construction. Signed-off-by: Sergei Barannikov * Add copyright information. --- libs/cpp14/include/cpp14/memory.h | 53 +++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/libs/cpp14/include/cpp14/memory.h b/libs/cpp14/include/cpp14/memory.h index b3e678b..7070e1c 100644 --- a/libs/cpp14/include/cpp14/memory.h +++ b/libs/cpp14/include/cpp14/memory.h @@ -1,7 +1,24 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /** * @file memory.h * @ingroup COM_AI_RUNTIME * @brief This file contains @c make_unique which is not supported by C++11 + * @details Implementation is based on http://isocpp.org/files/papers/N3656.txt */ #ifndef __NNFW_CPP14_MEMORY_H__ #define __NNFW_CPP14_MEMORY_H__ @@ -12,18 +29,38 @@ namespace nnfw { namespace cpp14 { -/** - * @brief Provide @c make_unique function supported from C++14 - * @param[in] args List of arguments with which an instance of T will be constructed. - * @return @c std::unique_ptr of an instance of type T - */ -template std::unique_ptr make_unique(Args &&... args) + +template struct _Unique_if +{ + typedef std::unique_ptr _Single_object; +}; + +template struct _Unique_if +{ + typedef std::unique_ptr _Unknown_bound; +}; + +template struct _Unique_if +{ + typedef void _Known_bound; +}; + +template +typename _Unique_if::_Single_object make_unique(Args &&... args) { - // NOTE std::make_unique is missing in C++11 standard return std::unique_ptr(new T(std::forward(args)...)); } -} // napesapce cpp14 +template typename _Unique_if::_Unknown_bound make_unique(size_t n) +{ + typedef typename std::remove_extent::type U; + return std::unique_ptr(new U[n]()); +} + +template +typename _Unique_if::_Known_bound make_unique(Args &&...) = delete; + +} // namespace cpp14 } // namespace nnfw #endif // __NNFW_CPP14_MEMORY_H__ -- 2.7.4