/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * 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. */ #ifndef THRIFT_CLONEABLE_IOBUF_H_ #define THRIFT_CLONEABLE_IOBUF_H_ #include #include #include namespace apache { namespace thrift { namespace detail { // Implementation of a subset of unique_ptr functionality that also supports // copy constructor template > class CloneableUniquePtr : public std::unique_ptr { public: typedef std::unique_ptr Base; CloneableUniquePtr() : std::unique_ptr() {} explicit CloneableUniquePtr(std::nullptr_t) : std::unique_ptr(nullptr) {} explicit CloneableUniquePtr(typename Base::pointer p) : std::unique_ptr(p) {} template CloneableUniquePtr(typename Base::pointer p, D&& d) : std::unique_ptr(p, std::forward(d)) {} explicit CloneableUniquePtr(Base&& other) : std::unique_ptr(std::move(other)) {} explicit CloneableUniquePtr(CloneableUniquePtr&& other) noexcept : std::unique_ptr(std::move(other)) {} explicit CloneableUniquePtr(const Base& other) : std::unique_ptr(other ? other->clone() : nullptr) {} CloneableUniquePtr(const CloneableUniquePtr& other) : std::unique_ptr(other ? other->clone() : nullptr) {} CloneableUniquePtr& operator=(CloneableUniquePtr&& other) { Base::operator=(std::move(other)); return *this; } CloneableUniquePtr& operator=(Base&& other) { Base::operator=(std::move(other)); return *this; } CloneableUniquePtr& operator=(std::nullptr_t) { Base::operator=(nullptr); return *this; } CloneableUniquePtr& operator=(const CloneableUniquePtr& other) { Base::operator=(other ? other->clone() : nullptr); return *this; } CloneableUniquePtr& operator=(const Base& other) { Base::operator=(other ? other->clone() : nullptr); return *this; } }; template void swap( CloneableUniquePtr& lhs, CloneableUniquePtr& rhs) { using base = std::unique_ptr; return swap(static_cast(lhs), static_cast(rhs)); } } // namespace detail typedef apache::thrift::detail::CloneableUniquePtr CloneableIOBuf; } // namespace thrift } // namespace apache #endif // #ifndef THRIFT_CLONEABLE_IOBUF_H_