From 97be5845253b8a16d3448bd8cad2dd23b156732c Mon Sep 17 00:00:00 2001 From: Jooyung Han Date: Wed, 17 Nov 2021 01:41:11 +0900 Subject: Add @FixedSize union @FixedSize union uses a custom implementation rather than std::variant so that its value can have a stable/fixed layout and be passed via FMQ. A custom implementation relies on the fact that @FixedSize union/parcelable should have "trivially copyable" fields. So we can't use it for union in general. It would be nice if we could replace std::variant with it and have the same implementation for union types. Maybe in the future. Bug: 206496560 Test: golden_test.sh check Test: aidl_integration_test (no regression) atest fmq_test fmq_unit_test Change-Id: I86351ecb1990818d0fe2faaad7c4b8c2c33881b5 --- .../gen/include/android/aidl/tests/BnFixedSize.h | 1 + .../gen/include/android/aidl/tests/BpFixedSize.h | 1 + .../gen/include/android/aidl/tests/FixedSize.h | 220 +++++++++++++++++++++ 3 files changed, 222 insertions(+) create mode 100644 tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/BnFixedSize.h create mode 100644 tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/BpFixedSize.h create mode 100644 tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/FixedSize.h (limited to 'tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl') diff --git a/tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/BnFixedSize.h b/tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/BnFixedSize.h new file mode 100644 index 00000000..ce72aa31 --- /dev/null +++ b/tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/BnFixedSize.h @@ -0,0 +1 @@ +#error TODO(b/111362593) parcelables do not have bn classes \ No newline at end of file diff --git a/tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/BpFixedSize.h b/tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/BpFixedSize.h new file mode 100644 index 00000000..27af6b1c --- /dev/null +++ b/tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/BpFixedSize.h @@ -0,0 +1 @@ +#error TODO(b/111362593) parcelables do not have bp classes \ No newline at end of file diff --git a/tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/FixedSize.h b/tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/FixedSize.h new file mode 100644 index 00000000..2c889e7b --- /dev/null +++ b/tests/golden_output/aidl-test-interface-cpp-source/gen/include/android/aidl/tests/FixedSize.h @@ -0,0 +1,220 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef __BIONIC__ +#define __assert2(a,b,c,d) ((void)0) +#endif + +namespace android { +namespace aidl { +namespace tests { +class FixedSize : public ::android::Parcelable { +public: + class FixedUnion : public ::android::Parcelable { + public: + enum Tag : uint8_t { + booleanValue = 0, // boolean booleanValue; + byteValue, // byte byteValue; + charValue, // char charValue; + intValue, // int intValue; + longValue, // long longValue; + floatValue, // float floatValue; + doubleValue, // double doubleValue; + enumValue, // android.aidl.tests.LongEnum enumValue; + }; + + template + using _at = typename std::tuple_element<_Tag, std::tuple>::type; + template + static FixedUnion make(_Type&& _arg) { + FixedUnion _inst; + _inst.set<_Tag>(std::forward<_Type>(_arg)); + return _inst; + } + constexpr Tag getTag() const { + return _tag; + } + template + const _at<_Tag>& get() const { + if (_Tag != _tag) { __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, "bad access: a wrong tag"); } + return *(_at<_Tag>*)(&_value); + } + template + _at<_Tag>& get() { + if (_Tag != _tag) { __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, "bad access: a wrong tag"); } + return *(_at<_Tag>*)(&_value); + } + template + void set(_Type&& _arg) { + _tag = _Tag; + get<_Tag>() = std::forward<_Type>(_arg); + } + static int _cmp(const FixedUnion& _lhs, const FixedUnion& _rhs) { + return _cmp_value(_lhs.getTag(), _rhs.getTag()) || _cmp_value_at(_lhs, _rhs); + } + template + static int _cmp_value_at(const FixedUnion& _lhs, const FixedUnion& _rhs) { + if constexpr (_Tag == booleanValue) { + return _cmp_value(_lhs.get<_Tag>(), _rhs.get<_Tag>()); + } else { + return (_lhs.getTag() == _Tag) + ? _cmp_value(_lhs.get<_Tag>(), _rhs.get<_Tag>()) + : _cmp_value_at<(Tag)(_Tag-1)>(_lhs, _rhs); + } + } + template + static int _cmp_value(const _Type& _lhs, const _Type& _rhs) { + return (_lhs == _rhs) ? 0 : (_lhs < _rhs) ? -1 : 1; + } + inline bool operator!=(const FixedUnion&_rhs) const { + return _cmp(*this, _rhs) != 0; + } + inline bool operator<(const FixedUnion&_rhs) const { + return _cmp(*this, _rhs) < 0; + } + inline bool operator<=(const FixedUnion&_rhs) const { + return _cmp(*this, _rhs) <= 0; + } + inline bool operator==(const FixedUnion&_rhs) const { + return _cmp(*this, _rhs) == 0; + } + inline bool operator>(const FixedUnion&_rhs) const { + return _cmp(*this, _rhs) > 0; + } + inline bool operator>=(const FixedUnion&_rhs) const { + return _cmp(*this, _rhs) >= 0; + } + ::android::status_t readFromParcel(const ::android::Parcel* _aidl_parcel) final; + ::android::status_t writeToParcel(::android::Parcel* _aidl_parcel) const final; + static const ::android::String16& getParcelableDescriptor() { + static const ::android::StaticString16 DESCIPTOR (u"android.aidl.tests.FixedSize.FixedUnion"); + return DESCIPTOR; + } + inline std::string toString() const { + std::ostringstream os; + os << "FixedUnion{"; + switch (getTag()) { + case booleanValue: os << "booleanValue: " << ::android::internal::ToString(get()); break; + case byteValue: os << "byteValue: " << ::android::internal::ToString(get()); break; + case charValue: os << "charValue: " << ::android::internal::ToString(get()); break; + case intValue: os << "intValue: " << ::android::internal::ToString(get()); break; + case longValue: os << "longValue: " << ::android::internal::ToString(get()); break; + case floatValue: os << "floatValue: " << ::android::internal::ToString(get()); break; + case doubleValue: os << "doubleValue: " << ::android::internal::ToString(get()); break; + case enumValue: os << "enumValue: " << ::android::internal::ToString(get()); break; + } + os << "}"; + return os.str(); + } + private: + Tag _tag __attribute__((aligned (1))) = booleanValue; + union { + bool booleanValue __attribute__((aligned (1))) = bool(false); + int8_t byteValue __attribute__((aligned (1))); + char16_t charValue __attribute__((aligned (2))); + int32_t intValue __attribute__((aligned (4))); + int64_t longValue __attribute__((aligned (8))); + float floatValue __attribute__((aligned (4))); + double doubleValue __attribute__((aligned (8))); + ::android::aidl::tests::LongEnum enumValue __attribute__((aligned (8))); + } _value; + }; // class FixedUnion + class FixedParcelable : public ::android::Parcelable { + public: + bool booleanValue = false; + int8_t byteValue = 0; + char16_t charValue; + int32_t intValue = 0; + int64_t longValue = 0L; + float floatValue = 0.000000f; + double doubleValue = 0.000000; + ::android::aidl::tests::LongEnum enumValue = ::android::aidl::tests::LongEnum::FOO; + ::android::aidl::tests::FixedSize::FixedUnion parcelableValue; + inline bool operator!=(const FixedParcelable& rhs) const { + return std::tie(booleanValue, byteValue, charValue, intValue, longValue, floatValue, doubleValue, enumValue, parcelableValue) != std::tie(rhs.booleanValue, rhs.byteValue, rhs.charValue, rhs.intValue, rhs.longValue, rhs.floatValue, rhs.doubleValue, rhs.enumValue, rhs.parcelableValue); + } + inline bool operator<(const FixedParcelable& rhs) const { + return std::tie(booleanValue, byteValue, charValue, intValue, longValue, floatValue, doubleValue, enumValue, parcelableValue) < std::tie(rhs.booleanValue, rhs.byteValue, rhs.charValue, rhs.intValue, rhs.longValue, rhs.floatValue, rhs.doubleValue, rhs.enumValue, rhs.parcelableValue); + } + inline bool operator<=(const FixedParcelable& rhs) const { + return std::tie(booleanValue, byteValue, charValue, intValue, longValue, floatValue, doubleValue, enumValue, parcelableValue) <= std::tie(rhs.booleanValue, rhs.byteValue, rhs.charValue, rhs.intValue, rhs.longValue, rhs.floatValue, rhs.doubleValue, rhs.enumValue, rhs.parcelableValue); + } + inline bool operator==(const FixedParcelable& rhs) const { + return std::tie(booleanValue, byteValue, charValue, intValue, longValue, floatValue, doubleValue, enumValue, parcelableValue) == std::tie(rhs.booleanValue, rhs.byteValue, rhs.charValue, rhs.intValue, rhs.longValue, rhs.floatValue, rhs.doubleValue, rhs.enumValue, rhs.parcelableValue); + } + inline bool operator>(const FixedParcelable& rhs) const { + return std::tie(booleanValue, byteValue, charValue, intValue, longValue, floatValue, doubleValue, enumValue, parcelableValue) > std::tie(rhs.booleanValue, rhs.byteValue, rhs.charValue, rhs.intValue, rhs.longValue, rhs.floatValue, rhs.doubleValue, rhs.enumValue, rhs.parcelableValue); + } + inline bool operator>=(const FixedParcelable& rhs) const { + return std::tie(booleanValue, byteValue, charValue, intValue, longValue, floatValue, doubleValue, enumValue, parcelableValue) >= std::tie(rhs.booleanValue, rhs.byteValue, rhs.charValue, rhs.intValue, rhs.longValue, rhs.floatValue, rhs.doubleValue, rhs.enumValue, rhs.parcelableValue); + } + + ::android::status_t readFromParcel(const ::android::Parcel* _aidl_parcel) final; + ::android::status_t writeToParcel(::android::Parcel* _aidl_parcel) const final; + static const ::android::String16& getParcelableDescriptor() { + static const ::android::StaticString16 DESCIPTOR (u"android.aidl.tests.FixedSize.FixedParcelable"); + return DESCIPTOR; + } + inline std::string toString() const { + std::ostringstream os; + os << "FixedParcelable{"; + os << "booleanValue: " << ::android::internal::ToString(booleanValue); + os << ", byteValue: " << ::android::internal::ToString(byteValue); + os << ", charValue: " << ::android::internal::ToString(charValue); + os << ", intValue: " << ::android::internal::ToString(intValue); + os << ", longValue: " << ::android::internal::ToString(longValue); + os << ", floatValue: " << ::android::internal::ToString(floatValue); + os << ", doubleValue: " << ::android::internal::ToString(doubleValue); + os << ", enumValue: " << ::android::internal::ToString(enumValue); + os << ", parcelableValue: " << ::android::internal::ToString(parcelableValue); + os << "}"; + return os.str(); + } + }; // class FixedParcelable + inline bool operator!=(const FixedSize&) const { + return std::tie() != std::tie(); + } + inline bool operator<(const FixedSize&) const { + return std::tie() < std::tie(); + } + inline bool operator<=(const FixedSize&) const { + return std::tie() <= std::tie(); + } + inline bool operator==(const FixedSize&) const { + return std::tie() == std::tie(); + } + inline bool operator>(const FixedSize&) const { + return std::tie() > std::tie(); + } + inline bool operator>=(const FixedSize&) const { + return std::tie() >= std::tie(); + } + + ::android::status_t readFromParcel(const ::android::Parcel* _aidl_parcel) final; + ::android::status_t writeToParcel(::android::Parcel* _aidl_parcel) const final; + static const ::android::String16& getParcelableDescriptor() { + static const ::android::StaticString16 DESCIPTOR (u"android.aidl.tests.FixedSize"); + return DESCIPTOR; + } + inline std::string toString() const { + std::ostringstream os; + os << "FixedSize{"; + os << "}"; + return os.str(); + } +}; // class FixedSize +} // namespace tests +} // namespace aidl +} // namespace android -- cgit v1.2.3