libvisual  0.5.0
lv_param_validators.c
1 #include "config.h"
2 #include "lv_param.h"
3 #include "lv_common.h"
4 
5 typedef struct {
6  VisParamValue lower;
7  VisParamValue upper;
8 } VisParamInRangeArgs;
9 
10 static int in_range_func (VisParamValue *value, VisParamInRangeArgs *data);
11 static void in_range_args_free (VisParamInRangeArgs *args);
12 
13 VisClosure *visual_param_in_range (VisParamType type, void *lower, void *upper)
14 {
15  VisParamInRangeArgs *args = visual_mem_new0 (VisParamInRangeArgs, 1);
16  visual_param_value_set (&args->lower, type, lower);
17  visual_param_value_set (&args->upper, type, upper);
18 
19  return visual_closure_new (in_range_func, args, (VisDestroyFunc) in_range_args_free);
20 }
21 
22 int in_range_func (VisParamValue *value, VisParamInRangeArgs *data)
23 {
24  visual_return_val_if_fail (value != NULL, FALSE);
25  visual_return_val_if_fail (data != NULL, FALSE);
26 
27  switch (value->type) {
28  case VISUAL_PARAM_TYPE_INTEGER: {
29  int v = visual_param_value_get_integer (value);
30  int a = visual_param_value_get_integer (&data->lower);
31  int b = visual_param_value_get_integer (&data->upper);
32  return (v >= a && v <= b);
33  }
34  case VISUAL_PARAM_TYPE_FLOAT: {
35  float v = visual_param_value_get_float (value);
36  float a = visual_param_value_get_float (&data->lower);
37  float b = visual_param_value_get_float (&data->upper);
38  return (v >= a && v <= b);
39  }
40  case VISUAL_PARAM_TYPE_DOUBLE: {
41  double v = visual_param_value_get_double (value);
42  double a = visual_param_value_get_double (&data->lower);
43  double b = visual_param_value_get_double (&data->upper);
44  return (v >= a && v <= b);
45  }
46  default: {
47  return FALSE;
48  }
49  }
50 }
51 
52 void in_range_args_free (VisParamInRangeArgs *args)
53 {
54  visual_param_value_free_value (&args->lower);
55  visual_param_value_free_value (&args->upper);
56  visual_mem_free (args);
57 }