2 Copyright (C) 2012 Modelon AB
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the BSD style license.
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.
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>.
16 #ifndef FMI2_XML_TYPEIMPL_H
17 #define FMI2_XML_TYPEIMPL_H
19 #include <JM/jm_named_ptr.h>
20 #include <JM/jm_string_set.h>
21 #include <FMI2/fmi2_xml_model_description.h>
23 #include "fmi2_xml_parser.h"
29 /** \defgroup Type definitions supporting structures
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.
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.
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.
51 For Enums there are two different property structs since type definition
52 gives the list of items and variables give min and max.
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;
61 typedef struct fmi2_xml_variable_type_base_t fmi2_xml_variable_type_base_t;
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 */
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*/
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) */
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).
79 struct fmi2_xml_variable_typedef_t {
80 fmi2_xml_variable_type_base_t typeBase;
81 jm_string description;
85 typedef struct fmi2_xml_real_type_props_t {
86 fmi2_xml_variable_type_base_t typeBase;
89 fmi2_xml_display_unit_t* displayUnit;
94 } fmi2_xml_real_type_props_t;
96 typedef struct fmi2_xml_integer_type_props_t {
97 fmi2_xml_variable_type_base_t typeBase;
103 } fmi2_xml_integer_type_props_t;
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;
108 typedef struct fmi2_xml_enum_type_item_t {
111 char itemDesciption[1];
112 } fmi2_xml_enum_type_item_t;
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);
122 typedef struct fmi2_xml_enum_variable_props_t {
123 fmi2_xml_variable_type_base_t typeBase;
129 } fmi2_xml_enum_variable_props_t;
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;
136 typedef struct fmi2_xml_variable_start_real_t {
137 fmi2_xml_variable_type_base_t typeBase;
139 } fmi2_xml_variable_start_real_t ;
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;
145 } fmi2_xml_variable_start_integer_t ;
147 typedef struct fmi2_xml_variable_start_string_t {
148 fmi2_xml_variable_type_base_t typeBase;
150 } fmi2_xml_variable_start_string_t;
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;
155 if(typeBase->structKind == kind) return typeBase;
156 typeBase = typeBase->baseTypeStruct;
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;
164 if(typeBase->structKind == fmi2_xml_type_struct_enu_props) return typeBase;
165 typeBase = typeBase->baseTypeStruct;
170 struct fmi2_xml_type_definitions_t {
171 jm_vector(jm_named_ptr) typeDefinitions;
173 jm_string_set quantities;
175 fmi2_xml_variable_type_base_t* typePropsList;
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;
184 extern void fmi2_xml_init_type_definitions(fmi2_xml_type_definitions_t* td, jm_callbacks* cb) ;
186 extern void fmi2_xml_free_type_definitions_data(fmi2_xml_type_definitions_t* td);
188 extern void fmi2_xml_init_integer_typedef(fmi2_xml_integer_typedef_t* type);
190 extern void fmi2_xml_init_enum_typedef(fmi2_xml_enumeration_typedef_t* type, jm_callbacks* cb);
192 extern void fmi2_xml_free_enum_type(jm_named_ptr named);
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);
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);
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);
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);
202 extern int fmi2_check_last_elem_is_specific_type(fmi2_xml_parser_context_t *context);
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);
206 extern void* fmi2_xml_variable_type_create(fmi2_xml_parser_context_t* context, size_t size, jm_vector(jm_named_ptr)* typeList );
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 );
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 );
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);
218 #endif /* FMI2_XML_TYPEIMPL_H */