Skip to content

Commit

Permalink
common Accessor: Add access API using std::function
Browse files Browse the repository at this point in the history
  • Loading branch information
JSUYA authored and hermet committed Nov 25, 2022
1 parent 6e26aab commit 3ba0b8a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 30 deletions.
10 changes: 6 additions & 4 deletions inc/thorvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef _THORVG_H_
#define _THORVG_H_

#include <functional>
#include <memory>
#include <string>

Expand Down Expand Up @@ -1603,7 +1604,7 @@ class TVG_EXPORT Accessor final
~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.
Expand All @@ -1615,17 +1616,18 @@ class TVG_EXPORT Accessor final
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.
Expand Down
2 changes: 1 addition & 1 deletion src/examples/Accessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
31 changes: 6 additions & 25 deletions src/lib/tvgAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,15 @@
/* 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
if (!func(child)) return false;

//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;
}
Expand All @@ -77,27 +58,27 @@ 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;

//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;
Expand Down

0 comments on commit 3ba0b8a

Please sign in to comment.