common Accessor: Add access API using std::function 86/289786/1
authorJunsuChoi <jsuya.choi@samsung.com>
Thu, 24 Nov 2022 04:46:44 +0000 (20:46 -0800)
committerMichal Szczecinski <mihashco89@gmail.com>
Tue, 14 Mar 2023 08:42:58 +0000 (09:42 +0100)
Change-Id: I50eb9443137f40d329105fea24739a549dd0ea41

inc/thorvg.h
src/examples/Accessor.cpp
src/lib/tvgAccessor.cpp

index 666af3c335bf0d72f38c466614a030ca9b2d788c..8bc7f1880273378feffe5bf5c121f86198e07329 100644 (file)
@@ -14,6 +14,7 @@
 #ifndef _THORVG_H_
 #define _THORVG_H_
 
+#include <functional>
 #include <memory>
 #include <string>
 
@@ -1603,7 +1604,7 @@ public:
     ~Accessor();
 
     /**
-     * @brief Access the Picture scene stree nodes.
+     * @brief Access the Picture scene tree nodes.
      *
      * @param[in] picture The picture node to traverse the internal scene-tree.
      * @param[in] func The callback function calling for every paint nodes of the Picture.
@@ -1615,17 +1616,18 @@ public:
     std::unique_ptr<Picture> access(std::unique_ptr<Picture> picture, bool(*func)(const Paint* paint)) noexcept;
 
     /**
-     * @brief Access the Picture scene stree nodes.
+     * @brief Set the access function for traversing the Picture scene tree nodes.
      *
      * @param[in] picture The picture node to traverse the internal scene-tree.
      * @param[in] func The callback function calling for every paint nodes of the Picture.
-     * @param[in] data Data will be passed to callback function.
      *
      * @return Return the given @p picture instance.
      *
      * @note The bitmap based picture might not have the scene-tree.
+     *
+     * @BETA_API
      */
-    std::unique_ptr<Picture> access(std::unique_ptr<Picture> picture, bool(*func)(const Paint* paint, void* data), void* data) noexcept;
+    std::unique_ptr<Picture> set(std::unique_ptr<Picture> picture, std::function<bool(const Paint* paint)> func) noexcept;
 
     /**
      * @brief Creates a new Accessor object.
index 89dcbf44d1aa42972cc43af7acd365c49d0ec43e..433b3f9b755f364f9ec0cce5f1b3229398d71948 100644 (file)
@@ -55,7 +55,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
         return true;
     };
 
-    picture = accessor->access(move(picture), f);
+    picture = accessor->set(move(picture), f);
 
     canvas->push(move(picture));
 }
index eb63bdfc9828a62af7b22a8ecd0f82caa4c00b8d..971f2dd4c53b8e999c9a8f09e317d15d6a50eb6c 100644 (file)
@@ -23,7 +23,7 @@
 /* Internal Class Implementation                                        */
 /************************************************************************/
 
-static bool accessChildren(Iterator* it, bool(*func)(const Paint* paint), IteratorAccessor& itrAccessor)
+static bool accessChildren(Iterator* it, IteratorAccessor& itrAccessor, function<bool(const Paint* paint)> func)
 {
     while (auto child = it->next()) {
         //Access the child
@@ -31,26 +31,7 @@ static bool accessChildren(Iterator* it, bool(*func)(const Paint* paint), Iterat
 
         //Access the children of the child
         if (auto it2 = itrAccessor.iterator(child)) {
-            if (!accessChildren(it2, func, itrAccessor)) {
-                delete(it2);
-                return false;
-            }
-            delete(it2);
-        }
-    }
-    return true;
-}
-
-
-static bool accessChildren(Iterator* it, bool(*func)(const Paint* paint, void* data), IteratorAccessor& itrAccessor, void* data)
-{
-    while (auto child = it->next()) {
-        //Access the child
-        if (!func(child, data)) return false;
-
-        //Access the children of the child
-        if (auto it2 = itrAccessor.iterator(child)) {
-            if (!accessChildren(it2, func, itrAccessor, data)) {
+            if (!accessChildren(it2, itrAccessor, func)) {
                 delete(it2);
                 return false;
             }
@@ -77,14 +58,14 @@ unique_ptr<Picture> Accessor::access(unique_ptr<Picture> picture, bool(*func)(co
     //Children
     IteratorAccessor itrAccessor;
     if (auto it = itrAccessor.iterator(p)) {
-        accessChildren(it, func, itrAccessor);
+        accessChildren(it, itrAccessor, func);
         delete(it);
     }
     return picture;
 }
 
 
-unique_ptr<Picture> Accessor::access(unique_ptr<Picture> picture, bool(*func)(const Paint* paint, void* data), void* data) noexcept
+unique_ptr<Picture> Accessor::set(unique_ptr<Picture> picture, function<bool(const Paint* paint)> func) noexcept
 {
     auto p = picture.get();
     if (!p || !func) return picture;
@@ -92,12 +73,12 @@ unique_ptr<Picture> Accessor::access(unique_ptr<Picture> picture, bool(*func)(co
     //Use the Preorder Tree-Search
 
     //Root
-    if (!func(p, data)) return picture;
+    if (!func(p)) return picture;
 
     //Children
     IteratorAccessor itrAccessor;
     if (auto it = itrAccessor.iterator(p)) {
-        accessChildren(it, func, itrAccessor, data);
+        accessChildren(it, itrAccessor, func);
         delete(it);
     }
     return picture;