Change type of variable 'cacheable' bool to optional
The variable name of cached value was hardcoded before.
So it may be hard to expand if another meta method is added and it use cache.
So make it optional to make using cache in the other method easier.
Bug: 120158278
Test: atest android.binder.cts.NdkBinderTest
Test: m -j
Change-Id: I14aaef6c653c9e128d63083b4c8832e85f704199
diff --git a/generate_ndk.cpp b/generate_ndk.cpp
index effc8be..2ca255d 100644
--- a/generate_ndk.cpp
+++ b/generate_ndk.cpp
@@ -30,6 +30,7 @@
static constexpr const char* kClazz = "clazz";
static constexpr const char* kDescriptor = "descriptor";
static constexpr const char* kVersion = "version";
+static constexpr const char* kCacheVariable = "_aidl_cached_value";
using namespace internals;
using cpp::ClassNames;
@@ -237,9 +238,9 @@
return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)";
}
-static void GenerateClientMethodDefinition(CodeWriter& out, const AidlTypenames& types,
- const AidlInterface& defined_type,
- const AidlMethod& method, const bool cacheable) {
+static void GenerateClientMethodDefinition(
+ CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type,
+ const AidlMethod& method, const std::optional<std::string> return_value_cached_to) {
const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
out << NdkMethodDecl(types, method, clazz) << " {\n";
@@ -247,10 +248,10 @@
out << "binder_status_t _aidl_ret_status = STATUS_OK;\n";
out << "::ndk::ScopedAStatus _aidl_status;\n";
- if (cacheable) {
- out << "if (_aidl_cached_value != -1) {\n";
+ if (return_value_cached_to) {
+ out << "if (" << *return_value_cached_to << " != -1) {\n";
out.Indent();
- out << "*_aidl_return = _aidl_cached_value;\n"
+ out << "*_aidl_return = " << *return_value_cached_to << ";\n"
<< "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n"
<< "return _aidl_status;\n";
out.Dedent();
@@ -311,8 +312,8 @@
ReadFromParcelFor({out, types, method.GetType(), "_aidl_out.get()", "_aidl_return"});
out << ";\n";
StatusCheckGoto(out);
- if (cacheable) {
- out << "_aidl_cached_value = *_aidl_return;\n";
+ if (return_value_cached_to) {
+ out << *return_value_cached_to << " = *_aidl_return;\n";
}
}
for (const AidlArgument* arg : method.GetOutArguments()) {
@@ -469,7 +470,9 @@
// Only getInterfaceVersion can use cache.
const bool cacheable = !method->IsUserDefined() && method->GetName() == kGetInterfaceVersion &&
options.Version() > 0;
- GenerateClientMethodDefinition(out, types, defined_type, *method, cacheable);
+ const auto return_value_cached_to =
+ cacheable ? std::make_optional<std::string>(kCacheVariable) : std::nullopt;
+ GenerateClientMethodDefinition(out, types, defined_type, *method, return_value_cached_to);
}
}
void GenerateServerSource(CodeWriter& out, const AidlTypenames& types,
@@ -642,7 +645,7 @@
out << clazz << "(const ::ndk::SpAIBinder& binder);\n";
if (options.Version() > 0) {
- out << "int32_t _aidl_cached_value = -1;\n";
+ out << "int32_t " << kCacheVariable << " = -1;\n";
}
out.Dedent();