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

io.joynr.generator.cpp.util.JoynrCppGeneratorExtensions Maven / Gradle / Ivy

package io.joynr.generator.cpp.util;

import com.google.common.base.Objects;
import com.google.common.collect.UnmodifiableIterator;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import io.joynr.generator.cpp.util.CommonApiJoynrGeneratorExtensions;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.franca.core.franca.FArgument;
import org.franca.core.franca.FBasicTypeId;
import org.franca.core.franca.FCompoundType;
import org.franca.core.franca.FField;
import org.franca.core.franca.FInterface;
import org.franca.core.franca.FMethod;
import org.franca.core.franca.FModelElement;
import org.franca.core.franca.FType;
import org.franca.core.franca.FTypeRef;
import org.franca.core.franca.FTypedElement;

@SuppressWarnings("all")
public class JoynrCppGeneratorExtensions extends CommonApiJoynrGeneratorExtensions {
  @Inject
  @Named("generationId")
  private String dllExportName;
  
  private Map primitiveDataTypeDefaultMap;
  
  public String getPrimitiveTypeName(final FBasicTypeId basicType) {
    String _xblockexpression = null;
    {
      boolean _equals = Objects.equal(basicType, FBasicTypeId.BYTE_BUFFER);
      if (_equals) {
        return "QByteArray";
      }
      String _primitiveTypeName = super.getPrimitiveTypeName(basicType);
      _xblockexpression = (_primitiveTypeName);
    }
    return _xblockexpression;
  }
  
  public String getNamespaceStarter(final FInterface interfaceType) {
    UnmodifiableIterator _packageNames = this.getPackageNames(interfaceType);
    String _namespaceStarter = this.getNamespaceStarter(_packageNames);
    return _namespaceStarter;
  }
  
  public String getNamespaceStarter(final FType datatype) {
    UnmodifiableIterator _packageNames = this.getPackageNames(datatype);
    String _namespaceStarter = this.getNamespaceStarter(_packageNames);
    return _namespaceStarter;
  }
  
  public String getNamespaceEnder(final FInterface interfaceType) {
    UnmodifiableIterator _packageNames = this.getPackageNames(interfaceType);
    String _namespaceEnder = this.getNamespaceEnder(_packageNames);
    return _namespaceEnder;
  }
  
  public String getNamespaceEnder(final FType datatype) {
    UnmodifiableIterator _packageNames = this.getPackageNames(datatype);
    String _namespaceEnder = this.getNamespaceEnder(_packageNames);
    return _namespaceEnder;
  }
  
  private String getNamespaceStarter(final Iterator packageList) {
    return this.getNamespaceStarterFromPackageList(packageList);
  }
  
  public String getNamespaceStarterFromPackageList(final Iterator packageList) {
    StringBuilder _stringBuilder = new StringBuilder();
    StringBuilder sb = _stringBuilder;
    String _joynrGenerationPrefix = this.getJoynrGenerationPrefix();
    String _plus = ("namespace " + _joynrGenerationPrefix);
    String _plus_1 = (_plus + " { ");
    sb.append(_plus_1);
    boolean _hasNext = packageList.hasNext();
    boolean _while = _hasNext;
    while (_while) {
      String _next = packageList.next();
      String _plus_2 = ("namespace " + _next);
      String _plus_3 = (_plus_2 + " { ");
      sb.append(_plus_3);
      boolean _hasNext_1 = packageList.hasNext();
      _while = _hasNext_1;
    }
    return sb.toString();
  }
  
  private String getNamespaceEnder(final Iterator packageList) {
    return this.getNameSpaceEnderFromPackageList(packageList);
  }
  
  public String getNameSpaceEnderFromPackageList(final Iterator packageList) {
    StringBuilder _stringBuilder = new StringBuilder();
    StringBuilder sb = _stringBuilder;
    String _joynrGenerationPrefix = this.getJoynrGenerationPrefix();
    String _plus = ("} /* namespace " + _joynrGenerationPrefix);
    String _plus_1 = (_plus + " */ ");
    sb.append(_plus_1);
    boolean _hasNext = packageList.hasNext();
    boolean _while = _hasNext;
    while (_while) {
      String _next = packageList.next();
      String _plus_2 = ("} /* namespace " + _next);
      String _plus_3 = (_plus_2 + " */ ");
      sb.insert(0, _plus_3);
      boolean _hasNext_1 = packageList.hasNext();
      _while = _hasNext_1;
    }
    return sb.toString();
  }
  
  public JoynrCppGeneratorExtensions() {
    HashMap _hashMap = new HashMap();
    final Map bMap = _hashMap;
    bMap.put(FBasicTypeId.BOOLEAN, "false");
    bMap.put(FBasicTypeId.INT8, "-1");
    bMap.put(FBasicTypeId.UINT8, "-1");
    bMap.put(FBasicTypeId.INT16, "-1");
    bMap.put(FBasicTypeId.UINT16, "-1");
    bMap.put(FBasicTypeId.INT32, "-1");
    bMap.put(FBasicTypeId.UINT32, "-1");
    bMap.put(FBasicTypeId.INT64, "-1");
    bMap.put(FBasicTypeId.UINT64, "-1");
    bMap.put(FBasicTypeId.FLOAT, "-1");
    bMap.put(FBasicTypeId.DOUBLE, "-1");
    bMap.put(FBasicTypeId.STRING, "\"\"");
    bMap.put(FBasicTypeId.BYTE_BUFFER, "\"\"");
    bMap.put(FBasicTypeId.UNDEFINED, "");
    Map _unmodifiableMap = Collections.unmodifiableMap(bMap);
    this.primitiveDataTypeDefaultMap = _unmodifiableMap;
  }
  
  public String getCommaSeperatedTypedOutputParameterList(final FMethod method) {
    StringBuilder _stringBuilder = new StringBuilder();
    final StringBuilder returnStringBuilder = _stringBuilder;
    Iterable _outputParameters = this.getOutputParameters(method);
    for (final FArgument argument : _outputParameters) {
      {
        String _mappedDatatypeOrList = this.getMappedDatatypeOrList(argument);
        returnStringBuilder.append(_mappedDatatypeOrList);
        returnStringBuilder.append("& ");
        String _joynrName = this.joynrName(argument);
        returnStringBuilder.append(_joynrName);
        returnStringBuilder.append(", ");
      }
    }
    final String returnString = returnStringBuilder.toString();
    int _length = returnString.length();
    boolean _equals = (_length == 0);
    if (_equals) {
      return "";
    } else {
      int _length_1 = returnString.length();
      int _minus = (_length_1 - 2);
      return returnString.substring(0, _minus);
    }
  }
  
  public String getCommaSeperatedUntypedOutputParameterList(final FMethod method) {
    StringBuilder _stringBuilder = new StringBuilder();
    final StringBuilder returnStringBuilder = _stringBuilder;
    Iterable _outputParameters = this.getOutputParameters(method);
    for (final FArgument argument : _outputParameters) {
      {
        String _joynrName = this.joynrName(argument);
        returnStringBuilder.append(_joynrName);
        returnStringBuilder.append(", ");
      }
    }
    final String returnString = returnStringBuilder.toString();
    int _length = returnString.length();
    boolean _equals = (_length == 0);
    if (_equals) {
      return "";
    } else {
      int _length_1 = returnString.length();
      int _minus = (_length_1 - 2);
      return returnString.substring(0, _minus);
    }
  }
  
  public String getCommaSeperatedTypedParameterList(final FMethod method) {
    StringBuilder _stringBuilder = new StringBuilder();
    final StringBuilder returnStringBuilder = _stringBuilder;
    Iterable _inputParameters = this.getInputParameters(method);
    for (final FArgument param : _inputParameters) {
      {
        String _mappedDatatypeOrList = this.getMappedDatatypeOrList(param);
        returnStringBuilder.append(_mappedDatatypeOrList);
        returnStringBuilder.append(" ");
        String _joynrName = this.joynrName(param);
        returnStringBuilder.append(_joynrName);
        returnStringBuilder.append(", ");
      }
    }
    final String returnString = returnStringBuilder.toString();
    int _length = returnString.length();
    boolean _equals = (_length == 0);
    if (_equals) {
      return "";
    } else {
      int _length_1 = returnString.length();
      int _minus = (_length_1 - 2);
      return returnString.substring(0, _minus);
    }
  }
  
  public String buildPackagePath(final FType datatype, final String separator) {
    boolean _equals = Objects.equal(datatype, null);
    if (_equals) {
      return "";
    }
    String packagepath = "";
    try {
      String _packagePathWithJoynrPrefix = this.getPackagePathWithJoynrPrefix(datatype, separator);
      packagepath = _packagePathWithJoynrPrefix;
    } catch (final Throwable _t) {
      if (_t instanceof IllegalStateException) {
        final IllegalStateException e = (IllegalStateException)_t;
      } else {
        throw Exceptions.sneakyThrow(_t);
      }
    }
    boolean _notEquals = (!Objects.equal(packagepath, ""));
    if (_notEquals) {
      String _plus = (packagepath + separator);
      packagepath = _plus;
    }
    return packagepath;
  }
  
  public String getMappedDatatype(final FType datatype) {
    final String packagepath = this.buildPackagePath(datatype, "::");
    boolean _isEnum = this.isEnum(datatype);
    if (_isEnum) {
      String _joynrName = this.joynrName(datatype);
      String _plus = (packagepath + _joynrName);
      String _plus_1 = (_plus + "::");
      String _nestedEnumName = this.getNestedEnumName();
      return (_plus_1 + _nestedEnumName);
    } else {
      String _joynrName_1 = this.joynrName(datatype);
      return (packagepath + _joynrName_1);
    }
  }
  
  public String getMappedDatatypeOrList(final FType datatype, final boolean array) {
    final String mappedDatatype = this.getMappedDatatype(datatype);
    if (array) {
      String _plus = ("QList<" + mappedDatatype);
      return (_plus + "> ");
    } else {
      return mappedDatatype;
    }
  }
  
  public String getMappedDatatypeOrList(final FBasicTypeId datatype, final boolean array) {
    final String mappedDatatype = this.getPrimitiveTypeName(datatype);
    if (array) {
      String _plus = ("QList<" + mappedDatatype);
      return (_plus + "> ");
    } else {
      return mappedDatatype;
    }
  }
  
  public String getDefaultValue(final FTypedElement element) {
    String _xifexpression = null;
    boolean _equals = (1 == 0);
    if (_equals) {
      _xifexpression = null;
    } else {
      FTypeRef _type = element.getType();
      boolean _isComplex = this.isComplex(_type);
      if (_isComplex) {
        return "";
      } else {
        boolean _isArray = this.isArray(element);
        if (_isArray) {
          return "";
        } else {
          FTypeRef _type_1 = element.getType();
          boolean _isEnum = this.isEnum(_type_1);
          if (_isEnum) {
            return " /* should have enum default value here */";
          } else {
            FTypeRef _type_2 = element.getType();
            FBasicTypeId _predefined = _type_2.getPredefined();
            boolean _containsKey = this.primitiveDataTypeDefaultMap.containsKey(_predefined);
            boolean _not = (!_containsKey);
            if (_not) {
              return "NaN";
            } else {
              FTypeRef _type_3 = element.getType();
              FBasicTypeId _predefined_1 = _type_3.getPredefined();
              return this.primitiveDataTypeDefaultMap.get(_predefined_1);
            }
          }
        }
      }
    }
    return _xifexpression;
  }
  
  public Iterable getRequiredIncludesFor(final FCompoundType datatype) {
    final Iterable members = this.getComplexAndEnumMembers(datatype);
    TreeSet _treeSet = new TreeSet();
    final TreeSet typeList = _treeSet;
    boolean _hasExtendsDeclaration = this.hasExtendsDeclaration(datatype);
    if (_hasExtendsDeclaration) {
      FCompoundType _extendedType = this.getExtendedType(datatype);
      String _includeOf = this.getIncludeOf(_extendedType);
      typeList.add(_includeOf);
    }
    for (final FField member : members) {
      {
        FTypeRef _type = member.getType();
        final Object type = this.getDatatype(_type);
        if ((type instanceof FType)) {
          String _includeOf_1 = this.getIncludeOf(((FType) type));
          typeList.add(_includeOf_1);
        }
      }
    }
    return typeList;
  }
  
  public Iterable getRequiredIncludesFor(final FInterface serviceInterface) {
    TreeSet _treeSet = new TreeSet();
    final TreeSet includeSet = _treeSet;
    HashSet _allComplexAndEnumTypes = this.getAllComplexAndEnumTypes(serviceInterface);
    for (final Object datatype : _allComplexAndEnumTypes) {
      if ((datatype instanceof FType)) {
        String _includeOf = this.getIncludeOf(((FType) datatype));
        includeSet.add(_includeOf);
      }
    }
    return includeSet;
  }
  
  public String getOneLineWarning() {
    return "/* Generated Code */  ";
  }
  
  public String getEnumContainer(final FType enumeration) {
    String packagepath = this.buildPackagePath(enumeration, "::");
    String _joynrName = this.joynrName(enumeration);
    return (packagepath + _joynrName);
  }
  
  public String getEnumContainer(final FTypeRef enumeration) {
    FType _derived = enumeration.getDerived();
    return this.getEnumContainer(_derived);
  }
  
  public String getNestedEnumName() {
    return "Enum";
  }
  
  public String getJoynrTypeName(final FTypedElement element) {
    String _xblockexpression = null;
    {
      final FTypeRef datatypeRef = element.getType();
      final FType datatype = datatypeRef.getDerived();
      final FBasicTypeId predefined = datatypeRef.getPredefined();
      String _switchResult = null;
      boolean _matched = false;
      if (!_matched) {
        boolean _isArray = this.isArray(element);
        if (_isArray) {
          _matched=true;
          _switchResult = "List";
        }
      }
      if (!_matched) {
        boolean _isEnum = this.isEnum(datatypeRef);
        if (_isEnum) {
          _matched=true;
          String _packagePathWithJoynrPrefix = this.getPackagePathWithJoynrPrefix(datatype, ".");
          String _plus = (_packagePathWithJoynrPrefix + 
            ".");
          String _joynrName = this.joynrName(datatype);
          String _plus_1 = (_plus + _joynrName);
          _switchResult = _plus_1;
        }
      }
      if (!_matched) {
        boolean _isString = this.isString(predefined);
        if (_isString) {
          _matched=true;
          _switchResult = "String";
        }
      }
      if (!_matched) {
        boolean _isInt = this.isInt(predefined);
        if (_isInt) {
          _matched=true;
          _switchResult = "Integer";
        }
      }
      if (!_matched) {
        boolean _isLong = this.isLong(predefined);
        if (_isLong) {
          _matched=true;
          _switchResult = "Long";
        }
      }
      if (!_matched) {
        boolean _isDouble = this.isDouble(predefined);
        if (_isDouble) {
          _matched=true;
          _switchResult = "Double";
        }
      }
      if (!_matched) {
        boolean _isFloat = this.isFloat(predefined);
        if (_isFloat) {
          _matched=true;
          _switchResult = "Double";
        }
      }
      if (!_matched) {
        boolean _isBool = this.isBool(predefined);
        if (_isBool) {
          _matched=true;
          _switchResult = "Boolean";
        }
      }
      if (!_matched) {
        boolean _isByte = this.isByte(predefined);
        if (_isByte) {
          _matched=true;
          _switchResult = "Byte";
        }
      }
      if (!_matched) {
        boolean _notEquals = (!Objects.equal(datatype, null));
        if (_notEquals) {
          _matched=true;
          String _packagePathWithJoynrPrefix_1 = this.getPackagePathWithJoynrPrefix(datatype, ".");
          String _plus_2 = (_packagePathWithJoynrPrefix_1 + 
            ".");
          String _joynrName_1 = this.joynrName(datatype);
          String _plus_3 = (_plus_2 + _joynrName_1);
          _switchResult = _plus_3;
        }
      }
      if (!_matched) {
        String _name = predefined.name();
        String _plus_4 = ("Unhandled primitive type: " + _name);
        RuntimeException _runtimeException = new RuntimeException(_plus_4);
        throw _runtimeException;
      }
      _xblockexpression = (_switchResult);
    }
    return _xblockexpression;
  }
  
  public String getDllExportMacro() {
    boolean _isEmpty = this.dllExportName.isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
      String _upperCase = this.dllExportName.toUpperCase();
      return (_upperCase + "_EXPORT");
    }
    return "";
  }
  
  public String getDllExportIncludeStatement() {
    boolean _isEmpty = this.dllExportName.isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
      String _joynrGenerationPrefix = this.getJoynrGenerationPrefix();
      String _plus = ("#include \"" + _joynrGenerationPrefix);
      String _plus_1 = (_plus + "/");
      String _plus_2 = (_plus_1 + this.dllExportName);
      return (_plus_2 + "Export.h\"");
    }
    return "";
  }
  
  public String getIncludeOf(final FType dataType) {
    final String path = this.getPackagePathWithJoynrPrefix(dataType, "/");
    String _plus = (path + "/");
    String _joynrName = this.joynrName(dataType);
    String _plus_1 = (_plus + _joynrName);
    return (_plus_1 + ".h");
  }
  
  public String getPackageSourceDirectory(final FModelElement fModelElement) {
    String _packageName = super.getPackageName(fModelElement);
    return _packageName.replace(".", File.separator);
  }
}