libvisual  0.5.0
lv_param_value.c
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 #include "config.h"
23 #include "lv_param_value.h"
24 #include "lv_common.h"
25 #include "lv_util.h"
26 #include <string.h>
27 
28 VisParamValue *visual_param_value_new (VisParamType type, void *value)
29 {
30  VisParamValue *self = visual_mem_new0 (VisParamValue, 1);
31  visual_param_value_init (self, type, value);
32  return self;
33 }
34 
35 void visual_param_value_init (VisParamValue *self, VisParamType type, void *value)
36 {
37  self->type = VISUAL_PARAM_TYPE_NONE;
38  visual_param_value_set (self, type, value);
39 }
40 
41 void visual_param_value_copy (VisParamValue *self, VisParamValue *src)
42 {
43  visual_return_if_fail (self != NULL);
44 
45  visual_param_value_free_value (self);
46 
47  self->type = src->type;
48 
49  switch (src->type)
50  {
51  case VISUAL_PARAM_TYPE_NONE:
52  break;
53  case VISUAL_PARAM_TYPE_BOOL:
54  case VISUAL_PARAM_TYPE_INTEGER:
55  case VISUAL_PARAM_TYPE_FLOAT:
56  case VISUAL_PARAM_TYPE_DOUBLE:
57  self->value = self->value;
58  break;
59  case VISUAL_PARAM_TYPE_COLOR:
60  self->value.color = visual_color_clone (src->value.color);
61  break;
62  case VISUAL_PARAM_TYPE_STRING:
63  self->value.string = visual_strdup (src->value.string);
64  break;
65  case VISUAL_PARAM_TYPE_PALETTE:
66  self->value.palette = visual_palette_clone (src->value.palette);
67  break;
68  default:
69  break;
70  }
71 }
72 
73 void visual_param_value_set (VisParamValue *self, VisParamType type, void *value)
74 {
75  visual_return_if_fail (self != NULL);
76  visual_return_if_fail (type != VISUAL_PARAM_TYPE_NONE);
77 
78  visual_param_value_free_value (self);
79 
80  self->type = type;
81 
82  switch (type)
83  {
84  case VISUAL_PARAM_TYPE_NONE:
85  break;
86  case VISUAL_PARAM_TYPE_BOOL:
87  case VISUAL_PARAM_TYPE_INTEGER:
88  self->value.integer = (intptr_t) value;
89  break;
90  case VISUAL_PARAM_TYPE_FLOAT: {
91  self->value.single_float = *(float *) value;
92  break;
93  }
94  case VISUAL_PARAM_TYPE_DOUBLE:
95  self->value.double_float = *(double *) value;
96  break;
97  case VISUAL_PARAM_TYPE_STRING:
98  self->value.string = visual_strdup ((const char *) value);
99  break;
100  case VISUAL_PARAM_TYPE_COLOR:
101  self->value.color = visual_color_clone (value);
102  break;
103  case VISUAL_PARAM_TYPE_PALETTE:
104  self->value.palette = visual_palette_clone (value);
105  break;
106  default:
107  break;
108  }
109 }
110 
111 void visual_param_value_free_value (VisParamValue *self)
112 {
113  visual_return_if_fail (self != NULL);
114 
115  switch (self->type)
116  {
117  case VISUAL_PARAM_TYPE_STRING:
118  visual_mem_free (self->value.string);
119  break;
120  case VISUAL_PARAM_TYPE_COLOR:
121  visual_color_free (self->value.color);
122  break;
123  case VISUAL_PARAM_TYPE_PALETTE:
124  visual_palette_free (self->value.palette);
125  break;
126  default:
127  break;
128  }
129 
130  self->type = VISUAL_PARAM_TYPE_NONE;
131 }
132 
133 void visual_param_value_free (VisParamValue *self)
134 {
135  visual_return_if_fail (self != NULL);
136 
137  visual_param_value_free_value (self);
138  visual_mem_free (self);
139 }
140 
141 int visual_param_value_compare (VisParamValue *lhs, VisParamValue *rhs)
142 {
143  visual_return_val_if_fail (lhs != NULL, FALSE);
144  visual_return_val_if_fail (rhs != NULL, FALSE);
145 
146  if (lhs->type != rhs->type)
147  return FALSE;
148 
149  switch (lhs->type) {
150  case VISUAL_PARAM_TYPE_NONE:
151  return TRUE;
152  case VISUAL_PARAM_TYPE_BOOL:
153  case VISUAL_PARAM_TYPE_INTEGER:
154  return lhs->value.integer == rhs->value.integer;
155  case VISUAL_PARAM_TYPE_FLOAT:
156  return lhs->value.single_float == rhs->value.single_float;
157  case VISUAL_PARAM_TYPE_DOUBLE:
158  return lhs->value.double_float == rhs->value.double_float;
159  case VISUAL_PARAM_TYPE_STRING:
160  return strcmp (lhs->value.string, rhs->value.string) == 0;
161  case VISUAL_PARAM_TYPE_COLOR:
162  return visual_color_compare (lhs->value.color, rhs->value.color);
163  case VISUAL_PARAM_TYPE_PALETTE:
164  return FALSE;
165  default:
166  return FALSE;
167  }
168 }