1 /*
2  * Copyright (c) 2021 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 "display_common.h"
21 
22 namespace OHOS {
23     namespace HDI {
24         namespace DISPLAY {
25             class HdiSharedFd {
26             public:
HdiSharedFd()27                 HdiSharedFd()
28                 {
29                     DISPLAY_DEBUGLOG();
30                 }
HdiSharedFd(int fd)31                 explicit HdiSharedFd(int fd) : mFd(fd)
32                 {
33                     DISPLAY_DEBUGLOG("mFd %{public}d", mFd);
34                 }
GetFd() const35                 int GetFd() const
36                 {
37                     return mFd;
38                 };
39 
operator =(int fd)40                 HdiSharedFd &operator=(int fd)
41                 {
42                     if (mFd >= 0) {
43                         close(mFd);
44                     }
45                     mFd = fd;
46                     return *this;
47                 }
48 
~HdiSharedFd()49                 virtual ~HdiSharedFd()
50                 {
51                     if (mFd >= 0) {
52                         close(mFd);
53                     }
54                 }
55 
56             private:
57                 int mFd = -1;
58             };
59 
60             using FdPtr = std::shared_ptr<HdiSharedFd>;
61         } // OHOS
62     }     // HDIO
63 } // DISPLAY
64 
65 #endif // HDI_SHARED_FD