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