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

io.joynr.generator.cpp.JoynrCppGenerator Maven / Gradle / Ivy

package io.joynr.generator.cpp;

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.inject.AbstractModule;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import com.google.inject.name.Named;
import io.joynr.generator.IJoynrGenerator;
import io.joynr.generator.cpp.communicationmodel.CommunicationModelGenerator;
import io.joynr.generator.cpp.defaultProvider.DefaultInterfaceProviderGenerator;
import io.joynr.generator.cpp.filter.FilterGenerator;
import io.joynr.generator.cpp.interfaces.InterfaceGenerator;
import io.joynr.generator.cpp.joynrmessaging.JoynrMessagingGenerator;
import io.joynr.generator.cpp.provider.ProviderGenerator;
import io.joynr.generator.cpp.proxy.ProxyGenerator;
import io.joynr.generator.cpp.util.CppStdTypeUtil;
import io.joynr.generator.cpp.util.CppTemplateFactory;
import io.joynr.generator.templates.util.JoynrGeneratorExtensions;
import io.joynr.generator.templates.util.NamingUtil;
import io.joynr.generator.templates.util.SupportedFrancaFeatureChecker;
import io.joynr.generator.templates.util.TypeUtil;
import io.joynr.generator.util.FileSystemAccessUtil;
import io.joynr.generator.util.InvocationArguments;
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.xbase.lib.Extension;
import org.franca.core.dsl.FrancaPersistenceManager;
import org.franca.core.franca.FInterface;
import org.franca.core.franca.FModel;

@SuppressWarnings("all")
public class JoynrCppGenerator implements IJoynrGenerator {
  @Inject
  private CommunicationModelGenerator communicationModelGenerator;

  @Inject
  private ProxyGenerator proxyGenerator;

  @Inject
  private ProviderGenerator providerGenerator;

  @Inject
  private FilterGenerator filterGenerator;

  @Inject
  private InterfaceGenerator interfaceGenerator;

  @Inject
  private JoynrMessagingGenerator joynrMessagingGenerator;

  @Inject
  private DefaultInterfaceProviderGenerator defaultProviderGenerator;

  @Inject
  private IFileSystemAccess outputHeaderFileSystem;

  @Inject
  @Extension
  protected JoynrGeneratorExtensions _joynrGeneratorExtensions;

  @Inject
  @Named(NamingUtil.JOYNR_GENERATOR_NOVERSIONGENERATION_COMMENT)
  public boolean versioningComment;

  @Inject
  @Named(NamingUtil.JOYNR_GENERATOR_PACKAGEWITHVERSION)
  public boolean packageWithVersion;

  @Inject
  @Named("generateProxyCode")
  public boolean generateProxyCode;

  @Inject
  @Named("generateProviderCode")
  public boolean generateProviderCode;

  public static final String OUTPUT_HEADER_PATH = "outputHeaderPath";

  private Map parameters;

  private boolean generateVersionedCommunicationModel = false;

  private boolean generateUnversionedCommunicationModel = false;

  @Override
  public void doGenerate(final Resource input, final IFileSystemAccess fsa) {
    this.doGenerate(input, fsa, this.getHeaderFileSystemAccess(fsa));
  }

  @Override
  public String getLanguageId() {
    return "cpp";
  }

  @Override
  public com.google.inject.Module getGeneratorModule() {
    return new AbstractModule() {
      @Override
      protected void configure() {
        this.bind(TypeUtil.class).to(CppStdTypeUtil.class);
        this.install(new FactoryModuleBuilder().build(CppTemplateFactory.class));
      }
    };
  }

  @Override
  public void updateCommunicationModelGeneration(final Resource input) {
    final FModel fModel = this.getModel(input);
    int _size = fModel.getInterfaces().size();
    boolean _equals = (_size == 0);
    if (_equals) {
      this.generateVersionedCommunicationModel = true;
    }
    EList _interfaces = fModel.getInterfaces();
    for (final FInterface fInterface : _interfaces) {
      {
        this._joynrGeneratorExtensions.checkVersioningOption(fInterface, this.packageWithVersion);
        boolean _xifexpression = false;
        if (this.versioningComment) {
          boolean _commentContainsNoVersionGeneration = this._joynrGeneratorExtensions.commentContainsNoVersionGeneration(fInterface);
          _xifexpression = (!_commentContainsNoVersionGeneration);
        } else {
          _xifexpression = this.packageWithVersion;
        }
        final boolean generateVersioning = _xifexpression;
        if (generateVersioning) {
          this.generateVersionedCommunicationModel = true;
        } else {
          this.generateUnversionedCommunicationModel = true;
        }
      }
    }
  }

  /**
   * Triggers the generation. In case the parameter "generate" is set to false, the generator is cleaning the generation folder
   */
  public void doGenerate(final Resource input, final IFileSystemAccess sourceFileSystem, final IFileSystemAccess headerFileSystem) {
    final FModel fModel = this.getModel(input);
    this._joynrGeneratorExtensions.checkForNamedArrays(fModel, input.getURI().path());
    SupportedFrancaFeatureChecker.checkModel(fModel);
    this.filterGenerator.doGenerate(fModel, sourceFileSystem, headerFileSystem, 
      this.getSourceContainerPath(sourceFileSystem, "filter"), 
      this.getHeaderContainerPath(sourceFileSystem, headerFileSystem, "filter"));
    this.interfaceGenerator.doGenerate(fModel, sourceFileSystem, headerFileSystem, 
      this.getSourceContainerPath(sourceFileSystem, "communication-model"), 
      this.getHeaderContainerPath(sourceFileSystem, headerFileSystem, "communication-model"));
    if (this.generateProxyCode) {
      this.proxyGenerator.doGenerate(fModel, sourceFileSystem, headerFileSystem, 
        this.getSourceContainerPath(sourceFileSystem, "proxy"), 
        this.getHeaderContainerPath(sourceFileSystem, headerFileSystem, "proxy"));
      this.joynrMessagingGenerator.doGenerate(fModel, sourceFileSystem, headerFileSystem, 
        this.getSourceContainerPath(sourceFileSystem, "joynr-messaging"), 
        this.getHeaderContainerPath(sourceFileSystem, headerFileSystem, "joynr-messaging"));
    }
    if (this.generateProviderCode) {
      this.providerGenerator.doGenerate(fModel, sourceFileSystem, headerFileSystem, 
        this.getSourceContainerPath(sourceFileSystem, "provider"), 
        this.getHeaderContainerPath(sourceFileSystem, headerFileSystem, "provider"));
      this.defaultProviderGenerator.doGenerate(fModel, sourceFileSystem, headerFileSystem, 
        this.getSourceContainerPath(sourceFileSystem, "provider"), 
        this.getHeaderContainerPath(sourceFileSystem, headerFileSystem, "provider"));
    }
  }

  @Override
  public void generateCommunicationModel(final Resource input, final IFileSystemAccess sourceFileSystem) {
    final IFileSystemAccess headerFileSystem = this.getHeaderFileSystemAccess(sourceFileSystem);
    final FModel fModel = this.getModel(input);
    if (this.generateVersionedCommunicationModel) {
      this.communicationModelGenerator.doGenerate(fModel, sourceFileSystem, headerFileSystem, 
        this.getSourceContainerPath(sourceFileSystem, "communication-model"), 
        this.getHeaderContainerPath(sourceFileSystem, headerFileSystem, "communication-model"), 
        true);
    }
    if (this.generateUnversionedCommunicationModel) {
      this.communicationModelGenerator.doGenerate(fModel, sourceFileSystem, headerFileSystem, 
        this.getSourceContainerPath(sourceFileSystem, "communication-model"), 
        this.getHeaderContainerPath(sourceFileSystem, headerFileSystem, "communication-model"), 
        false);
    }
  }

  @Override
  public void clearCommunicationModelGenerationSettings() {
    this.generateVersionedCommunicationModel = false;
    this.generateUnversionedCommunicationModel = false;
  }

  public String getSourceContainerPath(final IFileSystemAccess sourceFileSystem, final String directory) {
    return (((directory + File.separator) + "generated") + File.separator);
  }

  public String getHeaderContainerPath(final IFileSystemAccess sourceFileSystem, final IFileSystemAccess headerFileSystem, final String directory) {
    boolean _equals = Objects.equal(sourceFileSystem, headerFileSystem);
    if (_equals) {
      return this.getSourceContainerPath(sourceFileSystem, directory);
    } else {
      return "";
    }
  }

  @Override
  public void setParameters(final Map parameter) {
    this.parameters = parameter;
  }

  @Override
  public Set supportedParameters() {
    List _asList = Arrays.asList(JoynrCppGenerator.OUTPUT_HEADER_PATH);
    return new HashSet(_asList);
  }

  public String getOutputHeaderPath() {
    String result = null;
    if ((this.parameters != null)) {
      String _get = this.parameters.get(JoynrCppGenerator.OUTPUT_HEADER_PATH);
      boolean _tripleNotEquals = (_get != null);
      if (_tripleNotEquals) {
        result = this.parameters.get(JoynrCppGenerator.OUTPUT_HEADER_PATH);
      } else {
        String _get_1 = this.parameters.get(InvocationArguments.OUTPUT_PATH);
        boolean _tripleNotEquals_1 = (_get_1 != null);
        if (_tripleNotEquals_1) {
          String _get_2 = this.parameters.get(InvocationArguments.OUTPUT_PATH);
          String _plus = (_get_2 + File.separator);
          String _plus_1 = (_plus + "include");
          result = _plus_1;
        }
      }
    }
    return result;
  }

  public IFileSystemAccess getHeaderFileSystemAccess(final IFileSystemAccess fsa) {
    IFileSystemAccess _xblockexpression = null;
    {
      IFileSystemAccess headerFSA = fsa;
      String _outputHeaderPath = this.getOutputHeaderPath();
      boolean _tripleNotEquals = (_outputHeaderPath != null);
      if (_tripleNotEquals) {
        FileSystemAccessUtil.createFileSystemAccess(this.outputHeaderFileSystem, this.getOutputHeaderPath());
        headerFSA = this.outputHeaderFileSystem;
      }
      _xblockexpression = headerFSA;
    }
    return _xblockexpression;
  }

  public FModel getModel(final Resource input) {
    final boolean isFrancaIDLResource = input.getURI().fileExtension().equals(FrancaPersistenceManager.FRANCA_FILE_EXTENSION);
    Preconditions.checkArgument(isFrancaIDLResource, ("Unknown input: " + input));
    EObject _get = input.getContents().get(0);
    return ((FModel) _get);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy