Browse Source

add more convenience functions for type checking of variants

Julius Pfrommer 8 years ago
parent
commit
9c3426a48e
3 changed files with 35 additions and 5 deletions
  1. 2 2
      README.md
  2. 2 2
      examples/client_firstSteps.c
  3. 31 1
      include/ua_types.h

+ 2 - 2
README.md

@@ -123,8 +123,8 @@ int main(int argc, char *argv[])
     UA_Variant value; /* Variants can hold scalar values and arrays of any type */
     UA_Variant_init(&value);
     status = UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), &value);
-    if(status == UA_STATUSCODE_GOOD && UA_Variant_isScalar(&value) &&
-       value.type == &UA_TYPES[UA_TYPES_INT32]) {
+    if(status == UA_STATUSCODE_GOOD &&
+       UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_INT32])) {
         printf("the value is: %i\n", *(UA_Int32*)value.data);
     }
 

+ 2 - 2
examples/client_firstSteps.c

@@ -29,8 +29,8 @@ int main(void) {
     const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, NS0_CURRENT_TIME);
 
     retval = UA_Client_readValueAttribute(client, nodeId, &value);
-    if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(&value) &&
-       value.type == &UA_TYPES[UA_TYPES_DATETIME]) {
+    if(retval == UA_STATUSCODE_GOOD &&
+       UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
         UA_DateTime raw_date = *(UA_DateTime*)value.data;
         UA_String string_date = UA_DateTime_toString(raw_date);
         printf("string date is: %.*s\n", (int)string_date.length, string_date.data);

+ 31 - 1
include/ua_types.h

@@ -503,16 +503,46 @@ typedef struct {
     UA_UInt32 *arrayDimensions;   /* The length of each dimension */
 } UA_Variant;
 
+/* Returns true if the variant has no value defined (contains neither an array
+ * nor a scalar value).
+ *
+ * @param v The variant
+ * @return Is the variant empty */
+static UA_INLINE UA_Boolean
+UA_Variant_isEmpty(const UA_Variant *v) {
+    return v->type == NULL;
+}
+
 /* Returns true if the variant contains a scalar value. Note that empty variants
  * contain an array of length -1 (undefined).
  *
  * @param v The variant
- * @return Does the variant contain a scalar value. */
+ * @return Does the variant contain a scalar value */
 static UA_INLINE UA_Boolean
 UA_Variant_isScalar(const UA_Variant *v) {
     return (v->arrayLength == 0 && v->data > UA_EMPTY_ARRAY_SENTINEL);
 }
 
+/* Returns true if the variant contains a scalar value of the given type.
+ *
+ * @param v The variant
+ * @param type The data type
+ * @return Does the variant contain a scalar value of the given type */
+static UA_INLINE UA_Boolean
+UA_Variant_hasScalarType(const UA_Variant *v, const UA_DataType *type) {
+    return UA_Variant_isScalar(v) && type == v->type;
+}
+
+/* Returns true if the variant contains an array of the given type.
+ *
+ * @param v The variant
+ * @param type The data type
+ * @return Does the variant contain an array of the given type */
+static UA_INLINE UA_Boolean
+UA_Variant_hasArrayType(const UA_Variant *v, const UA_DataType *type) {
+    return (!UA_Variant_isScalar(v)) && type == v->type;
+}
+
 /* Set the variant to a scalar value that already resides in memory. The value
  * takes on the lifecycle of the variant and is deleted with it.
  *