Skip to content

Commit

Permalink
[fix] fix the Helper for the codegenerator so that out conversions fo…
Browse files Browse the repository at this point in the history
…r api classes don't require the actual xml node. This is required to support returning api types from other modules.
  • Loading branch information
Jim Carroll committed Oct 22, 2012
1 parent 5d37556 commit 9829ef0
Showing 1 changed file with 46 additions and 13 deletions.
59 changes: 46 additions & 13 deletions tools/codegenerator/Helper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,15 @@ public class Helper
// if (convertTemplate == null) convertTemplate = outTypemap[apiLType]

// is the returns a pointer to a known class
Node classNode = null
String className = null
if (convertTemplate == null && apiType.startsWith('p.'))
{
classNode = findClassNodeByName(parents(method)[0], SwigTypeParser.getRootType(apiType),method)
if (classNode) convertTemplate = defaultOutTypeConversion
Node classNode = findClassNodeByName(parents(method)[0], SwigTypeParser.getRootType(apiType),method)
if (classNode)
{
className = findFullClassName(classNode)
convertTemplate = defaultOutTypeConversion
}
}

if (convertTemplate == null)
Expand All @@ -196,12 +200,22 @@ public class Helper
convertTemplate = outTypemap.find({ key, value -> (key instanceof Pattern && key.matcher(apiType).matches()) })?.value
}

if (!convertTemplate)
{
String knownApiType = isKnownApiType(apiType,method)
if (knownApiType)
{
convertTemplate = defaultOutTypeConversion
className = knownApiType
}
}

if (!convertTemplate)
{
if (recurse)
return getOutConversion(SwigTypeParser.SwigType_ltype(apiType),apiName,method,overrideBindings,false)
else
throw new RuntimeException("WARNING: Cannot convert the return value of swig type ${apiType} for the call ${Helper.findFullClassName(method) + '::' + Helper.callingName(method)}")
else if (!isKnownApiType(apiType,method))
throw new RuntimeException("WARNING: Cannot convert the return value of swig type ${apiType} for the call ${Helper.findFullClassName(method) + '::' + Helper.callingName(method)}")
}

boolean seqSetHere = false
Expand All @@ -217,7 +231,7 @@ public class Helper
'method' : method, 'helper' : Helper.class,
'swigTypeParser' : SwigTypeParser.class,
'sequence' : seq ]
if (classNode) bindings['classnode'] = classNode
if (className) bindings['classname'] = className

if (overrideBindings) bindings.putAll(overrideBindings)

Expand Down Expand Up @@ -652,27 +666,46 @@ public class Helper
*/
public static boolean isKnownBaseType(String type, Node searchFrom)
{
return hasFeatureSetting(type,searchFrom,'feature_knownbasetypes',{ it.split(',').find({ it == type }) != null })
return hasFeatureSetting(type,searchFrom,'feature_knownbasetypes',{ it.split(',').find({ it.trim() == type }) != null })
}

/**
* This method will search from the 'searchFrom' Node up to the root
* looking for a %feature("knownapitypes") declaration that the given 'type' is
* known for 'searchFrom' Node.
*/
public static boolean isKnownApiType(String type, Node searchFrom)
public static String isKnownApiType(String type, Node searchFrom)
{
String rootType = SwigTypeParser.getRootType(type)
return hasFeatureSetting(type,searchFrom,'feature_knownapitypes',{ it.split(',').find({ it == rootType }) != null })
String namespace = findNamespace(searchFrom,'::',false)
return hasFeatureSetting(type,searchFrom,'feature_knownapitypes',{ it.split(',').find(
{
if (it.trim() == rootType)
return true
// we assume the 'type' is defined within namespace and
// so we can walk up the namespace appending the type until
// we find a match.
while (namespace != '')
{
// System.out.println('checking ' + (namespace + '::' + rootType))
if ((namespace + '::' + rootType) == it.trim())
return true
// truncate the last namespace
int chop = namespace.lastIndexOf('::')
namespace = (chop > 0) ? namespace.substring(0,chop) : ''
}
return false
}) != null })
}

private static boolean hasFeatureSetting(String type, Node searchFrom, String feature, Closure test)
private static String hasFeatureSetting(String type, Node searchFrom, String feature, Closure test)
{
if (!searchFrom)
return false
return null

if (searchFrom.attribute(feature) && test.call(searchFrom.attribute(feature)))
return true
Object attr = searchFrom.attribute(feature)
if (attr && test.call(attr))
return attr.toString()

return hasFeatureSetting(type,searchFrom.parent(),feature,test)
}
Expand Down

0 comments on commit 9829ef0

Please sign in to comment.