datatypes.rst 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. Data Types Introduction
  2. =======================
  3. In open62541, all data types share the same basic API for creation, copying and deletion. Assume that *T* is the data type in question.
  4. void T_init(T *ptr)
  5. Initialize the data type. This is synonymous with zeroing out the memory, i.e. *memset(dataptr, 0, sizeof(T))*.
  6. T* T_new()
  7. Allocate and return the memory for the data type. The memory is already initialized.
  8. UA_StatusCode T_copy(const T *src, T *dst)
  9. Copy the content of the data type. Returns *UA_STATUSCODE_GOOD* if it succeeded.
  10. void T_deleteMembers(T *ptr)
  11. Delete the dynamically allocated content of the data type, but not the data type itself.
  12. void T_delete(T *ptr)
  13. Delete the content of the data type and the memory for the data type itself.
  14. Here's a small example for the UA_String data type. UA_String will be introduced in more detail later on, but you should be able to follow the example already.
  15. .. code-block:: c
  16. /* The definition of UA_String copied from ua_types.h */
  17. typedef struct {
  18. size_t length; ///< The length of the string
  19. UA_Byte *data; ///< The string's content (not null-terminated)
  20. } UA_String;
  21. UA_String s1 = UA_STRING("test1"); /* s1 points to the statically allocated string buffer */
  22. UA_String_init(&s1); /* Reset s1 (no memleak due to the statically allocated buffer) */
  23. UA_String s2 = UA_STRING_ALLOC("test2"); /* s2 points to a new copy of the string buffer (with malloc) */
  24. UA_String_deleteMembers(&s2); /* Free the content of s2, but not s2 itself */
  25. UA_String *s3 = UA_String_new(); /* The string s3 is malloced and initialized */
  26. *s3 = UA_STRING_ALLOC("test3"); /* s3 points to a new copy of the string buffer */
  27. UA_String s4;
  28. UA_copy(s3, &s4); /* Copy the content of s3 to s4 */
  29. UA_String_delete(s3); /* Free the string buffer and the string itself */
  30. UA_String_deleteMembers(&s4); /* Again, delete only the string buffer */
  31. Generic Data Type Handling
  32. ==========================
  33. All data types are combinations of the 25 builtin data types show below. Types
  34. are described in the UA_DataType structure.
  35. .. doxygenstruct:: UA_DataType
  36. :members:
  37. .. c:var:: const UA_DataType UA_TYPES[UA_TYPES_COUNT]
  38. The datatypes defined in the standard are stored in the ``UA_TYPES`` array.
  39. A typical function call is ``UA_Array_new(&data_ptr, 20, &UA_TYPES[UA_TYPES_STRING])``.
  40. .. doxygenfunction:: UA_new
  41. .. doxygenfunction:: UA_init
  42. .. doxygenfunction:: UA_copy
  43. .. doxygenfunction:: UA_deleteMembers
  44. .. doxygenfunction:: UA_delete
  45. For all datatypes, there are also macros with syntactic sugar over calling the
  46. generic functions with a pointer into the ``UA_TYPES`` array.
  47. .. c:function:: <typename>_new()
  48. Allocates the memory for the type and runs _init on the returned variable.
  49. Returns null if no memory could be allocated.
  50. .. c:function:: <typename>_init(<typename> *value)
  51. Sets all members of the type to a default value, usually zero. Arrays (e.g.
  52. for strings) are set to a length of -1.
  53. .. c:function:: <typename>_copy(<typename> *src, <typename> *dst)
  54. Copies a datatype. This performs a deep copy iterating over the members.
  55. Copying into variants with an external data source is not permitted. If
  56. copying fails, a deleteMembers is performed and an error code returned.
  57. .. c:function:: <typename>_deleteMembers(<typename> *value)
  58. Frees the memory of dynamically sized members of a datatype (e.g. arrays).
  59. .. c:function:: <typename>_delete(<typename> *value)
  60. Frees the memory of the datatype and its dynamically allocated members.
  61. Array Handling
  62. --------------
  63. .. doxygenfunction:: UA_Array_new
  64. .. doxygenfunction:: UA_Array_copy
  65. .. doxygenfunction:: UA_Array_delete
  66. Builtin Data Types
  67. ------------------
  68. Number-Types
  69. ^^^^^^^^^^^^
  70. .. doxygentypedef:: UA_Boolean
  71. .. doxygentypedef:: UA_SByte
  72. .. doxygentypedef:: UA_Byte
  73. .. doxygentypedef:: UA_Int16
  74. .. doxygentypedef:: UA_UInt16
  75. .. doxygentypedef:: UA_Int32
  76. .. doxygentypedef:: UA_UInt32
  77. .. doxygentypedef:: UA_Int64
  78. .. doxygentypedef:: UA_UInt64
  79. .. doxygentypedef:: UA_Float
  80. .. doxygentypedef:: UA_Double
  81. UA_String
  82. ^^^^^^^^^
  83. .. doxygenstruct:: UA_String
  84. :members:
  85. .. c:macro:: UA_STRING_NULL
  86. The empty string
  87. .. c:macro:: UA_STRING(CHARS)
  88. Creates an UA_String from an array of ``char``. The characters are not copied
  89. on the heap. Instead, the string points into the existing array.
  90. .. c:macro:: UA_STRING_ALLOC(CHARS)
  91. Creates an UA_String from an array of ``char``. The characters are copied on
  92. the heap.
  93. .. doxygenfunction:: UA_String_equal
  94. .. doxygenfunction:: UA_String_copyprintf
  95. UA_DateTime
  96. ^^^^^^^^^^^
  97. .. doxygentypedef:: UA_DateTime
  98. .. doxygenfunction:: UA_DateTime_now(void)
  99. .. doxygenfunction:: UA_DateTime_toString
  100. .. doxygenfunction:: UA_DateTime_toStruct
  101. UA_Guid
  102. ^^^^^^^
  103. .. doxygenstruct:: UA_Guid
  104. .. doxygenfunction:: UA_Guid_equal
  105. .. doxygenfunction:: UA_Guid_random
  106. UA_ByteString
  107. ^^^^^^^^^^^^^
  108. Bytestring are just a redefinition of strings. The semantic difference is that
  109. ByteStrings may hold non-UTF8 data.
  110. .. doxygentypedef:: UA_ByteString
  111. .. c:macro:: UA_BYTESTRING_NULL
  112. The empty ByteString
  113. .. c:function:: UA_Boolean UA_ByteString_equal(const UA_ByteString *s1, const UA_ByteString *s2)
  114. Compares two ByteStrings.
  115. UA_XmlElement
  116. ^^^^^^^^^^^^^
  117. XmlElements are just a redefinition of strings.
  118. .. doxygentypedef:: UA_XmlElement
  119. UA_NodeId
  120. ^^^^^^^^^
  121. .. doxygenstruct:: UA_NodeId
  122. :members:
  123. .. doxygenfunction:: UA_NodeId_equal
  124. .. doxygenfunction:: UA_NodeId_isNull
  125. .. c:macro:: UA_NODEID_NULL
  126. The null NodeId
  127. .. c:macro:: UA_NODEID_NUMERIC(NSID, NUMERICID)
  128. .. c:macro:: UA_NODEID_STRING(NSID, CHARS)
  129. .. c:macro:: UA_NODEID_STRING_ALLOC(NSID, CHARS)
  130. .. c:macro:: UA_NODEID_GUID(NSID, GUID)
  131. .. c:macro:: UA_NODEID_BYTESTRING(NSID, CHARS)
  132. .. c:macro:: UA_NODEID_BYTESTRING_ALLOC(NSID, CHARS)
  133. UA_ExpandedNodeId
  134. ^^^^^^^^^^^^^^^^^
  135. .. doxygenstruct:: UA_ExpandedNodeId
  136. :members:
  137. .. doxygenfunction:: UA_ExpandedNodeId_isNull
  138. .. c:macro:: UA_EXPANDEDNODEID_NUMERIC(NSID, NUMERICID)
  139. UA_StatusCode
  140. ^^^^^^^^^^^^^
  141. Many functions in open62541 return either ``UA_STATUSCODE_GOOD`` or an error code.
  142. .. doxygentypedef:: UA_StatusCode
  143. UA_QualifiedName
  144. ^^^^^^^^^^^^^^^^
  145. .. doxygenstruct:: UA_QualifiedName
  146. :members:
  147. .. c:macro:: UA_QUALIFIEDNAME(NSID, CHARS)
  148. .. c:macro:: UA_QUALIFIEDNAME_ALLOC(NSID, CHARS)
  149. UA_LocalizedText
  150. ^^^^^^^^^^^^^^^^
  151. .. doxygenstruct:: UA_LocalizedText
  152. :members:
  153. .. c:macro:: UA_LOCALIZEDTEXT(LOCALE, TEXT)
  154. Takes two arrays of ``char`` as the input.
  155. .. c:macro:: UA_LOCALIZEDTEXT_ALLOC(LOCALE, TEXT)
  156. UA_ExtensionObject
  157. ^^^^^^^^^^^^^^^^^^
  158. .. doxygenstruct:: UA_ExtensionObject
  159. :members:
  160. UA_DataValue
  161. ^^^^^^^^^^^^
  162. .. doxygenstruct:: UA_DataValue
  163. :members:
  164. :undoc-members:
  165. UA_Variant
  166. ^^^^^^^^^^
  167. .. doxygenstruct:: UA_Variant
  168. :members:
  169. .. doxygenfunction:: UA_Variant_isScalar
  170. .. doxygenfunction:: UA_Variant_setScalar
  171. .. doxygenfunction:: UA_Variant_setScalarCopy
  172. .. doxygenfunction:: UA_Variant_setArray
  173. .. doxygenfunction:: UA_Variant_setArrayCopy
  174. .. doxygenstruct:: UA_NumericRange
  175. :undoc-members:
  176. .. doxygenfunction:: UA_Variant_setRange
  177. .. doxygenfunction:: UA_Variant_setRangeCopy
  178. UA_DiagnosticInfo
  179. ^^^^^^^^^^^^^^^^^
  180. .. doxygenstruct:: UA_DiagnosticInfo
  181. :members:
  182. :undoc-members: