Line data Source code
1 : #include <iostream>
2 : #include <iow/io/io_base.hpp>
3 : #include <iow/io/basic/aspect.hpp>
4 : #include <iow/io/rw/aspect.hpp>
5 : /*
6 : * #include <iow/io/reader/aspect.hpp>
7 : #include <iow/io/writer/aspect.hpp>
8 : */
9 : #include <iow/memory.hpp>
10 : #include <iow/asio.hpp>
11 :
12 : #include <fas/testing.hpp>
13 :
14 : #include <vector>
15 : #include <memory>
16 : #include <list>
17 : #include <cstdlib>
18 : #include <boost/concept_check.hpp>
19 : typedef std::vector<char> data_type;
20 : typedef std::unique_ptr<data_type> data_ptr;
21 :
22 0 : struct ad_read_some
23 : {
24 : template<typename T, typename D>
25 4 : void operator()(T& t, D d) const
26 : {
27 4 : if ( t.input.empty() )
28 5 : return;
29 :
30 : //auto dd = std::make_shared<typename T::input_t>( std::move(d) );
31 :
32 6 : t.service.post([&t, d](){
33 3 : auto dd = d;
34 3 : auto tmp = std::move(t.input.front());
35 3 : t.input.pop_front();
36 : /*std::copy(tmp->begin(), tmp->end(), (*dd)->begin());
37 : (*dd)->resize(tmp->size());
38 : */
39 3 : std::copy(tmp->begin(), tmp->end(), dd.first);
40 3 : dd.second = tmp->size();
41 :
42 3 : t.get_aspect().template get< ::iow::io::reader::_complete_>()(t, std::move(dd));
43 3 : });
44 : }
45 : };
46 :
47 0 : struct ad_write_some
48 : {
49 : template<typename T, typename P>
50 3 : void operator()(T& t, P p) const
51 : {
52 3 : if ( p.first == nullptr )
53 3 : return;
54 :
55 6 : t.service.post([&t, p](){
56 3 : t.result += std::string(p.first, p.first + p.second);
57 3 : t.get_aspect().template get< ::iow::io::writer::_complete_>()(t, std::move(p) /*.first, p.second*/ );
58 3 : });
59 : }
60 : };
61 :
62 10 : struct stream_options
63 : {
64 : //typedef ::iow::io::data_pool< data_type > buffer_pool_type;
65 : //typedef std::shared_ptr<buffer_pool_type> buffer_pool_ptr;
66 : typedef ::iow::io::write_buffer_options write_buffer_options;
67 : typedef ::iow::io::read_buffer_options read_buffer_options;
68 : //typedef ::iow::io::data_pool_options data_pool_options;
69 : write_buffer_options writer;
70 : read_buffer_options reader;
71 : //data_pool_options data_pool;
72 : };
73 :
74 :
75 1 : class stream
76 : : public ::iow::io::io_base< fas::aspect<
77 : fas::advice< ::iow::io::reader::_some_, ad_read_some>,
78 : fas::advice< ::iow::io::writer::_some_, ad_write_some>,
79 : ::iow::io::basic::aspect<std::recursive_mutex>::advice_list,
80 : ::iow::io::rw::aspect::advice_list,
81 : fas::type< ::iow::io::_options_type_, fas::empty_type >,
82 : fas::alias< ::iow::io::reader::data::_input_, ::iow::io::writer::_output_>,
83 : fas::group< ::iow::io::_initialize_, ::iow::io::rw::_initialize_>
84 : > >
85 : {
86 : public:
87 : typedef stream_options options_type;
88 : typedef data_ptr input_t;
89 : typedef data_ptr output_t;
90 :
91 1 : explicit stream(::iow::asio::io_service& io)
92 1 : : service(io)
93 1 : {}
94 :
95 1 : void start()
96 : {
97 1 : stream_options opt;
98 1 : opt.reader.sep.clear();
99 1 : opt.writer.sep.clear();
100 1 : this->start_(*this, opt );
101 1 : }
102 :
103 3 : void add(std::string val)
104 : {
105 3 : input.push_back( std::make_unique<data_type>(val.begin(), val.end()) );
106 3 : }
107 :
108 : ::iow::asio::io_service& service;
109 : std::list<data_ptr> input;
110 : std::string result;
111 : };
112 :
113 3 : UNIT(stream_unit, "")
114 : {
115 : using namespace fas::testing;
116 :
117 1 : ::iow::asio::io_service io;
118 2 : stream f(io);
119 :
120 1 : f.add("Hello ");
121 1 : f.add("world");
122 1 : f.add("!");
123 1 : f.start();
124 1 : io.run();
125 2 : t << equal<expect, std::string>(f.result, "Hello world!") << f.result << FAS_TESTING_FILE_LINE;
126 :
127 1 : }
128 :
129 1 : BEGIN_SUITE(stream,"")
130 : ADD_UNIT(stream_unit)
131 7 : END_SUITE(stream)
132 :
|