/* * 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. */ #pragma once #include #include #include #include #include namespace apache { namespace thrift { namespace op { namespace detail { template void assertSameType() { static_assert( std::is_same_v, folly::remove_cvref_t>, "src and dst have different types."); } template void copy_field_ref(SrcRef src_ref, DstRef dst_ref) { assertSameType(); dst_ref.copy_from(src_ref); } // TODO: support adapted field, smart pointers with custom allocators, and union // We only support l-value reference for dst. struct Copy { template void operator()(field_ref src_ref, field_ref dst_ref) const { copy_field_ref(src_ref, dst_ref); } template void operator()( required_field_ref src_ref, required_field_ref dst_ref) const { copy_field_ref(src_ref, dst_ref); } template void operator()( optional_field_ref src_ref, optional_field_ref dst_ref) const { copy_field_ref(src_ref, dst_ref); } template void operator()( optional_boxed_field_ref src_ref, optional_boxed_field_ref dst_ref) const { copy_field_ref(src_ref, dst_ref); } template void operator()( terse_field_ref src_ref, terse_field_ref dst_ref) const { copy_field_ref(src_ref, dst_ref); } template void operator()( const std::optional& src_opt, std::optional& dst_opt) const { dst_opt = src_opt; } // Copy on unique pointer constructs a new ptr if src is not nullptr. // Copy on shared pointer just shares the same ptr. // This is consistent with the copy constructor of thrift struct. template void operator()( const std::unique_ptr& src_ptr, std::unique_ptr& dst_ptr) const { dst_ptr = !src_ptr ? nullptr : std::make_unique(*src_ptr); } template void operator()( const std::shared_ptr& src_ptr, std::shared_ptr& dst_ptr) const { dst_ptr = src_ptr; } }; } // namespace detail } // namespace op } // namespace thrift } // namespace apache