RIC:1060: Change in PTL
[ric-plt/sdl.git] / include / private / filedescriptor.hpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
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
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #ifndef SHAREDDATALAYER_FILEDESCRIPTOR_HPP_
23 #define SHAREDDATALAYER_FILEDESCRIPTOR_HPP_
24
25 #include <functional>
26
27 namespace shareddatalayer
28 {
29     class System;
30
31     /**
32      * @brief Wrapper for native file descriptor
33      *
34      * FileDescriptor provides a clever wrapper for native file descriptor and
35      * takes care of closing it in destructor.
36      */
37     class FileDescriptor
38     {
39     public:
40         /**
41          * Take ownership of the given native file descriptor.
42          *
43          * @param fd The native file descriptor to wrap.
44          */
45         explicit FileDescriptor(int fd) noexcept;
46
47         /**
48          * Take ownership of the given native file descriptor.
49          *
50          * @param fd The native file descriptor to wrap.
51          * @param system System instance to use.
52          */
53         FileDescriptor(System& system, int fd) noexcept;
54
55         /**
56          * Move ownership of the given fd.
57          *
58          * @param fd The file descriptor to move.
59          */
60         FileDescriptor(FileDescriptor&& fd) noexcept;
61
62         /**
63          * Move ownership of the given fd.
64          *
65          * @param fd The file descriptor to move.
66          */
67         FileDescriptor& operator = (FileDescriptor&& fd) noexcept;
68
69         /**
70          * Close the wrapped native file descriptor. If <i>at close</i> function
71          * is set, then it is called just before closing the file descriptor.
72          *
73          * @see atClose
74          */
75         ~FileDescriptor();
76
77         /**
78          * Get the wrapped native file descriptor.
79          *
80          * @return The wrapped native file descriptor.
81          */
82         operator int() const noexcept { return fd; }
83
84         /**
85          * Set function to be called just before closing the native file
86          * descriptor.
87          */
88         void atClose(std::function<void(int)>);
89
90         /**
91          * Close the native file descriptor.
92          */
93         void close();
94
95         FileDescriptor(const FileDescriptor&) = delete;
96         FileDescriptor& operator = (const FileDescriptor&) = delete;
97
98     private:
99         System* system;
100         int fd;
101         std::function<void(int)> atCloseCb;
102     };
103 }
104
105 #endif