2 Copyright (c) 2018-2019 Nokia.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 #ifndef SHAREDDATALAYER_FILEDESCRIPTOR_HPP_
18 #define SHAREDDATALAYER_FILEDESCRIPTOR_HPP_
22 namespace shareddatalayer
27 * @brief Wrapper for native file descriptor
29 * FileDescriptor provides a clever wrapper for native file descriptor and
30 * takes care of closing it in destructor.
36 * Take ownership of the given native file descriptor.
38 * @param fd The native file descriptor to wrap.
40 explicit FileDescriptor(int fd) noexcept;
43 * Take ownership of the given native file descriptor.
45 * @param fd The native file descriptor to wrap.
46 * @param system System instance to use.
48 FileDescriptor(System& system, int fd) noexcept;
51 * Move ownership of the given fd.
53 * @param fd The file descriptor to move.
55 FileDescriptor(FileDescriptor&& fd) noexcept;
58 * Move ownership of the given fd.
60 * @param fd The file descriptor to move.
62 FileDescriptor& operator = (FileDescriptor&& fd) noexcept;
65 * Close the wrapped native file descriptor. If <i>at close</i> function
66 * is set, then it is called just before closing the file descriptor.
73 * Get the wrapped native file descriptor.
75 * @return The wrapped native file descriptor.
77 operator int() const noexcept { return fd; }
80 * Set function to be called just before closing the native file
83 void atClose(std::function<void(int)>);
86 * Close the native file descriptor.
90 FileDescriptor(const FileDescriptor&) = delete;
91 FileDescriptor& operator = (const FileDescriptor&) = delete;
96 std::function<void(int)> atCloseCb;