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
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
15 \brief FileHandle inline non-template implementation
18 //#include "FileHandle.ih"
21 #include <senf/Utils/senfassert.hh>
23 #define prefix_ inline
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 prefix_ void * senf::FileHandleAccess::extraPtr(FileHandle const & fh)
31 prefix_ void senf::FileHandleAccess::extraPtr(FileHandle fh, void * ptr)
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39 prefix_ senf::FileBody::FileBody(int fd)
40 : fd_(fd), extraPtr_(nullptr)
43 prefix_ senf::FileHandle senf::FileBody::handle()
45 return FileHandle(ptr(this));
48 prefix_ int senf::FileBody::fd()
54 prefix_ void senf::FileBody::fd(int fd)
59 prefix_ void * senf::FileBody::extraPtr()
65 prefix_ void senf::FileBody::extraPtr(void * ptr)
70 prefix_ bool senf::FileBody::eof()
76 prefix_ bool senf::FileBody::valid()
79 return fd_!=-1 && v_valid();
82 prefix_ bool senf::FileBody::readable()
85 return pollCheck(fd(),true,0);
88 prefix_ bool senf::FileBody::waitReadable(senf::ClockService::clock_type timeout)
91 return pollCheck(fd(), true,
92 (timeout==senf::ClockService::clock_type(-1)?-1:senf::ClockService::in_milliseconds(timeout)) );
95 prefix_ bool senf::FileBody::writeable()
98 return pollCheck(fd(),false,0);
101 prefix_ bool senf::FileBody::waitWriteable(senf::ClockService::clock_type timeout)
104 return pollCheck(fd(), false,
105 (timeout==senf::ClockService::clock_type(-1)?-1:senf::ClockService::in_milliseconds(timeout)) );
108 prefix_ bool senf::FileBody::oobReadable()
111 return pollCheck(fd(),true,0,true);
114 prefix_ bool senf::FileBody::waitOOBReadable(senf::ClockService::clock_type timeout)
117 return pollCheck(fd(), true,
118 (timeout==senf::ClockService::clock_type(-1)?-1:senf::ClockService::in_milliseconds(timeout)), true);
121 //-/////////////////////////////////////////////////////////////////////////////////////////////////
124 prefix_ senf::FileBody & senf::FileHandle::body()
126 SENF_ASSERT(body_, "dereferencing in-valid() FileHandle");
130 prefix_ senf::FileBody const & senf::FileHandle::body()
133 SENF_ASSERT(body_, "dereferencing in-valid() FileHandle");
137 prefix_ void senf::FileHandle::close()
142 prefix_ void senf::FileHandle::terminate()
147 prefix_ bool senf::FileHandle::readable()
150 return body().readable();
153 prefix_ bool senf::FileHandle::waitReadable(senf::ClockService::clock_type timeout)
156 return body().waitReadable(timeout);
159 prefix_ bool senf::FileHandle::writeable()
162 return body().writeable();
165 prefix_ bool senf::FileHandle::waitWriteable(senf::ClockService::clock_type timeout)
168 return body().waitWriteable(timeout);
171 prefix_ bool senf::FileHandle::oobReadable()
174 return body().oobReadable();
177 prefix_ bool senf::FileHandle::waitOOBReadable(senf::ClockService::clock_type timeout)
180 return body().waitOOBReadable(timeout);
183 prefix_ bool senf::FileHandle::blocking()
186 return body().blocking();
189 prefix_ void senf::FileHandle::blocking(bool status)
191 body().blocking(status);
194 prefix_ bool senf::FileHandle::eof()
200 prefix_ bool senf::FileHandle::valid()
203 return body_ && body().valid();
206 prefix_ bool senf::FileHandle::boolean_test()
209 return valid() && !eof();
212 prefix_ int senf::FileHandle::fd()
218 prefix_ int senf::FileHandle::refcount()
221 return valid() ? body().refcount() : 0;
224 prefix_ bool senf::FileHandle::is_shared()
227 return valid() ? body().is_shared() : false;
230 prefix_ senf::FileHandle::FileHandle()
234 prefix_ senf::FileHandle::~FileHandle()
236 if (body_ && ! body().is_shared())
237 body().destroyClose();
240 prefix_ senf::FileHandle::FileHandle(std::unique_ptr<FileBody> body)
241 : body_(body.release())
244 prefix_ senf::FileHandle::FileHandle(FileBody::ptr body)
248 prefix_ senf::FileBody & senf::FileHandle::body(FileHandle & handle)
250 return handle.body();
253 prefix_ senf::FileBody const & senf::FileHandle::body(FileHandle const & handle)
255 return handle.body();
258 prefix_ void senf::FileHandle::fd(int fd)
263 prefix_ void * senf::FileHandle::extraPtr()
266 return body().extraPtr();
269 prefix_ void senf::FileHandle::extraPtr(void * ptr)
271 body().extraPtr(ptr);
274 prefix_ senf::FileHandle senf::FileHandle::cast_static(FileHandle handle)
279 prefix_ senf::FileHandle senf::FileHandle::cast_dynamic(FileHandle handle)
284 prefix_ int senf::retrieve_filehandle(FileHandle handle)
289 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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