Point Cloud Library (PCL)  1.12.0-dev
memory.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2019-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #pragma once
38 
39 /**
40  * \file pcl/memory.h
41  *
42  * \brief Defines functions, macros and traits for allocating and using memory.
43  * \ingroup common
44  */
45 
46 #include <pcl/type_traits.h> // for has_custom_allocator
47 
48 #include <Eigen/Core> // for EIGEN_MAKE_ALIGNED_OPERATOR_NEW
49 
50 #include <memory> // for std::allocate_shared, std::dynamic_pointer_cast, std::make_shared, std::shared_ptr, std::static_pointer_cast, std::weak_ptr
51 #include <type_traits> // for std::enable_if_t, std::false_type, std::true_type
52 #include <utility> // for std::forward
53 
54 /**
55  * \brief Macro to signal a class requires a custom allocator
56  *
57  * It's an implementation detail to have pcl::has_custom_allocator work, a
58  * thin wrapper over Eigen's own macro
59  *
60  * \see pcl::has_custom_allocator, pcl::make_shared
61  * \ingroup common
62  */
63 #define PCL_MAKE_ALIGNED_OPERATOR_NEW \
64  EIGEN_MAKE_ALIGNED_OPERATOR_NEW \
65  using _custom_allocator_type_trait = void;
66 
67 
68 namespace pcl
69 {
70 /**
71  * \brief Force ADL for `shared_ptr`
72  *
73  * For ease of switching from boost::shared_ptr to std::shared_ptr
74  *
75  * \see pcl::make_shared
76  */
77 using std::shared_ptr;
78 
79 /**
80  * \brief Force ADL for `weak_ptr`
81  *
82  * For ease of switching from boost::weak_ptr to std::weak_ptr
83  */
84 using std::weak_ptr;
85 
86 /** ADL doesn't work until C++20 for dynamic_pointer_cast since it requires an explicit Tparam */
87 using std::dynamic_pointer_cast;
88 
89 /** ADL doesn't work until C++20 for static_pointer_cast since it requires an explicit Tparam */
90 using std::static_pointer_cast;
91 
92 #ifdef DOXYGEN_ONLY
93 
94 /**
95  * \brief Returns a pcl::shared_ptr compliant with type T's allocation policy.
96  *
97  * std::allocate_shared or std::make_shared will be invoked in case T has or
98  * doesn't have a custom allocator, respectively.
99  *
100  * \note In MSVC < 1915 (before version 15.8) alignment was incorrectly set at
101  * most at alignof(max_align_t). This bug was fixed in said version and is
102  * acknowledged by defining _ENABLE_EXTENDED_ALIGNED_STORAGE. See #3752.
103  *
104  * \see pcl::has_custom_allocator, PCL_MAKE_ALIGNED_OPERATOR_NEW
105  * \tparam T Type of the object to create a pcl::shared_ptr of
106  * \tparam Args Types for the arguments to pcl::make_shared
107  * \param args List of arguments with which an instance of T will be constructed
108  * \return pcl::shared_ptr of an instance of type T
109  */
110 template<typename T, typename ... Args>
111 shared_ptr<T> make_shared(Args&&... args);
112 
113 #else
114 
115 template<typename T, typename ... Args>
116 std::enable_if_t<has_custom_allocator<T>::value, shared_ptr<T>> make_shared(Args&&... args)
117 {
118  return std::allocate_shared<T>(Eigen::aligned_allocator<T>(), std::forward<Args> (args)...);
119 }
120 
121 template<typename T, typename ... Args>
122 std::enable_if_t<!has_custom_allocator<T>::value, shared_ptr<T>> make_shared(Args&&... args)
123 {
124  return std::make_shared<T>(std::forward<Args> (args)...);
125 }
126 
127 #endif
128 }
pcl
Definition: convolution.h:46
pcl::make_shared
shared_ptr< T > make_shared(Args &&... args)
Returns a pcl::shared_ptr compliant with type T's allocation policy.