libvisual  0.5.0
lv_param_value.h
1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2012 Libvisual team
4  *
5  * Authors: Chong Kai Xiong <kaixiong@codeleft.sg>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 
22 #ifndef _LV_PARAM_VALUE_H
23 #define _LV_PARAM_VALUE_H
24 
25 #include <libvisual/lvconfig.h>
26 #include <libvisual/lv_types.h>
27 #include <libvisual/lv_checks.h>
28 #include <libvisual/lv_color.h>
29 #include <libvisual/lv_palette.h>
30 
31 #define VISUAL_PARAM_VALUE(obj) ((VisParamValue *) (obj))
32 
33 typedef struct _VisParamValue VisParamValue;
34 
35 typedef enum {
36  VISUAL_PARAM_TYPE_NONE,
37  VISUAL_PARAM_TYPE_BOOL,
38  VISUAL_PARAM_TYPE_INTEGER,
39  VISUAL_PARAM_TYPE_FLOAT,
40  VISUAL_PARAM_TYPE_DOUBLE,
41  VISUAL_PARAM_TYPE_COLOR,
42  VISUAL_PARAM_TYPE_STRING,
43  VISUAL_PARAM_TYPE_PALETTE,
44 } VisParamType;
45 
47 {
48  VisParamType type;
49 
50  union {
51  int integer;
52  float single_float;
53  double double_float;
54  char * string;
55  VisColor * color;
56  VisPalette * palette;
57  } value;
58 };
59 
60 LV_BEGIN_DECLS
61 
62 LV_API VisParamValue *visual_param_value_new (VisParamType type, void *value);
63 LV_API void visual_param_value_init (VisParamValue *self, VisParamType type, void *value);
64 LV_API void visual_param_value_copy (VisParamValue *value, VisParamValue *src);
65 LV_API void visual_param_value_set (VisParamValue *value, VisParamType type, void *new_value);
66 LV_API int visual_param_value_compare (VisParamValue *lhs, VisParamValue *rhs);
67 LV_API void visual_param_value_free (VisParamValue *value);
68 LV_API void visual_param_value_free_value (VisParamValue *value);
69 
70 #define _LV_PARAM_MARSHAL_INTEGER(x) ((void *) (intptr_t) (x))
71 #define _LV_PARAM_MARSHAL_FLOAT(x) ((void *) (&x))
72 #define _LV_PARAM_MARSHAL_DOUBLE(x) ((void *) (&x))
73 #define _LV_PARAM_MARSHAL_POINTER(x) ((void *) (x))
74 
75 #define _LV_DEFINE_PARAM_VALUE_SET(func,ctype,name,marshal) \
76  static inline void visual_param_value_set_##func (VisParamValue *self, ctype value) { \
77  visual_return_if_fail (self != NULL); \
78  visual_param_value_set (self, VISUAL_PARAM_TYPE_##name, _LV_PARAM_MARSHAL_##marshal (value)); \
79  }
80 
81 _LV_DEFINE_PARAM_VALUE_SET(bool , int , BOOL , INTEGER)
82 _LV_DEFINE_PARAM_VALUE_SET(integer, int , INTEGER, INTEGER)
83 _LV_DEFINE_PARAM_VALUE_SET(float , float , FLOAT , FLOAT)
84 _LV_DEFINE_PARAM_VALUE_SET(double , double , DOUBLE , DOUBLE)
85 _LV_DEFINE_PARAM_VALUE_SET(string , const char * , STRING , POINTER)
86 _LV_DEFINE_PARAM_VALUE_SET(color , const VisColor * , COLOR , POINTER)
87 _LV_DEFINE_PARAM_VALUE_SET(palette, const VisPalette *, PALETTE, POINTER)
88 
89 #define _LV_DEFINE_PARAM_VALUE_GET(func,ctype,name,member,defvalue) \
90  static inline ctype visual_param_value_get_##func (VisParamValue *self) { \
91  visual_return_val_if_fail (self != NULL, 0); \
92  visual_return_val_if_fail (self->type == VISUAL_PARAM_TYPE_##name, defvalue); \
93  return self->value.member; \
94  }
95 
96 _LV_DEFINE_PARAM_VALUE_GET(bool , int , BOOL , integer , FALSE)
97 _LV_DEFINE_PARAM_VALUE_GET(integer, int , INTEGER, integer , 0)
98 _LV_DEFINE_PARAM_VALUE_GET(float , float , FLOAT , single_float, 0.0f)
99 _LV_DEFINE_PARAM_VALUE_GET(double , double , DOUBLE , double_float, 0.0)
100 _LV_DEFINE_PARAM_VALUE_GET(string , const char *, STRING , string , NULL)
101 _LV_DEFINE_PARAM_VALUE_GET(color , VisColor * , COLOR , color , NULL)
102 _LV_DEFINE_PARAM_VALUE_GET(palette, VisPalette *, PALETTE, palette , NULL)
103 
104 LV_END_DECLS
105 
106 #endif /* LV_PARAM_VALUE_H */