4ace14c3be2921d9fda8659b5a3b7fc8cd6e1fb2
[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 "../Utils/senfassert.hh"
31 #include <errno.h>
32 #include "../Utils/Exception.hh"
33
34 #define prefix_ inline
35 ///////////////////////////////cci.p///////////////////////////////////////
36
37 ///////////////////////////////////////////////////////////////////////////
38 // senf::FileBody
39
40 prefix_ senf::FileBody::FileBody(int fd)
41     : fd_(fd)
42 {}
43
44 prefix_  senf::FileBody::~FileBody()
45 {
46     if (valid())
47         try {
48             close();
49         }
50         catch (...) {
51             terminate();
52         }
53 }
54
55 prefix_ void senf::FileBody::close()
56 {
57     if (!valid())
58         throwErrno(EBADF);
59     v_close();
60     fd_ = -1;
61 }
62
63 prefix_ void senf::FileBody::terminate()
64 {
65     if (valid()) {
66         v_terminate();
67         fd_ = -1;
68     }
69 }
70
71 prefix_ senf::FileHandle senf::FileBody::handle()
72 {
73     return FileHandle(ptr(this));
74 }
75
76 prefix_ int senf::FileBody::fd()
77     const
78 {
79     return fd_;
80 }
81
82 prefix_ void senf::FileBody::fd(int fd)
83 {
84     fd_ = fd;
85 }
86
87 prefix_ bool senf::FileBody::eof()
88     const
89 {
90     return v_eof();
91 }
92
93 prefix_ bool senf::FileBody::valid()
94     const
95 {
96     return fd_!=-1 && v_valid();
97 }
98
99 prefix_ bool senf::FileBody::readable()
100     const
101 {
102     return pollCheck(fd(),true);
103 }
104
105 prefix_ void senf::FileBody::waitReadable()
106     const
107 {
108     pollCheck(fd(),true,true);
109 }
110
111 prefix_ bool senf::FileBody::writeable()
112     const
113 {
114     return pollCheck(fd(),false);
115 }
116
117 prefix_ void senf::FileBody::waitWriteable()
118     const
119 {
120     pollCheck(fd(),false,true);
121 }
122
123 ///////////////////////////////////////////////////////////////////////////
124 // senf::FileHandle
125
126 prefix_ void senf::FileHandle::close()
127 {
128     body().close();
129 }
130
131 prefix_ void senf::FileHandle::terminate()
132 {
133     body().terminate();
134 }
135
136 prefix_ bool senf::FileHandle::readable()
137     const
138 {
139     return body().readable();
140 }
141
142 prefix_ void senf::FileHandle::waitReadable()
143     const
144 {
145     body().waitReadable();
146 }
147
148 prefix_ bool senf::FileHandle::writeable()
149     const
150 {
151     return body().writeable();
152 }
153
154 prefix_ void senf::FileHandle::waitWriteable()
155     const
156 {
157     body().waitWriteable();
158 }
159
160 prefix_ bool senf::FileHandle::blocking()
161     const
162 {
163     return body().blocking();
164 }
165
166 prefix_ void senf::FileHandle::blocking(bool status)
167 {
168     body().blocking(status);
169 }
170
171 prefix_ bool senf::FileHandle::eof()
172     const
173 {
174     return body().eof();
175 }
176
177 prefix_ bool senf::FileHandle::valid()
178     const
179 {
180     return body_ && body().valid();
181 }
182
183 prefix_ bool senf::FileHandle::boolean_test()
184     const
185 {
186     return valid() && !eof();
187 }
188
189 prefix_ int senf::FileHandle::fd()
190     const
191 {
192     return body().fd();
193 }
194
195 prefix_ senf::FileHandle::FileHandle()
196     : body_(0)
197 {}
198
199 prefix_  senf::FileHandle::FileHandle(std::auto_ptr<FileBody> body)
200     : body_(body.release())
201 {}
202
203 prefix_ senf::FileHandle::FileHandle(FileBody::ptr body)
204     : body_(body)
205 {}
206
207 prefix_ senf::FileBody & senf::FileHandle::body()
208 {
209     SENF_ASSERT(body_);
210     return *body_;
211 }
212
213 prefix_ senf::FileBody const & senf::FileHandle::body()
214     const
215 {
216     SENF_ASSERT(body_);
217     return *body_;
218 }
219
220 prefix_ senf::FileBody & senf::FileHandle::body(FileHandle & handle)
221 {
222     return handle.body();
223 }
224
225 prefix_ senf::FileBody const &
226 senf::FileHandle::body(FileHandle const & handle)
227 {
228     return handle.body();
229 }
230
231 prefix_ void senf::FileHandle::fd(int fd)
232 {
233     body().fd(fd);
234 }
235
236 prefix_ senf::FileHandle::FileHandle
237 senf::FileHandle::cast_static(FileHandle handle)
238 {
239     return handle;
240 }
241
242 prefix_ senf::FileHandle
243 senf::FileHandle::cast_dynamic(FileHandle handle)
244 {
245     return handle;
246 }
247
248 prefix_ int senf::retrieve_filehandle(FileHandle handle)
249 {
250     return handle.fd();
251 }
252
253 ///////////////////////////////cci.e///////////////////////////////////////
254 #undef prefix_
255
256 \f
257 // Local Variables:
258 // mode: c++
259 // fill-column: 100
260 // c-file-style: "senf"
261 // indent-tabs-mode: nil
262 // ispell-local-dictionary: "american"
263 // compile-command: "scons -u test"
264 // comment-column: 40
265 // End: