unsafe_access.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifndef BOOST_HISTOGRAM_UNSAFE_ACCESS_HPP
  7. #define BOOST_HISTOGRAM_UNSAFE_ACCESS_HPP
  8. #include <boost/histogram/detail/axes.hpp>
  9. #include <type_traits>
  10. namespace boost {
  11. namespace histogram {
  12. /** Unsafe read/write access to private data that potentially breaks consistency.
  13. This struct enables access to private data of some classes. It is intended for library
  14. developers who need this to implement algorithms efficiently, for example,
  15. serialization. Users should not use this. If you are a user who absolutely needs this to
  16. get a specific effect, please submit an issue on Github. Perhaps the public
  17. interface is insufficient and should be extended for your use case.
  18. Unlike the normal interface, the unsafe_access interface may change between versions.
  19. If your code relies on unsafe_access, it may or may not break when you update Boost.
  20. This is another reason to not use it unless you are ok with these conditions.
  21. */
  22. struct unsafe_access {
  23. /**
  24. Get axes.
  25. @param hist histogram.
  26. */
  27. template <class Histogram>
  28. static auto& axes(Histogram& hist) {
  29. return hist.axes_;
  30. }
  31. /// @copydoc axes()
  32. template <class Histogram>
  33. static const auto& axes(const Histogram& hist) {
  34. return hist.axes_;
  35. }
  36. /**
  37. Get mutable axis reference with compile-time number.
  38. @param hist histogram.
  39. @tparam I axis index (optional, default: 0).
  40. */
  41. template <class Histogram, unsigned I = 0>
  42. static decltype(auto) axis(Histogram& hist, std::integral_constant<unsigned, I> = {}) {
  43. assert(I < hist.rank());
  44. return detail::axis_get<I>(hist.axes_);
  45. }
  46. /**
  47. Get mutable axis reference with run-time number.
  48. @param hist histogram.
  49. @param i axis index.
  50. */
  51. template <class Histogram>
  52. static decltype(auto) axis(Histogram& hist, unsigned i) {
  53. assert(i < hist.rank());
  54. return detail::axis_get(hist.axes_, i);
  55. }
  56. /**
  57. Get storage.
  58. @param hist histogram.
  59. */
  60. template <class Histogram>
  61. static auto& storage(Histogram& hist) {
  62. return hist.storage_;
  63. }
  64. /// @copydoc storage()
  65. template <class Histogram>
  66. static const auto& storage(const Histogram& hist) {
  67. return hist.storage_;
  68. }
  69. /**
  70. Get index offset.
  71. @param hist histogram
  72. */
  73. template <class Histogram>
  74. static auto& offset(Histogram& hist) {
  75. return hist.offset_;
  76. }
  77. /// @copydoc offset()
  78. template <class Histogram>
  79. static const auto& offset(const Histogram& hist) {
  80. return hist.offset_;
  81. }
  82. /**
  83. Get buffer of unlimited_storage.
  84. @param storage instance of unlimited_storage.
  85. */
  86. template <class Allocator>
  87. static constexpr auto& unlimited_storage_buffer(unlimited_storage<Allocator>& storage) {
  88. return storage.buffer_;
  89. }
  90. /**
  91. Get implementation of storage_adaptor.
  92. @param storage instance of storage_adaptor.
  93. */
  94. template <class T>
  95. static constexpr auto& storage_adaptor_impl(storage_adaptor<T>& storage) {
  96. return static_cast<typename storage_adaptor<T>::impl_type&>(storage);
  97. }
  98. };
  99. } // namespace histogram
  100. } // namespace boost
  101. #endif