Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:27:07

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 #pragma once
0019 
0020 #include <memory>
0021 #include <type_traits>
0022 #include <utility>
0023 
0024 namespace arrow {
0025 namespace internal {
0026 
0027 template <typename OutputType, typename InputType>
0028 inline OutputType checked_cast(InputType&& value) {
0029   static_assert(std::is_class<typename std::remove_pointer<
0030                     typename std::remove_reference<InputType>::type>::type>::value,
0031                 "checked_cast input type must be a class");
0032   static_assert(std::is_class<typename std::remove_pointer<
0033                     typename std::remove_reference<OutputType>::type>::type>::value,
0034                 "checked_cast output type must be a class");
0035 #ifdef NDEBUG
0036   return static_cast<OutputType>(value);
0037 #else
0038   return dynamic_cast<OutputType>(value);
0039 #endif
0040 }
0041 
0042 template <class T, class U>
0043 std::shared_ptr<T> checked_pointer_cast(std::shared_ptr<U> r) noexcept {
0044 #ifdef NDEBUG
0045   return std::static_pointer_cast<T>(std::move(r));
0046 #else
0047   return std::dynamic_pointer_cast<T>(std::move(r));
0048 #endif
0049 }
0050 
0051 template <class T, class U>
0052 std::unique_ptr<T> checked_pointer_cast(std::unique_ptr<U> r) noexcept {
0053 #ifdef NDEBUG
0054   return std::unique_ptr<T>(static_cast<T*>(r.release()));
0055 #else
0056   return std::unique_ptr<T>(dynamic_cast<T*>(r.release()));
0057 #endif
0058 }
0059 
0060 }  // namespace internal
0061 }  // namespace arrow