[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] esx: Cleanup and refactor CastFromAnyType macros



Add CastFromAnyType functions for the String type.
---
 src/esx/esx_vi_generator.py |    5 +--
 src/esx/esx_vi_types.c      |   64 +++++++++++++++++++++++++++++++-----------
 src/esx/esx_vi_types.h      |    2 +
 3 files changed, 51 insertions(+), 20 deletions(-)

diff --git a/src/esx/esx_vi_generator.py b/src/esx/esx_vi_generator.py
index 622a34a..0fd84dd 100755
--- a/src/esx/esx_vi_generator.py
+++ b/src/esx/esx_vi_generator.py
@@ -773,12 +773,11 @@ class Object(Base):
         # cast from any type
         if self.features & Object.FEATURE__ANY_TYPE:
             source += "/* esxVI_%s_CastFromAnyType */\n" % self.name
-            source += "ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(%s,\n" % self.name
 
             if self.extended_by is None:
-                source += "{\n"
-                source += "})\n\n"
+                source += "ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(%s)\n\n" % self.name
             else:
+                source += "ESX_VI__TEMPLATE__DYNAMIC_CAST_FROM_ANY_TYPE(%s,\n" % self.name
                 source += "{\n"
 
                 for extended_by in self.extended_by:
diff --git a/src/esx/esx_vi_types.c b/src/esx/esx_vi_types.c
index f3df2b5..f3cdf2a 100644
--- a/src/esx/esx_vi_types.c
+++ b/src/esx/esx_vi_types.c
@@ -183,32 +183,53 @@
 
 
 
-#define ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(_type, _dispatch)                \
+#define ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE_EXTRA(_type, _dest_type, _extra, \
+                                                   _dest_extra)               \
     int                                                                       \
-    esxVI_##_type##_CastFromAnyType(esxVI_AnyType *anyType,                   \
-                                    esxVI_##_type **ptrptr)                   \
+    esxVI_##_type##_Cast##_dest_extra##FromAnyType(esxVI_AnyType *anyType,    \
+                                                   _dest_type **ptrptr)       \
     {                                                                         \
+        _dest_type *item;                                                     \
+                                                                              \
         if (anyType == NULL || ptrptr == NULL || *ptrptr != NULL) {           \
             ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",                        \
                          _("Invalid argument"));                              \
             return -1;                                                        \
         }                                                                     \
                                                                               \
-        switch (anyType->type) {                                              \
-          _dispatch                                                           \
+        item = *ptrptr;                                                       \
                                                                               \
-          case esxVI_Type_##_type:                                            \
-            break;                                                            \
+        _extra                                                                \
                                                                               \
-          default:                                                            \
+        return esxVI_##_type##_Deserialize##_dest_extra(anyType->node,        \
+                                                        ptrptr);              \
+    }
+
+
+
+#define ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(_type)                           \
+    ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE_EXTRA(_type, esxVI_##_type,          \
+    {                                                                         \
+        if (anyType->type != esxVI_Type_##_type) {                            \
             ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                              \
                          _("Call to %s for unexpected type '%s'"),            \
                          __FUNCTION__, anyType->other);                       \
             return -1;                                                        \
         }                                                                     \
-                                                                              \
-        return esxVI_##_type##_Deserialize(anyType->node, ptrptr);            \
-    }
+    }, /* nothing */)
+
+
+
+#define ESX_VI__TEMPLATE__CAST_VALUE_FROM_ANY_TYPE(_type, _value_type)        \
+    ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE_EXTRA(_type, _value_type,            \
+    {                                                                         \
+        if (anyType->type != esxVI_Type_##_type) {                            \
+            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                              \
+                         _("Call to %s for unexpected type '%s'"),            \
+                         __FUNCTION__, anyType->other);                       \
+            return -1;                                                        \
+        }                                                                     \
+    }, Value)
 
 
 
@@ -591,6 +612,13 @@
 
 
 
+#define ESX_VI__TEMPLATE__DYNAMIC_CAST_FROM_ANY_TYPE(__type, _dispatch)       \
+    ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE_EXTRA(__type, esxVI_##__type,        \
+      ESX_VI__TEMPLATE__DISPATCH(__type, _dispatch, -1),                      \
+      /* nothing */)
+
+
+
 #define ESX_VI__TEMPLATE__DYNAMIC_SERIALIZE(__type, _dispatch, _serialize)    \
     ESX_VI__TEMPLATE__SERIALIZE_EXTRA(__type,                                 \
       ESX_VI__TEMPLATE__DISPATCH(__type, _dispatch, -1),                      \
@@ -1111,6 +1139,12 @@ esxVI_String_DeepCopyValue(char **dest, const char *src)
     return 0;
 }
 
+/* esxVI_String_CastFromAnyType */
+ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(String)
+
+/* esxVI_String_CastValueFromAnyType */
+ESX_VI__TEMPLATE__CAST_VALUE_FROM_ANY_TYPE(String, char)
+
 int
 esxVI_String_Serialize(esxVI_String *string, const char *element,
                        virBufferPtr output)
@@ -1240,9 +1274,7 @@ ESX_VI__TEMPLATE__VALIDATE(Long,
 ESX_VI__TEMPLATE__LIST__APPEND(Long)
 
 /* esxVI_Long_CastFromAnyType */
-ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(Long,
-{
-})
+ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(Long)
 
 /* esxVI_Long_Serialize */
 ESX_VI__TEMPLATE__SERIALIZE(Long,
@@ -1522,9 +1554,7 @@ ESX_VI__TEMPLATE__DEEP_COPY(ManagedObjectReference,
 ESX_VI__TEMPLATE__LIST__APPEND(ManagedObjectReference)
 
 /* esxVI_ManagedObjectReference_CastFromAnyType */
-ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(ManagedObjectReference,
-{
-})
+ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(ManagedObjectReference)
 
 /* esxVI_ManagedObjectReference_CastListFromAnyType */
 ESX_VI__TEMPLATE__LIST__CAST_FROM_ANY_TYPE(ManagedObjectReference)
diff --git a/src/esx/esx_vi_types.h b/src/esx/esx_vi_types.h
index 1ab39da..e53ccda 100644
--- a/src/esx/esx_vi_types.h
+++ b/src/esx/esx_vi_types.h
@@ -178,6 +178,8 @@ int esxVI_String_AppendValueListToList(esxVI_String **stringList,
 int esxVI_String_DeepCopy(esxVI_String **dest, esxVI_String *src);
 int esxVI_String_DeepCopyList(esxVI_String **destList, esxVI_String *srcList);
 int esxVI_String_DeepCopyValue(char **dest, const char *src);
+int esxVI_String_CastFromAnyType(esxVI_AnyType *anyType, esxVI_String **string);
+int esxVI_String_CastValueFromAnyType(esxVI_AnyType *anyType, char **string);
 int esxVI_String_Serialize(esxVI_String *string, const char *element,
                            virBufferPtr output);
 int esxVI_String_SerializeList(esxVI_String *stringList, const char *element,
-- 
1.7.0.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]