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/reader/aspect.hpp>
5 : #include <iow/io/writer/aspect.hpp>
6 : #include <iow/memory.hpp>
7 : #include <iow/asio.hpp>
8 :
9 : #include <fas/testing.hpp>
10 :
11 : #include <vector>
12 : #include <memory>
13 : #include <list>
14 : #include <cstdlib>
15 : #include <boost/concept_check.hpp>
16 : typedef std::vector<char> data_type;
17 : typedef std::unique_ptr<data_type> data_ptr;
18 :
19 2 : struct ad_read_some
20 : {
21 : template<typename T>
22 4 : void operator()(T& t, typename T::input_t d) const
23 : {
24 4 : if ( t.input.empty() )
25 5 : return;
26 :
27 3 : auto dd = std::make_shared<typename T::input_t>( std::move(d) );
28 27 : t.service.post([&t, dd](){
29 3 : auto tmp = std::move(t.input.front());
30 3 : t.input.pop_front();
31 3 : std::copy(tmp->begin(), tmp->end(), (*dd)->begin());
32 3 : (*dd)->resize(tmp->size());
33 3 : t.get_aspect().template get< ::iow::io::reader::_complete_>()(t, std::move(*dd));
34 6 : });
35 : }
36 : };
37 :
38 2 : struct ad_write_some
39 : {
40 : template<typename T, typename P>
41 6 : void operator()(T& t, P p/*, size_t size*/) const
42 : {
43 6 : if ( p.first == nullptr )
44 9 : return;
45 :
46 6 : t.service.post([&t, p](){
47 3 : t.result += std::string(p.first, p.first + p.second);
48 3 : t.get_aspect().template get< ::iow::io::writer::_complete_>()(t, std::move(p) /*.first, p.second*/ );
49 3 : });
50 : }
51 : };
52 :
53 1 : struct ad_input_factory
54 : {
55 : template<typename T>
56 4 : typename T::input_t operator()(T& t) const
57 : {
58 4 : if ( t.input.empty() )
59 1 : return nullptr;
60 3 : return std::make_unique<data_type>(100ul);
61 : }
62 : };
63 :
64 2 : struct ad_entry
65 : {
66 : template<typename T>
67 3 : void operator()(T& , typename T::output_t d)
68 : {
69 3 : this->data = std::move(d);
70 3 : }
71 : data_ptr data;
72 : };
73 :
74 1 : struct ad_can_write
75 : {
76 : template<typename T, typename P>
77 6 : bool operator()(T& , P /*p*/) const
78 : {
79 6 : return true;
80 : }
81 : };
82 :
83 : struct ad_free
84 : {
85 : template<typename T>
86 : void operator()(T& , typename T::output_t ) const
87 : {
88 : }
89 : };
90 :
91 1 : struct ad_confirm
92 : {
93 : template<typename T, typename D>
94 3 : void operator()(T& t, D /*, size_t*/ ) const
95 : {
96 3 : auto &d = t.get_aspect().template get< ::iow::io::writer::_attach_ >().data;
97 3 : d.reset();
98 : //return std::move(d);
99 3 : }
100 : };
101 :
102 1 : struct ad_next
103 : {
104 : template<typename T>
105 6 : std::pair<const char*, size_t> operator()(T& t) const
106 : {
107 6 : auto &d = t.get_aspect().template get< ::iow::io::writer::_attach_ >().data;
108 6 : if ( d == nullptr )
109 3 : return std::pair<const char*, size_t>(nullptr, 0);
110 3 : return std::make_pair( &((*d)[0]), d->size() );
111 : }
112 : };
113 :
114 :
115 :
116 1 : class writer1
117 : : public ::iow::io::io_base< fas::aspect<
118 : fas::alias< ::iow::io::reader::_confirm_, ::iow::io::writer::_output_>,
119 : fas::advice< ::iow::io::reader::_next_, ad_input_factory>,
120 : fas::advice< ::iow::io::writer::_attach_, ad_entry>,
121 : fas::advice< ::iow::io::writer::_next_, ad_next>,
122 : //fas::advice< ::iow::io::writer::_free_, ad_free>,
123 : fas::advice< ::iow::io::writer::_confirm_, ad_confirm>,
124 : fas::advice< ::iow::io::reader::_some_, ad_read_some>,
125 : fas::advice< ::iow::io::writer::_can_write_, ad_can_write>,
126 : fas::advice< ::iow::io::writer::_some_, ad_write_some>,
127 : fas::stub< ::iow::io::reader::_handler_>,
128 : fas::stub< ::iow::io::_initialize_ >,
129 : fas::type< ::iow::io::_options_type_, fas::empty_type >,
130 : ::iow::io::basic::aspect<std::recursive_mutex>::advice_list,
131 : ::iow::io::reader::aspect::advice_list,
132 : ::iow::io::writer::aspect::advice_list
133 : > >
134 : {
135 : public:
136 : typedef data_ptr input_t;
137 : typedef data_ptr output_t;
138 :
139 1 : explicit writer1(::iow::asio::io_service& io)
140 1 : : service(io)
141 1 : {}
142 :
143 1 : void start()
144 : {
145 1 : this->start_(*this, fas::empty_type() );
146 1 : }
147 :
148 3 : void add(std::string val)
149 : {
150 3 : input.push_back( std::make_unique<data_type>(val.begin(), val.end()) );
151 3 : }
152 :
153 : ::iow::asio::io_service& service;
154 : std::list<data_ptr> input;
155 : std::string result;
156 : };
157 :
158 3 : UNIT(pipe_unit, "")
159 : {
160 : using namespace fas::testing;
161 :
162 :
163 1 : ::iow::asio::io_service io;
164 2 : writer1 f(io);
165 :
166 1 : f.add("Hello ");
167 1 : f.add("world");
168 1 : f.add("!");
169 1 : f.start();
170 1 : io.run();
171 2 : t << equal<expect, std::string>(f.result, "Hello world!") << f.result << FAS_TESTING_FILE_LINE;
172 :
173 1 : }
174 :
175 1 : BEGIN_SUITE(pipe,"")
176 : ADD_UNIT(pipe_unit)
177 7 : END_SUITE(pipe)
178 :
|