NEW FILE HEADER / COPYRIGHT FORMAT
[senf.git] / Socket / FileHandle.cci
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS) 
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY 
6 //     Stefan Bund <g0dil@berlios.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         throwErrno(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_ senf::FileHandle senf::FileBody::handle()
71 {
72     return FileHandle(ptr(this));
73 }
74
75 prefix_ int senf::FileBody::fd()
76     const
77 {
78     return fd_;
79 }
80
81 prefix_ void senf::FileBody::fd(int fd)
82 {
83     fd_ = fd;
84 }
85
86 prefix_ bool senf::FileBody::eof()
87     const
88 {
89     return v_eof();
90 }
91
92 prefix_ bool senf::FileBody::valid()
93     const
94 {
95     return fd_!=-1 && v_valid();
96 }
97
98 prefix_ bool senf::FileBody::readable()
99     const
100 {
101     return pollCheck(fd(),true);
102 }
103
104 prefix_ void senf::FileBody::waitReadable()
105     const
106 {
107     pollCheck(fd(),true,true);
108 }
109
110 prefix_ bool senf::FileBody::writeable()
111     const
112 {
113     return pollCheck(fd(),false);
114 }
115
116 prefix_ void senf::FileBody::waitWriteable()
117     const
118 {
119     pollCheck(fd(),false,true);
120 }
121
122 ///////////////////////////////////////////////////////////////////////////
123 // senf::FileHandle
124
125 prefix_ void senf::FileHandle::close()
126 {
127     body().close();
128 }
129
130 prefix_ void senf::FileHandle::terminate()
131 {
132     body().terminate();
133 }
134
135 prefix_ bool senf::FileHandle::readable()
136     const
137 {
138     return body().readable();
139 }
140
141 prefix_ void senf::FileHandle::waitReadable()
142     const
143 {
144     body().waitReadable();
145 }
146
147 prefix_ bool senf::FileHandle::writeable()
148     const
149 {
150     return body().writeable();
151 }
152
153 prefix_ void senf::FileHandle::waitWriteable()
154     const
155 {
156     body().waitWriteable();
157 }
158
159 prefix_ bool senf::FileHandle::blocking()
160     const
161 {
162     return body().blocking();
163 }
164
165 prefix_ void senf::FileHandle::blocking(bool status)
166 {
167     body().blocking(status);
168 }
169
170 prefix_ bool senf::FileHandle::eof()
171     const
172 {
173     return body().eof();
174 }
175
176 prefix_ bool senf::FileHandle::valid()
177     const
178 {
179     return body_ && body().valid();
180 }
181
182 prefix_ bool senf::FileHandle::boolean_test()
183     const
184 {
185     return valid() && !eof();
186 }
187
188 prefix_ int senf::FileHandle::fd()
189     const
190 {
191     return body().fd();
192 }
193
194 prefix_ senf::FileHandle::FileHandle()
195     : body_(0)
196 {}
197
198 prefix_  senf::FileHandle::FileHandle(std::auto_ptr<FileBody> body)
199     : body_(body.release())
200 {}
201
202 prefix_ senf::FileHandle::FileHandle(FileBody::ptr body)
203     : body_(body)
204 {}
205
206 prefix_ senf::FileBody & senf::FileHandle::body()
207 {
208     BOOST_ASSERT(body_);
209     return *body_;
210 }
211
212 prefix_ senf::FileBody const & senf::FileHandle::body()
213     const
214 {
215     BOOST_ASSERT(body_);
216     return *body_;
217 }
218
219 prefix_ senf::FileBody & senf::FileHandle::body(FileHandle & handle)
220 {
221     return handle.body();
222 }
223
224 prefix_ senf::FileBody const &
225 senf::FileHandle::body(FileHandle const & handle)
226 {
227     return handle.body();
228 }
229
230 prefix_ void senf::FileHandle::fd(int fd)
231 {
232     body().fd(fd);
233 }
234
235 prefix_ senf::FileHandle::FileHandle
236 senf::FileHandle::cast_static(FileHandle handle)
237 {
238     return handle;
239 }
240
241 prefix_ senf::FileHandle
242 senf::FileHandle::cast_dynamic(FileHandle handle)
243 {
244     return handle;
245 }
246
247 prefix_ int senf::retrieve_filehandle(FileHandle handle)
248 {
249     return handle.fd();
250 }
251
252 ///////////////////////////////cci.e///////////////////////////////////////
253 #undef prefix_
254
255 \f
256 // Local Variables:
257 // mode: c++
258 // fill-column: 100
259 // c-file-style: "senf"
260 // indent-tabs-mode: nil
261 // ispell-local-dictionary: "american"
262 // compile-command: "scons -u test"
263 // comment-column: 40
264 // End: