]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil.core/native/FMILibrary/src/Util/include/JM/jm_named_ptr.h
Add FMILibrary-2.0.3 to org.simantics.fmil.core\native.
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / src / Util / include / JM / jm_named_ptr.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 JM_NAMED_PTR_H
17 #define JM_NAMED_PTR_H
18
19 #include "jm_vector.h"
20 #include "jm_callbacks.h"
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 /** \file jm_named_ptr.h Definition of ::jm_named_ptr and supporting functions
26         *
27         * \addtogroup jm_utils
28         * @{
29                 \addtogroup jm_named_ptr
30         * @}
31 */
32 /** \addtogroup jm_named_ptr Named objects
33  @{
34 */
35 /** \brief Name and object pointer pair */
36 typedef struct jm_named_ptr jm_named_ptr;
37
38 /** \brief Name and object pointer pair */
39 struct jm_named_ptr {
40     jm_voidp ptr; /** \brief Object pointer */
41     jm_string name; /** \brief Name string */
42 };
43
44 /**
45 \brief Allocate memory for the object and the name string and sets pointer to it packed together with the name pointer.
46  \param name Name for the object.
47  \param size Size of the data structure.
48  \param nameoffset Offset of the name field within the data structure.
49  \param c Callbacks to be used for memory allocation.
50
51 The function jm_named_alloc() is intended for types defined as:
52 \code
53 struct T { 
54     < some data fields> 
55     char name[1]; 
56
57 \endcode
58 The "name" is copied into the allocated memory.
59 */
60 jm_named_ptr jm_named_alloc(jm_string name, size_t size, size_t nameoffset, jm_callbacks* c);
61
62 /** \brief Same as jm_named_alloc() but name is given as a jm_vector(char) pointer */
63 jm_named_ptr jm_named_alloc_v(jm_vector(char)* name, size_t size, size_t nameoffset, jm_callbacks* c);
64
65 /** \brief Free the memory allocated for the object pointed by jm_named_ptr */
66 static void jm_named_free(jm_named_ptr np, jm_callbacks* c) { c->free(np.ptr); }
67
68 jm_vector_declare_template(jm_named_ptr)
69
70 /** \brief Helper to construct comparison operation */
71 #define jm_diff_named(a, b) strcmp(a.name,b.name)
72
73 jm_define_comp_f(jm_compare_named, jm_named_ptr, jm_diff_named)
74
75 /** \brief Release the data allocated by the items
76   in a vector and then clears the memory used by the vector as well.
77
78   This should be used for vectors initialized with jm_vector_init.
79 */
80 static void jm_named_vector_free_data(jm_vector(jm_named_ptr)* v) {
81     jm_vector_foreach_c(jm_named_ptr)(v, (void (*)(jm_named_ptr, void*))jm_named_free,v->callbacks);
82     jm_vector_free_data(jm_named_ptr)(v);
83 }
84
85 /** \brief Release the data allocated by the items
86   in a vector and then clears the memory used by the vector as well.
87
88   This should be used for vectors created with jm_vector_alloc.
89 */
90 static void jm_named_vector_free(jm_vector(jm_named_ptr)* v) {
91     jm_vector_foreach_c(jm_named_ptr)(v,(void (*)(jm_named_ptr, void*))jm_named_free,v->callbacks);
92     jm_vector_free(jm_named_ptr)(v);
93 }
94 /** @} */
95 #ifdef __cplusplus
96 }
97 #endif
98
99 /* JM_NAMED_PTR_H */
100 #endif