View Javadoc

1   /***
2    * Cecilia ADL Compiler
3    * Copyright (C) 2006-2007 STMicroelectronics
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   *
19   * Contact: fractal@objectweb.org
20   *
21   * Author: Matthieu Leclercq, Ali Erdem Ozcan
22   */
23  
24  package org.objectweb.fractal.cecilia.composite.c.components;
25  
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.objectweb.fractal.adl.ADLException;
30  import org.objectweb.fractal.adl.ComponentVisitor;
31  import org.objectweb.fractal.adl.Node;
32  import org.objectweb.fractal.adl.components.ComponentContainer;
33  import org.objectweb.fractal.api.Component;
34  import org.objectweb.fractal.cecilia.adl.AbstractInstantiationTask;
35  import org.objectweb.fractal.cecilia.adl.SourceCodeProvider;
36  import org.objectweb.fractal.cecilia.adl.file.CodeWriter;
37  import org.objectweb.fractal.task.core.AbstractTaskFactoryUser;
38  import org.objectweb.fractal.task.core.TaskException;
39  import org.objectweb.fractal.task.core.primitive.annotations.ClientInterface;
40  import org.objectweb.fractal.task.core.primitive.annotations.ServerInterface;
41  import org.objectweb.fractal.task.core.primitive.annotations.ServerInterfaces;
42  import org.objectweb.fractal.task.core.primitive.annotations.TaskParameters;
43  
44  /**
45   * Visitor component that builds the instantiation of a static component
46   * instance for default composite component.
47   */
48  public class ComponentInstantiationVisitor extends AbstractTaskFactoryUser
49      implements
50        ComponentVisitor {
51  
52    // ---------------------------------------------------------------------------
53    // Implementation of the ComponentVisitor interface
54    // ---------------------------------------------------------------------------
55  
56    /**
57     * Visits {@link ComponentContainer} nodes and creates a task that writes the
58     * source code containing the component data structure definitions.
59     */
60    public Component visit(final List<Node> path,
61        final ComponentContainer container, final Map<Object, Object> context)
62        throws ADLException, TaskException {
63      return taskFactoryItf.newPrimitiveTask(new ComponentInstantiationTask(),
64          container);
65    }
66  
67    // ---------------------------------------------------------------------------
68    // Task classes
69    // ---------------------------------------------------------------------------
70  
71    /**
72     * Builds source code for the instantiation/initialization of a default
73     * composite component. This task provides the produced source code.
74     */
75    @TaskParameters("componentNode")
76    @ServerInterfaces(@ServerInterface(name = "component-instantiation-provider", signature = SourceCodeProvider.class, record = "role:componentInstantiation, id:%", parameters = "componentNode"))
77    public static class ComponentInstantiationTask
78        extends
79          AbstractInstantiationTask {
80  
81      // -------------------------------------------------------------------------
82      // Task client interfaces
83      // -------------------------------------------------------------------------
84  
85      /** Source code for binding-controller instantiation. */
86      @ClientInterface(name = "bc-instantiation-provider", record = "role:controllerInstantiation, id:%, controller:BC", parameters = "componentNode")
87      public SourceCodeProvider bcInstantiationProviderItf;
88  
89      /** Source code for binding-controller instantiation. */
90      @ClientInterface(name = "cc-instantiation-provider", record = "role:controllerInstantiation, id:%, controller:CC", parameters = "componentNode")
91      public SourceCodeProvider ccInstantiationProviderItf;
92  
93      // -------------------------------------------------------------------------
94      // Implementation of abstract methods of AbstractInstantiationTask
95      // -------------------------------------------------------------------------
96      @Override
97      protected String processSourceCode() throws Exception {
98        final CodeWriter cw = new CodeWriter("Composite Instantiation Builder");
99        final String instanceName = instanceNameProviderItf.getCInstanceName();
100 
101       cw.appendln("// --------------------------------------------------------"
102           + "---------------------");
103       cw.append("// Declare instance ").append(
104           instanceNameProviderItf.getInstanceName()).endl();
105       cw.append("//        from type ").append(
106           typeNameProviderItf.getTypeName()).endl();
107       cw.appendln("// --------------------------------------------------------"
108           + "---------------------");
109 
110       // Declare the component structure since it may be referenced by the
111       // controller structures.
112       // This declaration is extern since it is redeclared and initialized
113       // latter.
114       // This avoid warnings when compiling with -Wredundant-decls.
115       cw.append("extern struct __cecilia_defaultCompositeData_t ").append(
116           instanceName).append(";").endl();
117 
118       // Append the code for the initialization of the controller data
119       // structures (content controller must be appended before binding
120       // controller).
121       cw.appendln(ccInstantiationProviderItf.getSourceCode()).endl();
122       cw.appendln(bcInstantiationProviderItf.getSourceCode()).endl();
123 
124       // Initialize the component data structure.
125       cw.append("struct __cecilia_defaultCompositeData_t ")
126           .append(instanceName).append(" = {").endl();
127       cw.appendln("// Server interfaces");
128       cw.append("{ &__cecilia_defaultComposite_ComponentMeths, ").endl();
129       cw.append("&").append(instanceName).append("}, // myreference").endl();
130       cw.append("{ &__cecilia_defaultComposite_BindingControllerMeths, ")
131           .endl();
132       cw.append("&").append(instanceName).append("}, // bc").endl();
133       cw.append("{ &__cecilia_defaultComposite_LifeCycleControllerMeths, ")
134           .endl();
135       cw.append("&").append(instanceName).append("}, // lcc").endl();
136       cw.append("{ &__cecilia_defaultComposite_ContentControllerMeths, ")
137           .endl();
138       cw.append("&").append(instanceName).append("}, // cc").endl();
139       cw.appendln("// private data");
140       cw.append("sizeof(").append(instanceName).append(
141           "_subComponents) / sizeof(").append(instanceName).append(
142           "_subComponents[0]), // nbSubComponents").endl();
143       cw.append("sizeof(").append(instanceName).append("_bindings) / sizeof(")
144           .append(instanceName).append("_bindings[0]), // nbBindings").endl();
145       cw.append("0, // started").endl();
146       cw.append(instanceName).append("_subComponents,").endl();
147       cw.append(instanceName).append("_bindings,").endl();
148       cw.appendln("};");
149 
150       return cw.toString();
151     }
152   }
153 
154 }