ua_types.h 20 KB

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