add unsafe_access and test it

This commit is contained in:
Hans Dembinski 2021-04-27 13:20:00 +02:00
parent 2d8a55f80e
commit b731ef252c
3 changed files with 23 additions and 0 deletions

View File

@ -152,6 +152,8 @@ public:
} }
private: private:
friend struct unsafe_access;
internal_data_type data_{0, 0, 0}; internal_data_type data_{0, 0, 0};
}; };

View File

@ -110,6 +110,15 @@ struct unsafe_access {
static constexpr auto& storage_adaptor_impl(storage_adaptor<T>& storage) { static constexpr auto& storage_adaptor_impl(storage_adaptor<T>& storage) {
return static_cast<typename storage_adaptor<T>::impl_type&>(storage); return static_cast<typename storage_adaptor<T>::impl_type&>(storage);
} }
/**
Get internal data of accumulators::mean.
@param obj instance of accumulator.
*/
template <class T>
static constexpr auto& accumulators_mean_impl(T&& m) {
return m.data_;
}
}; };
} // namespace histogram } // namespace histogram

View File

@ -7,6 +7,7 @@
#include <boost/core/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <boost/histogram/accumulators/mean.hpp> #include <boost/histogram/accumulators/mean.hpp>
#include <boost/histogram/accumulators/ostream.hpp> #include <boost/histogram/accumulators/ostream.hpp>
#include <boost/histogram/unsafe_access.hpp>
#include <boost/histogram/weight.hpp> #include <boost/histogram/weight.hpp>
#include <sstream> #include <sstream>
#include "is_close.hpp" #include "is_close.hpp"
@ -110,5 +111,16 @@ int main() {
BOOST_TEST_IS_CLOSE(a.variance(), b.variance(), 1e-3); BOOST_TEST_IS_CLOSE(a.variance(), b.variance(), 1e-3);
} }
// unsafe_access
{
m_t a;
a(1);
a(2);
BOOST_TEST_EQ(a.count(), 2);
unsafe_access::accumulators_mean(a).sum_ = 1;
BOOST_TEST_EQ(a.count(), 1);
}
return boost::report_errors(); return boost::report_errors();
} }