浏览代码

hide some #ifdefs in a macro

Julius Pfrommer 7 年之前
父节点
当前提交
6b3186ab5d
共有 3 个文件被更改,包括 21 次插入21 次删除
  1. 7 0
      src/ua_util.h
  2. 9 17
      tests/check_types_custom.c
  3. 5 4
      tools/generate_datatypes.py

+ 7 - 0
src/ua_util.h

@@ -128,6 +128,13 @@ size_t UA_readNumber(u8 *buf, size_t buflen, u32 *number);
 #define MIN(A,B) (A > B ? B : A)
 #define MAX(A,B) (A > B ? A : B)
 
+/* The typename string can be disabled to safe memory */
+#ifdef UA_ENABLE_TYPENAMES
+# define UA_TYPENAME(name) name,
+#else
+# define UA_TYPENAME(name)
+#endif
+
 #ifdef __cplusplus
 } // extern "C"
 #endif

+ 9 - 17
tests/check_types_custom.c

@@ -30,37 +30,29 @@ typedef struct {
 static UA_DataTypeMember members[3] = {
     /* x */
     {
-#ifdef UA_ENABLE_TYPENAMES
-        "x",            /* .memberName */
-#endif
-        UA_TYPES_FLOAT, /* .memberTypeIndex, points into UA_TYPES since
-                           .namespaceZero is true */
-        0,              /* .padding */
-        true,           /* .namespaceZero, see .memberTypeIndex */
-        false           /* .isArray */
+        UA_TYPENAME("x") /* .memberName */
+        UA_TYPES_FLOAT,  /* .memberTypeIndex, points into UA_TYPES since
+                            .namespaceZero is true */
+        0,               /* .padding */
+        true,            /* .namespaceZero, see .memberTypeIndex */
+        false            /* .isArray */
     },
 
     /* y */
     {
-#ifdef UA_ENABLE_TYPENAMES
-        "y",
-#endif
+        UA_TYPENAME("y")
         UA_TYPES_FLOAT, padding_y, true, false
     },
 
     /* z */
     {
-#ifdef UA_ENABLE_TYPENAMES
-        "y",
-#endif
+        UA_TYPENAME("y")
         UA_TYPES_FLOAT, padding_z, true, false
     }
 };
 
 static const UA_DataType PointType = {
-#ifdef UA_ENABLE_TYPENAMES
-    "Point",                         /* .typeName */
-#endif
+    UA_TYPENAME("Point")             /* .typeName */
     {1, UA_NODEIDTYPE_NUMERIC, {1}}, /* .typeId */
     sizeof(Point),                   /* .memSize */
     0,                               /* .typeIndex, in the array of custom types */

+ 5 - 4
tools/generate_datatypes.py

@@ -88,7 +88,7 @@ class Type(object):
             binaryEncodingId = description.binaryEncodingId
         else:
             typeid = "{0, UA_NODEIDTYPE_NUMERIC, {0}}"
-        return "{\n#ifdef UA_ENABLE_TYPENAMES\n    \"%s\", /* .typeName */\n#endif\n" % self.name + \
+        return "{\n    UA_TYPENAME(\"%s\") /* .typeName */\n" % self.name + \
             "    " + typeid + ", /* .typeId */\n" + \
             "    sizeof(UA_" + self.name + "), /* .memSize */\n" + \
             "    " + self.typeIndex + ", /* .typeIndex */\n" + \
@@ -97,7 +97,7 @@ class Type(object):
             "    " + self.pointerfree + ", /* .pointerFree */\n" + \
             "    " + self.overlayable + ", /* .overlayable */ \n" + \
             "    " + binaryEncodingId + ", /* .binaryEncodingId */\n" + \
-            "    %s_members" % self.name + " /* .members */ }"
+            "    %s_members" % self.name + " /* .members */\n}"
 
     def members_c(self):
         if len(self.members)==0:
@@ -105,7 +105,7 @@ class Type(object):
         members = "static UA_DataTypeMember %s_members[%s] = {" % (self.name, len(self.members))
         before = None
         for index, member in enumerate(self.members):
-            m = "\n{\n#ifdef UA_ENABLE_TYPENAMES\n    \"%s\", /* .memberName */\n#endif\n" % member.name
+            m = "\n{\n    UA_TYPENAME(\"%s\") /* .memberName */\n" % member.name
             m += "    %s_%s, /* .memberTypeIndex */\n" % (member.memberType.outname.upper(), member.memberType.name.upper())
             m += "    "
             if not before:
@@ -494,7 +494,8 @@ printc('''/* Generated from ''' + inname + ''' with script ''' + sys.argv[0] + '
  * on host ''' + platform.uname()[1] + ''' by user ''' + getpass.getuser() + \
        ''' at ''' + time.strftime("%Y-%m-%d %I:%M:%S") + ''' */
 
-#include "''' + outname + '''_generated.h"''')
+#include "''' + outname + '''_generated.h"
+#include "ua_util.h"''')
 
 for t in iter_types(types):
     printc("")