Wisp 1.3.1 C++ Unit Test Coverage
Current view: top level - ssrc/wisp/utility - make_smart_ptr.h (source / functions) Hit Total Coverage
Test: Wisp 1.3.1 C++ Unit Tests Lines: 4 4 100.0 %
Date: 2017-01-16 Functions: 10 10 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* Copyright 2016, 2017 Savarese Software Research Corporation
       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.savarese.com/software/ApacheLicense-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                 :            : /**
      17                 :            :  * @file
      18                 :            :  * This header defines the make_smart_ptr template function.
      19                 :            :  */
      20                 :            : 
      21                 :            : #ifndef __SSRC_WISP_UTILITY_MAKE_SMART_PTR_H
      22                 :            : #define __SSRC_WISP_UTILITY_MAKE_SMART_PTR_H
      23                 :            : 
      24                 :            : #include <ssrc/wisp-packages.h>
      25                 :            : 
      26                 :            : #include <memory>
      27                 :            : 
      28                 :            : __BEGIN_NS_SSRC_WISP_UTILITY
      29                 :            : 
      30                 :            : template<typename T, typename P>
      31                 :            : struct smart_ptr_factory { };
      32                 :            : 
      33                 :            : template<typename T>
      34                 :            : struct smart_ptr_factory<T, std::shared_ptr<T> > {
      35                 :            :   template<typename... Args>
      36                 :         38 :   inline static std::shared_ptr<T> make(Args && ...args) {
      37                 :         38 :     return std::make_shared<T>(std::forward<Args>(args)...);
      38                 :            :   }
      39                 :            : };
      40                 :            : 
      41                 :            : template<typename T>
      42                 :            : struct smart_ptr_factory<T, std::unique_ptr<T> > {
      43                 :            :   template<typename... Args>
      44                 :            :   inline static std::unique_ptr<T> make(Args && ...args) {
      45                 :            :     return std::make_unique<T>(std::forward<Args>(args)...);
      46                 :            :   }
      47                 :            : };
      48                 :            : 
      49                 :            : template<typename SmartPtr, typename... Args>
      50                 :         38 : inline SmartPtr make_smart_ptr(Args && ...args) {
      51                 :         38 :   return smart_ptr_factory<typename SmartPtr::element_type, SmartPtr>::make(std::forward<Args>(args)...);
      52                 :            : }
      53                 :            : 
      54                 :            : __END_NS_SSRC_WISP_UTILITY
      55                 :            : 
      56                 :            : #endif