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