ua_types.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. #ifdef DEBUG
  23. #include <stdio.h>
  24. #endif
  25. /* For internal use */
  26. #define UA_NULL ((void *)0)
  27. #define UA_TRUE (42 == 42)
  28. #define UA_FALSE (!UA_TRUE)
  29. typedef enum UA_EQUALITY {
  30. UA_EQUAL,
  31. UA_NOT_EQUAL
  32. } UA_EQUALITY;
  33. /**
  34. * @defgroup types Datatypes
  35. *
  36. * @brief This module describes the datatypes used in OPC UA. There is a
  37. * division into built-in datatypes (integers, strings, etc.) and more complex
  38. * datatypes that are comprise of built-in datatypes (structs defined in the OPC
  39. * UA standard).
  40. *
  41. * All datatypes follow the same schema in the naming of relevant functions.
  42. *
  43. * - <type>_init: Sets all values to a "safe" standard. For example, if the
  44. * datatype contains a string-element, its size will be set to zero.
  45. *
  46. * - <type>_new: Allocates the memory for the type and runs <type>_init on the
  47. * returned pointer.
  48. *
  49. * - <type>_copy: Copies a datatype. This performs a deep copy that iterates
  50. * over the members. The copy function assumes that the target structure can
  51. * be overwritten (do a deleteMembers before if necessary). With one
  52. * exception: copying into a variant that points to an external datasource is
  53. * not permitted.
  54. *
  55. * - <type>_delete: Frees the memory where the datatype was stored.
  56. *
  57. * - <type>_deleteMembers: Frees the memory of dynamically sized members (e.g. a
  58. * string) of a datatype. This is useful when the datatype was allocated on
  59. * the stack, whereas the dynamically sized members is heap-allocated.
  60. *
  61. * @{
  62. */
  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. #include "ua_statuscodes.h"
  143. /** @brief A numeric identifier for a error or condition that is associated with a value or an operation. */
  144. typedef enum UA_StatusCode UA_StatusCode; // StatusCodes aren't an enum(=int) since 32 unsigned bits are needed. See also ua_statuscodes.h */
  145. /** @brief A name qualified by a namespace. */
  146. typedef struct UA_QualifiedName {
  147. UA_UInt16 namespaceIndex;
  148. UA_String name;
  149. } UA_QualifiedName;
  150. /** @brief Human readable text with an optional locale identifier. */
  151. typedef struct UA_LocalizedText {
  152. UA_String locale;
  153. UA_String text;
  154. } UA_LocalizedText;
  155. /** @brief A structure that contains an application specific data type that may
  156. not be recognized by the receiver. */
  157. typedef struct UA_ExtensionObject {
  158. UA_NodeId typeId;
  159. enum {
  160. UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED = 0,
  161. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING = 1,
  162. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML = 2
  163. } encoding;
  164. UA_ByteString body; // contains either the bytestring or a pointer to the memory-object
  165. } UA_ExtensionObject;
  166. struct UA_VTable_Entry; // forwards declaration
  167. typedef struct UA_VTable_Entry UA_VTable_Entry;
  168. /* Variant */
  169. /** @defgroup variant Variant
  170. *
  171. * @brief Variants may contain (arrays of) any other datatype.
  172. *
  173. * For that, we provide a reference to the utility functions of the type as well
  174. * as possible encoding by pointing an entry in the vtable of types. References
  175. * may either contain a structure with the variants data, or a datasource where
  176. * this structure may be obtained (using reference counting for async access).
  177. *
  178. * @{
  179. */
  180. typedef struct UA_VariantData {
  181. UA_Int32 arrayLength; // total number of elements in the data-pointer
  182. void *dataPtr;
  183. UA_Int32 arrayDimensionsLength;
  184. UA_Int32 *arrayDimensions;
  185. } UA_VariantData;
  186. /** @brief A datasource is the interface to interact with a local data provider.
  187. *
  188. * Implementors of datasources need to provide functions for the callbacks in
  189. * this structure. As a rule, datasources are never copied, but only their
  190. * content. The only way to write into a datasource is via the write-service. */
  191. typedef struct UA_VariantDataSource {
  192. const void *identifier; /**< whatever id the datasource uses internally. Can be ignored if the datasource functions do not use it. */
  193. UA_Int32 (*read)(const void *identifier, const UA_VariantData **); /**< Get the current data from the datasource. When it is no longer used, the data has to be relased. */
  194. void (*release)(const void *identifier, const UA_VariantData *); /**< For concurrent access, the datasource needs to know when the last reader releases replaced/deleted data. Release decreases the reference count. */
  195. UA_Int32 (*write)(const void **identifier, const UA_VariantData *); /**< Replace the data in the datasource. Also used to replace the identifier pointer for lookups. Do not free the variantdata afterwards! */
  196. void (*delete)(const void *identifier); /**< Indicates that the node with the datasource was removed from the namespace. */
  197. } UA_VariantDataSource;
  198. /** @brief A union of all of the types specified above.
  199. *
  200. * Variants store (arrays of) built-in types. If you want to store a more
  201. * complex (or self-defined) type, you have to use an UA_ExtensionObject.*/
  202. typedef struct UA_Variant {
  203. const UA_VTable_Entry *vt; // internal entry into vTable
  204. enum {
  205. UA_VARIANT_DATA,
  206. UA_VARIANT_DATA_NODELETE, // do not free the data (e.g. because it is "borrowed" and points into a larger structure)
  207. UA_VARIANT_DATASOURCE
  208. } storageType;
  209. union {
  210. UA_VariantData data;
  211. UA_VariantDataSource datasource;
  212. } storage;
  213. } UA_Variant;
  214. /** @} */
  215. /** @brief A data value with an associated status code and timestamps. */
  216. typedef struct UA_DataValue {
  217. UA_Byte encodingMask;
  218. UA_Variant value;
  219. UA_StatusCode status;
  220. UA_DateTime sourceTimestamp;
  221. UA_Int16 sourcePicoseconds;
  222. UA_DateTime serverTimestamp;
  223. UA_Int16 serverPicoseconds;
  224. } UA_DataValue;
  225. enum UA_DATAVALUE_ENCODINGMASKTYPE_enum {
  226. UA_DATAVALUE_ENCODINGMASK_VARIANT = 0x01,
  227. UA_DATAVALUE_ENCODINGMASK_STATUSCODE = 0x02,
  228. UA_DATAVALUE_ENCODINGMASK_SOURCETIMESTAMP = 0x04,
  229. UA_DATAVALUE_ENCODINGMASK_SERVERTIMESTAMP = 0x08,
  230. UA_DATAVALUE_ENCODINGMASK_SOURCEPICOSECONDS = 0x10,
  231. UA_DATAVALUE_ENCODINGMASK_SERVERPICOSECONDS = 0x20
  232. };
  233. /** @brief A structure that contains detailed error and diagnostic information associated with a StatusCode. */
  234. typedef struct UA_DiagnosticInfo {
  235. UA_Byte encodingMask; // Type of the Enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE
  236. UA_Int32 symbolicId;
  237. UA_Int32 namespaceUri;
  238. UA_Int32 localizedText;
  239. UA_Int32 locale;
  240. UA_String additionalInfo;
  241. UA_StatusCode innerStatusCode;
  242. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  243. } UA_DiagnosticInfo;
  244. enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE_enum {
  245. UA_DIAGNOSTICINFO_ENCODINGMASK_SYMBOLICID = 0x01,
  246. UA_DIAGNOSTICINFO_ENCODINGMASK_NAMESPACE = 0x02,
  247. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALIZEDTEXT = 0x04,
  248. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALE = 0x08,
  249. UA_DIAGNOSTICINFO_ENCODINGMASK_ADDITIONALINFO = 0x10,
  250. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERSTATUSCODE = 0x20,
  251. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO = 0x40
  252. };
  253. /** @brief Type use internally to denote an invalid datatype. */
  254. typedef void UA_InvalidType;
  255. /*************/
  256. /* Functions */
  257. /*************/
  258. #ifdef DEBUG
  259. #define UA_TYPE_PROTOTYPES(TYPE) \
  260. UA_StatusCode UA_EXPORT TYPE##_new(TYPE **p); \
  261. void UA_EXPORT TYPE##_init(TYPE * p); \
  262. void UA_EXPORT TYPE##_delete(TYPE * p); \
  263. void UA_EXPORT TYPE##_deleteMembers(TYPE * p); \
  264. UA_StatusCode UA_EXPORT TYPE##_copy(const TYPE *src, TYPE *dst); \
  265. void UA_EXPORT TYPE##_print(const TYPE *p, FILE *stream);
  266. #else
  267. #define UA_TYPE_PROTOTYPES(TYPE) \
  268. UA_StatusCode UA_EXPORT TYPE##_new(TYPE **p); \
  269. void UA_EXPORT TYPE##_init(TYPE * p); \
  270. void UA_EXPORT TYPE##_delete(TYPE * p); \
  271. void UA_EXPORT TYPE##_deleteMembers(TYPE * p); \
  272. UA_StatusCode UA_EXPORT TYPE##_copy(const TYPE *src, TYPE *dst);
  273. #endif
  274. /* Functions for all types */
  275. UA_TYPE_PROTOTYPES(UA_Boolean)
  276. UA_TYPE_PROTOTYPES(UA_SByte)
  277. UA_TYPE_PROTOTYPES(UA_Byte)
  278. UA_TYPE_PROTOTYPES(UA_Int16)
  279. UA_TYPE_PROTOTYPES(UA_UInt16)
  280. UA_TYPE_PROTOTYPES(UA_Int32)
  281. UA_TYPE_PROTOTYPES(UA_UInt32)
  282. UA_TYPE_PROTOTYPES(UA_Int64)
  283. UA_TYPE_PROTOTYPES(UA_UInt64)
  284. UA_TYPE_PROTOTYPES(UA_Float)
  285. UA_TYPE_PROTOTYPES(UA_Double)
  286. UA_TYPE_PROTOTYPES(UA_String)
  287. UA_TYPE_PROTOTYPES(UA_DateTime)
  288. UA_TYPE_PROTOTYPES(UA_Guid)
  289. UA_TYPE_PROTOTYPES(UA_ByteString)
  290. UA_TYPE_PROTOTYPES(UA_XmlElement)
  291. UA_TYPE_PROTOTYPES(UA_NodeId)
  292. UA_TYPE_PROTOTYPES(UA_ExpandedNodeId)
  293. UA_TYPE_PROTOTYPES(UA_StatusCode)
  294. UA_TYPE_PROTOTYPES(UA_QualifiedName)
  295. UA_TYPE_PROTOTYPES(UA_LocalizedText)
  296. UA_TYPE_PROTOTYPES(UA_ExtensionObject)
  297. UA_TYPE_PROTOTYPES(UA_DataValue)
  298. UA_TYPE_PROTOTYPES(UA_Variant)
  299. UA_TYPE_PROTOTYPES(UA_DiagnosticInfo)
  300. UA_TYPE_PROTOTYPES(UA_InvalidType)
  301. /* String */
  302. #define UA_STRING_NULL (UA_String) {-1, UA_NULL }
  303. #define UA_STRING_STATIC(VARIABLE, STRING) do { \
  304. VARIABLE.length = sizeof(STRING)-1; \
  305. VARIABLE.data = (UA_Byte *)STRING; } while(0)
  306. UA_StatusCode UA_EXPORT UA_String_copycstring(char const *src, UA_String *dst);
  307. UA_StatusCode UA_EXPORT UA_String_copyprintf(char const *fmt, UA_String *dst, ...);
  308. UA_EQUALITY UA_EXPORT UA_String_equal(const UA_String *string1, const UA_String *string2);
  309. #ifdef DEBUG
  310. void UA_EXPORT UA_String_printf(char const *label, const UA_String *string);
  311. void UA_EXPORT UA_String_printx(char const *label, const UA_String *string);
  312. void UA_EXPORT UA_String_printx_hex(char const *label, const UA_String *string);
  313. #endif
  314. /* DateTime */
  315. UA_DateTime UA_EXPORT UA_DateTime_now();
  316. typedef struct UA_DateTimeStruct {
  317. UA_Int16 nanoSec;
  318. UA_Int16 microSec;
  319. UA_Int16 milliSec;
  320. UA_Int16 sec;
  321. UA_Int16 min;
  322. UA_Int16 hour;
  323. UA_Int16 day;
  324. UA_Int16 mounth;
  325. UA_Int16 year;
  326. } UA_DateTimeStruct;
  327. UA_DateTimeStruct UA_EXPORT UA_DateTime_toStruct(UA_DateTime time);
  328. UA_StatusCode UA_EXPORT UA_DateTime_toString(UA_DateTime time, UA_String *timeString);
  329. /* Guid */
  330. UA_EQUALITY UA_EXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  331. /* ByteString */
  332. UA_EQUALITY UA_EXPORT UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2);
  333. UA_StatusCode UA_EXPORT UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length);
  334. #ifdef DEBUG
  335. void UA_EXPORT UA_ByteString_printf(char *label, const UA_ByteString *string);
  336. void UA_EXPORT UA_ByteString_printx(char *label, const UA_ByteString *string);
  337. void UA_EXPORT UA_ByteString_printx_hex(char *label, const UA_ByteString *string);
  338. #endif
  339. /* NodeId */
  340. UA_EQUALITY UA_EXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  341. UA_Boolean UA_EXPORT UA_NodeId_isNull(const UA_NodeId *p);
  342. UA_Boolean UA_EXPORT UA_NodeId_isBasicType(UA_NodeId const *id);
  343. #define NS0NODEID(NUMERIC_ID) \
  344. (UA_NodeId) {.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = \
  345. NUMERIC_ID }
  346. /* ExpandedNodeId */
  347. UA_Boolean UA_EXPORT UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p);
  348. #define NS0EXPANDEDNODEID(VARIABLE, NUMERIC_ID) do { \
  349. VARIABLE.nodeId = NS0NODEID(NUMERIC_ID); \
  350. VARIABLE.namespaceUri = UA_STRING_NULL; \
  351. VARIABLE.serverIndex = 0; } while(0)
  352. /* QualifiedName */
  353. #define UA_QUALIFIEDNAME_STATIC(VARIABLE, STRING) do { \
  354. VARIABLE.namespaceIndex = 0; \
  355. UA_STRING_STATIC(VARIABLE.name, STRING); } while(0)
  356. UA_StatusCode UA_EXPORT UA_QualifiedName_copycstring(char const *src, UA_QualifiedName *dst);
  357. #ifdef DEBUG
  358. void UA_EXPORT UA_QualifiedName_printf(char const *label, const UA_QualifiedName *qn);
  359. #endif
  360. /* LocalizedText */
  361. #define UA_LOCALIZEDTEXT_STATIC(VARIABLE, STRING) do { \
  362. UA_STRING_STATIC(VARIABLE.locale, "en"); \
  363. UA_STRING_STATIC(VARIABLE.text, STRING); } while(0)
  364. UA_StatusCode UA_EXPORT UA_LocalizedText_copycstring(char const *src, UA_LocalizedText *dst);
  365. /* Variant */
  366. UA_StatusCode UA_EXPORT UA_Variant_copySetValue(UA_Variant *v, const UA_VTable_Entry *vt, const void *value);
  367. UA_StatusCode UA_EXPORT UA_Variant_copySetArray(UA_Variant *v, const UA_VTable_Entry *vt, UA_Int32 arrayLength, const void *array);
  368. /* Array operations */
  369. UA_StatusCode UA_EXPORT UA_Array_new(void **p, UA_Int32 noElements, const UA_VTable_Entry *vt);
  370. void UA_EXPORT UA_Array_init(void *p, UA_Int32 noElements, const UA_VTable_Entry *vt);
  371. void UA_EXPORT UA_Array_delete(void *p, UA_Int32 noElements, const UA_VTable_Entry *vt);
  372. /* @brief The destination array is allocated according to noElements. */
  373. UA_StatusCode UA_EXPORT UA_Array_copy(const void *src, UA_Int32 noElements, const UA_VTable_Entry *vt, void **dst);
  374. #ifdef DEBUG
  375. void UA_EXPORT UA_Array_print(const void *p, UA_Int32 noElements, const UA_VTable_Entry *vt, FILE *stream);
  376. #endif
  377. /**********/
  378. /* VTable */
  379. /**********/
  380. typedef struct UA_Encoding {
  381. /** Returns the size of the encoded element.*/
  382. UA_Int32 (*calcSize)(const void *p);
  383. /** Encodes the type into the destination bytestring. */
  384. UA_StatusCode (*encode)(const void *src, UA_ByteString *dst, UA_UInt32 *offset);
  385. /** Decodes a ByteString into an UA datatype. */
  386. UA_StatusCode (*decode)(const UA_ByteString *src, UA_UInt32 *offset, void *dst);
  387. } UA_Encoding;
  388. #define UA_ENCODING_BINARY 0 // Binary encoding is always available
  389. struct UA_VTable_Entry {
  390. UA_NodeId typeId;
  391. UA_Byte *name;
  392. UA_StatusCode (*new)(void **p);
  393. void (*init)(void *p);
  394. UA_StatusCode (*copy)(void const *src, void *dst);
  395. void (*delete)(void *p);
  396. void (*deleteMembers)(void *p);
  397. #ifdef DEBUG
  398. void (*print)(const void *p, FILE *stream);
  399. #endif
  400. UA_UInt32 memSize; // size of the struct only in memory (no dynamic components)
  401. UA_Boolean dynMembers; // does the type contain members that are dynamically
  402. // allocated (strings, ..)? Otherwise, the size on
  403. // the wire == size in memory.
  404. UA_Encoding encodings[UA_ENCODING_AMOUNT]; // binary, xml, ... UA_ENCODING_AMOUNT is set by the build script
  405. };
  406. /***********************************/
  407. /* Macros for type implementations */
  408. /***********************************/
  409. #define UA_TYPE_DEFAULT(TYPE) \
  410. UA_TYPE_DELETE_DEFAULT(TYPE) \
  411. UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  412. UA_TYPE_INIT_DEFAULT(TYPE) \
  413. UA_TYPE_NEW_DEFAULT(TYPE) \
  414. UA_TYPE_COPY_DEFAULT(TYPE) \
  415. #define UA_TYPE_NEW_DEFAULT(TYPE) \
  416. UA_StatusCode TYPE##_new(TYPE **p) { \
  417. if(!(*p = UA_alloc(sizeof(TYPE)))) \
  418. return UA_STATUSCODE_BADOUTOFMEMORY; \
  419. TYPE##_init(*p); \
  420. return UA_STATUSCODE_GOOD; \
  421. }
  422. #define UA_TYPE_INIT_DEFAULT(TYPE) \
  423. void TYPE##_init(TYPE * p) { \
  424. if(!p) return; \
  425. *p = (TYPE)0; \
  426. }
  427. #define UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  428. void TYPE##_init(TYPE * p) { \
  429. TYPE_AS##_init((TYPE_AS *)p); \
  430. }
  431. #define UA_TYPE_DELETE_DEFAULT(TYPE) \
  432. void TYPE##_delete(TYPE *p) { \
  433. if(!p) return; \
  434. TYPE##_deleteMembers(p); \
  435. UA_free(p); \
  436. }
  437. #define UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  438. void TYPE##_delete(TYPE * p) { \
  439. TYPE_AS##_delete((TYPE_AS *)p); \
  440. }
  441. #define UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  442. void TYPE##_deleteMembers(TYPE * p) { return; }
  443. #define UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  444. void TYPE##_deleteMembers(TYPE * p) { TYPE_AS##_deleteMembers((TYPE_AS *)p); }
  445. /* Use only when the type has no arrays. Otherwise, implement deep copy */
  446. #define UA_TYPE_COPY_DEFAULT(TYPE) \
  447. UA_StatusCode TYPE##_copy(TYPE const *src, TYPE *dst) { \
  448. if(src == UA_NULL || dst == UA_NULL) \
  449. return UA_STATUSCODE_BADOUTOFMEMORY; \
  450. *dst = *src; \
  451. return UA_STATUSCODE_GOOD; \
  452. }
  453. #define UA_TYPE_COPY_AS(TYPE, TYPE_AS) \
  454. UA_Int32 TYPE##_copy(TYPE const *src, TYPE *dst) { \
  455. return TYPE_AS##_copy((TYPE_AS *)src, (TYPE_AS *)dst); \
  456. }
  457. #define UA_TYPE_PRINT_AS(TYPE, TYPE_AS) \
  458. void TYPE##_print(TYPE const *p, FILE *stream) { \
  459. TYPE_AS##_print((TYPE_AS *)p, stream); \
  460. } \
  461. #ifdef DEBUG //print functions only in debug mode
  462. #define UA_TYPE_AS(TYPE, TYPE_AS) \
  463. UA_TYPE_NEW_DEFAULT(TYPE) \
  464. UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  465. UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  466. UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  467. UA_TYPE_COPY_AS(TYPE, TYPE_AS) \
  468. UA_TYPE_PRINT_AS(TYPE, TYPE_AS)
  469. #else
  470. #define UA_TYPE_AS(TYPE, TYPE_AS) \
  471. UA_TYPE_NEW_DEFAULT(TYPE) \
  472. UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  473. UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  474. UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  475. UA_TYPE_COPY_AS(TYPE, TYPE_AS)
  476. #endif
  477. /// @} /* end of group */
  478. #ifdef __cplusplus
  479. } // extern "C"
  480. #endif
  481. #endif /* UA_TYPES_H_ */