Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r50127 - in sandbox/dataflow-rewrite: boost/dataflow/managed libs/dataflow/build/managed libs/dataflow/build/xcodeide/dataflow.xcodeproj libs/dataflow/src/managed libs/dataflow/test/managed
From: stipe_at_[hidden]
Date: 2008-12-05 01:42:26


Author: srajko
Date: 2008-12-05 01:42:25 EST (Fri, 05 Dec 2008)
New Revision: 50127
URL: http://svn.boost.org/trac/boost/changeset/50127

Log:
added async network port updating
Added:
   sandbox/dataflow-rewrite/libs/dataflow/src/managed/network.cpp (contents, props changed)
   sandbox/dataflow-rewrite/libs/dataflow/test/managed/test_async_network.cpp (contents, props changed)
Text files modified:
   sandbox/dataflow-rewrite/boost/dataflow/managed/component.hpp | 1
   sandbox/dataflow-rewrite/boost/dataflow/managed/network.hpp | 97 ++++++++++++++++++---------------------
   sandbox/dataflow-rewrite/libs/dataflow/build/managed/Jamfile | 6 ++
   sandbox/dataflow-rewrite/libs/dataflow/build/xcodeide/dataflow.xcodeproj/project.pbxproj | 4 +
   sandbox/dataflow-rewrite/libs/dataflow/src/managed/component.cpp | 12 +++-
   sandbox/dataflow-rewrite/libs/dataflow/test/managed/Jamfile | 1
   6 files changed, 63 insertions(+), 58 deletions(-)

Modified: sandbox/dataflow-rewrite/boost/dataflow/managed/component.hpp
==============================================================================
--- sandbox/dataflow-rewrite/boost/dataflow/managed/component.hpp (original)
+++ sandbox/dataflow-rewrite/boost/dataflow/managed/component.hpp 2008-12-05 01:42:25 EST (Fri, 05 Dec 2008)
@@ -6,6 +6,7 @@
 #ifndef BOOST_DATAFLOW_MANAGED_COMPONENT_HPP
 #define BOOST_DATAFLOW_MANAGED_COMPONENT_HPP
 
+
 namespace boost { namespace dataflow { namespace managed {
 
 class network;

Modified: sandbox/dataflow-rewrite/boost/dataflow/managed/network.hpp
==============================================================================
--- sandbox/dataflow-rewrite/boost/dataflow/managed/network.hpp (original)
+++ sandbox/dataflow-rewrite/boost/dataflow/managed/network.hpp 2008-12-05 01:42:25 EST (Fri, 05 Dec 2008)
@@ -9,15 +9,20 @@
 #include <vector>
 #include <set>
 
-#include <iostream>
+#include <boost/dataflow/generic/port.hpp>
 #include <boost/dataflow/managed/component.hpp>
 #include <boost/graph/adjacency_list.hpp>
 #include <boost/graph/topological_sort.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/function.hpp>
 
-#include <boost/foreach.hpp>
+
+namespace boost { namespace asio { class io_service; }}
 
 namespace boost { namespace dataflow { namespace managed {
 
+template<typename T, typename PortCategory>
+class port;
 
 namespace detail {
     struct component_ptr_cmp
@@ -27,6 +32,22 @@
             return lhs->topological_sort_index() < rhs->topological_sort_index();
         }
     };
+
+ template<typename T>
+ struct set_port_value
+ {
+ set_port_value(port<T, ports::producer> &port, const T &t)
+ : port(port), t(t)
+ {}
+
+ void operator()()
+ {
+ port.set(t);
+ }
+ private:
+ port<T, ports::producer> &port;
+ T t;
+ };
 }
 
 class network
@@ -39,32 +60,13 @@
         boost::vecS, boost::vecS, boost::bidirectionalS, node_t> graph_type;
 
 public:
- void register_component(component *c)
- {
- graph_type::vertex_descriptor v = add_vertex(m_graph);
- std::cout << this << " registering " << c << " with descriptor " << v << std::endl;
- m_graph[v].ptr = c;
- m_descriptor_map[c]=v;
- }
- void unregister_component(component *c)
- {
- BOOST_ASSERT(m_descriptor_map.find(c) != m_descriptor_map.end());
- graph_type::vertex_descriptor v = m_descriptor_map[c];
- std::cout << this << " unregistering " << c << " with descriptor " << v << std::endl;
- remove_vertex(v, m_graph);
- m_descriptor_map.erase(c);
- // vertex_descriptors have been adjusted
- BOOST_FOREACH(descriptor_map_type::value_type &pair, m_descriptor_map)
- if(pair.second >= v)
- pair.second--;
- }
- void notify_connect(component &producer, component &consumer)
- {
- std::cout << this << " connecting " << &producer << " and " << &consumer << std::endl;
- std::cout << m_descriptor_map.size() << std::endl;
- add_edge(m_descriptor_map[&producer], m_descriptor_map[&consumer], m_graph);
- update_topological_sort();
- }
+ network(boost::shared_ptr<asio::io_service> service=boost::shared_ptr<asio::io_service>())
+ : m_io_service(service)
+ {}
+
+ void register_component(component *c);
+ void unregister_component(component *c);
+ void notify_connect(component &producer, component &consumer);
     void notify_change(component &changed)
     {
         m_changed.insert(&changed);
@@ -74,40 +76,29 @@
     {
         return m_changed;
     }
- typedef std::vector<component *> topological_sort_type;
- void topological_sort(const topological_sort_type &topological_sort)
- {
- m_changed.clear();
- for(topological_sort_type::const_iterator it=topological_sort.begin(); it!=topological_sort.end(); it++)
- (*it)->topological_sort_index(it - topological_sort.begin());
- for(topological_sort_type::const_iterator it=topological_sort.begin(); it!=topological_sort.end(); it++)
- m_changed.insert(*it);
- }
- void update_topological_sort()
+
+ void update_topological_sort();
+ void update();
+
+ void set_io_service(boost::shared_ptr<asio::io_service> service)
+ { m_io_service = service; }
+ template<typename T>
+ void async_set_port_value(port<T, ports::producer> &port, const T &t)
     {
- m_changed.clear();
- std::vector<graph_type::vertex_descriptor> topological_sort;
- boost::topological_sort(m_graph, std::back_inserter(topological_sort));
- std::reverse(topological_sort.begin(), topological_sort.end());
- for(std::vector<graph_type::vertex_descriptor>::iterator it=topological_sort.begin(); it!=topological_sort.end(); it++)
- m_graph[*it].ptr->topological_sort_index(it - topological_sort.begin());
- for(std::vector<graph_type::vertex_descriptor>::iterator it=topological_sort.begin(); it!=topological_sort.end(); it++)
- m_changed.insert(m_graph[*it].ptr);
+ post_async(detail::set_port_value<T>(port, t));
     }
- void update()
+ asio::io_service &io_service()
     {
- while(!m_changed.empty())
- {
- component *next = *m_changed.begin();
- m_changed.erase(m_changed.begin());
- next->invoke();
- }
+ return *m_io_service;
     }
 private:
+ void post_async(const boost::function<void()> &f);
+
     graph_type m_graph;
     typedef std::map<component *, graph_type::vertex_descriptor> descriptor_map_type;
     descriptor_map_type m_descriptor_map;
     changed_type m_changed;
+ boost::shared_ptr<asio::io_service> m_io_service;
 };
 
 inline void notify_change(component &c)

Modified: sandbox/dataflow-rewrite/libs/dataflow/build/managed/Jamfile
==============================================================================
--- sandbox/dataflow-rewrite/libs/dataflow/build/managed/Jamfile (original)
+++ sandbox/dataflow-rewrite/libs/dataflow/build/managed/Jamfile 2008-12-05 01:42:25 EST (Fri, 05 Dec 2008)
@@ -5,9 +5,13 @@
 
 project dataflow/managed
     : source-location ../../src/managed
+ : requirements
+ <library>/boost/system//boost_system
+ : usage-requirements
+ <library>/boost/system//boost_system
     ;
 
-SOURCES = component ;
+SOURCES = component network ;
 
 lib dataflow_managed : $(SOURCES).cpp
     :

Modified: sandbox/dataflow-rewrite/libs/dataflow/build/xcodeide/dataflow.xcodeproj/project.pbxproj
==============================================================================
--- sandbox/dataflow-rewrite/libs/dataflow/build/xcodeide/dataflow.xcodeproj/project.pbxproj (original)
+++ sandbox/dataflow-rewrite/libs/dataflow/build/xcodeide/dataflow.xcodeproj/project.pbxproj 2008-12-05 01:42:25 EST (Fri, 05 Dec 2008)
@@ -156,6 +156,8 @@
                 08A77AEE0E4FB4C800B8793E /* is_type.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = is_type.hpp; sourceTree = "<group>"; };
                 08A77B010E4FC51C00B8793E /* Jamfile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.jam; path = Jamfile; sourceTree = "<group>"; };
                 08A77B060E4FC58D00B8793E /* Jamfile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.jam; path = Jamfile; sourceTree = "<group>"; };
+ 08A7ADE90EE8FB6200921D0D /* network.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = network.cpp; sourceTree = "<group>"; };
+ 08A7AE3B0EE9016F00921D0D /* test_async_network.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = test_async_network.cpp; sourceTree = "<group>"; };
                 08AA903B0EAD3BF100409A2E /* factory_window.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = factory_window.hpp; sourceTree = "<group>"; };
                 08AA90410EAD43BA00409A2E /* Jamfile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.jam; path = Jamfile; sourceTree = "<group>"; };
                 08AA90440EAD43F700409A2E /* test_factory_window.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = test_factory_window.cpp; sourceTree = "<group>"; };
@@ -465,6 +467,7 @@
                         isa = PBXGroup;
                         children = (
                                 08AEB3CA0EE71604009245DB /* component.cpp */,
+ 08A7ADE90EE8FB6200921D0D /* network.cpp */,
                         );
                         path = managed;
                         sourceTree = "<group>";
@@ -497,6 +500,7 @@
                                 08AB471F0EE72D940068375D /* test_io_component.cpp */,
                                 08B3CC260EC8B823002D2124 /* test_network.cpp */,
                                 08B3CC270EC8B823002D2124 /* test_port.cpp */,
+ 08A7AE3B0EE9016F00921D0D /* test_async_network.cpp */,
                         );
                         path = managed;
                         sourceTree = "<group>";

Modified: sandbox/dataflow-rewrite/libs/dataflow/src/managed/component.cpp
==============================================================================
--- sandbox/dataflow-rewrite/libs/dataflow/src/managed/component.cpp (original)
+++ sandbox/dataflow-rewrite/libs/dataflow/src/managed/component.cpp 2008-12-05 01:42:25 EST (Fri, 05 Dec 2008)
@@ -1,7 +1,11 @@
-// Copyright 2008 Stjepan Rajko.
-// Distributed under the Boost Software License, Version 1.0. (See
-// accompanying file LICENSE_1_0.txt or copy at
-// http://www.boost.org/LICENSE_1_0.txt)
+/*=================================---------------------------------------------
+ Copyright 2008 Stjepan Rajko
+
+ Distributed under the Boost Software License, Version 1.0.
+ (See accompanying file LICENSE_1_0.txt or copy at
+ http://www.boost.org/LICENSE_1_0.txt)
+-----------------------------------------------===============================*/
+
 
 #include <boost/dataflow/managed/component.hpp>
 #include <boost/dataflow/managed/network.hpp>

Added: sandbox/dataflow-rewrite/libs/dataflow/src/managed/network.cpp
==============================================================================
--- (empty file)
+++ sandbox/dataflow-rewrite/libs/dataflow/src/managed/network.cpp 2008-12-05 01:42:25 EST (Fri, 05 Dec 2008)
@@ -0,0 +1,75 @@
+/*=================================---------------------------------------------
+ Copyright 2008 Stjepan Rajko
+
+ Distributed under the Boost Software License, Version 1.0.
+ (See accompanying file LICENSE_1_0.txt or copy at
+ http://www.boost.org/LICENSE_1_0.txt)
+-----------------------------------------------===============================*/
+
+
+#include <boost/dataflow/managed/network.hpp>
+#include <boost/asio/io_service.hpp>
+#include <boost/foreach.hpp>
+
+#include <iostream>
+
+namespace boost { namespace dataflow { namespace managed {
+
+void network::register_component(component *c)
+{
+ graph_type::vertex_descriptor v = add_vertex(m_graph);
+ std::cout << this << " registering " << c << " with descriptor " << v << std::endl;
+ m_graph[v].ptr = c;
+ m_descriptor_map[c]=v;
+}
+
+void network::unregister_component(component *c)
+{
+ BOOST_ASSERT(m_descriptor_map.find(c) != m_descriptor_map.end());
+ graph_type::vertex_descriptor v = m_descriptor_map[c];
+ std::cout << this << " unregistering " << c << " with descriptor " << v << std::endl;
+ remove_vertex(v, m_graph);
+ m_descriptor_map.erase(c);
+ // vertex_descriptors have been adjusted
+ BOOST_FOREACH(descriptor_map_type::value_type &pair, m_descriptor_map)
+ if(pair.second >= v)
+ pair.second--;
+}
+
+void network::notify_connect(component &producer, component &consumer)
+{
+ std::cout << this << " connecting " << &producer << " and " << &consumer << std::endl;
+ std::cout << m_descriptor_map.size() << std::endl;
+ add_edge(m_descriptor_map[&producer], m_descriptor_map[&consumer], m_graph);
+ update_topological_sort();
+}
+
+void network::update_topological_sort()
+{
+ m_changed.clear();
+ std::vector<graph_type::vertex_descriptor> topological_sort;
+ boost::topological_sort(m_graph, std::back_inserter(topological_sort));
+ std::reverse(topological_sort.begin(), topological_sort.end());
+ for(std::vector<graph_type::vertex_descriptor>::iterator it=topological_sort.begin(); it!=topological_sort.end(); it++)
+ m_graph[*it].ptr->topological_sort_index(it - topological_sort.begin());
+ for(std::vector<graph_type::vertex_descriptor>::iterator it=topological_sort.begin(); it!=topological_sort.end(); it++)
+ m_changed.insert(m_graph[*it].ptr);
+}
+
+void network::update()
+{
+ while(!m_changed.empty())
+ {
+ component *next = *m_changed.begin();
+ m_changed.erase(m_changed.begin());
+ next->invoke();
+ }
+}
+
+void network::post_async(const boost::function<void()> &f)
+{
+ m_io_service->post(f);
+}
+
+}}}
+

Modified: sandbox/dataflow-rewrite/libs/dataflow/test/managed/Jamfile
==============================================================================
--- sandbox/dataflow-rewrite/libs/dataflow/test/managed/Jamfile (original)
+++ sandbox/dataflow-rewrite/libs/dataflow/test/managed/Jamfile 2008-12-05 01:42:25 EST (Fri, 05 Dec 2008)
@@ -16,3 +16,4 @@
 run test_port.cpp ;
 run test_network.cpp ;
 run test_io_component.cpp ;
+run test_async_network.cpp ;

Added: sandbox/dataflow-rewrite/libs/dataflow/test/managed/test_async_network.cpp
==============================================================================
--- (empty file)
+++ sandbox/dataflow-rewrite/libs/dataflow/test/managed/test_async_network.cpp 2008-12-05 01:42:25 EST (Fri, 05 Dec 2008)
@@ -0,0 +1,36 @@
+// Copyright Stjepan Rajko 2008. Use, modification and
+// distribution is subject to the Boost Software License, Version
+// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+
+#include <boost/dataflow/managed/port.hpp>
+#include <boost/dataflow/managed/network.hpp>
+#include <boost/asio/io_service.hpp>
+
+#define BOOST_TEST_MAIN
+#include <boost/test/unit_test.hpp>
+
+namespace df=boost::dataflow;
+
+
+BOOST_AUTO_TEST_CASE( test )
+{
+ // our network
+ df::managed::network network(boost::shared_ptr<boost::asio::io_service>(new boost::asio::io_service()));
+
+ df::managed::component c(network);
+ df::managed::port<int, df::ports::producer> p(c);
+ p.set(0);
+
+ network.async_set_port_value(p, 1);
+
+ BOOST_CHECK_EQUAL(p.get(), 0);
+
+ network.io_service().run();
+
+ BOOST_CHECK_EQUAL(p.get(), 1);
+} // int test_main(int, char* [])
+
+
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk