Add typedef for parcelables with @FixedSize
The typedef is only present in the cpp and ndk backend.
fixed_size is typedefined to be std::true_type when the parcelable
is annotated with @FixedSize and std::false_type when not.
Test: atest aidl_integration_test
Bug: 142326204
Change-Id: I34a1c187beaa603ddf3f75e9cdc9521fd432ab37
diff --git a/generate_cpp.cpp b/generate_cpp.cpp
index 5c26fe4..1354a21 100644
--- a/generate_cpp.cpp
+++ b/generate_cpp.cpp
@@ -1068,6 +1068,13 @@
parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(operator_code.str())));
}
+ if (parcel.IsFixedSize()) {
+ parcel_class->AddPublic(
+ std::unique_ptr<LiteralDecl>(new LiteralDecl("typedef std::true_type fixed_size;\n")));
+ } else {
+ parcel_class->AddPublic(
+ std::unique_ptr<LiteralDecl>(new LiteralDecl("typedef std::false_type fixed_size;\n")));
+ }
for (const auto& variable : parcel.GetFields()) {
std::ostringstream out;
diff --git a/generate_ndk.cpp b/generate_ndk.cpp
index 7617351..124d8f4 100644
--- a/generate_ndk.cpp
+++ b/generate_ndk.cpp
@@ -957,6 +957,11 @@
out << "class " << clazz << " {\n";
out << "public:\n";
out.Indent();
+ if (defined_type.IsFixedSize()) {
+ out << "typedef std::true_type fixed_size;\n";
+ } else {
+ out << "typedef std::false_type fixed_size;\n";
+ }
out << "static const char* descriptor;\n";
out << "\n";
for (const auto& variable : defined_type.GetFields()) {
diff --git a/tests/aidl_test_client_parcelables.cpp b/tests/aidl_test_client_parcelables.cpp
index aa56676..b3f418e 100644
--- a/tests/aidl_test_client_parcelables.cpp
+++ b/tests/aidl_test_client_parcelables.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include <android/aidl/tests/FixedSizeParcelable.h>
#include <android/aidl/tests/extension/MyExt.h>
#include <android/aidl/tests/extension/MyExt2.h>
#include <android/aidl/tests/extension/MyExtLike.h>
@@ -25,6 +26,7 @@
using android::sp;
using android::String16;
using android::aidl::tests::ConstantExpressionEnum;
+using android::aidl::tests::FixedSizeParcelable;
using android::aidl::tests::GenericStructuredParcelable;
using android::aidl::tests::INamedCallback;
using android::aidl::tests::IntEnum;
@@ -72,6 +74,16 @@
std::reverse(reversed.begin(), reversed.end());
}
+TEST_F(AidlTest, ConfirmFixedSizeTrue) {
+ bool res = std::is_same<FixedSizeParcelable::fixed_size, std::true_type>::value;
+ EXPECT_EQ(res, true);
+}
+
+TEST_F(AidlTest, ConfirmFixedSizeFalse) {
+ bool res = std::is_same<StructuredParcelable::fixed_size, std::true_type>::value;
+ EXPECT_EQ(res, false);
+}
+
TEST_F(AidlTest, ConfirmPersistableBundles) {
PersistableBundle empty_bundle, returned;
Status status = service->RepeatPersistableBundle(empty_bundle, &returned);
diff --git a/tests/android/aidl/tests/FixedSizeParcelable.aidl b/tests/android/aidl/tests/FixedSizeParcelable.aidl
new file mode 100644
index 0000000..f926974
--- /dev/null
+++ b/tests/android/aidl/tests/FixedSizeParcelable.aidl
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.aidl.tests;
+
+@FixedSize
+parcelable FixedSizeParcelable {
+ int a;
+}