1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 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 TENSORFLOW_STREAM_EXECUTOR_LIB_PTR_UTIL_H_ 17 #define TENSORFLOW_STREAM_EXECUTOR_LIB_PTR_UTIL_H_ 18 19 #include <memory> 20 21 namespace perftools { 22 namespace gputools { 23 namespace port { 24 25 // Trait to select overloads and return types for MakeUnique. 26 template <typename T> 27 struct MakeUniqueResult { 28 using scalar = std::unique_ptr<T>; 29 }; 30 template <typename T> 31 struct MakeUniqueResult<T[]> { 32 using array = std::unique_ptr<T[]>; 33 }; 34 template <typename T, size_t N> 35 struct MakeUniqueResult<T[N]> { 36 using invalid = void; 37 }; 38 39 // MakeUnique<T>(...) is an early implementation of C++14 std::make_unique. 40 // It is designed to be 100% compatible with std::make_unique so that the 41 // eventual switchover will be a simple renaming operation. 42 template <typename T, typename... Args> 43 typename MakeUniqueResult<T>::scalar MakeUnique(Args&&... args) { // NOLINT 44 return std::unique_ptr<T>( 45 new T(std::forward<Args>(args)...)); // NOLINT(build/c++11) 46 } 47 48 // Overload for array of unknown bound. 49 // The allocation of arrays needs to use the array form of new, 50 // and cannot take element constructor arguments. 51 template <typename T> 52 typename MakeUniqueResult<T>::array MakeUnique(size_t n) { 53 return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]()); 54 } 55 56 // Reject arrays of known bound. 57 template <typename T, typename... Args> 58 typename MakeUniqueResult<T>::invalid MakeUnique(Args&&... /* args */) = 59 delete; // NOLINT 60 61 } // namespace port 62 } // namespace gputools 63 } // namespace perftools 64 65 66 #endif // TENSORFLOW_STREAM_EXECUTOR_LIB_PTR_UTIL_H_ 67