13f4cbf05Sopenharmony_ci/*
23f4cbf05Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License.
53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at
63f4cbf05Sopenharmony_ci *
73f4cbf05Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83f4cbf05Sopenharmony_ci *
93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and
133f4cbf05Sopenharmony_ci * limitations under the License.
143f4cbf05Sopenharmony_ci */
153f4cbf05Sopenharmony_ci
163f4cbf05Sopenharmony_ci/**
173f4cbf05Sopenharmony_ci * @file unique_fd.h
183f4cbf05Sopenharmony_ci *
193f4cbf05Sopenharmony_ci * @brief Provides APIs to manage file descriptors (FDs) implemented in c_utils.
203f4cbf05Sopenharmony_ci *
213f4cbf05Sopenharmony_ci * The manager class `UniqueFdAddDeletor`,
223f4cbf05Sopenharmony_ci * the default deleter class `DefaultDeleter`,
233f4cbf05Sopenharmony_ci * and related global overloaded operator functions are provided.
243f4cbf05Sopenharmony_ci */
253f4cbf05Sopenharmony_ci#ifndef UNIQUE_FD_H
263f4cbf05Sopenharmony_ci#define UNIQUE_FD_H
273f4cbf05Sopenharmony_ci
283f4cbf05Sopenharmony_ci#include <unistd.h>
293f4cbf05Sopenharmony_ci
303f4cbf05Sopenharmony_cinamespace OHOS {
313f4cbf05Sopenharmony_ci
323f4cbf05Sopenharmony_ci/**
333f4cbf05Sopenharmony_ci * @brief Provides the default implementation for a deleter,
343f4cbf05Sopenharmony_ci * including a static function to close FDs.
353f4cbf05Sopenharmony_ci *
363f4cbf05Sopenharmony_ci * The deleter is used for closing FDs. You can implement a deleter to
373f4cbf05Sopenharmony_ci * deal with a different scenario. When `Deleter::Close()` is called to enable
383f4cbf05Sopenharmony_ci * a `UniqueFdAddDeletor` object to release the management of an FD,
393f4cbf05Sopenharmony_ci * the FD can no longer be taken over by other `UniqueFdAddDeletor` objects.
403f4cbf05Sopenharmony_ci */
413f4cbf05Sopenharmony_ciclass DefaultDeleter {
423f4cbf05Sopenharmony_cipublic:
433f4cbf05Sopenharmony_ci    /**
443f4cbf05Sopenharmony_ci     * @brief Default function to close an FD.
453f4cbf05Sopenharmony_ci     *
463f4cbf05Sopenharmony_ci     * Call `close()` if the input FD is valid (greater than or equal to 0).
473f4cbf05Sopenharmony_ci     *
483f4cbf05Sopenharmony_ci     * @param fd Indicates an FD.
493f4cbf05Sopenharmony_ci     */
503f4cbf05Sopenharmony_ci    static void Close(int fd)
513f4cbf05Sopenharmony_ci    {
523f4cbf05Sopenharmony_ci        if (fd >= 0) {
533f4cbf05Sopenharmony_ci            close(fd);
543f4cbf05Sopenharmony_ci        }
553f4cbf05Sopenharmony_ci    }
563f4cbf05Sopenharmony_ci};
573f4cbf05Sopenharmony_ci
583f4cbf05Sopenharmony_citemplate <typename Deleter>
593f4cbf05Sopenharmony_ciclass UniqueFdAddDeletor;
603f4cbf05Sopenharmony_citemplate <typename Deleter>
613f4cbf05Sopenharmony_cibool operator==(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
623f4cbf05Sopenharmony_citemplate <typename Deleter>
633f4cbf05Sopenharmony_cibool operator!=(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
643f4cbf05Sopenharmony_citemplate <typename Deleter>
653f4cbf05Sopenharmony_cibool operator>=(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
663f4cbf05Sopenharmony_citemplate <typename Deleter>
673f4cbf05Sopenharmony_cibool operator>(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
683f4cbf05Sopenharmony_citemplate <typename Deleter>
693f4cbf05Sopenharmony_cibool operator<=(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
703f4cbf05Sopenharmony_citemplate <typename Deleter>
713f4cbf05Sopenharmony_cibool operator<(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
723f4cbf05Sopenharmony_ci
733f4cbf05Sopenharmony_ci/**
743f4cbf05Sopenharmony_ci * @brief Defines an FD manager.
753f4cbf05Sopenharmony_ci *
763f4cbf05Sopenharmony_ci * To ensure unique management on an FD, avoid the double-close issue,
773f4cbf05Sopenharmony_ci * which may cause a file to be incorrectly closed./n
783f4cbf05Sopenharmony_ci * The management of an FD can be delivered between `UniqueFdAddDeletor`
793f4cbf05Sopenharmony_ci * objects. An FD will be closed if no `UniqueFdAddDeletor` object is available
803f4cbf05Sopenharmony_ci * to take over its management.
813f4cbf05Sopenharmony_ci *
823f4cbf05Sopenharmony_ci * @tparam Deleter Indicates a deleter.
833f4cbf05Sopenharmony_ci * @see DefaultDeleter
843f4cbf05Sopenharmony_ci */
853f4cbf05Sopenharmony_citemplate <typename Deleter = DefaultDeleter>
863f4cbf05Sopenharmony_ciclass UniqueFdAddDeletor final {
873f4cbf05Sopenharmony_ci    friend bool operator==<Deleter>(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
883f4cbf05Sopenharmony_ci
893f4cbf05Sopenharmony_ci    friend bool operator!=<Deleter>(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
903f4cbf05Sopenharmony_ci
913f4cbf05Sopenharmony_ci    friend bool operator>=<Deleter>(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
923f4cbf05Sopenharmony_ci
933f4cbf05Sopenharmony_ci    friend bool operator><Deleter>(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
943f4cbf05Sopenharmony_ci
953f4cbf05Sopenharmony_ci    friend bool operator<=<Deleter>(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
963f4cbf05Sopenharmony_ci
973f4cbf05Sopenharmony_ci    friend bool operator< <Deleter>(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs);
983f4cbf05Sopenharmony_ci
993f4cbf05Sopenharmony_cipublic:
1003f4cbf05Sopenharmony_ci    /**
1013f4cbf05Sopenharmony_ci     * @brief Creates a `UniqueFdAddDeletor` object to manage an FD.
1023f4cbf05Sopenharmony_ci     *
1033f4cbf05Sopenharmony_ci     * @param value Indicates the FD to be managed.
1043f4cbf05Sopenharmony_ci     */
1053f4cbf05Sopenharmony_ci    explicit UniqueFdAddDeletor(const int& value)
1063f4cbf05Sopenharmony_ci        : fd_(value)
1073f4cbf05Sopenharmony_ci    {
1083f4cbf05Sopenharmony_ci    }
1093f4cbf05Sopenharmony_ci
1103f4cbf05Sopenharmony_ci    /**
1113f4cbf05Sopenharmony_ci     * @brief Constructor used to create a `UniqueFdAddDeletor` object
1123f4cbf05Sopenharmony_ci     * with the FD set to `-1`.
1133f4cbf05Sopenharmony_ci     */
1143f4cbf05Sopenharmony_ci    UniqueFdAddDeletor()
1153f4cbf05Sopenharmony_ci        : fd_(-1)
1163f4cbf05Sopenharmony_ci    {
1173f4cbf05Sopenharmony_ci    }
1183f4cbf05Sopenharmony_ci
1193f4cbf05Sopenharmony_ci    /**
1203f4cbf05Sopenharmony_ci     * @brief Destructor used to destroy this `UniqueFdAddDeletor` object.
1213f4cbf05Sopenharmony_ci     *
1223f4cbf05Sopenharmony_ci     * This function is used to close the FD and set the FD to `-1`.
1233f4cbf05Sopenharmony_ci     */
1243f4cbf05Sopenharmony_ci    ~UniqueFdAddDeletor() { Reset(-1); }
1253f4cbf05Sopenharmony_ci
1263f4cbf05Sopenharmony_ci    /**
1273f4cbf05Sopenharmony_ci     * @brief Releases the management on the current FD and sets it to `-1`.
1283f4cbf05Sopenharmony_ci     *
1293f4cbf05Sopenharmony_ci     * @return Returns the original FD before release.
1303f4cbf05Sopenharmony_ci     * @note The released FD needs to be taken over by another
1313f4cbf05Sopenharmony_ci     * `UniqueFdAddDeletor` object; otherwise, it must be closed manually.
1323f4cbf05Sopenharmony_ci     */
1333f4cbf05Sopenharmony_ci    int Release()
1343f4cbf05Sopenharmony_ci    {
1353f4cbf05Sopenharmony_ci        int tmp = fd_;
1363f4cbf05Sopenharmony_ci        fd_ = -1;
1373f4cbf05Sopenharmony_ci        return tmp;
1383f4cbf05Sopenharmony_ci    }
1393f4cbf05Sopenharmony_ci
1403f4cbf05Sopenharmony_ci    // this is dangerous, when you use it , you should know it,
1413f4cbf05Sopenharmony_ci    // do not operator on the ret
1423f4cbf05Sopenharmony_ci    /**
1433f4cbf05Sopenharmony_ci     * @brief An overloaded cast operator function.
1443f4cbf05Sopenharmony_ci     *
1453f4cbf05Sopenharmony_ci     * This function will be called when passing a `UniqueFdAddDeletor` object
1463f4cbf05Sopenharmony_ci     * to a function that requires a parameter of `int`.
1473f4cbf05Sopenharmony_ci     *
1483f4cbf05Sopenharmony_ci     * @return Returns the current FD under management.
1493f4cbf05Sopenharmony_ci     */
1503f4cbf05Sopenharmony_ci    operator int() const { return Get(); } // NOLINT
1513f4cbf05Sopenharmony_ci
1523f4cbf05Sopenharmony_ci    // this is dangerous, when you use it , you should know it, do not operator
1533f4cbf05Sopenharmony_ci    // on the ret
1543f4cbf05Sopenharmony_ci    /**
1553f4cbf05Sopenharmony_ci     * @brief Obtains the current FD under management, without releasing it.
1563f4cbf05Sopenharmony_ci     *
1573f4cbf05Sopenharmony_ci     * @return Returns the current FD.
1583f4cbf05Sopenharmony_ci     */
1593f4cbf05Sopenharmony_ci    int Get() const
1603f4cbf05Sopenharmony_ci    {
1613f4cbf05Sopenharmony_ci        return fd_;
1623f4cbf05Sopenharmony_ci    }
1633f4cbf05Sopenharmony_ci
1643f4cbf05Sopenharmony_ci    // we need move fd from one to another
1653f4cbf05Sopenharmony_ci    /**
1663f4cbf05Sopenharmony_ci     * @brief Move constructor used to deliver the management of an FD from a
1673f4cbf05Sopenharmony_ci     * `UniqueFdAddDeletor` object to this object.
1683f4cbf05Sopenharmony_ci     *
1693f4cbf05Sopenharmony_ci     * @param rhs Indicates the source `UniqueFdAddDeletor` object.
1703f4cbf05Sopenharmony_ci     */
1713f4cbf05Sopenharmony_ci    UniqueFdAddDeletor(UniqueFdAddDeletor&& rhs)
1723f4cbf05Sopenharmony_ci    {
1733f4cbf05Sopenharmony_ci        int rhsfd = rhs.Release();
1743f4cbf05Sopenharmony_ci        fd_ = rhsfd;
1753f4cbf05Sopenharmony_ci    }
1763f4cbf05Sopenharmony_ci
1773f4cbf05Sopenharmony_ci    /**
1783f4cbf05Sopenharmony_ci     * @brief Overloaded move assignment operator function used to deliver the
1793f4cbf05Sopenharmony_ci     * management of an FD from a `UniqueFdAddDeletor` object to this object.
1803f4cbf05Sopenharmony_ci     *
1813f4cbf05Sopenharmony_ci     * @param rhs Indicates the source `UniqueFdAddDeletor` object.
1823f4cbf05Sopenharmony_ci     * @return Returns this `UniqueFdAddDeletor` object.
1833f4cbf05Sopenharmony_ci     * @note The current FD manged by this `UniqueFdAddDeletor` object will be
1843f4cbf05Sopenharmony_ci     * closed, and this object will take over
1853f4cbf05Sopenharmony_ci     * the FD originally managed by `rhs`.
1863f4cbf05Sopenharmony_ci     */
1873f4cbf05Sopenharmony_ci    UniqueFdAddDeletor& operator=(UniqueFdAddDeletor&& rhs)
1883f4cbf05Sopenharmony_ci    {
1893f4cbf05Sopenharmony_ci        int rhsfd = rhs.Release();
1903f4cbf05Sopenharmony_ci        Reset(rhsfd);
1913f4cbf05Sopenharmony_ci        return *this;
1923f4cbf05Sopenharmony_ci    }
1933f4cbf05Sopenharmony_ci
1943f4cbf05Sopenharmony_ci    /**
1953f4cbf05Sopenharmony_ci     * @brief Checks whether the FD managed by this object and that managed by
1963f4cbf05Sopenharmony_ci     * the source object are equal.
1973f4cbf05Sopenharmony_ci     *
1983f4cbf05Sopenharmony_ci     * @param rhs Indicates the source `UniqueFdAddDeletor` object.
1993f4cbf05Sopenharmony_ci     *
2003f4cbf05Sopenharmony_ci     * @return Returns `true` if the two FDs are equal; returns `false`
2013f4cbf05Sopenharmony_ci     * otherwise.
2023f4cbf05Sopenharmony_ci     */
2033f4cbf05Sopenharmony_ci    bool operator==(const int& rhs) const
2043f4cbf05Sopenharmony_ci    {
2053f4cbf05Sopenharmony_ci        return fd_ == rhs;
2063f4cbf05Sopenharmony_ci    }
2073f4cbf05Sopenharmony_ci
2083f4cbf05Sopenharmony_ci    /**
2093f4cbf05Sopenharmony_ci     * @brief Checks whether the FD managed by this object and that managed by
2103f4cbf05Sopenharmony_ci     * the source object are not equal.
2113f4cbf05Sopenharmony_ci     *
2123f4cbf05Sopenharmony_ci     * @param rhs Indicates the source `UniqueFdAddDeletor` object.
2133f4cbf05Sopenharmony_ci     *
2143f4cbf05Sopenharmony_ci     * @return Returns `true` if the two FDs are not equal; returns `false`
2153f4cbf05Sopenharmony_ci     * otherwise.
2163f4cbf05Sopenharmony_ci     */
2173f4cbf05Sopenharmony_ci    bool operator!=(const int& rhs) const
2183f4cbf05Sopenharmony_ci    {
2193f4cbf05Sopenharmony_ci        return !(fd_ == rhs);
2203f4cbf05Sopenharmony_ci    }
2213f4cbf05Sopenharmony_ci
2223f4cbf05Sopenharmony_ci    /**
2233f4cbf05Sopenharmony_ci     * @brief Checks whether the FD managed by this object is greater than or
2243f4cbf05Sopenharmony_ci     * equal to that managed by the source object.
2253f4cbf05Sopenharmony_ci     *
2263f4cbf05Sopenharmony_ci     * @param rhs Indicates the source `UniqueFdAddDeletor` object.
2273f4cbf05Sopenharmony_ci     *
2283f4cbf05Sopenharmony_ci     * @return Returns `true` if FD managed by this object is greater than or
2293f4cbf05Sopenharmony_ci     * equal to that managed by the source object; returns `false` otherwise.
2303f4cbf05Sopenharmony_ci     */
2313f4cbf05Sopenharmony_ci    bool operator>=(const int& rhs) const
2323f4cbf05Sopenharmony_ci    {
2333f4cbf05Sopenharmony_ci        return fd_ >= rhs;
2343f4cbf05Sopenharmony_ci    }
2353f4cbf05Sopenharmony_ci
2363f4cbf05Sopenharmony_ci    /**
2373f4cbf05Sopenharmony_ci     * @brief Checks whether the FD managed by this object is greater than that
2383f4cbf05Sopenharmony_ci     * managed by the source object.
2393f4cbf05Sopenharmony_ci     *
2403f4cbf05Sopenharmony_ci     * @param rhs Indicates the source `UniqueFdAddDeletor` object.
2413f4cbf05Sopenharmony_ci     *
2423f4cbf05Sopenharmony_ci     * @return Returns `true` if FD managed by this object is greater than that
2433f4cbf05Sopenharmony_ci     * managed by the source object; returns `false` otherwise.
2443f4cbf05Sopenharmony_ci     */
2453f4cbf05Sopenharmony_ci    bool operator>(const int& rhs) const
2463f4cbf05Sopenharmony_ci    {
2473f4cbf05Sopenharmony_ci        return fd_ > rhs;
2483f4cbf05Sopenharmony_ci    }
2493f4cbf05Sopenharmony_ci
2503f4cbf05Sopenharmony_ci    /**
2513f4cbf05Sopenharmony_ci     * @brief Checks whether the FD managed by this object is less than or equal
2523f4cbf05Sopenharmony_ci     * to that managed by the source object.
2533f4cbf05Sopenharmony_ci     *
2543f4cbf05Sopenharmony_ci     * @param rhs Indicates the source `UniqueFdAddDeletor` object.
2553f4cbf05Sopenharmony_ci     *
2563f4cbf05Sopenharmony_ci     * @return Returns `true` if FD managed by this object is less than or equal
2573f4cbf05Sopenharmony_ci     * to that managed by the source object; returns `false` otherwise.
2583f4cbf05Sopenharmony_ci     */
2593f4cbf05Sopenharmony_ci    bool operator<=(const int& rhs) const
2603f4cbf05Sopenharmony_ci    {
2613f4cbf05Sopenharmony_ci        return fd_ <= rhs;
2623f4cbf05Sopenharmony_ci    }
2633f4cbf05Sopenharmony_ci
2643f4cbf05Sopenharmony_ci    /**
2653f4cbf05Sopenharmony_ci     * @brief Checks whether the FD managed by this object is less than that
2663f4cbf05Sopenharmony_ci     * managed by the source object.
2673f4cbf05Sopenharmony_ci     *
2683f4cbf05Sopenharmony_ci     * @param rhs Indicates the source `UniqueFdAddDeletor` object.
2693f4cbf05Sopenharmony_ci     *
2703f4cbf05Sopenharmony_ci     * @return Returns `true` if FD managed by this object is less than that
2713f4cbf05Sopenharmony_ci     * managed by the source object; returns `false` otherwise.
2723f4cbf05Sopenharmony_ci     */
2733f4cbf05Sopenharmony_ci    bool operator<(const int& rhs) const
2743f4cbf05Sopenharmony_ci    {
2753f4cbf05Sopenharmony_ci        return fd_ < rhs;
2763f4cbf05Sopenharmony_ci    }
2773f4cbf05Sopenharmony_ci
2783f4cbf05Sopenharmony_ciprivate:
2793f4cbf05Sopenharmony_ci    int fd_ = -1;
2803f4cbf05Sopenharmony_ci
2813f4cbf05Sopenharmony_ci    void Reset(int newValue)
2823f4cbf05Sopenharmony_ci    {
2833f4cbf05Sopenharmony_ci        if (fd_ >= 0) {
2843f4cbf05Sopenharmony_ci            Deleter::Close(fd_);
2853f4cbf05Sopenharmony_ci        }
2863f4cbf05Sopenharmony_ci        fd_ = newValue;
2873f4cbf05Sopenharmony_ci    }
2883f4cbf05Sopenharmony_ci
2893f4cbf05Sopenharmony_ci    // disallow copy ctor and copy assign
2903f4cbf05Sopenharmony_ci    UniqueFdAddDeletor(const UniqueFdAddDeletor& rhs) = delete;
2913f4cbf05Sopenharmony_ci    UniqueFdAddDeletor& operator=(const UniqueFdAddDeletor& rhs) = delete;
2923f4cbf05Sopenharmony_ci};
2933f4cbf05Sopenharmony_ci
2943f4cbf05Sopenharmony_ci/**
2953f4cbf05Sopenharmony_ci* @brief Checks whether the FD managed by two objects (specified by `lhs` and
2963f4cbf05Sopenharmony_ci* `rhs` respectively) are equal.
2973f4cbf05Sopenharmony_ci*
2983f4cbf05Sopenharmony_ci * @tparam Deleter Indicates a deleter.
2993f4cbf05Sopenharmony_ci * @param lhs Indicates the first `UniqueFdAddDeletor` object.
3003f4cbf05Sopenharmony_ci * @param rhs Indicates the second `UniqueFdAddDeletor` object.
3013f4cbf05Sopenharmony_ci *
3023f4cbf05Sopenharmony_ci * @return Returns `true` if the two FDs are equal; returns `false` otherwise.
3033f4cbf05Sopenharmony_ci * @see DefaultDeleter
3043f4cbf05Sopenharmony_ci */
3053f4cbf05Sopenharmony_citemplate <typename Deleter = DefaultDeleter>
3063f4cbf05Sopenharmony_cibool operator==(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs)
3073f4cbf05Sopenharmony_ci{
3083f4cbf05Sopenharmony_ci    return lhs == rhs.fd_;
3093f4cbf05Sopenharmony_ci}
3103f4cbf05Sopenharmony_ci
3113f4cbf05Sopenharmony_ci/**
3123f4cbf05Sopenharmony_ci* @brief Checks whether the FD managed by two objects (specified by `lhs` and
3133f4cbf05Sopenharmony_ci* `rhs` respectively) are not equal.
3143f4cbf05Sopenharmony_ci*
3153f4cbf05Sopenharmony_ci * @tparam Deleter Indicates a deleter.
3163f4cbf05Sopenharmony_ci * @param lhs Indicates the first `UniqueFdAddDeletor` object.
3173f4cbf05Sopenharmony_ci * @param rhs Indicates the second `UniqueFdAddDeletor` object.
3183f4cbf05Sopenharmony_ci *
3193f4cbf05Sopenharmony_ci * @return Returns `true` if the two FDs are not equal; returns `false`
3203f4cbf05Sopenharmony_ci * otherwise.
3213f4cbf05Sopenharmony_ci * @see DefaultDeleter
3223f4cbf05Sopenharmony_ci */
3233f4cbf05Sopenharmony_citemplate <typename Deleter = DefaultDeleter>
3243f4cbf05Sopenharmony_cibool operator!=(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs)
3253f4cbf05Sopenharmony_ci{
3263f4cbf05Sopenharmony_ci    return !(lhs == rhs.fd_);
3273f4cbf05Sopenharmony_ci}
3283f4cbf05Sopenharmony_ci
3293f4cbf05Sopenharmony_ci/**
3303f4cbf05Sopenharmony_ci * @brief Checks whether the FD managed by `lhs` is greater than or equal to
3313f4cbf05Sopenharmony_ci * that managed by `rhs`.
3323f4cbf05Sopenharmony_ci *
3333f4cbf05Sopenharmony_ci * @tparam Deleter Indicates a deleter.
3343f4cbf05Sopenharmony_ci * @param lhs Indicates the first `UniqueFdAddDeletor` object.
3353f4cbf05Sopenharmony_ci * @param rhs Indicates the second `UniqueFdAddDeletor` object.
3363f4cbf05Sopenharmony_ci *
3373f4cbf05Sopenharmony_ci * @return Returns `true` if the FD managed by `lhs` is greater than or equal to
3383f4cbf05Sopenharmony_ci * that managed by `rhs`; returns `false` otherwise.
3393f4cbf05Sopenharmony_ci * @see DefaultDeleter
3403f4cbf05Sopenharmony_ci */
3413f4cbf05Sopenharmony_citemplate <typename Deleter = DefaultDeleter>
3423f4cbf05Sopenharmony_cibool operator>=(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs)
3433f4cbf05Sopenharmony_ci{
3443f4cbf05Sopenharmony_ci    return lhs >= rhs.fd_;
3453f4cbf05Sopenharmony_ci}
3463f4cbf05Sopenharmony_ci
3473f4cbf05Sopenharmony_ci/**
3483f4cbf05Sopenharmony_ci * @brief Checks whether the FD managed by `lhs` is greater than that
3493f4cbf05Sopenharmony_ci * managed by `rhs`.
3503f4cbf05Sopenharmony_ci *
3513f4cbf05Sopenharmony_ci * @tparam Deleter Indicates a deleter.
3523f4cbf05Sopenharmony_ci * @param lhs Indicates the first `UniqueFdAddDeletor` object.
3533f4cbf05Sopenharmony_ci * @param rhs Indicates the second `UniqueFdAddDeletor` object.
3543f4cbf05Sopenharmony_ci *
3553f4cbf05Sopenharmony_ci * @return Returns `true` if the FD managed by `lhs` is greater than that
3563f4cbf05Sopenharmony_ci * managed by `rhs`; returns `false` otherwise.
3573f4cbf05Sopenharmony_ci * @see DefaultDeleter
3583f4cbf05Sopenharmony_ci */
3593f4cbf05Sopenharmony_citemplate <typename Deleter = DefaultDeleter>
3603f4cbf05Sopenharmony_cibool operator>(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs)
3613f4cbf05Sopenharmony_ci{
3623f4cbf05Sopenharmony_ci    return lhs > rhs.fd_;
3633f4cbf05Sopenharmony_ci}
3643f4cbf05Sopenharmony_ci
3653f4cbf05Sopenharmony_ci/**
3663f4cbf05Sopenharmony_ci * @brief Checks whether the FD managed by `lhs` is less than or equal to that
3673f4cbf05Sopenharmony_ci * managed by `rhs`.
3683f4cbf05Sopenharmony_ci *
3693f4cbf05Sopenharmony_ci * @tparam Deleter Indicates a deleter.
3703f4cbf05Sopenharmony_ci * @param lhs Indicates the first `UniqueFdAddDeletor` object.
3713f4cbf05Sopenharmony_ci * @param rhs Indicates the second `UniqueFdAddDeletor` object.
3723f4cbf05Sopenharmony_ci *
3733f4cbf05Sopenharmony_ci * @return Returns `true` if the FD managed by `lhs` is less than or equal to
3743f4cbf05Sopenharmony_ci * that managed by `rhs`; returns `false` otherwise.
3753f4cbf05Sopenharmony_ci * @see DefaultDeleter
3763f4cbf05Sopenharmony_ci */
3773f4cbf05Sopenharmony_citemplate <typename Deleter = DefaultDeleter>
3783f4cbf05Sopenharmony_cibool operator<=(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs)
3793f4cbf05Sopenharmony_ci{
3803f4cbf05Sopenharmony_ci    return lhs <= rhs.fd_;
3813f4cbf05Sopenharmony_ci}
3823f4cbf05Sopenharmony_ci
3833f4cbf05Sopenharmony_ci/**
3843f4cbf05Sopenharmony_ci * @brief Checks whether the FD managed by `lhs` is less than that
3853f4cbf05Sopenharmony_ci * managed by `rhs`.
3863f4cbf05Sopenharmony_ci *
3873f4cbf05Sopenharmony_ci * @tparam Deleter Indicates a deleter.
3883f4cbf05Sopenharmony_ci * @param lhs Indicates the first `UniqueFdAddDeletor` object.
3893f4cbf05Sopenharmony_ci * @param rhs Indicates the second `UniqueFdAddDeletor` object.
3903f4cbf05Sopenharmony_ci *
3913f4cbf05Sopenharmony_ci * @return Returns `true` if the FD managed by `lhs` is less than that
3923f4cbf05Sopenharmony_ci * managed by `rhs`; returns `false` otherwise.
3933f4cbf05Sopenharmony_ci * @see DefaultDeleter
3943f4cbf05Sopenharmony_ci */
3953f4cbf05Sopenharmony_citemplate <typename Deleter = DefaultDeleter>
3963f4cbf05Sopenharmony_cibool operator<(const int& lhs, const UniqueFdAddDeletor<Deleter>& rhs)
3973f4cbf05Sopenharmony_ci{
3983f4cbf05Sopenharmony_ci    return lhs < rhs.fd_;
3993f4cbf05Sopenharmony_ci}
4003f4cbf05Sopenharmony_ci
4013f4cbf05Sopenharmony_ciusing UniqueFd = UniqueFdAddDeletor<DefaultDeleter>;
4023f4cbf05Sopenharmony_ci} // namespace OHOS
4033f4cbf05Sopenharmony_ci#endif
404