Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lihuiba committed Dec 4, 2023
1 parent 5cae1a2 commit a562379
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions common/simple_dom.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,13 @@ class RootNode : public Node {
protected:
RootNode() = default;

// reference counting is only applied on root
// reference counting is only applied to root
// node, which represents the whole document
std::atomic<uint32_t> _refcnt{0};

public:
void add_ref() {
_refcnt.fetch_add(1);
}
void rm_ref() {
auto cnt = _refcnt.fetch_sub(1);
if (cnt == 1)
delete this;
}
void add_ref() { ++_refcnt; }
void del_ref() { if (--_refcnt == 0) delete this; }
};

#define IF_RET(e) if (_node) return e; \
Expand All @@ -117,23 +111,23 @@ struct NodeWrapper {
auto rt = root_node();
auto rrt = rhs.root_node();
if (rt != rrt) {
if (rt) rt->rm_ref();
if (rt) rt->del_ref();
if (rrt) rrt->add_ref();
}
_node = rhs._node;
return *this;
}
NodeWrapper& operator = (NodeWrapper&& rhs) {
if (_node)
_node->root()->rm_ref();
_node->root()->del_ref();
_node = rhs._node;
rhs._node = nullptr;
return *this;
}
~NodeWrapper() {
_node->root()->rm_ref();
_node->root()->del_ref();
}
NodeWrapper root() const { return root_node(); }
NodeWrapper root() const { IF_RET(_node->root()); }
RootNode* root_node() const { IF_RET(_node->root()); }
NodeWrapper parent() const { IF_RET(_node->_parent); }
NodeWrapper next() const { IF_RET(_node->_next); }
Expand Down

0 comments on commit a562379

Please sign in to comment.