Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Implement a member-wise arithmetic and logical operations for KisMpl
Dmitry Kazakov6571a6d4f099
diff --git a/libs/global/KisMpl.h b/libs/global/KisMpl.hindex f00393706e..b85d7b78bf 100644--- a/libs/global/KisMpl.h+++ b/libs/global/KisMpl.h@@ -163,21 +163,21 @@ template<typename Op, typename Class, typename MemType, typename PtrType>struct mem_compare{template <typename Object>- bool operator() (Object &&lhs, Object &&rhs) const {+ auto operator() (Object &&lhs, Object &&rhs) const {Op op;return op(std::invoke(ptr, std::forward<Object>(lhs)),std::invoke(ptr, std::forward<Object>(rhs)));}template <typename Object>- bool operator() (Object &&lhs, const MemType &rhs) const {+ auto operator() (Object &&lhs, const MemType &rhs) const {Op op;return op(std::invoke(ptr, std::forward<Object>(lhs)),rhs);}template <typename Object>- bool operator() (const MemType &lhs, Object &&rhs) const {+ auto operator() (const MemType &lhs, Object &&rhs) const {Op op;return op(lhs,std::invoke(ptr, std::forward<Object>(rhs)));@@ -580,6 +580,111 @@ inline auto mem_greater_equal(MemType (Class::*ptr)() const noexcept) {return detail::mem_compare<std::greater_equal<>, Class, MemType, decltype(ptr)>{ptr};}+/**+ * @brief mem_bit_or is a binary functor that applies a bitwise-or operator to member of+ * an object and a given value or two members of two objects. The function+ * is supposed to be used with standard algorithms like std::accumulate.+ */+template<typename Class, typename MemType>+inline auto mem_bit_or(MemType Class::*ptr) {+ return detail::mem_compare<std::bit_or<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++template<typename Class, typename MemType>+inline auto mem_bit_or(MemType (Class::*ptr)() const) {+ return detail::mem_compare<std::bit_or<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++/**+ * @brief mem_bit_and is a binary functor that applies a bitwise-and operator to member of+ * an object and a given value or two members of two objects. The function+ * is supposed to be used with standard algorithms like std::accumulate.+ */+template<typename Class, typename MemType>+inline auto mem_bit_and(MemType Class::*ptr) {+ return detail::mem_compare<std::bit_and<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++template<typename Class, typename MemType>+inline auto mem_bit_and(MemType (Class::*ptr)()) {+ return detail::mem_compare<std::bit_and<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++/**+ * @brief mem_bit_xor is a binary functor that applies a bitwise-xor operator to member of+ * an object and a given value or two members of two objects. The function+ * is supposed to be used with standard algorithms like std::accumulate.+ */+template<typename Class, typename MemType>+inline auto mem_bit_xor(MemType Class::*ptr) {+ return detail::mem_compare<std::bit_xor<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++template<typename Class, typename MemType>+inline auto mem_bit_xor(MemType (Class::*ptr)()) {+ return detail::mem_compare<std::bit_xor<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++/**+ * @brief mem_plus is a binary functor that applies a plus operator to member of+ * an object and a given value or two members of two objects. The function+ * is supposed to be used with standard algorithms like std::accumulate.+ */+template<typename Class, typename MemType>+inline auto mem_plus(MemType Class::*ptr) {+ return detail::mem_compare<std::plus<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++template<typename Class, typename MemType>+inline auto mem_plus(MemType (Class::*ptr)()) {+ return detail::mem_compare<std::plus<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++/**+ * @brief mem_minus is a binary functor that applies a minus operator to member of+ * an object and a given value or two members of two objects. The function+ * is supposed to be used with standard algorithms like std::accumulate.+ */+template<typename Class, typename MemType>+inline auto mem_minus(MemType Class::*ptr) {+ return detail::mem_compare<std::minus<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++template<typename Class, typename MemType>+inline auto mem_minus(MemType (Class::*ptr)()) {+ return detail::mem_compare<std::minus<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++/**+ * @brief mem_multiplies is a binary functor that applies a multiplication operator to member of+ * an object and a given value or two members of two objects. The function+ * is supposed to be used with standard algorithms like std::accumulate.+ */+template<typename Class, typename MemType>+inline auto mem_multiplies(MemType Class::*ptr) {+ return detail::mem_compare<std::multiplies<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++template<typename Class, typename MemType>+inline auto mem_multiplies(MemType (Class::*ptr)()) {+ return detail::mem_compare<std::multiplies<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++/**+ * @brief mem_divides is a binary functor that applies a division operator to member of+ * an object and a given value or two members of two objects. The function+ * is supposed to be used with standard algorithms like std::accumulate.+ */+template<typename Class, typename MemType>+inline auto mem_divides(MemType Class::*ptr) {+ return detail::mem_compare<std::divides<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}++template<typename Class, typename MemType>+inline auto mem_divides(MemType (Class::*ptr)()) {+ return detail::mem_compare<std::divides<>, Class, std::invoke_result_t<decltype(ptr), Class>, decltype(ptr)>{ptr};+}+/*** A simple wrapper class that executes a passed lambda* on destruction. It might be used for cleaning-up resources,diff --git a/libs/global/tests/CMakeLists.txt b/libs/global/tests/CMakeLists.txtindex 7f2404f82b..a947485d66 100644--- a/libs/global/tests/CMakeLists.txt+++ b/libs/global/tests/CMakeLists.txt@@ -1,6 +1,7 @@kis_add_tests(KisSignalAutoConnectionTest.cppKisSignalCompressorTest.cppKisForestTest.cpp+ KisMplTest.cppKisRectsGridTest.cppKisLazyStorageTest.cppKisValueCacheTest.cppdiff --git a/libs/image/tests/KisMplTest.cpp b/libs/global/tests/KisMplTest.cppsimilarity index 85%rename from libs/image/tests/KisMplTest.cpprename to libs/global/tests/KisMplTest.cppindex 2dc2640272..a3aa4fa014 100644--- a/libs/image/tests/KisMplTest.cpp+++ b/libs/global/tests/KisMplTest.cpp@@ -261,6 +261,19 @@ void KisMplTest::testMemberOperatorsEqualToKisSharedPtr()}}+void KisMplTest::testMemberOperatorsEqualToKisSharedPtrFunction()+{+ std::vector<KisSharedPtr<StructWithShared>> vec({new StructWithShared(0),+ new StructWithShared(1),+ new StructWithShared(2),+ new StructWithShared(3),+ new StructWithShared(4)});+ {+ auto it = std::find_if(vec.begin(), vec.end(), kismpl::mem_equal_to(&StructWithShared::idConstFunc, 1));+ QVERIFY(it != vec.end());+ QCOMPARE(std::distance(vec.begin(), it), 1);+ }+}void KisMplTest::testMemberOperatorsLess(){@@ -478,5 +491,74 @@ void KisMplTest::testMemberOperatorsGreaterEqual()}}+namespace kismpl2 {++template<typename Class, typename MemType>+inline auto mem_bit_or(MemType Class::*ptr) {+ return kismpl::detail::mem_compare<std::bit_or<>, Class, MemType, decltype(ptr)>{ptr};+}++template<typename Class, typename MemType>+inline auto mem_bit_or(MemType (Class::*ptr)()) {+ return kismpl::detail::mem_compare<std::bit_or<>, Class, MemType, decltype(ptr)>{ptr};+}++}++void KisMplTest::testMemberOperatorsAccumulate()+{+ struct RectStruct {+ QRect rect;+ };++ std::vector<RectStruct> vec({{QRect(0,0,10,10)},+ {QRect(0,0,20,10)},+ {QRect(0,0,10,20)},+ {QRect(0,0,30,10)},+ {QRect(0,0,10,30)}});+ {+ const QRect result = std::accumulate(vec.begin(), vec.end(), QRect(), kismpl::mem_bit_or(&RectStruct::rect));+ QCOMPARE(result, QRect(0,0,30,30));+ }++ {+ const QRect result = std::accumulate(vec.begin(), vec.end(), QRect(0,0,100,100), kismpl::mem_bit_and(&RectStruct::rect));+ QCOMPARE(result, QRect(0,0,10,10));+ }+}++void KisMplTest::testMemberOperatorsAccumulateToKisSharedPtr()+{+ std::vector<KisSharedPtr<StructWithShared>> vec({new StructWithShared(0),+ new StructWithShared(1),+ new StructWithShared(2),+ new StructWithShared(3),+ new StructWithShared(4)});+ {+ const int result = std::accumulate(vec.begin(), vec.end(), 0, kismpl::mem_plus(&StructWithShared::idConstFunc));+ QCOMPARE(result, 10);+ }++ {+ const int result = std::accumulate(vec.begin(), vec.end(), 0, kismpl::mem_minus(&StructWithShared::idConstFunc));+ QCOMPARE(result, -10);+ }++ {+ const int result = std::accumulate(vec.begin(), vec.end(), 0, kismpl::mem_multiplies(&StructWithShared::idConstFunc));+ QCOMPARE(result, 0);+ }++ {+ const int result = std::accumulate(vec.begin(), vec.end(), 0, kismpl::mem_divides(&StructWithShared::idConstFunc));+ QCOMPARE(result, 0);+ }++ {+ const int result = std::accumulate(vec.begin(), vec.end(), 0, kismpl::mem_bit_xor(&StructWithShared::idConstFunc));+ QCOMPARE(result, 4);+ }+}+SIMPLE_TEST_MAIN(KisMplTest)diff --git a/libs/image/tests/KisMplTest.h b/libs/global/tests/KisMplTest.hsimilarity index 81%rename from libs/image/tests/KisMplTest.hrename to libs/global/tests/KisMplTest.hindex b054e93972..e6526d3515 100644--- a/libs/image/tests/KisMplTest.h+++ b/libs/global/tests/KisMplTest.h@@ -18,11 +18,15 @@ private Q_SLOTS:void testMemberOperatorsEqualToStdSharedPtr();void testMemberOperatorsEqualToQSharedPointer();void testMemberOperatorsEqualToKisSharedPtr();+ void testMemberOperatorsEqualToKisSharedPtrFunction();void testMemberOperatorsLess();void testMemberOperatorsLessEqual();void testMemberOperatorsGreater();void testMemberOperatorsGreaterEqual();++ void testMemberOperatorsAccumulate();+ void testMemberOperatorsAccumulateToKisSharedPtr();};#endif // KISMPLTEST_Hdiff --git a/libs/image/tests/CMakeLists.txt b/libs/image/tests/CMakeLists.txtindex 3694782550..8596093cab 100644--- a/libs/image/tests/CMakeLists.txt+++ b/libs/image/tests/CMakeLists.txt@@ -132,11 +132,6 @@ kis_add_tests(LINK_LIBRARIES kritaimage kritatestsdk)-kis_add_test(- KisMplTest.cpp- LINK_LIBRARIES kritaimage kritatestsdk kritaglobal- )-kis_add_test(kis_clone_layer_test.cppLINK_LIBRARIES kritaimage kritatestsdk kritaglobal kritatransformmaskstubs