[tests] another memory leak
[hkl.git] / hkl / hkl-parameter-private.h
blob48a6f641789440fc54b2fbbaf01878fde0e5748b
2 /* This file is part of the hkl library.
4 * The hkl library is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * The hkl library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) 2003-2015 Synchrotron SOLEIL
18 * L'Orme des Merisiers Saint-Aubin
19 * BP 48 91192 GIF-sur-YVETTE CEDEX
21 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
23 #ifndef __HKL_PARAMETER_PRIVATE_H__
24 #define __HKL_PARAMETER_PRIVATE_H__
26 #include <math.h> // for M_PI
27 #include <stdio.h> // for FILE, fprintf, NULL
28 #include <stdlib.h> // for free, rand, RAND_MAX
29 #include "hkl-interval-private.h" // for HklInterval
30 #include "hkl-macros-private.h" // for HKL_MALLOC
31 #include "hkl-unit-private.h" // for HklUnit, hkl_unit_factor
32 #include "hkl.h" // for HklParameter, TRUE, etc
34 G_BEGIN_DECLS
36 typedef struct _HklParameterOperations HklParameterOperations;
38 struct _HklParameter {
39 const char *name;
40 const char *description;
41 HklInterval range;
42 double _value;
43 const HklUnit *unit;
44 const HklUnit *punit;
45 int fit;
46 int changed;
47 const HklParameterOperations *ops;
50 #define HKL_PARAMETER_DEFAULTS .name="dummy", .description="no description", .range={.min=0, .max=0}, ._value=0, .unit=NULL, .punit=NULL, .fit=TRUE, .changed=TRUE, .ops = &hkl_parameter_operations_defaults
52 #define HKL_PARAMETER_DEFAULTS_ANGLE HKL_PARAMETER_DEFAULTS, .range={.min=-M_PI, .max=M_PI}, .unit = &hkl_unit_angle_rad, .punit = &hkl_unit_angle_deg
54 #define HKL_PARAMETER_ERROR hkl_parameter_error_quark ()
56 static GQuark hkl_parameter_error_quark (void)
58 return g_quark_from_static_string ("hkl-parameter-error-quark");
61 typedef enum {
62 HKL_PARAMETER_ERROR_MIN_MAX_SET, /* can not set the min max */
63 } HklParameterError;
65 /****************/
66 /* HklParameter */
67 /****************/
69 struct _HklParameterOperations {
70 HklParameter * (*copy)(const HklParameter *self);
71 void (*free)(HklParameter *self);
72 int (*init_copy)(HklParameter *self, const HklParameter *src,
73 GError **error);
74 double (*get_value_closest)(const HklParameter *self,
75 const HklParameter *other);
76 int (*set_value)(HklParameter *self, double value,
77 HklUnitEnum unit_type, GError **error);
78 void (*set_value_smallest_in_range)(HklParameter *self);
79 void (*randomize)(HklParameter *self);
80 int (*is_valid)(const HklParameter *self);
81 void (*fprintf)(FILE *f, const HklParameter *self);
82 const HklVector * (*axis_v_get)(const HklParameter *self);
83 const HklQuaternion * (*quaternion_get)(const HklParameter *self);
86 #define HKL_PARAMETER_OPERATIONS_DEFAULTS \
87 .copy = hkl_parameter_copy_real, \
88 .free = hkl_parameter_free_real, \
89 .init_copy = hkl_parameter_init_copy_real, \
90 .get_value_closest = hkl_parameter_value_get_closest_real, \
91 .set_value = hkl_parameter_value_set_real, \
92 .set_value_smallest_in_range = hkl_parameter_value_set_smallest_in_range_real, \
93 .randomize = hkl_parameter_randomize_real, \
94 .is_valid = hkl_parameter_is_valid_real, \
95 .fprintf = hkl_parameter_fprintf_real, \
96 .axis_v_get = hkl_parameter_axis_v_get_real, \
97 .quaternion_get = hkl_parameter_quaternion_get_real
99 static inline HklParameter *hkl_parameter_copy_real(const HklParameter *self)
101 HklParameter *dup = HKL_MALLOC(HklParameter);
103 *dup = *self;
105 return dup;
108 static inline void hkl_parameter_free_real(HklParameter *self)
110 free(self);
113 static inline int hkl_parameter_init_copy_real(HklParameter *self, const HklParameter *src,
114 GError **error)
116 hkl_error (error == NULL || *error == NULL);
117 hkl_error (self->name == src->name || strcmp(self->name, src->name) == 0);
119 *self = *src;
120 self->changed = TRUE;
122 return TRUE;
125 static inline double hkl_parameter_value_get_closest_real(const HklParameter *self,
126 const HklParameter *ref)
128 return self->_value;
131 static inline int hkl_parameter_value_set_real(HklParameter *self, double value,
132 HklUnitEnum unit_type, GError **error)
134 hkl_error (error == NULL || *error == NULL);
136 switch (unit_type) {
137 case HKL_UNIT_DEFAULT:
138 self->_value = value;
139 break;
140 case HKL_UNIT_USER:
141 self->_value = value / hkl_unit_factor(self->unit, self->punit);
142 break;
144 self->changed = TRUE;
146 return TRUE;
149 static inline void hkl_parameter_value_set_smallest_in_range_real(HklParameter *self)
151 /* DOES NOTHING for a standard parameter */
154 static inline void hkl_parameter_randomize_real(HklParameter *self)
156 if (self->fit) {
157 double alea = (double)rand() / (RAND_MAX + 1.);
158 self->_value = self->range.min
159 + (self->range.max - self->range.min) * alea;
160 self->changed = TRUE;
164 static inline int hkl_parameter_is_valid_real(const HklParameter *self)
166 if(self->_value < (self->range.min - HKL_EPSILON)
167 || self->_value > (self->range.max + HKL_EPSILON))
168 return FALSE;
169 else
170 return TRUE;
173 static inline void hkl_parameter_fprintf_real(FILE *f, const HklParameter *self)
175 double factor = hkl_unit_factor(self->unit, self->punit);
176 if (self->punit)
177 fprintf(f, "\"%s\" : %.7f %s [%.7f : %.7f] (%d)",
178 self->name,
179 self->_value * factor,
180 self->punit->repr,
181 self->range.min * factor,
182 self->range.max * factor,
183 self->fit);
184 else
185 fprintf(f, "\"%s\" : %.7f [%.7f : %.7f] (%d)",
186 self->name,
187 self->_value * factor,
188 self->range.min * factor,
189 self->range.max * factor,
190 self->fit);
193 static inline const HklVector *hkl_parameter_axis_v_get_real(const HklParameter *self)
195 return NULL;
198 static inline const HklQuaternion *hkl_parameter_quaternion_get_real(const HklParameter *self)
200 return NULL;
203 static HklParameterOperations hkl_parameter_operations_defaults = {
204 HKL_PARAMETER_OPERATIONS_DEFAULTS,
208 extern HklParameter *hkl_parameter_new(const char *name, const char *description,
209 double min, double value, double max,
210 int fit, int changed,
211 const HklUnit *unit,
212 const HklUnit *punit);
214 extern int hkl_parameter_init_copy(HklParameter *self, const HklParameter *src,
215 GError **error);
217 extern double hkl_parameter_value_get_closest(const HklParameter *self,
218 const HklParameter *ref);
220 extern void hkl_parameter_value_set_smallest_in_range(HklParameter *self);
222 extern int hkl_parameter_is_valid(const HklParameter *self);
224 extern void hkl_parameter_fprintf(FILE *f, HklParameter *self);
226 /********************/
227 /* HklParameterList */
228 /********************/
230 typedef darray(HklParameter *) darray_parameter;
232 G_END_DECLS
234 #endif /* __HKL_PARAMETER_PRIVATE_H__ */