void GenerateServerMethods::generate_CPP_USER(const std::string &destdir, const std::string &name) { const std::string h_name = theClass.getBaseName() + "_ulxr_server.h"; std::string cpp_name = destdir + theClass.getBaseName() + "_ulxr_server_user.cpp"; struct stat statbuf; if (stat(cpp_name.c_str(), &statbuf) >= 0) { std::cout << "User file already exists: " << cpp_name << std::endl; cpp_name += ".new"; std::cout << "New template will be created: " << cpp_name << std::endl; } std::ofstream cpp_file(cpp_name.c_str()); std::cout << "User file will be created: " << cpp_name << std::endl; generateUserSourceHead(cpp_file, h_name); cpp_file << "#include <ulxmlrpcpp/ulxr_response.h>\n"; cpp_file << "#include <ulxmlrpcpp/ulxr_method_adder.h>\n"; cpp_file << "#include <ulxmlrpcpp/ulxr_signature.h>\n\n"; cpp_file << "#include \"" << theClass.getSource() << "\"\n"; cpp_file << "#include \"" << name + "_ulxr_names.h" << "\"\n\n"; cpp_file << "\nvoid " << name << "Server::setupServerMethods()\n" "{\n"; for (unsigned i = 0; i < theClass.numMethods(); ++i) { if (i != 0) cpp_file << "\n"; Method method = theClass.getMethod(i); method.extractNamespace(); cpp_file << " // mapped to: " << method.getCppString(0, false, ""); if (method.getName() != method.getOverloadName()) cpp_file << " (there are overloaded methods)"; cpp_file << "\n" " method_adder.addMethod(ulxr::make_method(*this, &" << method.getOverloadName(true, "Server") << "),\n" " " << method.getType().getRpcName() << "::getValueName(),\n" " ULXR_CALLTO_" << method.getOverloadName(true, "", "_") << ",\n" " ulxr::Signature()"; for (unsigned p = 0; p < method.numArgs(); ++p) cpp_file << "\n << " << method.getArg(p).getType().getRpcName() << "::getValueName()"; cpp_file << ",\n" " ulxr_i18n(ULXR_PCHAR(\"Some descriptive comment about '" << method.getCppString(0, true, "") << "'.\"))); // TODO adjust comment\n"; } cpp_file << "}\n\n"; }
void GenerateServerMethods::generateSourceMethods(std::ostream & cpp_file) { for (unsigned i = 0; i < theClass.numMethods(); ++i) { Method method = theClass.getMethod(i); method.extractNamespace(); cpp_file << "// mapped to: " << method.getCppString(0, false, ""); if (method.getName() != method.getOverloadName()) cpp_file << " (there are overloaded methods)"; cpp_file << "\nulxr::MethodResponse\n " << method.getOverloadName(true, "Server") << " (const ulxr::MethodCall &calldata)\n" << "{\n" << " try\n" << " {\n"; for (unsigned iarg = 0; iarg < method.numArgs(); ++iarg) { std::string adap = method.getArg(iarg).getType().getTypeAdapter(); std::string adap2; if (adap.length() != 0) { adap += "("; adap2 = ")"; } cpp_file << " " << method.getArg(iarg).getType().getName() << " p" << iarg << " = " << "(" << method.getArg(iarg).getType().getName() << ") " << adap + method.getArg(iarg).getType().getRpcName() << "(calldata.getParam(" << iarg << "))." << method.getArg(iarg).getType().getRpcAccessor() << "()" << adap2 << ";\n"; } bool have_retval = false; if (method.getType().getName() != "void" || method.getType().getLeft() != "" || method.getType().getRight() != "") have_retval = true; if (have_retval) { cpp_file << " " << method.getType().getProxyType() << " retval = " << method.getType().getTypeDereference(); } else cpp_file << " "; cpp_file << "server." << method.getName() << "("; for (unsigned iarg = 0; iarg < method.numArgs(); ++iarg) { if (iarg != 0) cpp_file << ", "; // cpp_file << "(" << method.getArg(iarg).getType().getCppString() << ")"; if(method.getArg(iarg).getType().isPointer()) cpp_file << "&"; cpp_file << "p" << iarg; } cpp_file << ");\n"; std::string adap = method.getType().getInversTypeAdapter(); std::string adap2; if (adap.length() != 0) { adap += "("; adap2 = ")"; } if (have_retval) cpp_file << " return ulxr::MethodResponse ("<< method.getType().getRpcName() << " (" << adap << "retval" << adap2 << "));\n"; else cpp_file << " return ulxr::MethodResponse (ulxr::Void());\n"; cpp_file << " }\n" << " catch(std::exception &ex)\n" << " {\n" << " ulxr::CppString s = ULXR_PCHAR(\"C++ exception caught when invoking '" << method.getCppString(0, false, "") << "'\\n \");\n" << " s += ULXR_GET_STRING(ex.what());\n" << " return ulxr::MethodResponse(ulxr::ApplicationError, s);\n" << " }\n" << " catch(...)\n" << " {\n" << " ulxr::CppString s = ULXR_PCHAR(\"Unknown exception caught when invoking '" << method.getCppString(0, false, "") << "'\");\n" << " return ulxr::MethodResponse(ulxr::ApplicationError, s);\n" << " }\n"; cpp_file << "}\n\n\n"; } // ------------------------------------ cpp_file << "\nvoid " << theClass.getBaseName() << "Server::removeServerMethods()\n" "{\n"; for (unsigned i = 0; i < theClass.numMethods(); ++i) { Method method = theClass.getMethod(i); method.extractNamespace(); cpp_file << " method_adder.removeMethod(ULXR_CALLTO_" << method.getOverloadName(true, "", "_") << ");"; cpp_file << " // mapped to: " << method.getCppString(0, false, ""); if (method.getName() != method.getOverloadName()) cpp_file << " (there are overloaded methods)"; cpp_file << "\n"; } cpp_file << "\n}\n\n\n"; }