mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 05:33:36 +00:00
common Accessor: Add access API using std::function
This commit is contained in:
parent
6e26aab1b6
commit
3ba0b8adff
3 changed files with 13 additions and 30 deletions
10
inc/thorvg.h
10
inc/thorvg.h
|
@ -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.
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue