Cleanup: Don't do recursion where possible in node.cc #105394

Merged
Hans Goudey merged 25 commits from mod_moder/blender:cleanup_bke_nodes_unwrap_recursion into main 2023-03-06 18:39:30 +01:00
2 changed files with 60 additions and 56 deletions
Showing only changes of commit 458a3df5b0 - Show all commits

View File

@ -452,7 +452,7 @@ typedef struct bNodeTreeType {
struct bNodeTreeType *ntreeTypeFind(const char *idname);
void ntreeTypeAdd(struct bNodeTreeType *nt);
void ntreeTypeFreeLink(const struct bNodeTreeType *nt);
bool ntreeIsRegistered(struct bNodeTree *ntree);
bool ntreeIsRegistered(const struct bNodeTree *ntree);
struct GHashIterator *ntreeTypeGetIterator(void);
/* Helper macros for iterating over tree types. */
@ -620,7 +620,7 @@ struct GHashIterator *nodeTypeGetIterator(void);
struct bNodeSocketType *nodeSocketTypeFind(const char *idname);
void nodeRegisterSocketType(struct bNodeSocketType *stype);
void nodeUnregisterSocketType(struct bNodeSocketType *stype);
bool nodeSocketIsRegistered(struct bNodeSocket *sock);
bool nodeSocketIsRegistered(const struct bNodeSocket *sock);
struct GHashIterator *nodeSocketTypeGetIterator(void);
const char *nodeSocketTypeLabel(const bNodeSocketType *stype);
@ -644,7 +644,7 @@ const char *nodeStaticSocketLabel(int type, int subtype);
} \
((void)0)
struct bNodeSocket *nodeFindSocket(struct bNode *node,
struct bNodeSocket *nodeFindSocket(const struct bNode *node,
eNodeSocketInOut in_out,
const char *identifier);
struct bNodeSocket *nodeAddSocket(struct bNodeTree *ntree,
@ -762,9 +762,9 @@ void nodeAttachNode(struct bNodeTree *ntree, struct bNode *node, struct bNode *p
void nodeDetachNode(struct bNodeTree *ntree, struct bNode *node);
void nodePositionRelative(struct bNode *from_node,
struct bNode *to_node,
struct bNodeSocket *from_sock,
struct bNodeSocket *to_sock);
const struct bNode *to_node,
const struct bNodeSocket *from_sock,
const struct bNodeSocket *to_sock);
void nodePositionPropagate(struct bNode *node);
/**

View File

@ -1229,7 +1229,7 @@ static void update_typeinfo(Main *bmain,
bNodeTreeType *treetype,
bNodeType *nodetype,
bNodeSocketType *socktype,
bool unregister)
const bool unregister)
{
if (!bmain) {
return;
@ -1337,7 +1337,7 @@ void ntreeTypeFreeLink(const bNodeTreeType *nt)
BLI_ghash_remove(nodetreetypes_hash, nt->idname, nullptr, ntree_free_type);
}
bool ntreeIsRegistered(bNodeTree *ntree)
bool ntreeIsRegistered(const bNodeTree *ntree)
{
return (ntree->typeinfo != &NodeTreeTypeUndefined);
}
@ -1450,7 +1450,7 @@ void nodeUnregisterSocketType(bNodeSocketType *st)
BLI_ghash_remove(nodesockettypes_hash, st->idname, nullptr, node_free_socket_type);
}
bool nodeSocketIsRegistered(bNodeSocket *sock)
bool nodeSocketIsRegistered(const bNodeSocket *sock)
{
return (sock->typeinfo != &NodeSocketTypeUndefined);
}
@ -1466,7 +1466,9 @@ const char *nodeSocketTypeLabel(const bNodeSocketType *stype)
return stype->label[0] != '\0' ? stype->label : RNA_struct_ui_name(stype->ext_socket.srna);
}
bNodeSocket *nodeFindSocket(bNode *node, eNodeSocketInOut in_out, const char *identifier)
bNodeSocket *nodeFindSocket(const bNode *node,
const eNodeSocketInOut in_out,
const char *identifier)
{
const ListBase *sockets = (in_out == SOCK_IN) ? &node->inputs : &node->outputs;
LISTBASE_FOREACH (bNodeSocket *, sock, sockets) {
@ -1492,12 +1494,12 @@ bNodeSocket *node_find_enabled_socket(bNode &node,
return nullptr;
}
bNodeSocket *node_find_enabled_input_socket(bNode &node, StringRef name)
bNodeSocket *node_find_enabled_input_socket(bNode &node, const StringRef name)
{
return node_find_enabled_socket(node, SOCK_IN, name);
}
bNodeSocket *node_find_enabled_output_socket(bNode &node, StringRef name)
bNodeSocket *node_find_enabled_output_socket(bNode &node, const StringRef name)
{
return node_find_enabled_socket(node, SOCK_OUT, name);
}
@ -1517,7 +1519,7 @@ static bool unique_identifier_check(void *arg, const char *identifier)
static bNodeSocket *make_socket(bNodeTree *ntree,
bNode * /*node*/,
int in_out,
const int in_out,
ListBase *lb,
const char *idname,
const char *identifier,
@ -1681,7 +1683,7 @@ void nodeModifySocketType(bNodeTree *ntree,
}
void nodeModifySocketTypeStatic(
bNodeTree *ntree, bNode *node, bNodeSocket *sock, int type, int subtype)
bNodeTree *ntree, bNode *node, bNodeSocket *sock, const int type, const int subtype)
{
const char *idname = nodeStaticSocketType(type, subtype);
@ -1695,7 +1697,7 @@ void nodeModifySocketTypeStatic(
bNodeSocket *nodeAddSocket(bNodeTree *ntree,
bNode *node,
eNodeSocketInOut in_out,
const eNodeSocketInOut in_out,
const char *idname,
const char *identifier,
const char *name)
@ -1724,7 +1726,7 @@ bool nodeIsStaticSocketType(const bNodeSocketType *stype)
return RNA_struct_is_a(stype->ext_socket.srna, &RNA_NodeSocketStandard);
}
const char *nodeStaticSocketType(int type, int subtype)
const char *nodeStaticSocketType(const int type, const int subtype)
{
switch (type) {
case SOCK_FLOAT:
@ -1801,7 +1803,7 @@ const char *nodeStaticSocketType(int type, int subtype)
return nullptr;
}
const char *nodeStaticSocketInterfaceType(int type, int subtype)
const char *nodeStaticSocketInterfaceType(const int type, const int subtype)
{
switch (type) {
case SOCK_FLOAT:
@ -1878,7 +1880,7 @@ const char *nodeStaticSocketInterfaceType(int type, int subtype)
return nullptr;
}
const char *nodeStaticSocketLabel(int type, int /*subtype*/)
const char *nodeStaticSocketLabel(const int type, const int /*subtype*/)
{
switch (type) {
case SOCK_FLOAT:
@ -1955,7 +1957,7 @@ void nodeRemoveSocket(bNodeTree *ntree, bNode *node, bNodeSocket *sock)
nodeRemoveSocketEx(ntree, node, sock, true);
}
void nodeRemoveSocketEx(bNodeTree *ntree, bNode *node, bNodeSocket *sock, bool do_id_user)
void nodeRemoveSocketEx(bNodeTree *ntree, bNode *node, bNodeSocket *sock, const bool do_id_user)
{
LISTBASE_FOREACH_MUTABLE (bNodeLink *, link, &ntree->links) {
if (link->fromsock == sock || link->tosock == sock) {
@ -2019,7 +2021,7 @@ void nodeFindNode(bNodeTree *ntree, bNodeSocket *sock, bNode **r_node, int *r_so
bNode *node = &sock->owner_node();
*r_node = node;
if (r_sockindex) {
ListBase *sockets = (sock->in_out == SOCK_IN) ? &node->inputs : &node->outputs;
const ListBase *sockets = (sock->in_out == SOCK_IN) ? &node->inputs : &node->outputs;
*r_sockindex = BLI_findindex(sockets, sock);
}
return;
@ -2032,9 +2034,9 @@ void nodeFindNode(bNodeTree *ntree, bNodeSocket *sock, bNode **r_node, int *r_so
bool nodeFindNodeTry(bNodeTree *ntree, bNodeSocket *sock, bNode **r_node, int *r_sockindex)
{
for (bNode *node : ntree->all_nodes()) {
ListBase *sockets = (sock->in_out == SOCK_IN) ? &node->inputs : &node->outputs;
const ListBase *sockets = (sock->in_out == SOCK_IN) ? &node->inputs : &node->outputs;
int i;
LISTBASE_FOREACH_INDEX (bNodeSocket *, tsock, sockets, i) {
LISTBASE_FOREACH_INDEX (const bNodeSocket *, tsock, sockets, i) {
if (sock == tsock) {
if (r_node != nullptr) {
*r_node = node;
@ -2111,7 +2113,7 @@ static void iter_backwards_ex(const bNodeTree *ntree,
const bNode *node_start,
bool (*callback)(bNode *, bNode *, void *),
void *userdata,
char recursion_mask)
const char recursion_mask)
{
LISTBASE_FOREACH (bNodeSocket *, sock, &node_start->inputs) {
bNodeLink *link = sock->link;
@ -2139,7 +2141,7 @@ void nodeChainIterBackwards(const bNodeTree *ntree,
const bNode *node_start,
bool (*callback)(bNode *, bNode *, void *),
void *userdata,
int recursion_lvl)
const int recursion_lvl)
{
if (!node_start) {
return;
@ -2147,7 +2149,7 @@ void nodeChainIterBackwards(const bNodeTree *ntree,
/* Limited by iter_flag type. */
BLI_assert(recursion_lvl < 8);
char recursion_mask = (1 << recursion_lvl);
const char recursion_mask = (1 << recursion_lvl);
/* Reset flag. */
LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
@ -2242,7 +2244,7 @@ bNode *nodeAddNode(const bContext *C, bNodeTree *ntree, const char *idname)
return node;
}
bNode *nodeAddStaticNode(const bContext *C, bNodeTree *ntree, int type)
bNode *nodeAddStaticNode(const bContext *C, bNodeTree *ntree, const int type)
{
const char *idname = nullptr;
@ -2472,8 +2474,8 @@ bool nodeLinkIsSelected(const bNodeLink *link)
/* Adjust the indices of links connected to the given multi input socket after deleting the link at
* `deleted_index`. This function also works if the link has not yet been deleted. */
static void adjust_multi_input_indices_after_removed_link(bNodeTree *ntree,
bNodeSocket *sock,
int deleted_index)
const bNodeSocket *sock,
const int deleted_index)
{
LISTBASE_FOREACH (bNodeLink *, link, &ntree->links) {
/* We only need to adjust those with a greater index, because the others will have the same
@ -2578,7 +2580,7 @@ static void nodeViewMapping(const bNode *node, float &mapping_x, float &mapping_
}
}
void nodeToView(const bNode *node, float x, float y, float *rx, float *ry)
void nodeToView(const bNode *node, const float x, const float y, float *rx, float *ry)
{
float mapping_x = 0.0f;
float mapping_y = 0.0f;
@ -2588,7 +2590,7 @@ void nodeToView(const bNode *node, float x, float y, float *rx, float *ry)
*ry = mapping_y + y;
}
void nodeFromView(const bNode *node, float x, float y, float *rx, float *ry)
void nodeFromView(const bNode *node, const float x, const float y, float *rx, float *ry)
{
float mapping_x = 0.0f;
float mapping_y = 0.0f;
@ -2639,9 +2641,9 @@ void nodeDetachNode(bNodeTree *ntree, bNode *node)
}
void nodePositionRelative(bNode *from_node,
bNode *to_node,
bNodeSocket *from_sock,
bNodeSocket *to_sock)
const bNode *to_node,
const bNodeSocket *from_sock,
const bNodeSocket *to_sock)
{
float offset_x;
int tot_sock_idx;
@ -2942,16 +2944,13 @@ void BKE_node_preview_merge_tree(bNodeTree *to_ntree, bNodeTree *from_ntree, boo
void nodeUnlinkNode(bNodeTree *ntree, bNode *node)
{
LISTBASE_FOREACH_MUTABLE (bNodeLink *, link, &ntree->links) {
ListBase *lb;
ListBase *lb = nullptr;
if (link->fromnode == node) {
lb = &node->outputs;
}
else if (link->tonode == node) {
lb = &node->inputs;
}
else {
lb = nullptr;
}
if (lb) {
/* Only bother adjusting if the socket is not on the node we're deleting. */
@ -2959,7 +2958,7 @@ void nodeUnlinkNode(bNodeTree *ntree, bNode *node)
adjust_multi_input_indices_after_removed_link(
ntree, link->tosock, link->multi_input_socket_index);
}
LISTBASE_FOREACH (bNodeSocket *, sock, lb) {
LISTBASE_FOREACH (const bNodeSocket *, sock, lb) {
if (link->fromsock == sock || link->tosock == sock) {
nodeRemLink(ntree, link);
break;
@ -2969,7 +2968,7 @@ void nodeUnlinkNode(bNodeTree *ntree, bNode *node)
}
}
static void node_unlink_attached(bNodeTree *ntree, bNode *parent)
static void node_unlink_attached(bNodeTree *ntree, const bNode *parent)
{
for (bNode *node : ntree->all_nodes()) {
if (node->parent == parent) {
@ -3057,7 +3056,7 @@ void ntreeFreeLocalNode(bNodeTree *ntree, bNode *node)
nodeRebuildIDVector(ntree);
}
void nodeRemoveNode(Main *bmain, bNodeTree *ntree, bNode *node, bool do_id_user)
void nodeRemoveNode(Main *bmain, bNodeTree *ntree, bNode *node, const bool do_id_user)
{
/* This function is not for localized node trees, we do not want
* do to ID user reference-counting and removal of animdation data then. */
@ -3340,7 +3339,7 @@ void ntreeLocalMerge(Main *bmain, bNodeTree *localtree, bNodeTree *ntree)
/* ************ NODE TREE INTERFACE *************** */
static bNodeSocket *make_socket_interface(bNodeTree *ntree,
eNodeSocketInOut in_out,
const eNodeSocketInOut in_out,
const char *idname,
const char *name)
{
@ -3376,7 +3375,7 @@ static bNodeSocket *make_socket_interface(bNodeTree *ntree,
}
bNodeSocket *ntreeFindSocketInterface(bNodeTree *ntree,
eNodeSocketInOut in_out,
const eNodeSocketInOut in_out,
const char *identifier)
{
ListBase *sockets = (in_out == SOCK_IN) ? &ntree->inputs : &ntree->outputs;
@ -3389,7 +3388,7 @@ bNodeSocket *ntreeFindSocketInterface(bNodeTree *ntree,
}
bNodeSocket *ntreeAddSocketInterface(bNodeTree *ntree,
eNodeSocketInOut in_out,
const eNodeSocketInOut in_out,
const char *idname,
const char *name)
{
@ -3405,7 +3404,7 @@ bNodeSocket *ntreeAddSocketInterface(bNodeTree *ntree,
}
bNodeSocket *ntreeInsertSocketInterface(bNodeTree *ntree,
eNodeSocketInOut in_out,
const eNodeSocketInOut in_out,
const char *idname,
bNodeSocket *next_sock,
const char *name)
@ -3478,7 +3477,7 @@ void ntreeRemoveSocketInterface(bNodeTree *ntree, bNodeSocket *sock)
/* ************ find stuff *************** */
bNode *ntreeFindType(bNodeTree *ntree, int type)
bNode *ntreeFindType(bNodeTree *ntree, const int type)
{
if (ntree) {
LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
@ -3563,7 +3562,7 @@ bNode *nodeGetActive(bNodeTree *ntree)
return nullptr;
}
void nodeSetSelected(bNode *node, bool select)
void nodeSetSelected(bNode *node, const bool select)
{
if (select) {
node->flag |= NODE_SELECT;
@ -3607,7 +3606,7 @@ void nodeSetActive(bNodeTree *ntree, bNode *node)
node->flag |= flags_to_set;
}
void nodeSetSocketAvailability(bNodeTree *ntree, bNodeSocket *sock, bool is_available)
void nodeSetSocketAvailability(bNodeTree *ntree, bNodeSocket *sock, const bool is_available)
{
const bool was_available = (sock->flag & SOCK_UNAVAIL) == 0;
if (is_available == was_available) {
@ -3624,12 +3623,13 @@ void nodeSetSocketAvailability(bNodeTree *ntree, bNodeSocket *sock, bool is_avai
int nodeSocketLinkLimit(const bNodeSocket *sock)
{
bNodeSocketType *stype = sock->typeinfo;
const bNodeSocketType *stype = sock->typeinfo;
if (sock->flag & SOCK_MULTI_INPUT) {
return 4095;
}
if (stype != nullptr && stype->use_link_limits_of_type) {
int limit = (sock->in_out == SOCK_IN) ? stype->input_link_limit : stype->output_link_limit;
const int limit = (sock->in_out == SOCK_IN) ? stype->input_link_limit :
stype->output_link_limit;
return limit;
}
@ -3913,7 +3913,7 @@ void ntreeUpdateAllUsers(Main *main, ID *id)
/* ************* node type access ********** */
void nodeLabel(const bNodeTree *ntree, const bNode *node, char *label, int maxlen)
void nodeLabel(const bNodeTree *ntree, const bNode *node, char *label, const int maxlen)
{
label[0] = '\0';
@ -3964,7 +3964,7 @@ static bool node_poll_instance_default(const bNode *node,
return node->typeinfo->poll(node->typeinfo, ntree, disabled_hint);
}
void node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
void node_type_base(bNodeType *ntype, const int type, const char *name, const short nclass)
{
/* Use static type info header to map static int type to identifier string and RNA struct type.
* Associate the RNA struct type with the bNodeType.
@ -3998,7 +3998,10 @@ void node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
ntype->poll_instance = node_poll_instance_default;
}
void node_type_base_custom(bNodeType *ntype, const char *idname, const char *name, short nclass)
void node_type_base_custom(bNodeType *ntype,
const char *idname,
const char *name,
const short nclass)
{
BLI_strncpy(ntype->idname, idname, sizeof(ntype->idname));
ntype->type = NODE_CUSTOM;
@ -4015,7 +4018,8 @@ struct SocketTemplateIdentifierCallbackData {
static bool unique_socket_template_identifier_check(void *arg, const char *name)
{
SocketTemplateIdentifierCallbackData *data = (SocketTemplateIdentifierCallbackData *)arg;
const SocketTemplateIdentifierCallbackData *data = (const SocketTemplateIdentifierCallbackData *)
arg;
for (bNodeSocketTemplate *ntemp = data->list; ntemp->type >= 0; ntemp++) {
if (ntemp != data->ntemp) {
@ -4031,7 +4035,7 @@ static bool unique_socket_template_identifier_check(void *arg, const char *name)
static void unique_socket_template_identifier(bNodeSocketTemplate *list,
bNodeSocketTemplate *ntemp,
const char defname[],
char delim)
const char delim)
{
SocketTemplateIdentifierCallbackData data;
data.list = list;
@ -4077,7 +4081,7 @@ void node_type_socket_templates(bNodeType *ntype,
}
}
void node_type_size(bNodeType *ntype, int width, int minwidth, int maxwidth)
void node_type_size(bNodeType *ntype, const int width, const int minwidth, const int maxwidth)
{
ntype->width = width;
ntype->minwidth = minwidth;
@ -4089,7 +4093,7 @@ void node_type_size(bNodeType *ntype, int width, int minwidth, int maxwidth)
}
}
void node_type_size_preset(bNodeType *ntype, eNodeSizePreset size)
void node_type_size_preset(bNodeType *ntype, const eNodeSizePreset size)
{
switch (size) {
case NODE_SIZE_DEFAULT: