Skip to content
Snippets Groups Projects
Commit f18c03f7 authored by Jaswant Panchumarti (Kitware)'s avatar Jaswant Panchumarti (Kitware)
Browse files

WrapSerDes: Add support for C-style enums and scoped enums

- Finishes the implementation to tackle both kinds of enums.
- Paraphrased enum detection code from vtkWrapPython
parent 86101207
No related branches found
No related tags found
No related merge requests found
......@@ -208,6 +208,66 @@ static void vtkWrapSerDes_GenerateSpecialHeaders(
free((char**)types);
}
/* -------------------------------------------------------------------- */
/* check whether an enum type will be wrapped */
int vtkWrapSerDes_IsEnumWrapped(const HierarchyInfo* hinfo, const char* enumname)
{
int rval = 0;
const HierarchyEntry* entry;
if (hinfo && enumname)
{
entry = vtkParseHierarchy_FindEntry(hinfo, enumname);
if (entry && entry->IsEnum && !vtkParseHierarchy_GetProperty(entry, "WRAPEXCLUDE"))
{
rval = 1;
}
}
return rval;
}
/* -------------------------------------------------------------------- */
/* find and mark all enum parameters by setting IsEnum=1 */
static void vtkWrapSerDes_MarkAllEnums(NamespaceInfo* contents, const HierarchyInfo* hinfo)
{
FunctionInfo* currentFunction;
int i, j, n, m, ii, nn;
ClassInfo* data;
ValueInfo* val;
nn = contents->NumberOfClasses;
for (ii = 0; ii < nn; ii++)
{
data = contents->Classes[ii];
n = data->NumberOfFunctions;
for (i = 0; i < n; i++)
{
currentFunction = data->Functions[i];
if (!currentFunction->IsExcluded && currentFunction->Access == VTK_ACCESS_PUBLIC)
{
/* we start with the return value */
val = currentFunction->ReturnValue;
m = vtkWrap_CountWrappedParameters(currentFunction);
/* the -1 is for the return value */
for (j = (val ? -1 : 0); j < m; j++)
{
if (j >= 0)
{
val = currentFunction->Parameters[j];
}
if (vtkWrap_IsEnumMember(data, val) || vtkWrapSerDes_IsEnumWrapped(hinfo, val->Class))
{
val->IsEnum = 1;
}
}
}
}
}
}
/**
* This is the main entry point for generating object coders.
* When called, it will print the vtkXXXSerialization.cxx file contents to "fp".
......@@ -294,6 +354,9 @@ int VTK_PARSE_MAIN(int argc, char* argv[])
/* get the global namespace */
contents = file_info->Contents;
/* Identify all enum types that are used by methods */
vtkWrapSerDes_MarkAllEnums(contents, hinfo);
/* SPDX */
fprintf(fp,
"// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen\n"
......
......@@ -275,6 +275,7 @@ int vtkWrapSerDes_WritePropertySerializer(FILE* fp, const ClassInfo* classInfo,
const int isArray = vtkWrap_IsArray(propertyValueInfo);
const int isStdVector = vtkWrap_IsStdVector(propertyValueInfo);
const int isEnumMember = vtkWrap_IsEnumMember(classInfo, propertyValueInfo);
const int isEnum = functionInfo->ReturnValue->IsEnum;
const int isConst = vtkWrap_IsConst(propertyValueInfo);
free(propertyValueInfo);
propertyValueInfo = NULL;
......@@ -407,6 +408,30 @@ int vtkWrapSerDes_WritePropertySerializer(FILE* fp, const ClassInfo* classInfo,
propertyInfo->ClassName, getterName);
return 1;
}
else if (isEnum)
{
fprintf(fp, " state[\"%s\"] = ", keyName);
const char* cp = functionInfo->ReturnValue->Class;
size_t l;
/* search for scope operator */
for (l = 0; cp[l] != '\0'; l++)
{
if (cp[l] == ':')
{
break;
}
}
if (cp[l] == ':' && cp[l + 1] == ':')
{
fprintf(fp, "static_cast<std::underlying_type<%*.*s::%s>::type>(object->%s());\n", (int)l,
(int)l, cp, &cp[l + 2], getterName);
}
else
{
fprintf(fp, "static_cast<std::underlying_type<%s>::type>(object->%s());\n", cp, getterName);
}
return 1;
}
else if (strncmp(propertyInfo->ClassName, "vtkVector", 9) == 0 ||
strncmp(propertyInfo->ClassName, "vtkTuple", 8) == 0 ||
strncmp(propertyInfo->ClassName, "vtkColor", 8) == 0 ||
......@@ -532,6 +557,12 @@ int vtkWrapSerDes_WritePropertyDeserializer(FILE* fp, const ClassInfo* classInfo
const int isArray = vtkWrap_IsArray(val);
const int isStdVector = vtkWrap_IsStdVector(val);
int isEnum = 0;
if (functionInfo->NumberOfParameters > 0)
{
isEnum = functionInfo->Parameters[0]->IsEnum;
}
if (vtkWrapSerDes_IsCollectionLike(propertyInfo->PublicMethods) ||
vtkWrapSerDes_IsCollectionLikeNoDiscard(propertyInfo->PublicMethods))
{
......@@ -680,6 +711,43 @@ int vtkWrapSerDes_WritePropertyDeserializer(FILE* fp, const ClassInfo* classInfo
fprintf(fp, " }\n");
return 1;
}
else if (isEnum)
{
fprintf(fp, " {\n");
fprintf(fp, " const auto iter = state.find(\"%s\");\n", keyName);
fprintf(fp, " if ((iter != state.end()) && !iter->is_null())\n");
fprintf(fp, " {\n");
const char* cp = functionInfo->Parameters[0]->Class;
size_t l;
/* search for scope operator */
for (l = 0; cp[l] != '\0'; l++)
{
if (cp[l] == ':')
{
break;
}
}
if (cp[l] == ':' && cp[l + 1] == ':')
{
fprintf(fp,
" auto value = "
"static_cast<%*.*s::%s>(iter->get<std::underlying_type<%*.*s::%s>::type>());\n",
(int)l, (int)l, cp, &cp[l + 2], (int)l, (int)l, cp, &cp[l + 2]);
}
else
{
fprintf(fp,
" auto value = "
"static_cast<%s>(iter->get<std::underlying_type<%s>::type>());\n",
cp, cp);
}
callSetterBeginMacro(fp, " ");
callSetterParameterMacro(fp, "value");
callSetterEndMacro(fp);
fprintf(fp, " }\n");
fprintf(fp, " }\n");
return 1;
}
else if (isStdVector)
{
char* arg = vtkWrap_TemplateArg(val->Class);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment