Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c014caf01d6df5e1095aa82d9177fa329867bdf0 (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
/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are property of the CEA, their use is subject to specific agreement 
 * with the CEA.
 * 
 * Contributors:
 *    CEA List - initial API and implementation
 *******************************************************************************/

package org.eclipse.papyrus.cpp.codegen.jet.util;

import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.papyrus.cpp.codegen.jet.doc.*;
import Cpp.*;
import org.eclipse.papyrus.cpp.codegen.utils.GenUtils;

public class CppPrimitiveTypeDefinition
{
  protected static String nl;
  public static synchronized CppPrimitiveTypeDefinition create(String lineSeparator)
  {
    nl = lineSeparator;
    CppPrimitiveTypeDefinition result = new CppPrimitiveTypeDefinition();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "";
  protected final String TEXT_2 = NL;
  protected final String TEXT_3 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    
//////////////////////////////////////////////////////////////////////////////////////////
// Java preparation
//////////////////////////////////////////////////////////////////////////////////////////
	
	// Retrieve the type passed as argument
	PrimitiveType currentPType	= (PrimitiveType) argument;
	String currentPTypeName		= currentPType.getName();
	String definition			= "";
	
	// Doc
	String typeDoc		= "";
	
	// Retrieve enum doc
	CppElementDoc jDoc		= new CppElementDoc();
	
	/**
	 * Support two different kinds of primitive types
	 * (1) those that are native types of the programming language such as long
	 *     For these, no additional definition has to be done and they should be referenced
	 *     with their name only
	 * (2) those that correspond to a typedef (e.g. typedef long ErrorType). These require
	 *     a typedef definition within the package and need to be referenced with their
	 *     fully qualified name (e.g. MyPackage::ErrorType)
	 */
	// Retrieve type definition
	CppType cppType = GenUtils.getApplication(currentPType, CppType.class);
	if (cppType != null) {
		typeDoc				= jDoc.generate(currentPType);	
		definition			= "typedef " + cppType.getDefinition();

		// If definition string contains "typeName" it should be replaced with type name...
		if (definition.indexOf("typeName") != -1) {
			definition			= definition.replaceAll("typeName", currentPTypeName);
		} else {
			definition			= definition + " " + currentPTypeName;
		}
		definition = definition + ";";
	}
	else {
		definition = GenUtils.getStdtypes(currentPType);
	}
	
//////////////////////////////////////////////////////////////////////////////////////////
// The following part contains the template
//////////////////////////////////////////////////////////////////////////////////////////
    stringBuffer.append(TEXT_1);
    stringBuffer.append( typeDoc );
    stringBuffer.append(TEXT_2);
    stringBuffer.append( definition );
    stringBuffer.append(TEXT_3);
    return stringBuffer.toString();
  }
}

Back to the top