libaurum: apply smart pointer wider and extract impl out
[platform/core/uifw/aurum.git] / libaurum / src / Comparer.cc
1 #include "Comparer.h"
2
3 #include <loguru.hpp>
4
5 Comparer::Comparer(const std::shared_ptr<UiDevice> device, const std::shared_ptr<UiSelector> selector,
6                    const bool &earlyReturn)
7     : mDevice(device), mSelector(selector), mEarlyReturn(earlyReturn)
8 {
9 }
10
11 Comparer::~Comparer() {}
12
13 std::shared_ptr<AccessibleNode> Comparer::findObject(const std::shared_ptr<UiDevice> device,
14                                      const std::shared_ptr<UiSelector> selector,
15                                      const std::shared_ptr<AccessibleNode> root)
16 {
17     Comparer                      comparer(device, selector, true);
18     std::vector<std::shared_ptr<AccessibleNode>> ret = comparer.findObjects(root);
19     if (ret.size() > 0)
20         return std::move(ret[0]);
21     else
22         return nullptr;
23 }
24
25 std::vector<std::shared_ptr<AccessibleNode>> Comparer::findObjects(const std::shared_ptr<UiDevice> device,
26                                                     const std::shared_ptr<UiSelector> selector,
27                                                     const std::shared_ptr<AccessibleNode> root)
28 {
29     Comparer                      comparer(device, selector, false);
30     std::vector<std::shared_ptr<AccessibleNode>> ret = comparer.findObjects(root);
31     return ret;
32 }
33
34 std::vector<std::shared_ptr<AccessibleNode>> Comparer::findObjects(const std::shared_ptr<AccessibleNode> root)
35 {
36     std::list<std::shared_ptr<PartialMatch>> partialList{};
37     std::vector<std::shared_ptr<AccessibleNode>> ret = findObjects(root, 0, 0, partialList);
38     return ret;
39 }
40
41 std::vector<std::shared_ptr<AccessibleNode>> Comparer::findObjects(
42     const std::shared_ptr<AccessibleNode> root, const int &index, const int &depth,
43     std::list<std::shared_ptr<PartialMatch>> &partialMatches)
44 {
45     std::vector<std::shared_ptr<AccessibleNode>> ret;
46     root->refresh();
47     LOG_SCOPE_F(INFO, "findObjects %s, %s, %s / i:%d d:%d / p.matches:%d", root->getText().c_str(), root->getType().c_str(), root->getStyle().c_str(), index, depth, partialMatches.size());
48
49     for (auto &match : partialMatches)
50         match->update(root, index, depth, partialMatches);
51
52     std::shared_ptr<PartialMatch> currentMatch =
53         PartialMatch::accept(root, mSelector, index, depth);
54     if (currentMatch) partialMatches.push_front(currentMatch);
55
56     if (!(mSelector->mMaxDepth && (depth+1 > *(mSelector->mMaxDepth)))) {
57         int childCnt = root->getChildCount();
58         for (int i = 0; i < childCnt; i++) {
59             std::shared_ptr<AccessibleNode> childNode = root->getChildAt(i);
60             if (childNode == nullptr) continue;
61
62             std::vector<std::shared_ptr<AccessibleNode>> childret =
63                 findObjects(childNode, i, depth + 1, partialMatches);
64             std::move(std::begin(childret), std::end(childret), std::back_inserter(ret));
65
66             if (!ret.empty() && mEarlyReturn) return ret;
67         }
68     } else {
69         LOG_F(INFO, "no need to search children(maxDepth limit overflow, %d < %d < %d)", mSelector->mMinDepth?*(mSelector->mMinDepth):-1, depth, mSelector->mMaxDepth?*(mSelector->mMaxDepth):9999999);
70     }
71
72     if (currentMatch && currentMatch->finalizeMatch()){
73         LOG_F(INFO, "child 3 %p(raw:%p)", root.get(), root->getRawHandler());
74         ret.push_back(root);
75     }
76
77     return ret;
78 }