datatypes.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. Data Types
  2. ==========
  3. Introduction
  4. ------------
  5. In open62541, all data types share the same basic API for creation, copying and
  6. deletion. The following functions are present for all data types ``T``.
  7. ``void T_init(T *ptr)``
  8. Initialize the data type. This is synonymous with zeroing out the memory, i.e. *memset(dataptr, 0, sizeof(T))*.
  9. ``T* T_new()``
  10. Allocate and return the memory for the data type. The memory is already initialized.
  11. ``UA_StatusCode T_copy(const T *src, T *dst)``
  12. Copy the content of the data type. Returns *UA_STATUSCODE_GOOD* if it succeeded.
  13. ``void T_deleteMembers(T *ptr)``
  14. Delete the dynamically allocated content of the data type, but not the data type itself.
  15. ``void T_delete(T *ptr)``
  16. Delete the content of the data type and the memory for the data type itself.
  17. Here's a small example for the UA_String data type. UA_String will be introduced
  18. in more detail later on, but you should be able to follow the example already.
  19. .. code-block:: c
  20. /* The definition of UA_String copied from ua_types.h */
  21. typedef struct {
  22. size_t length; ///< The length of the string
  23. UA_Byte *data; ///< The string's content (not null-terminated)
  24. } UA_String;
  25. UA_String s1 = UA_STRING("test1"); /* s1 points to the statically allocated string buffer */
  26. UA_String_init(&s1); /* Reset s1 (no memleak due to the statically allocated buffer) */
  27. UA_String s2 = UA_STRING_ALLOC("test2"); /* s2 points to a new copy of the string buffer (with malloc) */
  28. UA_String_deleteMembers(&s2); /* Free the content of s2, but not s2 itself */
  29. UA_String *s3 = UA_String_new(); /* The string s3 is malloced and initialized */
  30. *s3 = UA_STRING_ALLOC("test3"); /* s3 points to a new copy of the string buffer */
  31. UA_String s4;
  32. UA_copy(s3, &s4); /* Copy the content of s3 to s4 */
  33. UA_String_delete(s3); /* Free the string buffer and the string itself */
  34. UA_String_deleteMembers(&s4); /* Again, delete only the string buffer */
  35. The builtin data types
  36. ----------------------
  37. OPC UA defines 25 builtin data types. All other data types are combinations of
  38. the 25 builtin data types.
  39. *TODO*
  40. Generic Data Type Handling
  41. --------------------------
  42. All standard-defined types are described with an ``UA_DataType`` structure.
  43. .. doxygenstruct:: UA_DataType
  44. :members:
  45. .. c:var:: const UA_DataType UA_TYPES[UA_TYPES_COUNT]
  46. The datatypes defined in the standard are stored in the ``UA_TYPES`` array.
  47. A typical function call is ``UA_Array_new(&data_ptr, 20, &UA_TYPES[UA_TYPES_STRING])``.
  48. .. doxygenfunction:: UA_new
  49. .. doxygenfunction:: UA_init
  50. .. doxygenfunction:: UA_copy
  51. .. doxygenfunction:: UA_deleteMembers
  52. .. doxygenfunction:: UA_delete
  53. For all datatypes, there are also macros with syntactic sugar over calling the
  54. generic functions with a pointer into the ``UA_TYPES`` array.
  55. .. c:function:: <typename>_new()
  56. Allocates the memory for the type and runs _init on the returned variable.
  57. Returns null if no memory could be allocated.
  58. .. c:function:: <typename>_init(<typename> *value)
  59. Sets all members of the type to a default value, usually zero. Arrays (e.g.
  60. for strings) are set to a length of -1.
  61. .. c:function:: <typename>_copy(<typename> *src, <typename> *dst)
  62. Copies a datatype. This performs a deep copy iterating over the members.
  63. Copying into variants with an external data source is not permitted. If
  64. copying fails, a deleteMembers is performed and an error code returned.
  65. .. c:function:: <typename>_deleteMembers(<typename> *value)
  66. Frees the memory of dynamically sized members of a datatype (e.g. arrays).
  67. .. c:function:: <typename>_delete(<typename> *value)
  68. Frees the memory of the datatype and its dynamically allocated members.
  69. Array Handling
  70. --------------
  71. .. doxygenfunction:: UA_Array_new
  72. .. doxygenfunction:: UA_Array_copy
  73. .. doxygenfunction:: UA_Array_delete