scad_device.h (6302B)
1 /* Copyright (C) 2022-2024 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #ifndef SCAD_DEVICE_H 17 #define SCAD_DEVICE_H 18 19 #include "scad.h" 20 #include "scad_geometry.h" 21 22 #include <rsys/rsys.h> 23 #include <rsys/ref_count.h> 24 #include <rsys/logger.h> 25 #include <rsys/hash_table.h> 26 #include <rsys/dynamic_array.h> 27 #include <rsys/dynamic_array_int.h> 28 #include <rsys/str.h> 29 30 static INLINE char 31 eq_str(const struct str* a, const struct str* b) 32 { 33 return !strcmp(str_cget(a), str_cget(b)); 34 } 35 36 static INLINE size_t 37 hash_str(const struct str* a) 38 { 39 return hash_fnv32(str_cget(a), str_len(a)); 40 } 41 42 #define HTABLE_NAME names 43 #define HTABLE_KEY struct str 44 #define HTABLE_KEY_FUNCTOR_INIT str_init 45 #define HTABLE_KEY_FUNCTOR_RELEASE str_release 46 #define HTABLE_KEY_FUNCTOR_COPY str_copy 47 #define HTABLE_KEY_FUNCTOR_COPY_AND_RELEASE str_copy_and_release 48 #define HTABLE_KEY_FUNCTOR_EQ eq_str 49 #define HTABLE_KEY_FUNCTOR_HASH hash_str 50 #define HTABLE_DATA struct scad_geometry* 51 #include <rsys/hash_table.h> 52 53 #define HTABLE_NAME geometries 54 #define HTABLE_KEY struct scad_geometry* 55 #define HTABLE_DATA char 56 #include <rsys/hash_table.h> 57 58 enum delete_policy { 59 /* Delete the tag and all its lower-dimension components */ 60 Scad_delete_recursive, 61 /* Delete only the tag, not its lower-dimension components */ 62 Scad_delete_non_recursive, 63 Scad_do_not_delete 64 }; 65 66 struct tag_desc { 67 enum delete_policy delete_policy; 68 size_t refcount; 69 struct darray_int tags_to_refput; 70 }; 71 72 static INLINE void 73 tag_desc_init 74 (struct mem_allocator* allocator, /* May be NULL <=> use default allocator */ 75 struct tag_desc* data) 76 { 77 ASSERT(data); 78 data->delete_policy = Scad_delete_recursive; 79 data->refcount = 1; 80 darray_int_init(allocator, &data->tags_to_refput); 81 } 82 static INLINE void 83 tag_desc_release 84 (struct tag_desc* data) 85 { 86 ASSERT(data); 87 darray_int_release(&data->tags_to_refput); 88 } 89 static FINLINE res_T 90 tag_desc_copy 91 (struct tag_desc* dst, 92 struct tag_desc const* src) 93 { 94 ASSERT(dst && src); 95 dst->delete_policy = src->delete_policy; 96 dst->refcount = src->refcount; 97 darray_int_copy(&dst->tags_to_refput, &src->tags_to_refput); 98 return RES_OK; 99 } 100 static FINLINE res_T 101 tag_desc_copy_and_release 102 (struct tag_desc* dst, 103 struct tag_desc* src) 104 { 105 ASSERT(dst && src); 106 dst->delete_policy = src->delete_policy; 107 dst->refcount = src->refcount; 108 darray_int_copy_and_release(&dst->tags_to_refput, &src->tags_to_refput); 109 return RES_OK; 110 } 111 #define HTABLE_NAME tags2desc 112 #define HTABLE_KEY int 113 #define HTABLE_DATA struct tag_desc 114 #define HTABLE_DATA_FUNCTOR_INIT tag_desc_init 115 #define HTABLE_DATA_FUNCTOR_RELEASE tag_desc_release 116 #define HTABLE_DATA_FUNCTOR_COPY tag_desc_copy 117 #define HTABLE_DATA_FUNCTOR_COPY_AND_RELEASE tag_desc_copy_and_release 118 #include <rsys/hash_table.h> 119 120 #define HTABLE_NAME size_modifiers 121 #define HTABLE_KEY int 122 #define HTABLE_DATA double 123 #include <rsys/hash_table.h> 124 125 struct scad_device { 126 struct logger* logger; 127 struct mem_allocator* allocator; 128 struct scad_options options; 129 struct htable_names geometry_names; 130 struct htable_geometries allgeom; 131 struct htable_tags2desc tags2desc[2]; /* Only geoms for 2D and 3D tags for now */ 132 struct htable_size_modifiers size_modifiers_by_dim[4]; 133 int verbose; 134 int need_synchro; 135 136 ref_T ref; 137 /* Used only when releasing the geometry */ 138 int log; 139 enum log_type log_type; 140 }; 141 142 /******************************************************************************* 143 * Helper functions 144 ******************************************************************************/ 145 /* Conditionally log a message on the LOG_ERROR stream of the device logger, 146 * with respect to the device verbose flag */ 147 extern LOCAL_SYM void 148 log_error 149 (struct scad_device* dev, 150 const char* msg, 151 ...) 152 #ifdef COMPILER_GCC 153 __attribute((format(printf, 2, 3))) 154 #endif 155 ; 156 157 /* Conditionally log a message on the LOG_WARNING stream of the device logger, 158 * with respect to the device verbose flag */ 159 extern LOCAL_SYM void 160 log_warning 161 (struct scad_device* dev, 162 const char* msg, 163 ...) 164 #ifdef COMPILER_GCC 165 __attribute((format(printf, 2, 3))) 166 #endif 167 ; 168 169 /* Conditionally log a message on the LOG_OUTPUT stream of the device logger, 170 * with respect to the device verbose flag */ 171 extern LOCAL_SYM void 172 log_message 173 (struct scad_device* dev, 174 const char* msg, 175 ...) 176 #ifdef COMPILER_GCC 177 __attribute((format(printf, 2, 3))) 178 #endif 179 ; 180 181 static INLINE void 182 log_msg 183 (struct scad_device* dev, 184 const enum log_type stream, 185 const char* msg, 186 va_list vargs) 187 { 188 res_T res; (void)res; 189 ASSERT(dev && msg); 190 res = logger_vprint(dev->logger, stream, msg, vargs); 191 ASSERT(res == RES_OK); 192 } 193 194 /******************************************************************************* 195 * Exported scad_device functions 196 ******************************************************************************/ 197 LOCAL_SYM res_T 198 check_device 199 (const char* function_name); 200 201 LOCAL_SYM res_T 202 sync_device(void); 203 204 LOCAL_SYM struct scad_device* 205 get_device 206 (void); 207 208 LOCAL_SYM struct tag_desc* 209 device_get_description 210 (const int dim, 211 const int tag); 212 213 LOCAL_SYM res_T 214 do_device_tags_ref_get 215 (const int* dimTags, 216 const size_t count); 217 218 LOCAL_SYM res_T 219 device_register_tags 220 (struct scad_geometry* geom); 221 222 LOCAL_SYM res_T 223 do_device_tags_ref_put 224 (const int log, 225 const enum log_type log_type, 226 int* dimTags, 227 size_t count); 228 229 LOCAL_SYM res_T 230 device_register_ref_to_tags 231 (const int dim, 232 const int tag, 233 const int* dimTags, 234 const size_t count); 235 236 LOCAL_SYM res_T 237 device_unregister_tags 238 (const int log, 239 const enum log_type log_type, 240 struct scad_geometry* geom); 241 242 LOCAL_SYM res_T 243 check_empty_gmsh_occ 244 (struct scad_device* dev); 245 246 #endif