Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ACS
Public
VILLASframework
VILLAScommon
Commits
a9e36b42
Commit
a9e36b42
authored
Aug 25, 2020
by
Steffen Vogel
🎅🏼
Browse files
list: rework lookup methods to use C++ templates
parent
9079ca42
Changes
4
Hide whitespace changes
Inline
Side-by-side
include/villas/list.h
View file @
a9e36b42
...
...
@@ -29,6 +29,9 @@
#pragma once
#include
<sys/types.h>
#include
<cstring>
#include
<string>
#include
<uuid/uuid.h>
#include
<pthread.h>
#include
<villas/common.hpp>
...
...
@@ -87,23 +90,8 @@ int vlist_remove(struct vlist *l, size_t idx);
int
vlist_insert
(
struct
vlist
*
l
,
size_t
idx
,
void
*
p
);
/** Return the first list element which is identified by a string in its first member variable.
*
* List elements are pointers to structures of the following form:
*
* struct obj {
* char *name;
* // more members
* }
*
* @see Only possible because of §1424 of http://c0x.coding-guidelines.com/6.7.2.1.html
*/
void
*
vlist_lookup
(
struct
vlist
*
l
,
const
char
*
name
);
ssize_t
vlist_lookup_index
(
struct
vlist
*
l
,
const
void
*
ptr
);
/** Return the first element of the list for which cmp returns zero */
void
*
vlist_search
(
struct
vlist
*
l
,
cmp_cb_t
cmp
,
void
*
ctx
);
void
*
vlist_search
(
struct
vlist
*
l
,
cmp_cb_t
cmp
,
const
void
*
ctx
);
/** Returns the number of occurences for which cmp returns zero when called on all list elements. */
int
vlist_count
(
struct
vlist
*
l
,
cmp_cb_t
cmp
,
void
*
ctx
);
...
...
@@ -129,3 +117,38 @@ void vlist_extend(struct vlist *l, size_t len, void *val);
/** Remove all elements for which the callback returns a non-zero return code. */
void
vlist_filter
(
struct
vlist
*
l
,
dtor_cb_t
cb
);
#include
<villas/log.h>
/** Lookup an element from the list based on an UUID */
template
<
typename
T
>
T
*
vlist_lookup_uuid
(
struct
vlist
*
l
,
uuid_t
uuid
)
{
return
(
T
*
)
vlist_search
(
l
,
[](
const
void
*
a
,
const
void
*
b
)
->
int
{
auto
*
n
=
reinterpret_cast
<
const
T
*>
(
a
);
uuid_t
u
;
memcpy
(
u
,
b
,
sizeof
(
uuid_t
));
return
uuid_compare
(
n
->
uuid
,
u
);
},
uuid
);
}
/** Lookup an element from the list based on a name */
template
<
typename
T
>
T
*
vlist_lookup_name
(
struct
vlist
*
l
,
const
std
::
string
&
name
)
{
return
(
T
*
)
vlist_search
(
l
,
[](
const
void
*
a
,
const
void
*
b
)
->
int
{
auto
*
e
=
reinterpret_cast
<
const
T
*>
(
a
);
auto
*
s
=
reinterpret_cast
<
const
std
::
string
*>
(
b
);
return
*
s
==
e
->
name
?
0
:
1
;
},
&
name
);
}
/** Lookup index of list element based on name */
template
<
typename
T
>
ssize_t
vlist_lookup_index
(
struct
vlist
*
l
,
const
std
::
string
&
name
)
{
auto
*
f
=
vlist_lookup_name
<
T
>
(
l
,
name
);
return
f
?
vlist_index
(
l
,
f
)
:
-
1
;
}
\ No newline at end of file
lib/list.cpp
View file @
a9e36b42
...
...
@@ -28,21 +28,6 @@
#include
<villas/list.h>
#include
<villas/utils.hpp>
/* Compare functions */
static
int
cmp_lookup
(
const
void
*
a
,
const
void
*
b
)
{
struct
name_tag
{
char
*
name
;
};
const
name_tag
*
obj
=
(
struct
name_tag
*
)
a
;
return
strcmp
(
obj
->
name
,
(
char
*
)
b
);
}
static
int
cmp_contains
(
const
void
*
a
,
const
void
*
b
)
{
return
a
==
b
?
0
:
1
;
}
#ifdef __APPLE__
static
int
cmp_sort
(
void
*
thunk
,
const
void
*
a
,
const
void
*
b
)
{
#else
...
...
@@ -183,21 +168,11 @@ void vlist_remove_all(struct vlist *l, void *p)
pthread_mutex_unlock
(
&
l
->
lock
);
}
void
*
vlist_lookup
(
struct
vlist
*
l
,
const
char
*
name
)
{
return
vlist_search
(
l
,
cmp_lookup
,
(
void
*
)
name
);
}
ssize_t
vlist_lookup_index
(
struct
vlist
*
l
,
const
void
*
ptr
)
{
void
*
found
=
vlist_lookup
(
l
,
(
char
*
)
ptr
);
return
found
?
vlist_index
(
l
,
found
)
:
-
1
;
}
int
vlist_contains
(
struct
vlist
*
l
,
void
*
p
)
{
return
vlist_count
(
l
,
cmp_contains
,
p
);
return
vlist_count
(
l
,
[](
const
void
*
a
,
const
void
*
b
)
{
return
a
==
b
?
0
:
1
;
},
p
);
}
int
vlist_count
(
struct
vlist
*
l
,
cmp_cb_t
cmp
,
void
*
ctx
)
...
...
@@ -220,7 +195,7 @@ int vlist_count(struct vlist *l, cmp_cb_t cmp, void *ctx)
return
c
;
}
void
*
vlist_search
(
struct
vlist
*
l
,
cmp_cb_t
cmp
,
void
*
ctx
)
void
*
vlist_search
(
struct
vlist
*
l
,
cmp_cb_t
cmp
,
const
void
*
ctx
)
{
void
*
e
;
...
...
lib/log.cpp
View file @
a9e36b42
...
...
@@ -88,8 +88,7 @@ void Log::parse(json_t *cfg)
const
char
*
path
=
nullptr
;
const
char
*
pattern
=
nullptr
;
int
syslog
;
int
ret
;
int
ret
,
syslog
=
0
;
json_error_t
err
;
json_t
*
json_expressions
=
nullptr
;
...
...
tests/unit/list.cpp
View file @
a9e36b42
...
...
@@ -39,7 +39,7 @@ struct data {
TestSuite
(
list
,
.
description
=
"List datastructure"
);
Test
(
list
,
vlist_lookup
)
Test
(
list
,
vlist_lookup
_name
)
{
struct
vlist
l
;
...
...
@@ -56,7 +56,7 @@ Test(list, vlist_lookup)
vlist_push
(
&
l
,
d
);
}
struct
data
*
found
=
(
struct
data
*
)
vlist_lookup
(
&
l
,
"woman"
);
struct
data
*
found
=
vlist_lookup_name
<
struct
data
>
(
&
l
,
"woman"
);
cr_assert_eq
(
found
->
data
,
13
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment