ua_types.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #ifndef UA_TYPES_H_
  2. #define UA_TYPES_H_
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. /**
  6. * @defgroup types Datatypes
  7. *
  8. * @brief This module describes the datatypes used in OPC UA. There is a
  9. * division into built-in datatypes (integers, strings, etc.) and more complex
  10. * datatypes that are comprise of built-in datatypes (structs defined in the OPC
  11. * UA standard).
  12. *
  13. * All datatypes follow the same schema in the naming of relevant functions.
  14. *
  15. * - <type>_init: Sets all values to a "safe" standard. For example, if the
  16. * datatype contains a string-element, its size will be set to zero.
  17. *
  18. * - <type>_new: Allocates the memory for the type and runs <type>_init on the
  19. * returned pointer.
  20. *
  21. * - <type>_copy: Copies a datatype. This performs a deep copy that iterates
  22. * over the members.
  23. *
  24. * - <type>_delete: Frees the memory where the datatype was stored.
  25. *
  26. * - <type>_deleteMembers: Frees the memory of dynamically sized members (e.g. a
  27. * string) of a datatype. This is useful when the datatype was allocated on
  28. * the stack, whereas the dynamically sized members is heap-allocated.
  29. *
  30. * @{
  31. */
  32. /* Function return values */
  33. #define UA_SUCCESS 0
  34. #define UA_NO_ERROR UA_SUCCESS
  35. #define UA_ERROR (0x01 << 31)
  36. #define UA_ERR_INCONSISTENT (UA_ERROR | (0x01 << 1))
  37. #define UA_ERR_INVALID_VALUE (UA_ERROR | (0x01 << 2))
  38. #define UA_ERR_NO_MEMORY (UA_ERROR | (0x01 << 3))
  39. #define UA_ERR_NOT_IMPLEMENTED (UA_ERROR | (0x01 << 4))
  40. /* Boolean values and null */
  41. #define UA_TRUE (42 == 42)
  42. #define TRUE UA_TRUE
  43. #define UA_FALSE (!UA_TRUE)
  44. #define FALSE UA_FALSE
  45. /* Compare values */
  46. #define UA_EQUAL 0
  47. #define UA_NOT_EQUAL (!UA_EQUAL)
  48. /** @brief A two-state logical value (true or false). */
  49. typedef _Bool UA_Boolean;
  50. /** @brief An integer value between −128 and 127. */
  51. typedef int8_t UA_SByte;
  52. #define UA_SBYTE_MAX -128
  53. #define UA_SBYTE_MIN 127
  54. /** @brief An integer value between 0 and 256. */
  55. typedef uint8_t UA_Byte;
  56. #define UA_BYTE_MAX 256
  57. #define UA_BYTE_MIN 0
  58. /** @brief An integer value between −32 768 and 32 767. */
  59. typedef int16_t UA_Int16;
  60. #define UA_INT16_MAX 32767
  61. #define UA_INT16_MIN -32768
  62. /** @brief An integer value between 0 and 65 535. */
  63. typedef uint16_t UA_UInt16;
  64. #define UA_UINT16_MAX 65535
  65. #define UA_UINT16_MIN 0
  66. /** @brief An integer value between −2 147 483 648 and 2 147 483 647. */
  67. typedef int32_t UA_Int32;
  68. #define UA_INT32_MAX 2147483647
  69. #define UA_INT32_MIN −2147483648
  70. /** @brief An integer value between 0 and 429 4967 295. */
  71. typedef uint32_t UA_UInt32;
  72. #define UA_UINT32_MAX 4294967295;
  73. #define UA_UINT32_MIN 0;
  74. /** @brief An integer value between −9 223 372 036 854 775 808 and 9 223 372 036 854 775 807 */
  75. typedef int64_t UA_Int64;
  76. #define UA_INT64_MAX 9223372036854775807
  77. #define UA_INT64_MIN −9223372036854775808
  78. /** @brief An integer value between 0 and 18 446 744 073 709 551 615. */
  79. typedef uint64_t UA_UInt64;
  80. #define UA_UINT64_MAX = 18446744073709551615
  81. #define UA_UINT64_MIN = 0
  82. /** @brief An IEEE single precision (32 bit) floating point value. */
  83. typedef float UA_Float;
  84. /** @brief An IEEE double precision (64 bit) floating point value. */
  85. typedef double UA_Double;
  86. /** @brief A sequence of Unicode characters. */
  87. typedef struct UA_String {
  88. UA_Int32 length;
  89. UA_Byte *data;
  90. } UA_String;
  91. /** @brief An instance in time. */
  92. typedef UA_Int64 UA_DateTime; //100 nanosecond resolution
  93. /** @brief A 16 byte value that can be used as a globally unique identifier. */
  94. typedef struct UA_Guid {
  95. UA_UInt32 data1;
  96. UA_UInt16 data2;
  97. UA_UInt16 data3;
  98. UA_Byte data4[8];
  99. } UA_Guid;
  100. /** @brief A sequence of octets. */
  101. typedef struct UA_String UA_ByteString;
  102. /** @brief An XML element. */
  103. typedef struct UA_String UA_XmlElement;
  104. /** @brief An identifier for a node in the address space of an OPC UA Server. */
  105. typedef struct UA_NodeId {
  106. UA_Byte encodingByte; //enum BID_NodeIdEncodingValuesType
  107. UA_UInt16 namespace;
  108. union {
  109. UA_UInt32 numeric;
  110. UA_String string;
  111. UA_Guid guid;
  112. UA_ByteString byteString;
  113. } identifier;
  114. } UA_NodeId;
  115. #define NS0NODEID(NUMERIC_ID) \
  116. (UA_NodeId){.encodingByte = 0 /*UA_NODEIDTYPE_TWOBYTE*/, .namespace = 0, .identifier.numeric = NUMERIC_ID}
  117. #define UA_NODEIDTYPE_NAMESPACE_URI_FLAG 0x80
  118. #define UA_NODEIDTYPE_SERVERINDEX_FLAG 0x40
  119. #define UA_NODEIDTYPE_MASK (~(UA_NODEIDTYPE_NAMESPACE_URI_FLAG | UA_NODEIDTYPE_SERVERINDEX_FLAG))
  120. /** @brief A NodeId that allows the namespace URI to be specified instead of an index. */
  121. typedef struct UA_ExpandedNodeId {
  122. UA_NodeId nodeId;
  123. UA_String namespaceUri;
  124. UA_UInt32 serverIndex;
  125. } UA_ExpandedNodeId;
  126. #define NS0EXPANDEDNODEID(NUMERIC_ID) \
  127. (UA_ExpandedNodeId){.nodeId = NS0NODEID(NUMERIC_ID), .namespaceUri = {-1, UA_NULL}, .serverIndex = 0}
  128. /** @brief A numeric identifier for a error or condition that is associated with a value or an operation. */
  129. typedef UA_UInt32 UA_StatusCode; // StatusCodes aren't an enum(=int) since 32 unsigned bits are needed. See also ua_statuscodes.h */
  130. /** @brief A name qualified by a namespace. */
  131. typedef struct UA_QualifiedName {
  132. UA_UInt16 namespaceIndex;
  133. UA_String name;
  134. } UA_QualifiedName;
  135. /** @brief Human readable text with an optional locale identifier. */
  136. typedef struct UA_LocalizedText {
  137. UA_String locale;
  138. UA_String text;
  139. } UA_LocalizedText;
  140. enum UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_enum {
  141. UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE = 0x01,
  142. UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT = 0x02
  143. };
  144. /** @brief A structure that contains an application specific data type that may
  145. not be recognized by the receiver. */
  146. typedef struct UA_ExtensionObject {
  147. UA_NodeId typeId;
  148. UA_Byte encoding; // Type of the enum UA_ExtensionObjectEncodingMaskType
  149. UA_ByteString body; // contains either the bytestring or a pointer to the memory-object
  150. } UA_ExtensionObject;
  151. enum UA_ExtensionObject_EncodingMaskType_enum {
  152. UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED = 0x00,
  153. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING = 0x01,
  154. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML = 0x02
  155. };
  156. struct UA_VTable_Entry; // forwards declaration
  157. typedef struct UA_VTable_Entry UA_VTable_Entry;
  158. /** @brief A union of all of the types specified above.
  159. *
  160. * Variants store (arrays of) built-in types. If you want to store a more
  161. * complex (or self-defined) type, you have to use an UA_ExtensionObject.*/
  162. typedef struct UA_Variant {
  163. UA_VTable_Entry *vt; // internal entry into vTable
  164. UA_Int32 arrayLength; // total number of elements
  165. void *data;
  166. UA_Int32 arrayDimensionsLength;
  167. UA_Int32 *arrayDimensions;
  168. } UA_Variant;
  169. /* VariantBinaryEncoding - Part: 6, Chapter: 5.2.2.16, Page: 22 */
  170. enum UA_VARIANT_ENCODINGMASKTYPE_enum {
  171. UA_VARIANT_ENCODINGMASKTYPE_TYPEID_MASK = 0x3F, // bits 0:5
  172. UA_VARIANT_ENCODINGMASKTYPE_DIMENSIONS = (0x01 << 6), // bit 6
  173. UA_VARIANT_ENCODINGMASKTYPE_ARRAY = (0x01 << 7) // bit 7
  174. };
  175. /** @brief A data value with an associated status code and timestamps. */
  176. typedef struct UA_DataValue {
  177. UA_Byte encodingMask;
  178. UA_Variant value;
  179. UA_StatusCode status;
  180. UA_DateTime sourceTimestamp;
  181. UA_Int16 sourcePicoseconds;
  182. UA_DateTime serverTimestamp;
  183. UA_Int16 serverPicoseconds;
  184. } UA_DataValue;
  185. enum UA_DATAVALUE_ENCODINGMASKTYPE_enum {
  186. UA_DATAVALUE_ENCODINGMASK_VARIANT = 0x01,
  187. UA_DATAVALUE_ENCODINGMASK_STATUSCODE = 0x02,
  188. UA_DATAVALUE_ENCODINGMASK_SOURCETIMESTAMP = 0x04,
  189. UA_DATAVALUE_ENCODINGMASK_SERVERTIMESTAMP = 0x08,
  190. UA_DATAVALUE_ENCODINGMASK_SOURCEPICOSECONDS = 0x10,
  191. UA_DATAVALUE_ENCODINGMASK_SERVERPICOSECONDS = 0x20
  192. };
  193. /** @brief A structure that contains detailed error and diagnostic information associated with a StatusCode. */
  194. typedef struct UA_DiagnosticInfo {
  195. UA_Byte encodingMask; // Type of the Enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE
  196. UA_Int32 symbolicId;
  197. UA_Int32 namespaceUri;
  198. UA_Int32 localizedText;
  199. UA_Int32 locale;
  200. UA_String additionalInfo;
  201. UA_StatusCode innerStatusCode;
  202. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  203. } UA_DiagnosticInfo;
  204. enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE_enum {
  205. UA_DIAGNOSTICINFO_ENCODINGMASK_SYMBOLICID = 0x01,
  206. UA_DIAGNOSTICINFO_ENCODINGMASK_NAMESPACE = 0x02,
  207. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALIZEDTEXT = 0x04,
  208. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALE = 0x08,
  209. UA_DIAGNOSTICINFO_ENCODINGMASK_ADDITIONALINFO = 0x10,
  210. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERSTATUSCODE = 0x20,
  211. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO = 0x40
  212. };
  213. /** @brief Type use internally to denote an invalid datatype. */
  214. typedef void UA_InvalidType;
  215. /*************/
  216. /* Functions */
  217. /*************/
  218. #ifdef DEBUG
  219. #define UA_TYPE_PROTOTYPES(TYPE) \
  220. UA_Int32 TYPE##_new(TYPE **p); \
  221. UA_Int32 TYPE##_init(TYPE * p); \
  222. UA_Int32 TYPE##_delete(TYPE * p); \
  223. UA_Int32 TYPE##_deleteMembers(TYPE * p); \
  224. UA_Int32 TYPE##_copy(const TYPE *src, TYPE *dst); \
  225. void TYPE##_print(const TYPE *p, FILE *stream);
  226. #else
  227. #define UA_TYPE_PROTOTYPES(TYPE) \
  228. UA_Int32 TYPE##_new(TYPE **p); \
  229. UA_Int32 TYPE##_init(TYPE * p); \
  230. UA_Int32 TYPE##_delete(TYPE * p); \
  231. UA_Int32 TYPE##_deleteMembers(TYPE * p); \
  232. UA_Int32 TYPE##_copy(const TYPE *src, TYPE *dst);
  233. #endif
  234. /* Functions for all types */
  235. UA_TYPE_PROTOTYPES(UA_Boolean)
  236. UA_TYPE_PROTOTYPES(UA_SByte)
  237. UA_TYPE_PROTOTYPES(UA_Byte)
  238. UA_TYPE_PROTOTYPES(UA_Int16)
  239. UA_TYPE_PROTOTYPES(UA_UInt16)
  240. UA_TYPE_PROTOTYPES(UA_Int32)
  241. UA_TYPE_PROTOTYPES(UA_UInt32)
  242. UA_TYPE_PROTOTYPES(UA_Int64)
  243. UA_TYPE_PROTOTYPES(UA_UInt64)
  244. UA_TYPE_PROTOTYPES(UA_Float)
  245. UA_TYPE_PROTOTYPES(UA_Double)
  246. UA_TYPE_PROTOTYPES(UA_String)
  247. UA_TYPE_PROTOTYPES(UA_DateTime)
  248. UA_TYPE_PROTOTYPES(UA_Guid)
  249. UA_TYPE_PROTOTYPES(UA_ByteString)
  250. UA_TYPE_PROTOTYPES(UA_XmlElement)
  251. UA_TYPE_PROTOTYPES(UA_NodeId)
  252. UA_TYPE_PROTOTYPES(UA_ExpandedNodeId)
  253. UA_TYPE_PROTOTYPES(UA_StatusCode)
  254. UA_TYPE_PROTOTYPES(UA_QualifiedName)
  255. UA_TYPE_PROTOTYPES(UA_LocalizedText)
  256. UA_TYPE_PROTOTYPES(UA_ExtensionObject)
  257. UA_TYPE_PROTOTYPES(UA_DataValue)
  258. UA_TYPE_PROTOTYPES(UA_Variant)
  259. UA_TYPE_PROTOTYPES(UA_DiagnosticInfo)
  260. UA_TYPE_PROTOTYPES(UA_InvalidType)
  261. /* String */
  262. #define UA_STRING_NULL (UA_String){-1, UA_NULL}
  263. #define UA_STRING_STATIC(STRING) (UA_String){sizeof(STRING)-1, (UA_Byte*)STRING}
  264. UA_Int32 UA_String_copycstring(char const *src, UA_String *dst);
  265. UA_Int32 UA_String_copyprintf(char const *fmt, UA_String *dst, ...);
  266. UA_Int32 UA_String_equal(const UA_String *string1, const UA_String *string2);
  267. #ifdef DEBUG
  268. void UA_String_printf(char const *label, const UA_String *string);
  269. void UA_String_printx(char const *label, const UA_String *string);
  270. void UA_String_printx_hex(char const *label, const UA_String *string);
  271. #endif
  272. /* DateTime */
  273. UA_DateTime UA_DateTime_now();
  274. typedef struct UA_DateTimeStruct {
  275. UA_Int16 nanoSec;
  276. UA_Int16 microSec;
  277. UA_Int16 milliSec;
  278. UA_Int16 sec;
  279. UA_Int16 min;
  280. UA_Int16 hour;
  281. UA_Int16 day;
  282. UA_Int16 mounth;
  283. UA_Int16 year;
  284. } UA_DateTimeStruct;
  285. UA_DateTimeStruct UA_DateTime_toStruct(UA_DateTime time);
  286. UA_Int32 UA_DateTime_toString(UA_DateTime time, UA_String *timeString);
  287. /* Guid */
  288. UA_Int32 UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  289. /* ByteString */
  290. UA_Int32 UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2);
  291. UA_Int32 UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length);
  292. #ifdef DEBUG
  293. void UA_ByteString_printf(char *label, const UA_ByteString *string);
  294. void UA_ByteString_printx(char *label, const UA_ByteString *string);
  295. void UA_ByteString_printx_hex(char *label, const UA_ByteString *string);
  296. #endif
  297. /* NodeId */
  298. UA_Int32 UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  299. #ifdef DEBUG
  300. void UA_NodeId_printf(char *label, const UA_NodeId *node);
  301. #endif
  302. UA_Boolean UA_NodeId_isNull(const UA_NodeId *p);
  303. UA_Boolean UA_NodeId_isBasicType(UA_NodeId const *id);
  304. /* ExpandedNodeId */
  305. UA_Boolean UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p);
  306. /* QualifiedName */
  307. #define UA_QUALIFIEDNAME_STATIC(STRING) \
  308. (UA_QualifiedName){0, {sizeof(STRING)-1, (UA_Byte*)STRING}}
  309. #ifdef DEBUG
  310. void UA_QualifiedName_printf(char const *label, const UA_QualifiedName *qn);
  311. #endif
  312. /* LocalizedText */
  313. #define UA_LOCALIZEDTEXT_STATIC(STRING) (UA_LocalizedText){{2, (UA_Byte*)"en"}, UA_STRING_STATIC(STRING)}
  314. UA_Int32 UA_LocalizedText_copycstring(char const *src, UA_LocalizedText *dst);
  315. /* Variant */
  316. UA_Int32 UA_Variant_copySetValue(UA_Variant *v, UA_VTable_Entry *vt, const void *value);
  317. UA_Int32 UA_Variant_copySetArray(UA_Variant *v, UA_VTable_Entry *vt, UA_Int32 arrayLength, const void *array);
  318. /** @brief Functions UA_Variant_borrowSetValue and ..Array allow to define
  319. variants whose payload will not be deleted. This is achieved by a second
  320. vtable. The functionality can be used e.g. when UA_VariableNodes point into a
  321. "father" structured object that is stored in an UA_VariableNode itself. */
  322. UA_Int32 UA_Variant_borrowSetValue(UA_Variant *v, UA_VTable_Entry *vt, const void *value);
  323. UA_Int32 UA_Variant_borrowSetArray(UA_Variant *v, UA_VTable_Entry *vt, UA_Int32 arrayLength,
  324. const void *array);
  325. /* Array operations */
  326. UA_Int32 UA_Array_new(void **p, UA_Int32 noElements, UA_VTable_Entry *vt);
  327. UA_Int32 UA_Array_init(void *p, UA_Int32 noElements, UA_VTable_Entry *vt);
  328. UA_Int32 UA_Array_delete(void *p, UA_Int32 noElements, UA_VTable_Entry *vt);
  329. /* @brief The destination array is allocated according to noElements. */
  330. UA_Int32 UA_Array_copy(const void *src, UA_Int32 noElements, UA_VTable_Entry *vt, void **dst);
  331. #ifdef DEBUG
  332. void UA_Array_print(const void *p, UA_Int32 noElements, UA_VTable_Entry *vt, FILE *stream);
  333. #endif
  334. /**********/
  335. /* VTable */
  336. /**********/
  337. /* @brief Returns the size of the encoded element.*/
  338. typedef UA_Int32 (*UA_calcSize)(const void *p);
  339. /* @brief The encoding function allocates the target bytestring. */
  340. typedef UA_Int32 (*UA_encode)(const void *src, UA_ByteString *dst, UA_UInt32 *offset);
  341. /* @brief The decoding function decodes a ByteString into an UA datatype. */
  342. typedef UA_Int32 (*UA_decode)(const UA_ByteString *src, UA_UInt32 *offset, void *dst);
  343. typedef struct UA_Encoding {
  344. UA_calcSize calcSize;
  345. UA_encode encode;
  346. UA_decode decode;
  347. } UA_Encoding;
  348. #define UA_ENCODING_BINARY 0
  349. #define UA_ENCODING_XML 1
  350. struct UA_VTable_Entry {
  351. UA_NodeId typeId;
  352. UA_Byte *name;
  353. UA_Int32 (*new)(void **p);
  354. UA_Int32 (*init)(void *p);
  355. UA_Int32 (*copy)(void const *src, void *dst);
  356. UA_Int32 (*delete)(void *p);
  357. UA_Int32 (*deleteMembers)(void *p);
  358. #ifdef DEBUG
  359. void (*print)(const void *p, FILE *stream);
  360. #endif
  361. UA_UInt32 memSize; // size of the struct only in memory (no dynamic components)
  362. UA_Boolean dynMembers; // does the type contain members that are dynamically
  363. // allocated (strings, ..)? Otherwise, the size on
  364. // the wire == size in memory.
  365. UA_Encoding encodings[2]; // binary, xml, ...
  366. };
  367. typedef UA_Int32 (*UA_nodeIdToVTableIndex)(const UA_NodeId *id);
  368. typedef struct UA_VTable {
  369. UA_nodeIdToVTableIndex getTypeIndex;
  370. UA_VTable_Entry *types;
  371. } UA_VTable;
  372. /***********************************/
  373. /* Macros for type implementations */
  374. /***********************************/
  375. #define UA_TYPE_DEFAULT(TYPE) \
  376. UA_TYPE_DELETE_DEFAULT(TYPE) \
  377. UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  378. UA_TYPE_INIT_DEFAULT(TYPE) \
  379. UA_TYPE_NEW_DEFAULT(TYPE) \
  380. UA_TYPE_COPY_DEFAULT(TYPE) \
  381. #define UA_TYPE_NEW_DEFAULT(TYPE) \
  382. UA_Int32 TYPE##_new(TYPE **p) { \
  383. UA_Int32 retval = UA_SUCCESS; \
  384. retval |= UA_alloc((void **)p, sizeof(TYPE)); \
  385. retval |= TYPE##_init(*p); \
  386. return retval; \
  387. }
  388. #define UA_TYPE_INIT_DEFAULT(TYPE) \
  389. UA_Int32 TYPE##_init(TYPE * p) { \
  390. if(p == UA_NULL) return UA_ERROR; \
  391. *p = (TYPE)0; \
  392. return UA_SUCCESS; \
  393. }
  394. #define UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  395. UA_Int32 TYPE##_init(TYPE * p) { \
  396. return TYPE_AS##_init((TYPE_AS *)p); \
  397. }
  398. #define UA_TYPE_DELETE_DEFAULT(TYPE) \
  399. UA_Int32 TYPE##_delete(TYPE *p) { \
  400. UA_Int32 retval = UA_SUCCESS; \
  401. retval |= TYPE##_deleteMembers(p); \
  402. retval |= UA_free(p); \
  403. return retval; \
  404. }
  405. #define UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  406. UA_Int32 TYPE##_delete(TYPE * p) { return TYPE_AS##_delete((TYPE_AS *)p); }
  407. #define UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  408. UA_Int32 TYPE##_deleteMembers(TYPE * p) { return UA_SUCCESS; }
  409. #define UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  410. UA_Int32 TYPE##_deleteMembers(TYPE * p) { return TYPE_AS##_deleteMembers((TYPE_AS *)p); }
  411. /* Use only when the type has no arrays. Otherwise, implement deep copy */
  412. #define UA_TYPE_COPY_DEFAULT(TYPE) \
  413. UA_Int32 TYPE##_copy(TYPE const *src, TYPE *dst) { \
  414. if(src == UA_NULL || dst == UA_NULL) return UA_ERROR; \
  415. UA_Int32 retval = UA_SUCCESS; \
  416. retval |= UA_memcpy(dst, src, sizeof(TYPE)); \
  417. return retval; \
  418. }
  419. #define UA_TYPE_COPY_AS(TYPE, TYPE_AS) \
  420. UA_Int32 TYPE##_copy(TYPE const *src, TYPE *dst) { \
  421. return TYPE_AS##_copy((TYPE_AS *)src, (TYPE_AS *)dst); \
  422. }
  423. #define UA_TYPE_PRINT_AS(TYPE, TYPE_AS) \
  424. void TYPE##_print(TYPE const *p, FILE *stream) { \
  425. TYPE_AS##_print((TYPE_AS *)p, stream); \
  426. } \
  427. #ifdef DEBUG //print functions only in debug mode
  428. #define UA_TYPE_AS(TYPE, TYPE_AS) \
  429. UA_TYPE_NEW_DEFAULT(TYPE) \
  430. UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  431. UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  432. UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  433. UA_TYPE_COPY_AS(TYPE, TYPE_AS) \
  434. UA_TYPE_PRINT_AS(TYPE, TYPE_AS)
  435. #else
  436. #define UA_TYPE_AS(TYPE, TYPE_AS) \
  437. UA_TYPE_NEW_DEFAULT(TYPE) \
  438. UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  439. UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  440. UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  441. UA_TYPE_COPY_AS(TYPE, TYPE_AS)
  442. #endif
  443. /// @} /* end of group */
  444. #endif /* UA_TYPES_H_ */