rename hkl_engine_modes -> hkl_engine_modes_get
[hkl.git] / hkl / hkl-binding.c
bloba6c4f0a0a6796c403fd821d46b0be5b07e9ec96a
1 /* This file is part of the hkl library.
3 * The hkl library 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.
8 * The hkl library 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.
13 * You should have received a copy of the GNU General Public License
14 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2012-2013 Synchrotron SOLEIL
17 * L'Orme des Merisiers Saint-Aubin
18 * BP 48 91192 GIF-sur-YVETTE CEDEX
20 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
23 #include <stdlib.h> // for malloc
24 #include <string.h> // for NULL, strdup
25 #include <sys/types.h> // for uint
26 #include <glib.h> // for g_set_error, GError etc
27 #include "hkl-error-private.h" // for hkl_error_clear, _HklError
28 #include "hkl-factory-private.h" // for __start_xautodata_factories, etc
29 #include "hkl-geometry-private.h" // for _HklGeometry, etc
30 #include "hkl-parameter-private.h"
31 #include "hkl-pseudoaxis-private.h" // for _HklEngine, HklEngineInfo
32 #include "hkl-sample-private.h" // for _HklSampleReflection, etc
33 #include "hkl.h" // for HklGeometry, HklEngine, etc
34 #include "hkl/ccan/autodata/autodata.h" // for autodata_get
35 #include "hkl/ccan/darray/darray.h" // for darray_foreach, darray_size
36 #include "hkl/ccan/list/list.h" // for list_for_each, list_head
38 /**************/
39 /* HklFactory */
40 /**************/
42 /**
43 * hkl_factories:
45 * return all the Hkl factories objects as a dictionnary
47 * Returns: (element-type utf8 Hkl.Factory) (transfer container):
48 **/
49 GHashTable *hkl_factories(void)
51 GHashTable *table = NULL;
52 unsigned int i;
53 unsigned int n;
54 HklFactory **factories;
56 table = g_hash_table_new(g_str_hash, g_str_equal);
57 factories = autodata_get(factories, &n);
58 for(i=0; i<n; ++i){
59 g_hash_table_insert(table,
60 hkl_factory_name(factories[i]),
61 factories[i]);
64 return table;
67 /********************/
68 /* HklParameterList */
69 /********************/
71 #define HKL_PARAMETER_LIST_ERROR hkl_parameter_list_error_quark ()
73 GQuark hkl_parameter_list_error_quark (void)
75 return g_quark_from_static_string ("hkl-parameter-list-error-quark");
78 typedef enum {
79 HKL_PARAMETER_LIST_ERROR_VALUES_SET /* can not set the parameter list values */
80 } HklParameterListError;
82 /**
83 * hkl_parameter_list_values_unit_set_binding:
84 * @self: the this ptr
85 * @values: (array length=len): the values to set
86 * @len: the length of the values
87 * @error: error set if something goes wrong
89 * set the parameter list with the given values
91 * Rename to: hkl_parameter_list_values_unit_set
93 * Return value: true if succeed or false otherwise
94 **/
95 gboolean hkl_parameter_list_values_unit_set_binding(HklParameterList *self,
96 double values[], uint len,
97 GError **error)
99 HklError *err = NULL;
101 g_return_val_if_fail(error == NULL ||*error == NULL, FALSE);
103 if(!hkl_parameter_list_values_unit_set(self,
104 values, len, &err)){
105 g_assert(&err == NULL || err != NULL);
107 g_set_error(error,
108 HKL_PARAMETER_LIST_ERROR,
109 HKL_PARAMETER_LIST_ERROR_VALUES_SET,
110 strdup(err->message));
112 hkl_error_clear(&err);
114 return FALSE;
116 return TRUE;
120 * hkl_parameter_list_parameters:
121 * @self: the this ptr
123 * Return value: (element-type HklParameter) (transfer container): list of parameters
124 * free the list with g_slist_free when done.
126 GSList* hkl_parameter_list_parameters(HklParameterList *self)
128 GSList *list = NULL;
129 HklParameter **parameter;
131 darray_foreach(parameter, *self){
132 list = g_slist_append(list, *parameter);
135 return list;
138 /************/
139 /* Geometry */
140 /************/
143 * hkl_geometry_axes:
144 * @self: the this ptr
146 * Returns: (element-type HklParameter) (transfer container): list of HklParameter,
147 * free the list with g_slist_free when done.
149 GSList *hkl_geometry_axes(HklGeometry *self)
151 GSList *list = NULL;
152 HklParameter **axis;
154 darray_foreach(axis, self->axes){
155 list = g_slist_append(list, *axis);
158 return list;
163 * hkl_geometry_get_axes_values_unit:
164 * @self: the this ptr
165 * @len: (out caller-allocates): the length of the returned array
167 * Return value: (array length=len) (transfer container): list of axes values,
168 * free the list with free when done.
170 double *hkl_geometry_get_axes_values_unit(const HklGeometry *self, guint *len)
172 double *values;
173 uint i = 0;
174 HklParameter **axis;
176 if(!self || !len || darray_size(self->axes) == 0)
177 return NULL;
179 *len = darray_size(self->axes);
180 values = malloc(darray_size(self->axes) * sizeof(*values));
182 darray_foreach(axis, self->axes){
183 values[i++] = hkl_parameter_value_unit_get(*axis);
186 return values;
190 * hkl_geometry_set_axes_values_unit:
191 * @self: the this ptr
192 * @values: (array length=len): the values to set.
193 * @len: the length of the values array.
195 void hkl_geometry_set_axes_values_unit(HklGeometry *self, double *values, unsigned int len)
197 uint i = 0;
198 HklParameter **axis;
200 if (!self || !values || len != darray_size(self->axes))
201 return;
203 darray_foreach(axis, self->axes){
204 hkl_parameter_value_unit_set(*axis,
205 values[i++],
206 NULL);
209 hkl_geometry_update(self);
212 /*******************/
213 /* HklGeometryList */
214 /*******************/
217 * hkl_geometry_list_items:
218 * @self: the #HklGeometryList
220 * Return value: (element-type HklGeometryListItem) (transfer container): list of items,
221 * free the list with g_slist_free when done.
223 GSList* hkl_geometry_list_items(HklGeometryList *self)
225 GSList *list = NULL;
226 HklGeometryListItem **item;
228 darray_foreach(item, self->items){
229 list = g_slist_append(list, *item);
232 return list;
235 /***********************/
236 /* HklGeometryListItem */
237 /***********************/
240 * hkl_geometry_list_item_geometry:
241 * @self: the this ptr
243 * Return value: The geometry contain inside the HklGeometryListItem
245 const HklGeometry *hkl_geometry_list_item_geometry(const HklGeometryListItem *self)
247 return hkl_geometry_list_item_geometry_get(self);
250 /*************/
251 /* HklEngine */
252 /*************/
254 #define HKL_ENGINE_ERROR hkl_engine_error_quark ()
256 GQuark hkl_engine_error_quark (void)
258 return g_quark_from_static_string ("hkl-pseudo-axis-engine-error-quark");
261 typedef enum {
262 HKL_ENGINE_ERROR_SET /* can not set the pseudo axis engine */
263 } HklEngineError;
267 * hkl_engine_modes_get_as_gslist:
268 * @self: the this ptr
270 * Return value: (element-type HklMode) (transfer container): list of mode,
271 * free the list with g_slist_free when done.
273 * Rename to: hkl_engine_modes_get
275 GSList* hkl_engine_modes_get_as_gslist(HklEngine *self)
277 GSList *list = NULL;
278 HklMode **mode;
280 darray_foreach(mode, self->modes){
281 list = g_slist_append(list, *mode);
284 return list;
288 * hkl_engine_set_values_unit:
289 * @self: the this ptr
290 * @values: (array length=len): the values to set
291 * @len: the len of the values array
292 * @error: return location of a GError or NULL
294 * compute the #HklGeometry angles for this #HklEngine
296 * Return value: TRUE on success or FALSE if an error occurred
298 gboolean hkl_engine_set_values_unit(HklEngine *self,
299 double values[], unsigned int len,
300 GError **error)
302 HklParameter *parameter;
303 uint i = 0;
304 HklError *err = NULL;
306 g_return_val_if_fail(error == NULL ||*error == NULL, FALSE);
308 if(len != self->info->n_pseudo_axes)
309 return FALSE;
311 if(!hkl_parameter_list_values_unit_set(&self->pseudo_axes,
312 values, len, &err)){
313 g_assert(&err == NULL || err != NULL);
315 g_set_error(error,
316 HKL_ENGINE_ERROR,
317 HKL_ENGINE_ERROR_SET,
318 strdup(err->message));
320 hkl_error_clear(&err);
322 return FALSE;
325 if(!hkl_engine_set(self, &err)){
326 g_assert(&err == NULL || err != NULL);
328 g_set_error(error,
329 HKL_ENGINE_ERROR,
330 HKL_ENGINE_ERROR_SET,
331 strdup(err->message));
333 hkl_error_clear(&err);
335 return FALSE;
337 g_assert(error != NULL ||*error != NULL);
339 return TRUE;
343 * hkl_engine_list_engines_as_gslist:
344 * @self: the this ptr
346 * Return value: (element-type HklEngine) (transfer container): list of engines,
347 * free the list with g_slist_free when done.
349 * Rename to: hkl_engine_list_engines
351 GSList* hkl_engine_list_engines_as_gslist(HklEngineList *self)
353 GSList *list = NULL;
354 HklEngine **engine;
356 darray_foreach(engine, *self){
357 list = g_slist_append(list, *engine);
360 return list;
363 /*************/
364 /* HklSample */
365 /*************/
368 * hkl_sample_reflections_get:
369 * @self: the this ptr
371 * Return value: (element-type HklSampleReflection) (transfer container): list of reflecions,
372 * free the list with g_slist_free when done.
374 const GSList *hkl_sample_reflections_get(const HklSample *self)
376 GSList *list = NULL;
377 HklSampleReflection *reflection;
379 list_for_each(&self->reflections, reflection, list){
380 list = g_slist_append(list, reflection);
383 return list;
387 * hkl_sample_add_reflection_binding:
388 * @self: the this ptr
389 * @geometry: the geometry of the HklSampleReflection
390 * @detector: the detector of the HklSampleReflection
391 * @h: the h coordinate
392 * @k: the k coordinate
393 * @l: the l coordinate
395 * Return value: (element-type HklEngine) (transfer container): list of engines,
396 * free the list with g_slist_free when done.
398 * Rename to: hkl_sample_add_reflection
400 HklSampleReflection *hkl_sample_add_reflection_binding(HklSample *self,
401 const HklGeometry *geometry,
402 const HklDetector *detector,
403 double h, double k, double l)
405 HklSampleReflection *reflection;
407 reflection = hkl_sample_reflection_new(geometry, detector, h, k, l);
408 hkl_sample_add_reflection(self, reflection);
410 return reflection;