Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:11

0001 // Copyright 2019 Hans Dembinski
0002 //
0003 // Distributed under the Boost Software License, Version 1.0.
0004 // (See accompanying file LICENSE_1_0.txt
0005 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef BOOST_HISTOGRAM_DETAIL_ARRAY_WRAPPER_HPP
0008 #define BOOST_HISTOGRAM_DETAIL_ARRAY_WRAPPER_HPP
0009 
0010 #include <boost/core/make_span.hpp>
0011 #include <boost/core/nvp.hpp>
0012 #include <boost/histogram/detail/static_if.hpp>
0013 #include <boost/mp11/function.hpp>
0014 #include <boost/mp11/utility.hpp>
0015 #include <type_traits>
0016 
0017 namespace boost {
0018 namespace histogram {
0019 namespace detail {
0020 
0021 template <class T, class = decltype(&T::template save_array<int>)>
0022 struct has_save_array_impl;
0023 
0024 template <class T, class = decltype(&T::template load_array<int>)>
0025 struct has_load_array_impl;
0026 
0027 template <class T>
0028 using has_array_optimization = mp11::mp_or<mp11::mp_valid<has_save_array_impl, T>,
0029                                            mp11::mp_valid<has_load_array_impl, T>>;
0030 
0031 template <class T>
0032 struct array_wrapper {
0033   using pointer = T*;
0034 
0035   pointer ptr;
0036   std::size_t size;
0037 
0038   template <class Archive>
0039   void serialize(Archive& ar, unsigned /* version */) {
0040     static_if_c<(has_array_optimization<Archive>::value &&
0041                  std::is_trivially_copyable<T>::value)>(
0042         [this](auto& ar) {
0043           // cannot use and therefore bypass save_array / load_array interface, because
0044           // it requires exact type boost::serialization::array_wrapper<T>
0045           static_if_c<Archive::is_loading::value>(
0046               [this](auto& ar) { ar.load_binary(this->ptr, sizeof(T) * this->size); },
0047               [this](auto& ar) { ar.save_binary(this->ptr, sizeof(T) * this->size); },
0048               ar);
0049         },
0050         [this](auto& ar) {
0051           for (auto&& x : make_span(this->ptr, this->size)) ar& make_nvp("item", x);
0052         },
0053         ar);
0054   }
0055 };
0056 
0057 template <class T>
0058 auto make_array_wrapper(T* t, std::size_t s) {
0059   return array_wrapper<T>{t, s};
0060 }
0061 
0062 } // namespace detail
0063 } // namespace histogram
0064 } // namespace boost
0065 
0066 #endif