diff options
author | Steven Moreland <smoreland@google.com> | 2019-03-08 15:54:24 -0800 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-03-08 15:54:24 -0800 |
commit | e342b27404d53793f18d35641f9d6c819365a0ce (patch) | |
tree | f51bf7ee8d4e7ef6777f452bbef6a333d96e861c | |
parent | 68c59e987de3bc28973a56d283655f326d843e2d (diff) | |
parent | 5a64d6bb90c1eb0ffd2d112b2e6cf48d7a4e97ac (diff) | |
download | aidl-e342b27404d53793f18d35641f9d6c819365a0ce.tar.gz |
Move data class out of auto-generated code. am: 15b3ba7ede
am: 5a64d6bb90
Change-Id: I2d7579620faf97228276ae18b4e989eb40c18815
-rw-r--r-- | generate_ndk.cpp | 91 |
1 files changed, 18 insertions, 73 deletions
diff --git a/generate_ndk.cpp b/generate_ndk.cpp index 8d2927f2..fa90a581 100644 --- a/generate_ndk.cpp +++ b/generate_ndk.cpp @@ -27,7 +27,7 @@ namespace android { namespace aidl { namespace ndk { -static constexpr const char* kClazz = "clazz"; +static constexpr const char* kClazz = "_g_aidl_clazz"; static constexpr const char* kDescriptor = "descriptor"; static constexpr const char* kVersion = "version"; static constexpr const char* kCacheVariable = "_aidl_cached_value"; @@ -231,9 +231,6 @@ void GenerateSource(CodeWriter& out, const AidlTypenames& types, const AidlInter LeaveNdkNamespace(out, defined_type); } -static std::string DataClassFor(const AidlInterface& defined_type) { - return "AidlClassData_" + ClassName(defined_type, ClassNames::INTERFACE); -} static std::string MethodId(const AidlMethod& m) { return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)"; } @@ -393,34 +390,9 @@ void GenerateClassSource(CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type, const Options& /*options*/) { const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE); const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER); - const std::string data_clazz = DataClassFor(defined_type); - const std::string on_create = data_clazz + "_onCreate"; - const std::string on_destroy = data_clazz + "_onDestory"; - const std::string on_transact = data_clazz + "_onTransact"; - const std::string on_dump = data_clazz + "_onDump"; - - out << "struct " << data_clazz << " {\n"; - out.Indent(); - out << "static AIBinder_Class* " << kClazz << ";\n"; - out << "std::shared_ptr<" << bn_clazz << "> instance;\n"; - out.Dedent(); - out << "};\n\n"; - - out << "static void* " << on_create << "(void* args) {\n"; - out.Indent(); - out << data_clazz << "* data = new " << data_clazz << "{static_cast<" << bn_clazz - << "*>(args)->ref<" << bn_clazz << ">()};\n"; - out << "return static_cast<void*>(data);\n"; - out.Dedent(); - out << "};\n\n"; - out << "static void " << on_destroy << "(void* userData) {\n"; - out.Indent(); - out << "delete static_cast<" << data_clazz << "*>(userData);\n"; - out.Dedent(); - out << "};\n\n"; - - out << "static binder_status_t " << on_transact + out << "static binder_status_t " + << "_aidl_onTransact" << "(AIBinder* _aidl_binder, transaction_code_t _aidl_code, const AParcel* _aidl_in, " "AParcel* _aidl_out) {\n"; out.Indent(); @@ -428,8 +400,10 @@ void GenerateClassSource(CodeWriter& out, const AidlTypenames& types, out << "(void)_aidl_out;\n"; out << "binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;\n"; if (!defined_type.GetMethods().empty()) { - out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = static_cast<" << data_clazz - << "*>(AIBinder_getUserData(_aidl_binder))->instance;\n"; + // we know this cast is valid because this method is only called by the ICInterface + // AIBinder_Class object which is associated with this class. + out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = std::static_pointer_cast<" << bn_clazz + << ">(::ndk::ICInterface::asInterface(_aidl_binder));\n"; out << "switch (_aidl_code) {\n"; out.Indent(); for (const auto& method : defined_type.GetMethods()) { @@ -445,33 +419,12 @@ void GenerateClassSource(CodeWriter& out, const AidlTypenames& types, out.Dedent(); out << "};\n\n"; - out << "static binder_status_t " << on_dump - << "(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {\n"; - out.Indent(); - out << data_clazz << "* data = static_cast<" << data_clazz - << "*>(AIBinder_getUserData(binder));\n"; - out << "return data->instance->dump(fd, args, numArgs);\n"; - out.Dedent(); - out << "};\n\n"; - - out << "AIBinder_Class* " << data_clazz << ":: " << kClazz - << " = ::ndk::ICInterface::defineClass(" << clazz << "::" << kDescriptor << ", " << on_create - << ", " << on_destroy << ", " << on_transact << ", " << on_dump << ");\n\n"; + out << "static AIBinder_Class* " << kClazz << " = ::ndk::ICInterface::defineClass(" << clazz + << "::" << kDescriptor << ", _aidl_onTransact);\n\n"; } void GenerateClientSource(CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type, const Options& options) { const std::string clazz = ClassName(defined_type, ClassNames::CLIENT); - const std::string data_clazz = DataClassFor(defined_type); - - out << "// Source for " << clazz << "\n"; - out << "std::shared_ptr<" << clazz << "> " << clazz - << "::associate(const ::ndk::SpAIBinder& binder) {\n"; - out.Indent(); - out << "if (!AIBinder_associateClass(binder.get(), " << data_clazz << "::" << kClazz - << ")) { return nullptr; }\n"; - out << "return (new " << clazz << "(binder))->ref<" << clazz << ">();\n"; - out.Dedent(); - out << "}\n\n"; out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n"; out << clazz << "::~" << clazz << "() {}\n"; @@ -489,7 +442,6 @@ void GenerateServerSource(CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type, const Options& options) { const std::string clazz = ClassName(defined_type, ClassNames::SERVER); const std::string iface = ClassName(defined_type, ClassNames::INTERFACE); - const std::string data_clazz = DataClassFor(defined_type); out << "// Source for " << clazz << "\n"; out << clazz << "::" << clazz << "() {}\n"; @@ -497,8 +449,7 @@ void GenerateServerSource(CodeWriter& out, const AidlTypenames& types, out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n"; out.Indent(); - out << "AIBinder* binder = AIBinder_new(" << data_clazz << "::" << kClazz - << ", static_cast<void*>(this));\n"; + out << "AIBinder* binder = AIBinder_new(" << kClazz << ", static_cast<void*>(this));\n"; out << "return ::ndk::SpAIBinder(binder);\n"; out.Dedent(); out << "}\n"; @@ -521,7 +472,7 @@ void GenerateServerSource(CodeWriter& out, const AidlTypenames& types, void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type, const Options& options) { const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE); - const std::string data_clazz = DataClassFor(defined_type); + const std::string bp_clazz = ClassName(defined_type, ClassNames::CLIENT); out << "// Source for " << clazz << "\n"; out << "const char* " << clazz << "::" << kDescriptor << " = \"" @@ -535,16 +486,15 @@ void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& types, out << "std::shared_ptr<" << clazz << "> " << clazz << "::fromBinder(const ::ndk::SpAIBinder& binder) {\n"; out.Indent(); - out << data_clazz << "* data = static_cast<" << data_clazz - << "*>(AIBinder_getUserData(binder.get()));\n"; - out << "if (data) {\n"; + out << "if (!AIBinder_associateClass(binder.get(), " << kClazz << ")) { return nullptr; }\n"; + out << "std::shared_ptr<::ndk::ICInterface> interface = " + "::ndk::ICInterface::asInterface(binder.get());\n"; + out << "if (interface) {\n"; out.Indent(); - out << "return data->instance;\n"; + out << "return std::static_pointer_cast<" << clazz << ">(interface);\n"; out.Dedent(); out << "}\n"; - // If it is local, it is not an 'ndk' instance, and parceling will happen locally. - out << "return " << NdkFullClassName(defined_type, ClassNames::CLIENT) - << "::associate(binder);\n"; + out << "return (new " << bp_clazz << "(binder))->ref<" << clazz << ">();\n"; out.Dedent(); out << "}\n\n"; @@ -643,16 +593,12 @@ void GenerateClientHeader(CodeWriter& out, const AidlTypenames& types, << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n"; out << "public:\n"; out.Indent(); - out << "static std::shared_ptr<" << clazz << "> associate(const ::ndk::SpAIBinder& binder);\n"; + out << clazz << "(const ::ndk::SpAIBinder& binder);\n"; out << "virtual ~" << clazz << "();\n"; out << "\n"; for (const auto& method : defined_type.GetMethods()) { out << NdkMethodDecl(types, *method) << " override;\n"; } - out.Dedent(); - out << "private:\n"; - out.Indent(); - out << clazz << "(const ::ndk::SpAIBinder& binder);\n"; if (options.Version() > 0) { out << "int32_t " << kCacheVariable << " = -1;\n"; @@ -718,7 +664,6 @@ void GenerateInterfaceHeader(CodeWriter& out, const AidlTypenames& types, out << "class " << clazz << " : public ::ndk::ICInterface {\n"; out << "public:\n"; out.Indent(); - out << "static AIBinder_Class* " << kClazz << ";\n"; out << "static const char* " << kDescriptor << ";\n"; out << clazz << "();\n"; out << "virtual ~" << clazz << "();\n"; |