Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9324fe993b23355315a6e65ec439d9889d321bb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*******************************************************************************
 * Copyright (c) 2014 CEA LIST.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA LIST - initial API and implementation
 *******************************************************************************/
 
 package org.eclipse.papyrus.cpp.codegen.xtend

import org.eclipse.uml2.uml.Classifier
import org.eclipse.uml2.uml.Class
import org.eclipse.uml2.uml.DataType
import org.eclipse.uml2.uml.Interface
import org.eclipse.uml2.uml.Signal
import org.eclipse.uml2.uml.Property
import org.eclipse.papyrus.codegen.base.GenUtils
import org.eclipse.papyrus.C_Cpp.Const
import org.eclipse.papyrus.cpp.codegen.utils.Modifier
import org.eclipse.papyrus.cpp.codegen.utils.CppGenUtils
import org.eclipse.papyrus.C_Cpp.Mutable

/**
 * @author Önder GÜRCAN (onder.gurcan@cea.fr)
 */
class CppAttribute {

	//  Loop over attributes. Check that the attribute is not a static const.
	//	Constant static attributes are declared within the class declaration 
	static def CppStaticAttributes(Classifier classifier) {
		var code = '''
			// static attributes (if any)
			«FOR ownedAttribute : getOwnedAttributes(classifier)»
				«IF (ownedAttribute.isStatic)»
					«CppStaticAttributeImplementation(ownedAttribute)»
				«ENDIF»
			«ENDFOR»
		'''
		return code
	}

	// return a list of owned attributes, empty set, if null
	static def getOwnedAttributes(Classifier cl) {
		val attributes = getOwnedAttributesWNull(cl)
		if (attributes == null) {
			emptySet
		}
		else {
			attributes
		}
	}
	
	// return a list of owned attributes, since this is not supported directly on a classifier
	static def getOwnedAttributesWNull(Classifier cl) {	
		if (cl instanceof Class) {
			(cl as Class).ownedAttributes
		} else if (cl instanceof DataType) {
			(cl as DataType).ownedAttributes
		} else if (cl instanceof Interface) {
			(cl as Interface).ownedAttributes
		} else if (cl instanceof Signal) {
			(cl as Signal).ownedAttributes
		} else {
			//Sequence{}
		}
	}

	static def CppStaticAttributeImplementation(Property attribute) '''
		«CppDocumentation.CppElementDoc(attribute)»
		«Modifier.modCVQualifier(attribute)» «CppGenUtils.cppQualifiedName(attribute.type)»«Modifier.modPtr(attribute)»«Modifier.modRef(attribute)»  «attribute.class_.name»::«attribute.name»«Modifier.modArray(attribute)»«defaultValue(attribute)»;
	'''

	static def defaultValue(Property attribute) {
		if ((attribute.defaultValue != null) && attribute.isStatic) {
			"=" + attribute.defaultValue.stringValue()
		}
	}

	static def CppAttributeDeclaration(Property attribute) '''
		«CppDocumentation.CppElementDoc(attribute)»
		«staticValue(attribute)» «Modifier.modCVQualifier(attribute)»«CppGenUtils.cppQualifiedName(attribute.type)»«Modifier.
			modPtr(attribute)»«Modifier.modRef(attribute)» «attribute.name»«Modifier.modArray(attribute)»;
	'''

	static def staticValue(Property attribute) {
		if (attribute.isStatic) 'static'
	}

}

Back to the top