Przeglądaj źródła

a helper function to test if a variant contains a scalar value

Julius Pfrommer 10 lat temu
rodzic
commit
d0c2c27cd8
4 zmienionych plików z 18 dodań i 5 usunięć
  1. 4 4
      README.md
  2. 1 1
      examples/client.c
  3. 9 0
      include/ua_types.h
  4. 4 0
      src/ua_types.c

+ 4 - 4
README.md

@@ -95,10 +95,10 @@ int main(int argc, char *argv[]) {
     UA_ReadResponse resp = UA_Client_read(client, &req);
     if(resp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
        resp.resultsSize > 0 && resp.results[0].hasValue &&
-       resp.results[0].value.data /* an empty array returns a null-ptr */ &&
-       resp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]){
-           UA_Int32 value = *(UA_Int32*)resp.results[0].value.data;
-           printf("the value is: %i\n", value);
+       UA_Variant_isScalar(&resp.results[0].value) &&
+       resp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]) {
+           UA_Int32 *value = (UA_Int32*)resp.results[0].value.data;
+           printf("the value is: %i\n", *value);
    }
 
     UA_ReadRequest_deleteMembers(&req);

+ 1 - 1
examples/client.c

@@ -62,7 +62,7 @@ int main(int argc, char *argv[]) {
     UA_ReadResponse rResp = UA_Client_read(client, &rReq);
     if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
        rResp.resultsSize > 0 && rResp.results[0].hasValue &&
-       rResp.results[0].value.data /* an empty array returns a null-ptr */ &&
+       UA_Variant_isScalar(&rResp.results[0].value) &&
        rResp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]){
         value = *(UA_Int32*)rResp.results[0].value.data;
         printf("the value is: %i\n", value);

+ 9 - 0
include/ua_types.h

@@ -455,6 +455,15 @@ UA_Boolean UA_EXPORT UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p);
 
 /* Variant */
 
+/**
+ * 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.
+ */
+UA_Boolean UA_EXPORT UA_Variant_isScalar(UA_Variant *v);
+    
 /**
  * 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.

+ 4 - 0
src/ua_types.c

@@ -592,6 +592,10 @@ UA_StatusCode UA_Variant_copy(UA_Variant const *src, UA_Variant *dst) {
     return retval;
 }
 
+UA_Boolean UA_Variant_isScalar(UA_Variant *v) {
+    return (v->data != UA_NULL && v->arrayLength == -1);
+}
+
 /**
  * Tests if a range is compatible with a variant. If yes, the following values are set:
  * - total: how many elements are indicated by the range