[Bf-blender-cvs] [548eb3a8225] functions: listbase wrapper

Jacques Lucke noreply at git.blender.org
Mon Feb 11 18:30:25 CET 2019


Commit: 548eb3a8225ce154d4f202ef718edbc5b24eeb3e
Author: Jacques Lucke
Date:   Mon Feb 11 16:51:33 2019 +0100
Branches: functions
https://developer.blender.org/rB548eb3a8225ce154d4f202ef718edbc5b24eeb3e

listbase wrapper

===================================================================

A	source/blender/blenlib/BLI_listbase_wrapper.hpp
M	source/blender/blenlib/CMakeLists.txt
M	source/blender/functions/function_nodes/function_nodes.cpp

===================================================================

diff --git a/source/blender/blenlib/BLI_listbase_wrapper.hpp b/source/blender/blenlib/BLI_listbase_wrapper.hpp
new file mode 100644
index 00000000000..6a25196245a
--- /dev/null
+++ b/source/blender/blenlib/BLI_listbase_wrapper.hpp
@@ -0,0 +1,66 @@
+#include "BLI_listbase.h"
+#include "DNA_listBase.h"
+
+namespace BLI {
+
+	template<typename T, bool intrusive>
+	class ListBaseWrapper {
+	private:
+		ListBase *m_listbase;
+
+	public:
+		ListBaseWrapper(ListBase *listbase)
+			: m_listbase(listbase) {}
+
+		class Iterator {
+		private:
+			ListBase *m_listbase;
+			Link *m_current;
+
+		public:
+			Iterator(ListBase *listbase, Link *current)
+				: m_listbase(listbase), m_current(current) {}
+
+			Iterator &operator++()
+			{
+				m_current = m_current->next;
+				return *this;
+			}
+
+			Iterator operator++(int)
+			{
+				Iterator iterator = *this;
+				++*this;
+				return iterator;
+			}
+
+			bool operator!=(const Iterator &iterator) const
+			{
+				return m_current != iterator.m_current;
+			}
+
+			T *operator*() const
+			{
+				if (intrusive) {
+					return (T *)m_current;
+				}
+				else {
+					return (T *)((LinkData *)m_current)->data;
+				}
+			}
+
+		};
+
+		Iterator begin() const
+		{
+			return Iterator(m_listbase, (Link *)m_listbase->first);
+		}
+
+		Iterator end() const
+		{
+			return Iterator(m_listbase, nullptr);
+		}
+
+	};
+
+} /* namespace BLI */
\ No newline at end of file
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index bc0b78bec51..a99aabe57cc 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -226,6 +226,7 @@ set(SRC
 	PIL_time_utildefines.h
 
 	BLI_composition.hpp
+	BLI_listbase_wrapper.hpp
 	BLI_shared.hpp
 	BLI_small_vector.hpp
 	BLI_small_map.hpp
diff --git a/source/blender/functions/function_nodes/function_nodes.cpp b/source/blender/functions/function_nodes/function_nodes.cpp
index 48aced7010e..0f511b8e018 100644
--- a/source/blender/functions/function_nodes/function_nodes.cpp
+++ b/source/blender/functions/function_nodes/function_nodes.cpp
@@ -1,11 +1,17 @@
 #include "function_nodes.hpp"
 
 #include "BLI_listbase.h"
+#include "BLI_listbase_wrapper.hpp"
+
 #include "BKE_node.h"
 #include "BKE_idprop.h"
 
 namespace FN::FunctionNodes {
 
+	using bNodeList = ListBaseWrapper<bNode, true>;
+	using bLinkList = ListBaseWrapper<bNodeLink, true>;
+	using bSocketList = ListBaseWrapper<bNodeSocket, true>;
+
 	using SocketMap = SmallMap<bNodeSocket *, Socket>;
 	typedef void (*InsertInGraphFunction)(
 		SharedDataFlowGraph &graph,
@@ -176,7 +182,7 @@ namespace FN::FunctionNodes {
 		bNode *bnode)
 	{
 		SmallTypeVector types;
-		for (bNodeSocket *bsocket = (bNodeSocket *)bnode->inputs.first; bsocket; bsocket = bsocket->next) {
+		for (bNodeSocket *bsocket : bSocketList(&bnode->inputs)) {
 			if (STREQ(bsocket->idname, "fn_VectorSocket")) {
 				types.append(Types::get_fvec3_type());
 			}
@@ -205,7 +211,7 @@ namespace FN::FunctionNodes {
 		SocketMap &socket_map,
 		bNode *bnode)
 	{
-		for (bNodeSocket *bsocket = (bNodeSocket *)bnode->outputs.first; bsocket; bsocket = bsocket->next) {
+		for (bNodeSocket *bsocket : bSocketList(&bnode->outputs)) {
 			const Node *node = get_input_node_for_socket(graph, bsocket);
 			socket_map.add(bsocket, node->output(0));
 		}
@@ -222,11 +228,6 @@ namespace FN::FunctionNodes {
 		return STREQ(bnode->idname, "fn_FunctionOutputNode");
 	}
 
-	static bool is_function_node(const bNode *bnode)
-	{
-		return !(is_input_node(bnode) || is_output_node(bnode));
-	}
-
 	FunctionGraph FunctionNodeTree::to_function_graph() const
 	{
 		SocketMap socket_map;
@@ -242,32 +243,32 @@ namespace FN::FunctionNodes {
 		SmallSocketVector input_sockets;
 		SmallSocketVector output_sockets;
 
-		for (bNode *bnode = (bNode *)m_tree->nodes.first; bnode; bnode = bnode->next) {
+		for (bNode *bnode : bNodeList(&m_tree->nodes)) {
 			auto insert = inserters.lookup(bnode->idname);
 			insert(graph, socket_map, bnode);
 
 			if (is_input_node(bnode)) {
-				for (bNodeSocket *bsocket = (bNodeSocket *)bnode->outputs.first; bsocket; bsocket = bsocket->next) {
+				for (bNodeSocket *bsocket : bSocketList(&bnode->outputs)) {
 					Socket socket = socket_map.lookup(bsocket);
 					input_sockets.append(socket);
 				}
 			}
 			if (is_output_node(bnode)) {
-				for (bNodeSocket *bsocket = (bNodeSocket *)bnode->inputs.first; bsocket; bsocket = bsocket->next) {
+				for (bNodeSocket *bsocket : bSocketList(&bnode->inputs)) {
 					Socket socket = socket_map.lookup(bsocket);
 					output_sockets.append(socket);
 				}
 			}
 		}
 
-		for (bNodeLink *blink = (bNodeLink *)m_tree->links.first; blink; blink = blink->next) {
+		for (bNodeLink *blink : bLinkList(&m_tree->links)) {
 			Socket from = socket_map.lookup(blink->fromsock);
 			Socket to = socket_map.lookup(blink->tosock);
 			graph->link(from, to);
 		}
 
-		for (bNode *bnode = (bNode *)m_tree->nodes.first; bnode; bnode = bnode->next) {
-			for (bNodeSocket *bsocket = (bNodeSocket *)bnode->inputs.first; bsocket; bsocket = bsocket->next) {
+		for (bNode *bnode : bNodeList(&m_tree->nodes)) {
+			for (bNodeSocket *bsocket : bSocketList(&bnode->inputs)) {
 				Socket socket = socket_map.lookup(bsocket);
 				if (!socket.is_linked()) {
 					insert_input_socket_node(graph, socket, bsocket);



More information about the Bf-blender-cvs mailing list