Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
wireplumber
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pkg
wireplumber
Commits
b8d1f401
Commit
b8d1f401
authored
4 years ago
by
George Kiagiadakis
Browse files
Options
Downloads
Patches
Plain Diff
iterator: add a generic GPtrArray iterator implementation
parent
1f2e2b7f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/wp/iterator.c
+105
-0
105 additions, 0 deletions
lib/wp/iterator.c
lib/wp/iterator.h
+9
-0
9 additions, 0 deletions
lib/wp/iterator.h
with
114 additions
and
0 deletions
lib/wp/iterator.c
+
105
−
0
View file @
b8d1f401
...
...
@@ -189,3 +189,108 @@ wp_iterator_foreach (WpIterator *self, WpIteratorForeachFunc func,
return
wp_iterator_default_foreach
(
self
,
func
,
data
);
}
struct
ptr_array_iterator_data
{
GPtrArray
*
array
;
GType
item_type
;
guint
index
;
void
(
*
set_value
)
(
GValue
*
,
gpointer
);
};
static
void
ptr_array_iterator_reset
(
WpIterator
*
it
)
{
struct
ptr_array_iterator_data
*
it_data
=
wp_iterator_get_user_data
(
it
);
it_data
->
index
=
0
;
}
static
gboolean
ptr_array_iterator_next
(
WpIterator
*
it
,
GValue
*
item
)
{
struct
ptr_array_iterator_data
*
it_data
=
wp_iterator_get_user_data
(
it
);
if
(
it_data
->
index
<
it_data
->
array
->
len
)
{
gpointer
ptr
=
g_ptr_array_index
(
it_data
->
array
,
it_data
->
index
++
);
g_value_init
(
item
,
it_data
->
item_type
);
it_data
->
set_value
(
item
,
ptr
);
return
TRUE
;
}
return
FALSE
;
}
static
gboolean
ptr_array_iterator_fold
(
WpIterator
*
it
,
WpIteratorFoldFunc
func
,
GValue
*
ret
,
gpointer
data
)
{
struct
ptr_array_iterator_data
*
it_data
=
wp_iterator_get_user_data
(
it
);
gpointer
*
ptr
,
*
base
;
guint
len
;
ptr
=
base
=
it_data
->
array
->
pdata
;
len
=
it_data
->
array
->
len
;
while
((
ptr
-
base
)
<
len
)
{
g_auto
(
GValue
)
item
=
G_VALUE_INIT
;
g_value_init
(
&
item
,
it_data
->
item_type
);
it_data
->
set_value
(
&
item
,
*
ptr
);
if
(
!
func
(
&
item
,
ret
,
data
))
return
FALSE
;
ptr
++
;
}
return
TRUE
;
}
static
void
ptr_array_iterator_finalize
(
WpIterator
*
it
)
{
struct
ptr_array_iterator_data
*
it_data
=
wp_iterator_get_user_data
(
it
);
g_ptr_array_unref
(
it_data
->
array
);
}
static
const
WpIteratorMethods
ptr_array_iterator_methods
=
{
.
reset
=
ptr_array_iterator_reset
,
.
next
=
ptr_array_iterator_next
,
.
fold
=
ptr_array_iterator_fold
,
.
finalize
=
ptr_array_iterator_finalize
,
};
/**
* wp_iterator_new_ptr_array:
* @items: (transfer full): the items to iterate over
* @item_type: the type of each item
*
* Returns: (transfer full): a new iterator that iterates over @items
*/
WpIterator
*
wp_iterator_new_ptr_array
(
GPtrArray
*
items
,
GType
item_type
)
{
g_autoptr
(
WpIterator
)
it
=
NULL
;
struct
ptr_array_iterator_data
*
it_data
;
g_return_val_if_fail
(
items
!=
NULL
,
NULL
);
it
=
wp_iterator_new
(
&
ptr_array_iterator_methods
,
sizeof
(
struct
ptr_array_iterator_data
));
it_data
=
wp_iterator_get_user_data
(
it
);
it_data
->
array
=
items
;
it_data
->
item_type
=
item_type
;
it_data
->
index
=
0
;
if
(
g_type_is_a
(
item_type
,
G_TYPE_POINTER
))
it_data
->
set_value
=
g_value_set_pointer
;
else
if
(
g_type_is_a
(
item_type
,
G_TYPE_BOXED
))
it_data
->
set_value
=
(
void
(
*
)
(
GValue
*
,
gpointer
))
g_value_set_boxed
;
else
if
(
g_type_is_a
(
item_type
,
G_TYPE_OBJECT
))
it_data
->
set_value
=
g_value_set_object
;
else
if
(
g_type_is_a
(
item_type
,
G_TYPE_INTERFACE
))
it_data
->
set_value
=
g_value_set_object
;
else
if
(
g_type_is_a
(
item_type
,
G_TYPE_VARIANT
))
it_data
->
set_value
=
(
void
(
*
)
(
GValue
*
,
gpointer
))
g_value_set_variant
;
else
if
(
g_type_is_a
(
item_type
,
G_TYPE_STRING
))
it_data
->
set_value
=
(
void
(
*
)
(
GValue
*
,
gpointer
))
g_value_set_string
;
else
g_return_val_if_reached
(
NULL
);
return
g_steal_pointer
(
&
it
);
}
This diff is collapsed.
Click to expand it.
lib/wp/iterator.h
+
9
−
0
View file @
b8d1f401
...
...
@@ -47,12 +47,16 @@ GType wp_iterator_get_type (void);
typedef
struct
_WpIterator
WpIterator
;
/* ref count */
WP_API
WpIterator
*
wp_iterator_ref
(
WpIterator
*
self
);
WP_API
void
wp_iterator_unref
(
WpIterator
*
self
);
/* iteration api */
WP_API
void
wp_iterator_reset
(
WpIterator
*
self
);
...
...
@@ -67,6 +71,11 @@ WP_API
gboolean
wp_iterator_foreach
(
WpIterator
*
self
,
WpIteratorForeachFunc
func
,
gpointer
data
);
/* constructors */
WP_API
WpIterator
*
wp_iterator_new_ptr_array
(
GPtrArray
*
items
,
GType
item_type
);
G_DEFINE_AUTOPTR_CLEANUP_FUNC
(
WpIterator
,
wp_iterator_unref
)
G_END_DECLS
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment