package com.kurento.ktool.rom.processor.codegen.function;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.kurento.ktool.rom.processor.model.Return;
import com.kurento.ktool.rom.processor.model.TypeRef;

import freemarker.ext.beans.StringModel;
import freemarker.template.TemplateBooleanModel;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

public class CppObjectType implements TemplateMethodModelEx {

	private final Set<String> nativeTypes;

	public CppObjectType() {
		nativeTypes = new HashSet<String>();

		nativeTypes.add("float");
		nativeTypes.add("int");
	}

	@Override
	public Object exec(@SuppressWarnings("rawtypes") List arguments)
			throws TemplateModelException {

		Object type = arguments.get(0);

		if (type instanceof StringModel) {
			type = ((StringModel) type).getWrappedObject();
			if (type instanceof Return) {
				type = ((Return) type).getType();
			}
		}

		boolean isParam = true;
		if (arguments.size() > 1) {
			isParam = ((TemplateBooleanModel) arguments.get(1)).getAsBoolean();
		}

		if (type == null) {
			return "void";
		}

		if (type instanceof TypeRef) {
			TypeRef typeRef = (TypeRef) type;
			if (typeRef.isList()) {
				if (isParam)
					return "const std::vector<"
							+ getTypeAsString(typeRef.getName(), false) + ">&";
				else
					return "std::vector<"
							+ getTypeAsString(typeRef.getName(), false) + ">";
			} else {
				return getTypeAsString(typeRef.getName(), isParam);
			}
		}

		return getTypeAsString(type.toString(), isParam);
	}

	private String getTypeAsString(String typeName, boolean isParam) {
		if (typeName.equals("boolean")) {
			return "bool";
		} else if (typeName.equals("String")) {
			if (isParam) {
				return "const std::string&";
			} else {
				return "std::string";
			}
		} else if (nativeTypes.contains(typeName)) {
			return typeName;
		} else {
			return "std::shared_ptr<" + typeName + ">";
		}
	}
}
@@ -995,16 +995,61 @@
 = true;%0A
+%09%09String prefix = %22%22;%0A%09%09String suffix = %22%22;%0A%0A
 %09%09if (ar
@@ -1142,24 +1142,176 @@
 ean();%0A%09%09%7D%0A%0A
+%09%09if (arguments.size() %3E 2) %7B%0A%09%09%09prefix = arguments.get(2).toString();%0A%09%09%7D%0A%0A%09%09if (arguments.size() %3E 3) %7B%0A%09%09%09suffix = arguments.get(3).toString();%0A%09%09%7D%0A%0A
 %09%09if (type =
@@ -1535,24 +1535,49 @@
 ame(), false
+, prefix,%0A%09%09%09%09%09%09%09%09%09suffix
 ) + %22%3E&%22;%0A%09%09
@@ -1659,16 +1659,41 @@
 ), false
+, prefix,%0A%09%09%09%09%09%09%09%09%09suffix
 ) + %22%3E%22;
@@ -1754,24 +1754,46 @@
 e(), isParam
+, prefix,%0A%09%09%09%09%09%09suffix
 );%0A%09%09%09%7D%0A%09%09%7D%0A
@@ -1842,16 +1842,32 @@
  isParam
+, prefix, suffix
 );%0A%09%7D%0A%0A%09
@@ -1925,24 +1925,57 @@
 lean isParam
+,%0A%09%09%09String prefix, String suffix
 ) %7B%0A%09%09if (ty
@@ -2265,24 +2265,42 @@
 ptr%3C%22 + 
-typeName
+prefix + typeName + suffix
  + %22%3E%22;%0A
@@ -2307,8 +2307,9 @@
 %09%09%7D%0A%09%7D%0A%7D
+%0A

Allow adding a prefix and a suffix to the type name