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 toString()45 inline std::string toString() const { return "ParcelFileDescriptor:" + std::to_string(get()); } 46 inline bool operator!=(const ParcelFileDescriptor& rhs) const { 47 return mFd.get() != rhs.mFd.get(); 48 } 49 inline bool operator<(const ParcelFileDescriptor& rhs) const { 50 return mFd.get() < rhs.mFd.get(); 51 } 52 inline bool operator<=(const ParcelFileDescriptor& rhs) const { 53 return mFd.get() <= rhs.mFd.get(); 54 } 55 inline bool operator==(const ParcelFileDescriptor& rhs) const { 56 return mFd.get() == rhs.mFd.get(); 57 } 58 inline bool operator>(const ParcelFileDescriptor& rhs) const { 59 return mFd.get() > rhs.mFd.get(); 60 } 61 inline bool operator>=(const ParcelFileDescriptor& rhs) const { 62 return mFd.get() >= rhs.mFd.get(); 63 } 64 private: 65 android::base::unique_fd mFd; 66 }; 67 68 } // namespace os 69 } // namespace android 70