Skip to content

Commit b731ef2

Browse files
committed
add unsafe_access and test it
1 parent 2d8a55f commit b731ef2

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

include/boost/histogram/accumulators/mean.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ class mean {
152152
}
153153

154154
private:
155+
friend struct unsafe_access;
156+
155157
internal_data_type data_{0, 0, 0};
156158
};
157159

include/boost/histogram/unsafe_access.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ struct unsafe_access {
110110
static constexpr auto& storage_adaptor_impl(storage_adaptor<T>& storage) {
111111
return static_cast<typename storage_adaptor<T>::impl_type&>(storage);
112112
}
113+
114+
/**
115+
Get internal data of accumulators::mean.
116+
@param obj instance of accumulator.
117+
*/
118+
template <class T>
119+
static constexpr auto& accumulators_mean_impl(T&& m) {
120+
return m.data_;
121+
}
113122
};
114123

115124
} // namespace histogram

test/accumulators_mean_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <boost/core/lightweight_test.hpp>
88
#include <boost/histogram/accumulators/mean.hpp>
99
#include <boost/histogram/accumulators/ostream.hpp>
10+
#include <boost/histogram/unsafe_access.hpp>
1011
#include <boost/histogram/weight.hpp>
1112
#include <sstream>
1213
#include "is_close.hpp"
@@ -110,5 +111,16 @@ int main() {
110111
BOOST_TEST_IS_CLOSE(a.variance(), b.variance(), 1e-3);
111112
}
112113

114+
// unsafe_access
115+
{
116+
m_t a;
117+
a(1);
118+
a(2);
119+
120+
BOOST_TEST_EQ(a.count(), 2);
121+
unsafe_access::accumulators_mean(a).sum_ = 1;
122+
BOOST_TEST_EQ(a.count(), 1);
123+
}
124+
113125
return boost::report_errors();
114126
}

0 commit comments

Comments
 (0)