1 // Copyright 2018 Hans Dembinski 2 // 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt 5 // or copy at http://www.boost.org/LICENSE_1_0.txt) 6 7 #ifndef BOOST_HISTOGRAM_UNSAFE_ACCESS_HPP 8 #define BOOST_HISTOGRAM_UNSAFE_ACCESS_HPP 9 10 #include <boost/histogram/detail/axes.hpp> 11 #include <type_traits> 12 13 namespace boost { 14 namespace histogram { 15 16 /** Unsafe read/write access to private data that potentially breaks consistency. 17 18 This struct enables access to private data of some classes. It is intended for library 19 developers who need this to implement algorithms efficiently, for example, 20 serialization. Users should not use this. If you are a user who absolutely needs this to 21 get a specific effect, please submit an issue on Github. Perhaps the public 22 interface is insufficient and should be extended for your use case. 23 24 Unlike the normal interface, the unsafe_access interface may change between versions. 25 If your code relies on unsafe_access, it may or may not break when you update Boost. 26 This is another reason to not use it unless you are ok with these conditions. 27 */ 28 struct unsafe_access { 29 /** 30 Get axes. 31 @param hist histogram. 32 */ 33 template <class Histogram> axesboost::histogram::unsafe_access34 static auto& axes(Histogram& hist) { 35 return hist.axes_; 36 } 37 38 /// @copydoc axes() 39 template <class Histogram> axesboost::histogram::unsafe_access40 static const auto& axes(const Histogram& hist) { 41 return hist.axes_; 42 } 43 44 /** 45 Get mutable axis reference with compile-time number. 46 @param hist histogram. 47 @tparam I axis index (optional, default: 0). 48 */ 49 template <class Histogram, unsigned I = 0> axisboost::histogram::unsafe_access50 static decltype(auto) axis(Histogram& hist, std::integral_constant<unsigned, I> = {}) { 51 assert(I < hist.rank()); 52 return detail::axis_get<I>(hist.axes_); 53 } 54 55 /** 56 Get mutable axis reference with run-time number. 57 @param hist histogram. 58 @param i axis index. 59 */ 60 template <class Histogram> axisboost::histogram::unsafe_access61 static decltype(auto) axis(Histogram& hist, unsigned i) { 62 assert(i < hist.rank()); 63 return detail::axis_get(hist.axes_, i); 64 } 65 66 /** 67 Get storage. 68 @param hist histogram. 69 */ 70 template <class Histogram> storageboost::histogram::unsafe_access71 static auto& storage(Histogram& hist) { 72 return hist.storage_; 73 } 74 75 /// @copydoc storage() 76 template <class Histogram> storageboost::histogram::unsafe_access77 static const auto& storage(const Histogram& hist) { 78 return hist.storage_; 79 } 80 81 /** 82 Get index offset. 83 @param hist histogram 84 */ 85 template <class Histogram> offsetboost::histogram::unsafe_access86 static auto& offset(Histogram& hist) { 87 return hist.offset_; 88 } 89 90 /// @copydoc offset() 91 template <class Histogram> offsetboost::histogram::unsafe_access92 static const auto& offset(const Histogram& hist) { 93 return hist.offset_; 94 } 95 96 /** 97 Get buffer of unlimited_storage. 98 @param storage instance of unlimited_storage. 99 */ 100 template <class Allocator> unlimited_storage_bufferboost::histogram::unsafe_access101 static constexpr auto& unlimited_storage_buffer(unlimited_storage<Allocator>& storage) { 102 return storage.buffer_; 103 } 104 105 /** 106 Get implementation of storage_adaptor. 107 @param storage instance of storage_adaptor. 108 */ 109 template <class T> storage_adaptor_implboost::histogram::unsafe_access110 static constexpr auto& storage_adaptor_impl(storage_adaptor<T>& storage) { 111 return static_cast<typename storage_adaptor<T>::impl_type&>(storage); 112 } 113 }; 114 115 } // namespace histogram 116 } // namespace boost 117 118 #endif 119