-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_base.h
118 lines (109 loc) · 3.44 KB
/
io_base.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// (C) Copyright 2009-2011 Vit Kasal
//
// 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)
#pragma once
#include <boost/mpl/deque.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/push_back.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/end.hpp>
#include <boost/mpl/is_sequence.hpp>
#include <boost/asio/serial_port_service.hpp>
#include <boost/thread.hpp>
namespace si
{
struct io_base
{
// typedef std::shared_ptr<boost::asio::io_service> service_pointer;
struct extended_service_ptr: public boost::shared_ptr<boost::asio::io_service>
{
typedef boost::shared_ptr<boost::asio::io_service> base_type;
extended_service_ptr()
{}
extended_service_ptr(base_type::element_type *element)
: base_type(element)
{}
extended_service_ptr(base_type const& base)
: base_type(base)
{}
operator base_type::element_type&()
{
return **(base_type*)this;
}
~extended_service_ptr()
{
//std::cout << "~io_service&" << this->use_count() << std::endl;
}
};
//typedef boost::shared_ptr<boost::asio::io_service> service_pointer;
typedef boost::shared_ptr<io_base> pointer;
typedef boost::shared_ptr<boost::asio::io_service> service_pointer;
typedef boost::shared_ptr<boost::asio::serial_port_service> serial_service_pointer;
typedef boost::shared_ptr<boost::asio::io_service::work> work_pointer;
typedef boost::shared_ptr<boost::thread> thread_pointer;
io_base(io_base::service_pointer service_ = io_base::service_pointer())
: service(service_)
{
if(!service)
{
service.reset(new service_pointer::element_type());
work.reset(new io_base::work_pointer::element_type(*service));
thread.reset(new io_base::thread_pointer::element_type
(boost::bind(&service_pointer::element_type::run, service.get())));
serial_service.reset(new serial_service_pointer::element_type(*service));
}
}
~io_base()
{
work.reset();
if(thread && (thread->joinable()))
{
service->stop();
service->reset();
thread->interrupt();
// thread->join();
serial_service.reset();
thread.reset();
}
}
service_pointer service;
serial_service_pointer serial_service;
work_pointer work;
thread_pointer thread;
//*/
};
/*
template<typename bases_tt, typename enabled = void> struct bases
: public boost::mpl::front<bases_tt>::type
, public bases<typename boost::mpl::pop_front<bases_tt>::type>
{
typedef typename boost::mpl::front<bases_tt>::type item_base_type;
typedef bases<typename boost::mpl::pop_front<bases_tt>::type> remaining_bases_type;
bases(io_base::service_pointer service)
: item_base_type(service)
, remaining_bases_type(service)
{}
};
template<typename bases_tt> struct bases
<bases_tt, typename boost::enable_if<typename boost::mpl::empty<bases_tt>::type>::type>
{
bases(io_base::service_pointer)
{}
};
template<typename base_tt> struct io_bases
: public io_base
, public bases<base_tt>
{
io_bases(io_base::service_pointer service_ = io_base::service_pointer())
: io_base(service_)
, bases<base_tt>(io_base::service)
{
}
};
*/
}//namespace si