1 /* 2 * PImpl.hpp 3 * 4 * Copyright (C) 2021 by RStudio, PBC 5 * 6 * Unless you have received this program directly from RStudio pursuant to the terms of a commercial license agreement 7 * with RStudio, then this program is licensed to you under the following terms: 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 10 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the 11 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 18 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 * 22 */ 23 24 #ifndef SHARED_CORE_P_IMPL_HPP 25 #define SHARED_CORE_P_IMPL_HPP 26 27 #include <memory> 28 29 /** 30 * @brief Shared start of the macro to define a private implementation for a class. 31 */ 32 #define PRIVATE_IMPL_START \ 33 struct Impl; 34 35 /** 36 * @brief Macro to define a private implementation for a non-copyable class. 37 * 38 * Class which use this macro must either be non-copyable or define a custom deep-copy constructor and operator which 39 * performs a deep copy of the impl. 40 * 41 * This macro should be included in the private or protected section of a classes declaration. 42 * The PRIVATE_IMPL_DELETER_IMPL macro must be used in the definition file, after the definition of the Impl struct. 43 * struct OwningClass::Impl should be defined in the definition file before defining OwningClass. 44 * 45 * @param in_memberName The name of the private implementation member variable (e.g. m_impl). 46 */ 47 #define PRIVATE_IMPL(in_memberName) \ 48 PRIVATE_IMPL_START \ 49 struct ImplDeleter { void operator()(Impl*); }; \ 50 std::unique_ptr<Impl, ImplDeleter> in_memberName; 51 52 /** 53 * @brief Macro to define a private implementation which would be shared with copied instances. 54 * 55 * This macro should be included in the private or protected section of a classes declaration. 56 * struct OwningClass::Impl should be defined in the definition file before defining OwningClass. 57 * 58 * @param in_memeberName The name of the private implementation member variable (e.g. m_impl). 59 */ 60 #define PRIVATE_IMPL_SHARED(in_memberName) \ 61 PRIVATE_IMPL_START \ 62 std::shared_ptr<Impl> in_memberName; 63 64 /** 65 * @brief Macro which implements the deleter for the class's private implementation. This macro must be used after the 66 * implementation of the Impl struct in the definition file. 67 * 68 * @param in_owningClass The name of the class which owns the private implementation (e.g. Error). 69 */ 70 #define PRIVATE_IMPL_DELETER_IMPL(in_owningClass) \ 71 void in_owningClass::ImplDeleter::operator()(in_owningClass::Impl* io_toDelete) \ 72 { \ 73 delete io_toDelete; \ 74 } 75 76 #endif 77