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.Node;
31  import org.objectweb.fractal.adl.components.ComponentContainer;
32  import org.objectweb.fractal.api.Component;
33  import org.objectweb.fractal.cecilia.adl.file.CodeWriter;
34  import org.objectweb.fractal.task.core.TaskException;
35  
36  /**
37   * Visitor component that builds the instantiation of a static component
38   * instance for cloneable composite component.
39   */
40  public class CloneableInstantiationVisitor
41      extends
42        ComponentInstantiationVisitor {
43  
44    // ---------------------------------------------------------------------------
45    // Implementation of the ComponentVisitor interface
46    // ---------------------------------------------------------------------------
47  
48    /**
49     * Visits {@link ComponentContainer} nodes and creates a task that writes the
50     * source code containing the initialization of the component data structure.
51     */
52    @Override
53    public Component visit(final List<Node> path,
54        final ComponentContainer container, final Map<Object, Object> context)
55        throws ADLException, TaskException {
56      return taskFactoryItf.newPrimitiveTask(new CloneableInstantiationTask(),
57          container);
58    }
59  
60    // ---------------------------------------------------------------------------
61    // Task classes
62    // ---------------------------------------------------------------------------
63  
64    /**
65     * Builds source code for the instantiation/initialization of a default
66     * composite component. This task provides the produced source code.
67     */
68    public static class CloneableInstantiationTask
69        extends
70          ComponentInstantiationTask {
71  
72      // -------------------------------------------------------------------------
73      // Implementation of abstract methods of AbstractInstantiationTask
74      // -------------------------------------------------------------------------
75      @Override
76      protected String processSourceCode() throws Exception {
77        final CodeWriter cw = new CodeWriter(
78            "Cloneable Composite Instantiation Builder");
79        final String instanceName = instanceNameProviderItf.getCInstanceName();
80  
81        cw.appendln("// --------------------------------------------------------"
82            + "---------------------");
83        cw.append("// Declare instance ").append(
84            instanceNameProviderItf.getInstanceName()).endl();
85        cw.append("//        from type ").append(
86            typeNameProviderItf.getTypeName()).endl();
87        cw.appendln("// --------------------------------------------------------"
88            + "---------------------");
89  
90        // Declare the component structure since it may be referenced by the
91        // controller structures.
92        // This declaration is extern since it is redeclared and initialized
93        // latter.
94        // This avoid warnings when compiling with -Wredundant-decls.
95        cw.append("extern struct __cecilia_cloneableCompositeData_t ").append(
96            instanceName).append(";").endl();
97  
98        // Append the code for the initialization of the controller data
99        // structures (content controller must be appended before binding
100       // controller).
101       cw.appendln(ccInstantiationProviderItf.getSourceCode()).endl();
102       cw.appendln(bcInstantiationProviderItf.getSourceCode()).endl();
103 
104       // Initialize the component data structure.
105       cw.append("struct __cecilia_cloneableCompositeData_t ").append(
106           instanceName).append(" = {").endl();
107       cw.append("{").endl();
108 
109       cw.appendln("// Server interfaces");
110       cw.append("{&__cecilia_cloneableComposite_ComponentMeths, &").append(
111           instanceName).append("}, // myreference").endl();
112       cw.append("{&__cecilia_cloneableComposite_BindingControllerMeths, &")
113           .append(instanceName).append("}, // bc").endl();
114       cw.append("{&__cecilia_defaultComposite_LifeCycleControllerMeths, &(")
115           .append(instanceName).append(".defaultComposite)}, ").append(
116               "// lcc: use the default composite implementation").endl();
117       cw.append("{&__cecilia_defaultComposite_ContentControllerMeths, &(")
118           .append(instanceName).append(".defaultComposite)}, ").append(
119               "// cc: use the default composite implementation").endl();
120 
121       cw.appendln("// private data");
122       cw.append("sizeof(").append(instanceName).append(
123           "_subComponents) / sizeof(").append(instanceName).append(
124           "_subComponents[0]), // nbSubComponents").endl();
125       cw.append("sizeof(").append(instanceName).append("_bindings) / sizeof(")
126           .append(instanceName).append("_bindings[0]), // nbBindings").endl();
127       cw.append("0, // started").endl();
128       cw.append(instanceName).append("_subComponents,").endl();
129       cw.append(instanceName).append("_bindings,").endl();
130       cw.appendln("},");
131 
132       cw.append("{&__cecilia_cloneableComposite_FactoryMeths, &").append(
133           instanceName).append("},").endl();
134       cw.append("0 // factory-allocator client interface").endl();
135       cw.appendln("};");
136 
137       return cw.toString();
138     }
139   }
140 }