]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil.core/native/FMILibrary/src/XML/src/FMI2/fmi2_xml_type_impl.h
Switch to full JavaSE-11+ compatibility
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / src / XML / src / FMI2 / fmi2_xml_type_impl.h
1 /*
2     Copyright (C) 2012 Modelon AB
3
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the BSD style license.
6
7      This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10     FMILIB_License.txt file for more details.
11
12     You should have received a copy of the FMILIB_License.txt file
13     along with this program. If not, contact Modelon AB <http://www.modelon.com>.
14 */
15
16 #ifndef FMI2_XML_TYPEIMPL_H
17 #define FMI2_XML_TYPEIMPL_H
18
19 #include <JM/jm_named_ptr.h>
20 #include <JM/jm_string_set.h>
21 #include <FMI2/fmi2_xml_model_description.h>
22
23 #include "fmi2_xml_parser.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 /** \defgroup Type definitions supporting structures
30
31   The type structures are designed to save memory and
32   to enable handling of diff-sets in the future.
33   For each basic type (Real, Integer, each Enumeration, String & Boolean)
34   there is a default instance of fmi2_xml_variable_type_base_t with 
35   structKind=fmi2_xml_type_struct_enu_props. Those instances have 
36   baseTypeStruct = NULL. 
37
38   Each type definition creates 1 or 2 instances:
39   (1)  instance with structKind=fmi2_xml_type_struct_enu_typedef
40     that gives the name & description of the type. baseType is a
41     pointer to either  fmi2_xml_type_struct_enu_base or fmi2_xml_type_struct_enu_props
42   (2)  optionally, an instance with the structKind=fmi2_xml_type_contrain_properties
43     providing information on min/max/quantity/etc. baseType is a pointer
44     to the default properties struct.
45
46    Each variable definition may create none, 1 or 2 instances:
47     (1) fmi2_xml_type_struct_enu_start providing the start value
48     (2) structKind=fmi2_xml_type_struct_enu_props  providing information on min/max/quantity/etc.
49     baseType is a pointer to either fmi2_xml_type_struct_enu_base or fmi2_xml_type_struct_enu_typedef.
50
51    For Enums there are two different property structs since type definition
52    gives the list of items and variables give min and max.
53  */
54
55 typedef enum {
56     fmi2_xml_type_struct_enu_typedef,
57     fmi2_xml_type_struct_enu_props,
58     fmi2_xml_type_struct_enu_start
59 } fmi2_xml_type_struct_kind_enu_t;
60
61 typedef struct fmi2_xml_variable_type_base_t fmi2_xml_variable_type_base_t;
62
63 struct fmi2_xml_variable_type_base_t {
64     fmi2_xml_variable_type_base_t* baseTypeStruct; /* The fmi2_xml_variable_type_base structs are put on a list that provide needed info on a variable */
65
66     fmi2_xml_variable_type_base_t* next;    /** dynamically allocated fmi2_xml_variable_type_base structs are put on a linked list to prevent memory leaks*/
67
68     char structKind; /* one of fmi2_xml_type_contrains_kind.*/
69     char baseType;   /* one of fmi2_xml_base_type */
70     char isRelativeQuantity;   /* relativeQuantity flag set. Only used in fmi2_xml_real_type_props_t) */
71         char isUnbounded;          /* unbounded flag set only used in fmi2_xml_real_type_props_t) */
72 } ;
73
74 /* 
75         Variable type definition is general and is used for all types.
76         This is done to enable easy handling of SimpleType XML element 
77         (specific type element comes next).
78 */
79 struct fmi2_xml_variable_typedef_t {
80     fmi2_xml_variable_type_base_t typeBase;
81     jm_string description;
82     char typeName[1];
83 };
84
85 typedef struct fmi2_xml_real_type_props_t {
86     fmi2_xml_variable_type_base_t typeBase;
87     jm_string quantity;
88
89     fmi2_xml_display_unit_t* displayUnit;
90
91     double typeMin;
92     double typeMax;
93     double typeNominal;
94 } fmi2_xml_real_type_props_t;
95
96 typedef struct fmi2_xml_integer_type_props_t {
97     fmi2_xml_variable_type_base_t typeBase;
98
99     jm_string  quantity;
100
101     int typeMin;
102     int typeMax;
103 } fmi2_xml_integer_type_props_t;
104
105 typedef fmi2_xml_variable_type_base_t fmi2_xml_string_type_props_t;
106 typedef fmi2_xml_variable_type_base_t fmi2_xml_bool_type_props_t;
107
108 typedef struct fmi2_xml_enum_type_item_t {
109     jm_string itemName;
110         int value;
111     char itemDesciption[1];
112 } fmi2_xml_enum_type_item_t;
113
114 static int fmi1_xml_compare_enum_val (const void* first, const void* second) {
115         const jm_named_ptr* a = first;
116         const jm_named_ptr* b = second;
117         fmi2_xml_enum_type_item_t* ai = a->ptr;
118         fmi2_xml_enum_type_item_t* bi = b->ptr;
119         return (ai->value - bi->value);
120 }
121
122 typedef struct fmi2_xml_enum_variable_props_t {
123     fmi2_xml_variable_type_base_t typeBase;
124
125     jm_string  quantity;
126
127     int typeMin;
128     int typeMax;
129 } fmi2_xml_enum_variable_props_t;
130
131 typedef struct fmi2_xml_enum_typedef_props_t {
132         fmi2_xml_enum_variable_props_t base;
133     jm_vector(jm_named_ptr) enumItems;
134 } fmi2_xml_enum_typedef_props_t;
135
136 typedef struct fmi2_xml_variable_start_real_t {
137     fmi2_xml_variable_type_base_t typeBase;
138     double start;
139 } fmi2_xml_variable_start_real_t ;
140
141 /* fmi2_xml_variable_start_integer is used for boolean and enums as well*/
142 typedef struct fmi2_xml_variable_start_integer_t {
143     fmi2_xml_variable_type_base_t typeBase;
144     int start;
145 } fmi2_xml_variable_start_integer_t ;
146
147 typedef struct fmi2_xml_variable_start_string_t {
148     fmi2_xml_variable_type_base_t typeBase;
149     char start[1];
150 } fmi2_xml_variable_start_string_t;
151
152 static fmi2_xml_variable_type_base_t* fmi2_xml_find_type_struct(fmi2_xml_variable_type_base_t* type, fmi2_xml_type_struct_kind_enu_t kind) {
153     fmi2_xml_variable_type_base_t* typeBase = type;
154     while(typeBase) {
155         if(typeBase->structKind == kind) return typeBase;
156         typeBase = typeBase->baseTypeStruct;
157     }
158     return 0;
159 }
160
161 static fmi2_xml_variable_type_base_t* fmi2_xml_find_type_props(fmi2_xml_variable_type_base_t* type) {
162     fmi2_xml_variable_type_base_t* typeBase = type;
163     while(typeBase) {
164         if(typeBase->structKind == fmi2_xml_type_struct_enu_props) return typeBase;
165         typeBase = typeBase->baseTypeStruct;
166     }
167     return 0;
168 }
169
170 struct fmi2_xml_type_definitions_t {
171     jm_vector(jm_named_ptr) typeDefinitions;
172
173     jm_string_set quantities;
174
175     fmi2_xml_variable_type_base_t* typePropsList;
176
177     fmi2_xml_real_type_props_t defaultRealType;
178     fmi2_xml_enum_typedef_props_t defaultEnumType;
179     fmi2_xml_integer_type_props_t defaultIntegerType;
180     fmi2_xml_bool_type_props_t defaultBooleanType;
181     fmi2_xml_string_type_props_t defaultStringType;
182 };
183
184 extern void fmi2_xml_init_type_definitions(fmi2_xml_type_definitions_t* td, jm_callbacks* cb) ;
185
186 extern void fmi2_xml_free_type_definitions_data(fmi2_xml_type_definitions_t* td);
187
188 extern void fmi2_xml_init_integer_typedef(fmi2_xml_integer_typedef_t* type);
189
190 extern void fmi2_xml_init_enum_typedef(fmi2_xml_enumeration_typedef_t* type, jm_callbacks* cb);
191
192 extern void fmi2_xml_free_enum_type(jm_named_ptr named);
193
194 fmi2_xml_variable_type_base_t* fmi2_xml_alloc_variable_type_props(fmi2_xml_type_definitions_t* td, fmi2_xml_variable_type_base_t* base, size_t typeSize);
195
196 fmi2_xml_variable_type_base_t* fmi2_xml_alloc_variable_type_start(fmi2_xml_type_definitions_t* td,fmi2_xml_variable_type_base_t* base, size_t typeSize);
197
198 fmi2_xml_real_type_props_t* fmi2_xml_parse_real_type_properties(fmi2_xml_parser_context_t* context, fmi2_xml_elm_enu_t elmID);
199
200 fmi2_xml_integer_type_props_t *fmi2_xml_parse_integer_type_properties(fmi2_xml_parser_context_t* context, fmi2_xml_elm_enu_t elmID);
201
202 extern int fmi2_check_last_elem_is_specific_type(fmi2_xml_parser_context_t *context);
203
204 extern jm_named_ptr fmi2_xml_variable_type_alloc(fmi2_xml_parser_context_t* context, jm_vector(char)* name, jm_vector(char)* description, size_t size);
205
206 extern void* fmi2_xml_variable_type_create(fmi2_xml_parser_context_t* context, size_t size, jm_vector(jm_named_ptr)* typeList );
207
208 extern fmi2_xml_real_typedef_t* fmi2_xml_variable_type_create_real(fmi2_xml_parser_context_t* context, fmi2_xml_elm_enu_t elmID, jm_vector(jm_named_ptr)* typeList );
209
210 extern fmi2_xml_integer_typedef_t* fmi2_xml_variable_type_create_integer(fmi2_xml_parser_context_t* context, fmi2_xml_elm_enu_t elmID, jm_vector(jm_named_ptr)* typeList );
211
212 fmi2_xml_variable_type_base_t* fmi2_get_declared_type(fmi2_xml_parser_context_t *context, fmi2_xml_elm_enu_t elmID, fmi2_xml_variable_type_base_t* defaultType);
213
214 #ifdef __cplusplus
215 }
216 #endif
217
218 #endif /* FMI2_XML_TYPEIMPL_H */