PipeWire: spa/pod/compare.h Source File

PipeWire 1.4.2 |

Loading...
Searching...
No Matches
compare.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_POD_COMPARE_H
6#define SPA_POD_COMPARE_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <stdarg.h>
13#include <errno.h>
14#include <stdint.h>
15#include <stddef.h>
16#include <stdio.h>
17#include <string.h>
18
19#include <spa/param/props.h>
20#include <spa/pod/iter.h>
21#include <spa/pod/builder.h>
22
23#ifndef SPA_API_POD_COMPARE
24 #ifdef SPA_API_IMPL
25 #define SPA_API_POD_COMPARE SPA_API_IMPL
26 #else
27 #define SPA_API_POD_COMPARE static inline
28 #endif
29#endif
30
35
36 SPA_API_POD_COMPARE int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
37{
38 switch (type) {
39 case SPA_TYPE_None:
40 return 0;
41 case SPA_TYPE_Bool:
42 return SPA_CMP(!!*(int32_t *)r1, !!*(int32_t *)r2);
43 case SPA_TYPE_Id:
44 return SPA_CMP(*(uint32_t *)r1, *(uint32_t *)r2);
45 case SPA_TYPE_Int:
46 return SPA_CMP(*(int32_t *)r1, *(int32_t *)r2);
47 case SPA_TYPE_Long:
48 return SPA_CMP(*(int64_t *)r1, *(int64_t *)r2);
49 case SPA_TYPE_Float:
50 return SPA_CMP(*(float *)r1, *(float *)r2);
51 case SPA_TYPE_Double:
52 return SPA_CMP(*(double *)r1, *(double *)r2);
53 case SPA_TYPE_String:
54 return strcmp((char *)r1, (char *)r2);
55 case SPA_TYPE_Bytes:
56 return memcmp((char *)r1, (char *)r2, size);
57 case SPA_TYPE_Rectangle:
58 {
59 const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1,
60 *rec2 = (struct spa_rectangle *) r2;
61 if (rec1->width == rec2->width && rec1->height == rec2->height)
62 return 0;
63 else if (rec1->width < rec2->width || rec1->height < rec2->height)
64 return -1;
65 else
66 return 1;
67 }
68 case SPA_TYPE_Fraction:
69 {
70 const struct spa_fraction *f1 = (struct spa_fraction *) r1,
71 *f2 = (struct spa_fraction *) r2;
72 uint64_t n1, n2;
73 n1 = ((uint64_t) f1->num) * f2->denom;
74 n2 = ((uint64_t) f2->num) * f1->denom;
75 return SPA_CMP(n1, n2);
76 }
77 default:
78 break;
79 }
80 return 0;
81}
82
83 SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1,
84 const struct spa_pod *pod2)
85{
86 int res = 0;
87 uint32_t n_vals1, n_vals2;
88 uint32_t choice1, choice2;
89
90 spa_return_val_if_fail(pod1 != NULL, -EINVAL);
91 spa_return_val_if_fail(pod2 != NULL, -EINVAL);
92
93 pod1 = spa_pod_get_values(pod1, &n_vals1, &choice1);
94 pod2 = spa_pod_get_values(pod2, &n_vals2, &choice2);
95
96 if (n_vals1 != n_vals2)
97 return -EINVAL;
98
99 if (SPA_POD_TYPE(pod1) != SPA_POD_TYPE(pod2))
100 return -EINVAL;
101
102 switch (SPA_POD_TYPE(pod1)) {
103 case SPA_TYPE_Struct:
104 {
105 const struct spa_pod *p1, *p2;
106 size_t p1s, p2s;
107
108 p1 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod1);
109 p1s = SPA_POD_BODY_SIZE(pod1);
110 p2 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod2);
111 p2s = SPA_POD_BODY_SIZE(pod2);
112
113 while (true) {
114 if (!spa_pod_is_inside(pod1, p1s, p1) ||
115 !spa_pod_is_inside(pod2, p2s, p2))
116 return -EINVAL;
117
118 if ((res = spa_pod_compare(p1, p2)) != 0)
119 return res;
120
121 p1 = (const struct spa_pod*)spa_pod_next(p1);
122 p2 = (const struct spa_pod*)spa_pod_next(p2);
123 }
124 break;
125 }
126 case SPA_TYPE_Object:
127 {
128 const struct spa_pod_prop *p1, *p2;
129 const struct spa_pod_object *o1, *o2;
130
131 o1 = (const struct spa_pod_object*)pod1;
132 o2 = (const struct spa_pod_object*)pod2;
133
134 p2 = NULL;
135 SPA_POD_OBJECT_FOREACH(o1, p1) {
136 if ((p2 = spa_pod_object_find_prop(o2, p2, p1->key)) == NULL)
137 return 1;
138 if ((res = spa_pod_compare(&p1->value, &p2->value)) != 0)
139 return res;
140 }
141 p1 = NULL;
142 SPA_POD_OBJECT_FOREACH(o2, p2) {
143 if ((p1 = spa_pod_object_find_prop(o1, p1, p2->key)) == NULL)
144 return -1;
145 }
146 break;
147 }
148 case SPA_TYPE_Array:
149 {
150 if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2))
151 return -EINVAL;
152 res = memcmp(SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), SPA_POD_BODY_SIZE(pod2));
153 break;
154 }
155 default:
156 if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2))
157 return -EINVAL;
158 res = spa_pod_compare_value(SPA_POD_TYPE(pod1),
159 SPA_POD_BODY(pod1), SPA_POD_BODY(pod2),
160 SPA_POD_BODY_SIZE(pod1));
161 break;
162 }
163 return res;
164}
165
169
170#ifdef __cplusplus
171}
172#endif
173
174#endif
builder.h
spa/pod/builder.h
SPA_API_POD_COMPARE
#define SPA_API_POD_COMPARE
Definition compare.h:34
res
uint32_t int int res
Definition core.h:433
SPA_POD_BODY_CONST
#define SPA_POD_BODY_CONST(pod)
Definition pod.h:41
spa_pod_compare
SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1, const struct spa_pod *pod2)
Definition compare.h:90
spa_pod_get_values
SPA_API_POD_ITER struct spa_pod * spa_pod_get_values(const struct spa_pod *pod, uint32_t *n_vals, uint32_t *choice)
Definition iter.h:363
spa_pod_object_find_prop
SPA_API_POD_ITER const struct spa_pod_prop * spa_pod_object_find_prop(const struct spa_pod_object *pod, const struct spa_pod_prop *start, uint32_t key)
Definition iter.h:404
spa_pod_compare_value
SPA_API_POD_COMPARE int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
Definition compare.h:43
SPA_POD_OBJECT_FOREACH
#define SPA_POD_OBJECT_FOREACH(obj, iter)
Definition iter.h:124
SPA_POD_BODY
#define SPA_POD_BODY(pod)
Definition pod.h:39
SPA_POD_TYPE
#define SPA_POD_TYPE(pod)
Definition pod.h:28
spa_pod_next
SPA_API_POD_ITER void * spa_pod_next(const void *iter)
Definition iter.h:52
SPA_POD_BODY_SIZE
#define SPA_POD_BODY_SIZE(pod)
Definition pod.h:26
spa_pod_is_inside
SPA_API_POD_ITER bool spa_pod_is_inside(const void *pod, uint32_t size, const void *iter)
Definition iter.h:44
SPA_TYPE_Int
@ SPA_TYPE_Int
Definition type.h:45
SPA_TYPE_Rectangle
@ SPA_TYPE_Rectangle
Definition type.h:51
SPA_TYPE_Long
@ SPA_TYPE_Long
Definition type.h:46
SPA_TYPE_Bool
@ SPA_TYPE_Bool
Definition type.h:43
SPA_TYPE_Bytes
@ SPA_TYPE_Bytes
Definition type.h:50
SPA_TYPE_Object
@ SPA_TYPE_Object
Definition type.h:56
SPA_TYPE_Float
@ SPA_TYPE_Float
Definition type.h:47
SPA_TYPE_Fraction
@ SPA_TYPE_Fraction
Definition type.h:52
SPA_TYPE_None
@ SPA_TYPE_None
Definition type.h:42
SPA_TYPE_Double
@ SPA_TYPE_Double
Definition type.h:48
SPA_TYPE_Id
@ SPA_TYPE_Id
Definition type.h:44
SPA_TYPE_Array
@ SPA_TYPE_Array
Definition type.h:54
SPA_TYPE_String
@ SPA_TYPE_String
Definition type.h:49
SPA_TYPE_Struct
@ SPA_TYPE_Struct
Definition type.h:55
spa_return_val_if_fail
#define spa_return_val_if_fail(expr, val)
Definition defs.h:456
SPA_CMP
#define SPA_CMP(a, b)
3-way comparison.
Definition defs.h:209
iter.h
spa/pod/iter.h
props.h
string.h
spa/utils/string.h
spa_fraction
Definition defs.h:137
spa_fraction::num
uint32_t num
Definition defs.h:138
spa_fraction::denom
uint32_t denom
Definition defs.h:139
spa_pod_object
Definition pod.h:183
spa_pod_prop
Definition pod.h:208
spa_pod_prop::key
uint32_t key
key of property, list of valid keys depends on the object type
Definition pod.h:209
spa_pod_prop::value
struct spa_pod value
Definition pod.h:226
spa_pod
Definition pod.h:43
spa_rectangle
Definition defs.h:116
spa_rectangle::width
uint32_t width
Definition defs.h:117
spa_rectangle::height
uint32_t height
Definition defs.h:118