ua_types.h 19 KB

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