Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9609d975 authored by Steven Moreland's avatar Steven Moreland Committed by android-build-merger
Browse files

Merge "libbinder_ndk: support for nullable parcelables"

am: 56ef5878

Change-Id: I6adfa629a8506bab94704f4ab48ecc57f977cf68
parents ffb4e4d0 56ef5878
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -448,6 +448,41 @@ static inline binder_status_t AParcel_readParcelable(const AParcel* parcel, P* p
    return p->readFromParcel(parcel);
}

/**
 * Convenience API for writing a nullable parcelable.
 */
template <typename P>
static inline binder_status_t AParcel_writeNullableParcelable(AParcel* parcel,
                                                              const std::optional<P>& p) {
    if (p == std::nullopt) {
        return AParcel_writeInt32(parcel, 0);  // null
    }
    binder_status_t status = AParcel_writeInt32(parcel, 1);  // non-null
    if (status != STATUS_OK) {
        return status;
    }
    return p->writeToParcel(parcel);
}

/**
 * Convenience API for reading a nullable parcelable.
 */
template <typename P>
static inline binder_status_t AParcel_readNullableParcelable(const AParcel* parcel,
                                                             std::optional<P>* p) {
    int32_t null;
    binder_status_t status = AParcel_readInt32(parcel, &null);
    if (status != STATUS_OK) {
        return status;
    }
    if (null == 0) {
        *p = std::nullopt;
        return STATUS_OK;
    }
    *p = std::optional<P>(P{});
    return (*p)->readFromParcel(parcel);
}

/**
 * Writes a parcelable object of type P inside a std::vector<P> at index 'index' to 'parcel'.
 */