|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 // Routines to help generate C/C++ data model code |
|
18 |
|
19 /** |
|
20 * Declare accessors for the given instance, which is |
|
21 * expected to have an attribute data-model-cpp-type representing |
|
22 * the C/C++ type (e.g. "int", "TBool"), which dictates how |
|
23 * its code is generated. Note that "TDes" is the expected |
|
24 * type for generic string access and underneath "TBuf<...>" is |
|
25 * used for mutable storage; a property "maxLength" should appear on the |
|
26 * instance to describe the maximum length. |
|
27 * @param contribs the contrib list to append |
|
28 * @param instance the instance |
|
29 * @param dataModelClassName the name of the generated class |
|
30 * @param methodPhase the phase receiving method declarations |
|
31 * @param memberPhase the phase receiving member declarations |
|
32 * @param constantsPhase the phase receiving constant declarations |
|
33 * @param mainPhase the phase receiving the main *.cpp file contributions |
|
34 */ |
|
35 function setupDataModelAccessors(contribs, instance, dataModelClassName, methodPhase, memberPhase, constantsPhase, mainPhase) { |
|
36 var type = getDataModelItemType(instance); |
|
37 var memberName = "i"+titleCase(instance.name); |
|
38 |
|
39 var contrib; |
|
40 |
|
41 var getterName = getDataModelGetterMethodName(instance); |
|
42 var setterName = getDataModelSetterMethodName(instance); |
|
43 var storageType; |
|
44 var paramType; |
|
45 var returnType; |
|
46 var setterCode; |
|
47 if (type != "TDes") { |
|
48 storageType = type; |
|
49 paramType = type; |
|
50 returnType = type; |
|
51 setterCode = "\t" + memberName + " = aValue;\n"; |
|
52 } else { |
|
53 var constName = "K" + titleCase(instance.name) + "MaxLength"; |
|
54 contrib = Engine.createContributionForPhase(constantsPhase); |
|
55 contrib.setFormattedText("const int {0} = {1};\n", |
|
56 [ constName, |
|
57 instance.properties.maxLength ]); |
|
58 contribs.add(contrib); |
|
59 paramType = "TDesC"; |
|
60 returnType = "TDes"; |
|
61 storageType = "TBuf<" + constName + ">"; |
|
62 setterCode = |
|
63 "\tif ( aValue.Length() < " + constName + ")\n"+ |
|
64 "\t\t" + memberName + ".Copy( aValue );\n"+ |
|
65 "\telse\n"+ |
|
66 "\t\t" + memberName + ".Copy( aValue.Ptr(), " + constName + ");\n"; |
|
67 } |
|
68 contrib = Engine.createContributionForPhase(memberPhase); |
|
69 contrib.setFormattedText("{0} {1};\n", [ storageType, memberName ]); |
|
70 contribs.add(contrib); |
|
71 |
|
72 contrib = Engine.createContributionForPhase(methodPhase); |
|
73 contrib.setFormattedText("{0}& {1}();\n", [ returnType, getterName ]); |
|
74 contribs.add(contrib); |
|
75 |
|
76 contrib = Engine.createContributionForPhase(mainPhase); |
|
77 contrib.setFormattedText( |
|
78 "{0}& {2}::{1}()\n"+ |
|
79 "\t'{'\n"+ |
|
80 "\treturn {3};\n"+ |
|
81 "\t'}'\n"+ |
|
82 "\n", |
|
83 [ returnType, getterName, dataModelClassName, memberName ]); |
|
84 contribs.add(contrib); |
|
85 |
|
86 contrib = Engine.createContributionForPhase(methodPhase); |
|
87 contrib.setFormattedText("void {1}(const {0}& aValue);\n", |
|
88 [ paramType, setterName ]); |
|
89 contribs.add(contrib); |
|
90 |
|
91 contrib = Engine.createContributionForPhase(mainPhase); |
|
92 contrib.setFormattedText( |
|
93 "void {2}::{1}(const {0}& aValue)\n"+ |
|
94 "\t'{'\n"+ |
|
95 setterCode + |
|
96 "\t'}'\n"+ |
|
97 "\n", |
|
98 [ paramType, setterName, dataModelClassName ]); |
|
99 contribs.add(contrib); |
|
100 |
|
101 } |
|
102 |
|
103 /** |
|
104 * Get the name of the getter method for an item. |
|
105 */ |
|
106 function getDataModelGetterMethodName(instance) { |
|
107 return titleCase(instance.name); |
|
108 } |
|
109 |
|
110 /** |
|
111 * Get the name of the setter method for an item. |
|
112 */ |
|
113 function getDataModelSetterMethodName(instance) { |
|
114 return "Set" + titleCase(instance.name); |
|
115 } |
|
116 |
|
117 /** |
|
118 * Get the name of the C/C++ parameter type of an item. |
|
119 */ |
|
120 function getDataModelItemType(instance) { |
|
121 if (instance.component != null) |
|
122 return instance.component.attributes["data-model-cpp-type"]; |
|
123 return null; |
|
124 } |
|
125 |
|
126 /** |
|
127 * Generate a call that sets the data model item value. |
|
128 * @param contribs the contribution list to append |
|
129 * @param indent indentation adjustment |
|
130 * @param phaseName name of phase to use |
|
131 * @param formName the form to use |
|
132 * @param modelName the name of the model object (or null for this) |
|
133 * @param instance the instance |
|
134 * @param valueName the name of the value to set (expression) |
|
135 */ |
|
136 function generateDataModelItemInitializer(contribs, indent, phaseName, formName, modelName, instance, valueName) { |
|
137 var setterName = getDataModelSetterMethodName(instance); |
|
138 var contrib = Engine.createContributionForPhase(phaseName); |
|
139 contrib.setFormattedText("{0}{1}( {2} );\n", |
|
140 [modelName != null ? modelName + "." : "", |
|
141 setterName, |
|
142 valueName]); |
|
143 contrib.setForm(formName); |
|
144 contrib.indentAdjust(indent); |
|
145 contribs.add(contrib); |
|
146 } |