312bc4e382aad664b0100affea69fa133a7e6735
[senf.git] / Socket / FileHandle.cci
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief FileHandle inline non-template implementation
25  */
26
27 //#include "FileHandle.ih"
28
29 // Custom includes
30 #include <errno.h>
31 #include "../Utils/Exception.hh"
32
33 #define prefix_ inline
34 ///////////////////////////////cci.p///////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::FileBody
38
39 prefix_ senf::FileBody::FileBody(int fd)
40     : fd_(fd)
41 {}
42
43 prefix_  senf::FileBody::~FileBody()
44 {
45     if (valid())
46         try {
47             close();
48         }
49         catch (...) {
50             terminate();
51         }
52 }
53
54 prefix_ void senf::FileBody::close()
55 {
56     if (!valid())
57         throw SystemException(EBADF);
58     v_close();
59     fd_ = -1;
60 }
61
62 prefix_ void senf::FileBody::terminate()
63 {
64     if (valid()) {
65         v_terminate();
66         fd_ = -1;
67     }
68 }
69
70 prefix_ int senf::FileBody::fd()
71     const
72 {
73     return fd_;
74 }
75
76 prefix_ void senf::FileBody::fd(int fd)
77 {
78     fd_ = fd;
79 }
80
81 prefix_ bool senf::FileBody::eof()
82     const
83 {
84     return v_eof();
85 }
86
87 prefix_ bool senf::FileBody::valid()
88     const
89 {
90     return fd_!=-1 && v_valid();
91 }
92
93 prefix_ bool senf::FileBody::readable()
94     const
95 {
96     return pollCheck(fd(),true);
97 }
98
99 prefix_ void senf::FileBody::waitReadable()
100     const
101 {
102     pollCheck(fd(),true,true);
103 }
104
105 prefix_ bool senf::FileBody::writeable()
106     const
107 {
108     return pollCheck(fd(),false);
109 }
110
111 prefix_ void senf::FileBody::waitWriteable()
112     const
113 {
114     pollCheck(fd(),false,true);
115 }
116
117 ///////////////////////////////////////////////////////////////////////////
118 // senf::FileHandle
119
120 prefix_ void senf::FileHandle::close()
121 {
122     body().close();
123 }
124
125 prefix_ void senf::FileHandle::terminate()
126 {
127     body().terminate();
128 }
129
130 prefix_ bool senf::FileHandle::readable()
131     const
132 {
133     return body().readable();
134 }
135
136 prefix_ void senf::FileHandle::waitReadable()
137     const
138 {
139     body().waitReadable();
140 }
141
142 prefix_ bool senf::FileHandle::writeable()
143     const
144 {
145     return body().writeable();
146 }
147
148 prefix_ void senf::FileHandle::waitWriteable()
149     const
150 {
151     body().waitWriteable();
152 }
153
154 prefix_ bool senf::FileHandle::blocking()
155     const
156 {
157     return body().blocking();
158 }
159
160 prefix_ void senf::FileHandle::blocking(bool status)
161 {
162     body().blocking(status);
163 }
164
165 prefix_ bool senf::FileHandle::eof()
166     const
167 {
168     return body().eof();
169 }
170
171 prefix_ bool senf::FileHandle::valid()
172     const
173 {
174     return body_ && body().valid();
175 }
176
177 prefix_ bool senf::FileHandle::boolean_test()
178     const
179 {
180     return valid() && !eof();
181 }
182
183 prefix_ int senf::FileHandle::fd()
184     const
185 {
186     return body().fd();
187 }
188
189 prefix_ senf::FileHandle::FileHandle()
190     : body_(0)
191 {}
192
193 prefix_  senf::FileHandle::FileHandle(std::auto_ptr<FileBody> body)
194     : body_(body.release())
195 {}
196
197 prefix_ senf::FileBody & senf::FileHandle::body()
198 {
199     BOOST_ASSERT(body_);
200     return *body_;
201 }
202
203 prefix_ senf::FileBody const & senf::FileHandle::body()
204     const
205 {
206     BOOST_ASSERT(body_);
207     return *body_;
208 }
209
210 prefix_ senf::FileBody & senf::FileHandle::body(FileHandle & handle)
211 {
212     return handle.body();
213 }
214
215 prefix_ senf::FileBody const &
216 senf::FileHandle::body(FileHandle const & handle)
217 {
218     return handle.body();
219 }
220
221 prefix_ void senf::FileHandle::fd(int fd)
222 {
223     body().fd(fd);
224 }
225
226 prefix_ senf::FileHandle::FileHandle
227 senf::FileHandle::cast_static(FileHandle handle)
228 {
229     return handle;
230 }
231
232 prefix_ senf::FileHandle
233 senf::FileHandle::cast_dynamic(FileHandle handle)
234 {
235     return handle;
236 }
237
238 prefix_ int senf::retrieve_filehandle(FileHandle handle)
239 {
240     return handle.fd();
241 }
242
243 ///////////////////////////////cci.e///////////////////////////////////////
244 #undef prefix_
245
246 \f
247 // Local Variables:
248 // mode: c++
249 // fill-column: 100
250 // c-file-style: "senf"
251 // indent-tabs-mode: nil
252 // ispell-local-dictionary: "american"
253 // compile-command: "scons -u test"
254 // comment-column: 40
255 // End: