FileHandle.cci
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief FileHandle inline non-template implementation
16  */
17 
18 //#include "FileHandle.ih"
19 
20 // Custom includes
21 #include <senf/Utils/senfassert.hh>
22 
23 #define prefix_ inline
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 
26 prefix_ void * senf::FileHandleAccess::extraPtr(FileHandle const & fh)
27 {
28  return fh.extraPtr();
29 }
30 
31 prefix_ void senf::FileHandleAccess::extraPtr(FileHandle fh, void * ptr)
32 {
33  fh.extraPtr(ptr);
34 }
35 
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37 // senf::FileBody
38 
39 prefix_ senf::FileBody::FileBody(int fd)
40  : fd_(fd), extraPtr_(nullptr)
41 {}
42 
43 prefix_ senf::FileHandle senf::FileBody::handle()
44 {
45  return FileHandle(ptr(this));
46 }
47 
48 prefix_ int senf::FileBody::fd()
49  const
50 {
51  return fd_;
52 }
53 
54 prefix_ void senf::FileBody::fd(int fd)
55 {
56  fd_ = fd;
57 }
58 
59 prefix_ void * senf::FileBody::extraPtr()
60  const
61 {
62  return extraPtr_;
63 }
64 
65 prefix_ void senf::FileBody::extraPtr(void * ptr)
66 {
67  extraPtr_ = ptr;
68 }
69 
70 prefix_ bool senf::FileBody::eof()
71  const
72 {
73  return v_eof();
74 }
75 
76 prefix_ bool senf::FileBody::valid()
77  const
78 {
79  return fd_!=-1 && v_valid();
80 }
81 
82 prefix_ bool senf::FileBody::readable()
83  const
84 {
85  return pollCheck(fd(),true,0);
86 }
87 
88 prefix_ bool senf::FileBody::waitReadable(senf::ClockService::clock_type timeout)
89  const
90 {
91  return pollCheck(fd(), true,
92  (timeout==senf::ClockService::clock_type(-1)?-1:senf::ClockService::in_milliseconds(timeout)) );
93 }
94 
95 prefix_ bool senf::FileBody::writeable()
96  const
97 {
98  return pollCheck(fd(),false,0);
99 }
100 
101 prefix_ bool senf::FileBody::waitWriteable(senf::ClockService::clock_type timeout)
102  const
103 {
104  return pollCheck(fd(), false,
105  (timeout==senf::ClockService::clock_type(-1)?-1:senf::ClockService::in_milliseconds(timeout)) );
106 }
107 
108 prefix_ bool senf::FileBody::oobReadable()
109  const
110 {
111  return pollCheck(fd(),true,0,true);
112 }
113 
114 prefix_ bool senf::FileBody::waitOOBReadable(senf::ClockService::clock_type timeout)
115  const
116 {
117  return pollCheck(fd(), true,
118  (timeout==senf::ClockService::clock_type(-1)?-1:senf::ClockService::in_milliseconds(timeout)), true);
119 }
120 
121 //-/////////////////////////////////////////////////////////////////////////////////////////////////
122 // senf::FileHandle
123 
124 prefix_ senf::FileBody & senf::FileHandle::body()
125 {
126  SENF_ASSERT(body_, "dereferencing in-valid() FileHandle");
127  return *body_;
128 }
129 
130 prefix_ senf::FileBody const & senf::FileHandle::body()
131  const
132 {
133  SENF_ASSERT(body_, "dereferencing in-valid() FileHandle");
134  return *body_;
135 }
136 
137 prefix_ void senf::FileHandle::close()
138 {
139  body().close();
140 }
141 
142 prefix_ void senf::FileHandle::terminate()
143 {
144  body().terminate();
145 }
146 
147 prefix_ bool senf::FileHandle::readable()
148  const
149 {
150  return body().readable();
151 }
152 
153 prefix_ bool senf::FileHandle::waitReadable(senf::ClockService::clock_type timeout)
154  const
155 {
156  return body().waitReadable(timeout);
157 }
158 
159 prefix_ bool senf::FileHandle::writeable()
160  const
161 {
162  return body().writeable();
163 }
164 
165 prefix_ bool senf::FileHandle::waitWriteable(senf::ClockService::clock_type timeout)
166  const
167 {
168  return body().waitWriteable(timeout);
169 }
170 
171 prefix_ bool senf::FileHandle::oobReadable()
172  const
173 {
174  return body().oobReadable();
175 }
176 
177 prefix_ bool senf::FileHandle::waitOOBReadable(senf::ClockService::clock_type timeout)
178  const
179 {
180  return body().waitOOBReadable(timeout);
181 }
182 
183 prefix_ bool senf::FileHandle::blocking()
184  const
185 {
186  return body().blocking();
187 }
188 
189 prefix_ void senf::FileHandle::blocking(bool status)
190 {
191  body().blocking(status);
192 }
193 
194 prefix_ bool senf::FileHandle::eof()
195  const
196 {
197  return body().eof();
198 }
199 
200 prefix_ bool senf::FileHandle::valid()
201  const
202 {
203  return body_ && body().valid();
204 }
205 
206 prefix_ bool senf::FileHandle::boolean_test()
207  const
208 {
209  return valid() && !eof();
210 }
211 
212 prefix_ int senf::FileHandle::fd()
213  const
214 {
215  return body().fd();
216 }
217 
218 prefix_ int senf::FileHandle::refcount()
219  const
220 {
221  return valid() ? body().refcount() : 0;
222 }
223 
224 prefix_ bool senf::FileHandle::is_shared()
225  const
226 {
227  return valid() ? body().is_shared() : false;
228 }
229 
230 prefix_ senf::FileHandle::FileHandle()
231  : body_(nullptr)
232 {}
233 
234 prefix_ senf::FileHandle::~FileHandle()
235 {
236  if (body_ && ! body().is_shared())
237  body().destroyClose();
238 }
239 
240 prefix_ senf::FileHandle::FileHandle(std::unique_ptr<FileBody> body)
241  : body_(body.release())
242 {}
243 
244 prefix_ senf::FileHandle::FileHandle(FileBody::ptr body)
245  : body_(body)
246 {}
247 
248 prefix_ senf::FileBody & senf::FileHandle::body(FileHandle & handle)
249 {
250  return handle.body();
251 }
252 
253 prefix_ senf::FileBody const & senf::FileHandle::body(FileHandle const & handle)
254 {
255  return handle.body();
256 }
257 
258 prefix_ void senf::FileHandle::fd(int fd)
259 {
260  body().fd(fd);
261 }
262 
263 prefix_ void * senf::FileHandle::extraPtr()
264  const
265 {
266  return body().extraPtr();
267 }
268 
269 prefix_ void senf::FileHandle::extraPtr(void * ptr)
270 {
271  body().extraPtr(ptr);
272 }
273 
274 prefix_ senf::FileHandle senf::FileHandle::cast_static(FileHandle handle)
275 {
276  return handle;
277 }
278 
279 prefix_ senf::FileHandle senf::FileHandle::cast_dynamic(FileHandle handle)
280 {
281  return handle;
282 }
283 
284 prefix_ int senf::retrieve_filehandle(FileHandle handle)
285 {
286  return handle.fd();
287 }
288 
289 //-/////////////////////////////////////////////////////////////////////////////////////////////////
290 #undef prefix_
291 
292 
293 // Local Variables:
294 // mode: c++
295 // fill-column: 100
296 // c-file-style: "senf"
297 // indent-tabs-mode: nil
298 // ispell-local-dictionary: "american"
299 // compile-command: "scons -u test"
300 // comment-column: 40
301 // End: