1 /* 2 * Copyright (C) 2018 The Android Open Source Project 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 #pragma once 18 19 #include <android-base/unique_fd.h> 20 #include <binder/Parcel.h> 21 #include <binder/Parcelable.h> 22 23 namespace android { 24 namespace os { 25 26 /* 27 * C++ implementation of the Java class android.os.ParcelFileDescriptor 28 */ 29 class ParcelFileDescriptor : public android::Parcelable { 30 public: 31 ParcelFileDescriptor(); 32 explicit ParcelFileDescriptor(android::base::unique_fd fd); ParcelFileDescriptor(ParcelFileDescriptor && other)33 ParcelFileDescriptor(ParcelFileDescriptor&& other) noexcept : mFd(std::move(other.mFd)) { } 34 ParcelFileDescriptor& operator=(ParcelFileDescriptor&& other) noexcept = default; 35 ~ParcelFileDescriptor() override; 36 get()37 int get() const { return mFd.get(); } release()38 android::base::unique_fd release() { return std::move(mFd); } 39 void reset(android::base::unique_fd fd = android::base::unique_fd()) { mFd = std::move(fd); } 40 41 // android::Parcelable override: 42 android::status_t writeToParcel(android::Parcel* parcel) const override; 43 android::status_t readFromParcel(const android::Parcel* parcel) override; 44 45 inline bool operator!=(const ParcelFileDescriptor& rhs) const { 46 return mFd.get() != rhs.mFd.get(); 47 } 48 inline bool operator<(const ParcelFileDescriptor& rhs) const { 49 return mFd.get() < rhs.mFd.get(); 50 } 51 inline bool operator<=(const ParcelFileDescriptor& rhs) const { 52 return mFd.get() <= rhs.mFd.get(); 53 } 54 inline bool operator==(const ParcelFileDescriptor& rhs) const { 55 return mFd.get() == rhs.mFd.get(); 56 } 57 inline bool operator>(const ParcelFileDescriptor& rhs) const { 58 return mFd.get() > rhs.mFd.get(); 59 } 60 inline bool operator>=(const ParcelFileDescriptor& rhs) const { 61 return mFd.get() >= rhs.mFd.get(); 62 } 63 private: 64 android::base::unique_fd mFd; 65 }; 66 67 } // namespace os 68 } // namespace android 69