1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HDI_SHARED_FD
17 #define HDI_SHARED_FD
18 #include <fcntl.h>
19 #include <memory>
20 #include <unistd.h>
21 #include "display_log.h"
22 
23 namespace OHOS {
24 namespace HDI {
25 namespace DISPLAY {
26 class HdiFd {
27 public:
HdiFd()28     HdiFd()
29     {
30         DISPLAY_LOGD();
31     }
HdiFd(int fd)32     explicit HdiFd(int fd) : mFd(fd)
33     {
34         DISPLAY_LOGD("mFd %{public}d", mFd);
35     }
GetFd()36     int GetFd() const
37     {
38         return mFd;
39     };
40 
41     HdiFd &operator = (int fd)
42     {
43         if (mFd >= 0) {
44             close(mFd);
45         }
46         mFd = fd;
47         return *this;
48     }
49 
~HdiFd()50     virtual ~HdiFd()
51     {
52         if (mFd >= 0) {
53             close(mFd);
54         }
55     }
56 
57 private:
58     int mFd = -1;
59 };
60 
61 using FdPtr = std::shared_ptr<HdiFd>;
62 } // OHOS
63 } // HDIO
64 } // DISPLAY
65 
66 #endif // HDI_SHARED_FD
67