common Accessor: Add access API using std::function

This commit is contained in:
JunsuChoi 2022-11-23 20:46:44 -08:00 committed by Hermet Park
parent 6e26aab1b6
commit 3ba0b8adff
3 changed files with 13 additions and 30 deletions

View file

@ -14,6 +14,7 @@
#ifndef _THORVG_H_ #ifndef _THORVG_H_
#define _THORVG_H_ #define _THORVG_H_
#include <functional>
#include <memory> #include <memory>
#include <string> #include <string>
@ -1603,7 +1604,7 @@ public:
~Accessor(); ~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] 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] 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; 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] 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] 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. * @return Return the given @p picture instance.
* *
* @note The bitmap based picture might not have the scene-tree. * @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. * @brief Creates a new Accessor object.

View file

@ -55,7 +55,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
return true; return true;
}; };
picture = accessor->access(move(picture), f); picture = accessor->set(move(picture), f);
canvas->push(move(picture)); canvas->push(move(picture));
} }

View file

@ -23,7 +23,7 @@
/* Internal Class Implementation */ /* 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()) { while (auto child = it->next()) {
//Access the child //Access the child
@ -31,26 +31,7 @@ static bool accessChildren(Iterator* it, bool(*func)(const Paint* paint), Iterat
//Access the children of the child //Access the children of the child
if (auto it2 = itrAccessor.iterator(child)) { if (auto it2 = itrAccessor.iterator(child)) {
if (!accessChildren(it2, func, itrAccessor)) { if (!accessChildren(it2, itrAccessor, func)) {
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)) {
delete(it2); delete(it2);
return false; return false;
} }
@ -77,14 +58,14 @@ unique_ptr<Picture> Accessor::access(unique_ptr<Picture> picture, bool(*func)(co
//Children //Children
IteratorAccessor itrAccessor; IteratorAccessor itrAccessor;
if (auto it = itrAccessor.iterator(p)) { if (auto it = itrAccessor.iterator(p)) {
accessChildren(it, func, itrAccessor); accessChildren(it, itrAccessor, func);
delete(it); delete(it);
} }
return picture; 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(); auto p = picture.get();
if (!p || !func) return picture; 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 //Use the Preorder Tree-Search
//Root //Root
if (!func(p, data)) return picture; if (!func(p)) return picture;
//Children //Children
IteratorAccessor itrAccessor; IteratorAccessor itrAccessor;
if (auto it = itrAccessor.iterator(p)) { if (auto it = itrAccessor.iterator(p)) {
accessChildren(it, func, itrAccessor, data); accessChildren(it, itrAccessor, func);
delete(it); delete(it);
} }
return picture; return picture;