All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.joynr.generator.cpp.provider.InterfaceRequestCallerCppTemplate Maven / Gradle / Ivy

package io.joynr.generator.cpp.provider;

import com.google.inject.Inject;
import io.joynr.generator.cpp.util.CppStdTypeUtil;
import io.joynr.generator.cpp.util.JoynrCppGeneratorExtensions;
import io.joynr.generator.cpp.util.TemplateBase;
import io.joynr.generator.templates.InterfaceTemplate;
import io.joynr.generator.templates.util.AttributeUtil;
import io.joynr.generator.templates.util.InterfaceUtil;
import io.joynr.generator.templates.util.MethodUtil;
import io.joynr.generator.templates.util.NamingUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.franca.core.franca.FAttribute;
import org.franca.core.franca.FEnumerationType;
import org.franca.core.franca.FMethod;

@SuppressWarnings("all")
public class InterfaceRequestCallerCppTemplate extends InterfaceTemplate {
  @Inject
  @Extension
  private TemplateBase _templateBase;

  @Inject
  @Extension
  private CppStdTypeUtil _cppStdTypeUtil;

  @Inject
  @Extension
  private JoynrCppGeneratorExtensions _joynrCppGeneratorExtensions;

  @Inject
  @Extension
  private NamingUtil _namingUtil;

  @Inject
  @Extension
  private AttributeUtil _attributeUtil;

  @Inject
  @Extension
  private InterfaceUtil _interfaceUtil;

  @Inject
  @Extension
  private MethodUtil _methodUtil;

  @Override
  public CharSequence generate(final boolean generateVersion) {
    StringConcatenation _builder = new StringConcatenation();
    String interfaceName = this._namingUtil.joynrName(this.francaIntf);
    _builder.newLineIfNotEmpty();
    CharSequence _warning = this._templateBase.warning();
    _builder.append(_warning);
    _builder.newLineIfNotEmpty();
    _builder.append("#include ");
    _builder.newLine();
    _builder.newLine();
    _builder.append("#include \"");
    String _packagePathWithJoynrPrefix = this._joynrCppGeneratorExtensions.getPackagePathWithJoynrPrefix(this.francaIntf, "/", generateVersion);
    _builder.append(_packagePathWithJoynrPrefix);
    _builder.append("/");
    _builder.append(interfaceName);
    _builder.append("RequestCaller.h\"");
    _builder.newLineIfNotEmpty();
    {
      Set _dataTypeIncludesFor = this._cppStdTypeUtil.getDataTypeIncludesFor(this.francaIntf, generateVersion);
      for(final String datatype : _dataTypeIncludesFor) {
        _builder.append("#include ");
        _builder.append(datatype);
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("#include \"");
    String _packagePathWithJoynrPrefix_1 = this._joynrCppGeneratorExtensions.getPackagePathWithJoynrPrefix(this.francaIntf, "/", generateVersion);
    _builder.append(_packagePathWithJoynrPrefix_1);
    _builder.append("/");
    _builder.append(interfaceName);
    _builder.append("Provider.h\"");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    String _namespaceStarter = this._joynrCppGeneratorExtensions.getNamespaceStarter(this.francaIntf, generateVersion);
    _builder.append(_namespaceStarter);
    _builder.newLineIfNotEmpty();
    _builder.append(interfaceName);
    _builder.append("RequestCaller::");
    _builder.append(interfaceName);
    _builder.append("RequestCaller(std::shared_ptr<");
    String _packagePathWithJoynrPrefix_2 = this._joynrCppGeneratorExtensions.getPackagePathWithJoynrPrefix(this.francaIntf, "::", generateVersion);
    _builder.append(_packagePathWithJoynrPrefix_2);
    _builder.append("::");
    _builder.append(interfaceName);
    _builder.append("Provider> _provider)");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append(": joynr::RequestCaller(");
    _builder.append(interfaceName, "\t");
    _builder.append("Provider::INTERFACE_NAME(), joynr::types::Version(_provider ? _provider->MAJOR_VERSION : 0, _provider ? _provider->MINOR_VERSION : 0)),");
    _builder.newLineIfNotEmpty();
    _builder.append("\t  ");
    _builder.append("provider(std::move(_provider))");
    _builder.newLine();
    _builder.append("{");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    {
      boolean _isEmpty = this.francaIntf.getAttributes().isEmpty();
      boolean _not = (!_isEmpty);
      if (_not) {
        _builder.append("// attributes");
        _builder.newLine();
      }
    }
    {
      EList _attributes = this.francaIntf.getAttributes();
      for(final FAttribute attribute : _attributes) {
        String attributeName = this._namingUtil.joynrName(attribute);
        _builder.newLineIfNotEmpty();
        final String returnType = this._cppStdTypeUtil.getTypeName(attribute, generateVersion);
        _builder.newLineIfNotEmpty();
        {
          boolean _isReadable = this._attributeUtil.isReadable(attribute);
          if (_isReadable) {
            _builder.append("void ");
            _builder.append(interfaceName);
            _builder.append("RequestCaller::get");
            String _firstUpper = StringExtensions.toFirstUpper(attributeName);
            _builder.append(_firstUpper);
            _builder.append("(");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("std::function&& onSuccess,");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("std::function&");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append(")> onError");
            _builder.newLine();
            _builder.append(") {");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("std::function onErrorWrapper =");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("[onError] (const exceptions::ProviderRuntimeException& error) {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("onError(std::make_shared(error));");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("};");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("try {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("provider->get");
            String _firstUpper_1 = StringExtensions.toFirstUpper(attributeName);
            _builder.append(_firstUpper_1, "\t\t");
            _builder.append("(std::move(onSuccess), std::move(onErrorWrapper));");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("} catch (const exceptions::ProviderRuntimeException& e) {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("onError(std::make_shared(e));");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("} catch (const exceptions::JoynrException& e) {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("std::string message = \"Could not perform ");
            _builder.append(interfaceName, "\t\t");
            _builder.append("RequestCaller::get");
            String _firstUpper_2 = StringExtensions.toFirstUpper(attributeName);
            _builder.append(_firstUpper_2, "\t\t");
            _builder.append(", caught exception: \" +");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t\t\t\t\t\t");
            _builder.append("e.getTypeName() + \":\" + e.getMessage();");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("onError(std::make_shared(message));");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
          }
        }
        {
          boolean _isWritable = this._attributeUtil.isWritable(attribute);
          if (_isWritable) {
            _builder.append("void ");
            _builder.append(interfaceName);
            _builder.append("RequestCaller::set");
            String _firstUpper_3 = StringExtensions.toFirstUpper(attributeName);
            _builder.append(_firstUpper_3);
            _builder.append("(");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("const ");
            _builder.append(returnType, "\t\t");
            _builder.append("& ");
            _builder.append(attributeName, "\t\t");
            _builder.append(",");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("std::function&& onSuccess,");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("std::function&");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append(")> onError");
            _builder.newLine();
            _builder.append(") {");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("std::function onErrorWrapper =");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("[onError] (const exceptions::ProviderRuntimeException& error) {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("onError(std::make_shared(error));");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("};");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("try {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("provider->set");
            String _firstUpper_4 = StringExtensions.toFirstUpper(attributeName);
            _builder.append(_firstUpper_4, "\t\t");
            _builder.append("(");
            _builder.append(attributeName, "\t\t");
            _builder.append(", std::move(onSuccess), std::move(onErrorWrapper));");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("} catch (const exceptions::ProviderRuntimeException& e) {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("std::string message = \"Could not perform ");
            _builder.append(interfaceName, "\t\t");
            _builder.append("RequestCaller::set");
            String _firstUpper_5 = StringExtensions.toFirstUpper(attributeName);
            _builder.append(_firstUpper_5, "\t\t");
            _builder.append(", caught exception: \" +");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t\t\t\t\t\t");
            _builder.append("e.getTypeName() + \":\" + e.getMessage();");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("onError(std::make_shared(e));");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("} catch (const exceptions::JoynrException& e) {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("std::string message = \"Could not perform ");
            _builder.append(interfaceName, "\t\t");
            _builder.append("RequestCaller::set");
            String _firstUpper_6 = StringExtensions.toFirstUpper(attributeName);
            _builder.append(_firstUpper_6, "\t\t");
            _builder.append(", caught exception: \" +");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t\t\t\t\t\t");
            _builder.append("e.getTypeName() + \":\" + e.getMessage();");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("onError(std::make_shared(message));");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
          }
        }
        _builder.newLine();
      }
    }
    final HashMap methodToErrorEnumName = this._interfaceUtil.methodToErrorEnumName(this.francaIntf);
    _builder.newLineIfNotEmpty();
    {
      boolean _isEmpty_1 = this.francaIntf.getMethods().isEmpty();
      boolean _not_1 = (!_isEmpty_1);
      if (_not_1) {
        _builder.append("// methods");
        _builder.newLine();
      }
    }
    {
      EList _methods = this.francaIntf.getMethods();
      for(final FMethod method : _methods) {
        final String outputTypedParamList = this._cppStdTypeUtil.getCommaSeperatedTypedConstOutputParameterList(method, generateVersion);
        _builder.newLineIfNotEmpty();
        final String inputTypedParamList = this._cppStdTypeUtil.getCommaSeperatedTypedConstInputParameterList(method, generateVersion);
        _builder.newLineIfNotEmpty();
        final String inputUntypedParamList = this._cppStdTypeUtil.getCommaSeperatedUntypedInputParameterList(method);
        _builder.newLineIfNotEmpty();
        final String methodName = this._namingUtil.joynrName(method);
        _builder.newLineIfNotEmpty();
        _builder.append("void ");
        _builder.append(interfaceName);
        _builder.append("RequestCaller::");
        _builder.append(methodName);
        _builder.append("(");
        _builder.newLineIfNotEmpty();
        {
          boolean _isEmpty_2 = IterableExtensions.isEmpty(this._methodUtil.getInputParameters(method));
          boolean _not_2 = (!_isEmpty_2);
          if (_not_2) {
            _builder.append("\t\t");
            _builder.append(inputTypedParamList, "\t\t");
            {
              boolean _isFireAndForget = method.isFireAndForget();
              boolean _not_3 = (!_isFireAndForget);
              if (_not_3) {
                _builder.append(",");
              }
            }
            _builder.newLineIfNotEmpty();
          }
        }
        {
          boolean _isFireAndForget_1 = method.isFireAndForget();
          boolean _not_4 = (!_isFireAndForget_1);
          if (_not_4) {
            {
              boolean _isEmpty_3 = IterableExtensions.isEmpty(this._methodUtil.getOutputParameters(method));
              if (_isEmpty_3) {
                _builder.append("\t\t");
                _builder.append("std::function&& onSuccess,");
                _builder.newLine();
              } else {
                _builder.append("\t\t");
                _builder.append("std::function&& onSuccess,");
                _builder.newLine();
              }
            }
            _builder.append("\t\t");
            _builder.append("std::function&");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append(")> onError");
            _builder.newLine();
          }
        }
        _builder.append(") {");
        _builder.newLine();
        {
          boolean _isFireAndForget_2 = method.isFireAndForget();
          boolean _not_5 = (!_isFireAndForget_2);
          if (_not_5) {
            {
              boolean _hasErrorEnum = this._methodUtil.hasErrorEnum(method);
              if (_hasErrorEnum) {
                _builder.append("\t");
                final String errorTypeName = this.getErrorTypeName(method, methodToErrorEnumName, generateVersion);
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("std::function onErrorWrapper =");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("\t\t");
                _builder.append("[onError] (const ");
                _builder.append(errorTypeName, "\t\t\t");
                _builder.append("::");
                String _nestedEnumName_1 = this._joynrCppGeneratorExtensions.getNestedEnumName();
                _builder.append(_nestedEnumName_1, "\t\t\t");
                _builder.append("& errorEnum) {");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("\t\t\t");
                _builder.append("std::string typeName = ");
                _builder.append(errorTypeName, "\t\t\t\t");
                _builder.append("::getTypeName();");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("\t\t\t");
                _builder.append("std::string name = ");
                _builder.append(errorTypeName, "\t\t\t\t");
                _builder.append("::getLiteral(errorEnum);");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("\t\t\t");
                _builder.append("onError(std::make_shared(typeName + \"::\" + name, std::make_shared<");
                _builder.append(errorTypeName, "\t\t\t\t");
                _builder.append(">(name)));");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("\t");
                _builder.append("};");
                _builder.newLine();
              } else {
                _builder.append("\t");
                _builder.append("std::function onErrorWrapper =");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("\t\t");
                _builder.append("[onError] (const joynr::exceptions::ProviderRuntimeException& error) {");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("\t\t\t");
                _builder.append("onError(std::make_shared(error));");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("\t\t");
                _builder.append("};");
                _builder.newLine();
              }
            }
          }
        }
        _builder.append("\t");
        _builder.append("try {");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("provider->");
        _builder.append(methodName, "\t\t");
        _builder.append("(");
        _builder.newLineIfNotEmpty();
        _builder.append("\t\t\t\t");
        {
          boolean _isEmpty_4 = IterableExtensions.isEmpty(this._methodUtil.getInputParameters(method));
          boolean _not_6 = (!_isEmpty_4);
          if (_not_6) {
            _builder.append(inputUntypedParamList, "\t\t\t\t");
            {
              boolean _isFireAndForget_3 = method.isFireAndForget();
              boolean _not_7 = (!_isFireAndForget_3);
              if (_not_7) {
                _builder.append(",");
              }
            }
          }
        }
        _builder.newLineIfNotEmpty();
        {
          boolean _isFireAndForget_4 = method.isFireAndForget();
          boolean _not_8 = (!_isFireAndForget_4);
          if (_not_8) {
            _builder.append("\t\t\t\t");
            _builder.append("std::move(onSuccess),");
            _builder.newLine();
            _builder.append("\t\t\t\t");
            _builder.append("std::move(onErrorWrapper)");
            _builder.newLine();
          }
        }
        _builder.append("\t\t");
        _builder.append(");");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("// ApplicationExceptions should not be created by the application itself to ensure");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("// serializability. They are treated as JoynrExceptions. They can only be handled correctly");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("// if the constructor is used properly (with the appropriate literal of the reported error");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("// enumeration).");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("} catch (const exceptions::ProviderRuntimeException& e) {");
        _builder.newLine();
        {
          boolean _isFireAndForget_5 = method.isFireAndForget();
          if (_isFireAndForget_5) {
            _builder.append("\t\t");
            _builder.append("std::string message = \"Could not perform ");
            _builder.append(interfaceName, "\t\t");
            _builder.append("RequestCaller::");
            String _firstUpper_7 = StringExtensions.toFirstUpper(methodName);
            _builder.append(_firstUpper_7, "\t\t");
            _builder.append(", caught exception: \" +");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("\t\t\t\t\t");
            _builder.append("e.getTypeName() + \":\" + e.getMessage();");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("JOYNR_LOG_ERROR(logger(), message);");
            _builder.newLine();
          } else {
            _builder.append("\t\t");
            _builder.append("onError(std::make_shared(e));");
            _builder.newLine();
          }
        }
        _builder.append("\t");
        _builder.append("} catch (const exceptions::JoynrException& e) {");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("std::string message = \"Could not perform ");
        _builder.append(interfaceName, "\t\t");
        _builder.append("RequestCaller::");
        String _firstUpper_8 = StringExtensions.toFirstUpper(methodName);
        _builder.append(_firstUpper_8, "\t\t");
        _builder.append(", caught exception: \" +");
        _builder.newLineIfNotEmpty();
        _builder.append("\t\t\t\t\t\t\t");
        _builder.append("e.getTypeName() + \":\" + e.getMessage();");
        _builder.newLine();
        {
          boolean _isFireAndForget_6 = method.isFireAndForget();
          if (_isFireAndForget_6) {
            _builder.append("\t\t");
            _builder.append("JOYNR_LOG_ERROR(logger(), message);");
            _builder.newLine();
          } else {
            _builder.append("\t\t");
            _builder.append("onError(std::make_shared(message));");
            _builder.newLine();
          }
        }
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        _builder.newLine();
      }
    }
    _builder.newLine();
    _builder.append("std::shared_ptr ");
    _builder.append(interfaceName);
    _builder.append("RequestCaller::getProvider()");
    _builder.newLineIfNotEmpty();
    _builder.append("{");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("return provider;");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    String _namespaceEnder = this._joynrCppGeneratorExtensions.getNamespaceEnder(this.francaIntf, generateVersion);
    _builder.append(_namespaceEnder);
    _builder.newLineIfNotEmpty();
    return _builder;
  }

  public String getErrorTypeName(final FMethod method, final Map methodToErrorEnumName, final boolean generateVersion) {
    String _xifexpression = null;
    FEnumerationType _errors = method.getErrors();
    boolean _tripleNotEquals = (_errors != null);
    if (_tripleNotEquals) {
      String _xblockexpression = null;
      {
        final String packagePath = this._joynrCppGeneratorExtensions.getPackagePathWithJoynrPrefix(method.getErrors(), "::", generateVersion);
        String _get = methodToErrorEnumName.get(method);
        _xblockexpression = ((packagePath + "::") + _get);
      }
      _xifexpression = _xblockexpression;
    } else {
      String _buildPackagePath = this._joynrCppGeneratorExtensions.buildPackagePath(method.getErrorEnum(), "::", true, generateVersion);
      String _plus = (_buildPackagePath + "::");
      String _joynrName = this._namingUtil.joynrName(method.getErrorEnum());
      _xifexpression = (_plus + _joynrName);
    }
    return _xifexpression;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy