summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2017-03-30 20:35:16 +0200
committerSébastien Helleu <flashcode@flashtux.org>2017-03-30 20:35:16 +0200
commit467f482ea6653127d49ea7b6c487f9d51929d232 (patch)
treeb32bdd6776078c84adf5ac5bf193d649e26ba7c0
parent0436fff31b5ef7814391dd1a666cccfaa0f2ff67 (diff)
downloadweechat-467f482ea6653127d49ea7b6c487f9d51929d232.zip
core: make "callback_cmp" optional in call to function arraylist_new()
If no callback is given, a default callback is used, which just compares pointers.
-rw-r--r--doc/en/weechat_plugin_api.en.adoc3
-rw-r--r--doc/fr/weechat_plugin_api.fr.adoc4
-rw-r--r--doc/it/weechat_plugin_api.it.adoc3
-rw-r--r--doc/ja/weechat_plugin_api.ja.adoc4
-rw-r--r--src/core/wee-arraylist.c33
-rw-r--r--tests/unit/core/test-arraylist.cpp2
6 files changed, 39 insertions, 10 deletions
diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc
index 08978f590..d4e1e9630 100644
--- a/doc/en/weechat_plugin_api.en.adoc
+++ b/doc/en/weechat_plugin_api.en.adoc
@@ -3875,7 +3875,8 @@ Arguments:
* _sorted_: 1 to sort the array list, 0 for no sort
* _allow_duplicates_: 1 to allow duplicate entries, 0 to prevent a same entry
to be added again
-* _callback_cmp_: callback used to compare two items, arguments and return value:
+* _callback_cmp_: callback used to compare two items (optional), arguments and
+ return value:
** _void *data_: pointer
** _struct t_arraylist *arraylist_: array list pointer
** _void *pointer1_: pointer to first item
diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc
index 52d677356..a2fea8a0d 100644
--- a/doc/fr/weechat_plugin_api.fr.adoc
+++ b/doc/fr/weechat_plugin_api.fr.adoc
@@ -3937,8 +3937,8 @@ Paramètres :
* _sorted_ : 1 pour trier la liste avec tableau, 0 pour ne pas trier
* _allow_duplicates_ : 1 pour autoriser les entrées dupliquées, 0 pour empêcher
une même entrée d'être ajoutée à nouveau
-* _callback_cmp_ : fonction appelée pour comparer deux éléments, paramètres et
- valeur de retour :
+* _callback_cmp_ : fonction appelée pour comparer deux éléments (optionnelle),
+ paramètres et valeur de retour :
** _void *data_ : pointeur
** _struct t_arraylist *arraylist_ : pointeur vers la liste avec tableau
** _void *pointer1_ : pointeur vers le premier élément
diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc
index a44ba714f..31e610f08 100644
--- a/doc/it/weechat_plugin_api.it.adoc
+++ b/doc/it/weechat_plugin_api.it.adoc
@@ -4006,7 +4006,8 @@ Argomenti:
* _sorted_: 1 to sort the array list, 0 for no sort
* _allow_duplicates_: 1 to allow duplicate entries, 0 to prevent a same entry
to be added again
-* _callback_cmp_: callback used to compare two items, arguments and return value:
+* _callback_cmp_: callback used to compare two items (optional), arguments and
+ return value:
** _void *data_: pointer
** _struct t_arraylist *arraylist_: array list pointer
** _void *pointer1_: pointer to first item
diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc
index 923d8611b..12e89b394 100644
--- a/doc/ja/weechat_plugin_api.ja.adoc
+++ b/doc/ja/weechat_plugin_api.ja.adoc
@@ -3882,7 +3882,9 @@ struct t_arraylist *weechat_arraylist_new (int initial_size,
* _sorted_: 1 の場合には配列リストをソートし、0 の場合にはソートしません
* _allow_duplicates_: 1 の場合にはエントリの重複を許可し、0
の場合にはエントリが重複して追加されることを防ぎます
-* _callback_cmp_: 2 つの要素を比較する際に使われるコールバック、引数と戻り値は以下:
+// TRANSLATION MISSING
+* _callback_cmp_: callback used to compare two items (optional), arguments and
+ return value:
** _void *data_: ポインタ
** _struct t_arraylist *arraylist_: 配列リストポインタ
** _void *pointer1_: 1 番目の要素へのポインタ
diff --git a/src/core/wee-arraylist.c b/src/core/wee-arraylist.c
index 991e21a1e..df98e66a1 100644
--- a/src/core/wee-arraylist.c
+++ b/src/core/wee-arraylist.c
@@ -33,6 +33,31 @@
/*
+ * Compares two arraylist entries (default comparator).
+ * It just compares pointers.
+ *
+ * Returns:
+ * -1: pointer1 < pointer2
+ * 0: pointer1 == pointer2
+ * 1: pointer1 > pointer2
+ */
+
+int
+arraylist_cmp_default_cb (void *data, struct t_arraylist *arraylist,
+ void *pointer1, void *pointer2)
+{
+ /* make C compiler happy */
+ (void) data;
+ (void) arraylist;
+
+ if (pointer1 < pointer2)
+ return -1;
+ if (pointer1 > pointer2)
+ return 1;
+ return 0;
+}
+
+/*
* Creates a new arraylist.
*
* Returns pointer to arraylist, NULL if error.
@@ -48,7 +73,7 @@ arraylist_new (int initial_size,
struct t_arraylist *new_arraylist;
/* check arguments */
- if ((initial_size < 0) || !callback_cmp)
+ if (initial_size < 0)
return NULL;
new_arraylist = malloc (sizeof (*new_arraylist));
@@ -76,8 +101,10 @@ arraylist_new (int initial_size,
}
new_arraylist->sorted = sorted;
new_arraylist->allow_duplicates = allow_duplicates;
- new_arraylist->callback_cmp = callback_cmp;
- new_arraylist->callback_cmp_data = callback_cmp_data;
+ new_arraylist->callback_cmp = (callback_cmp) ?
+ callback_cmp : &arraylist_cmp_default_cb;
+ new_arraylist->callback_cmp_data = (callback_cmp) ?
+ callback_cmp_data : NULL;
new_arraylist->callback_free = callback_free;
new_arraylist->callback_free_data = callback_free_data;
diff --git a/tests/unit/core/test-arraylist.cpp b/tests/unit/core/test-arraylist.cpp
index 5c018b02b..a0350d611 100644
--- a/tests/unit/core/test-arraylist.cpp
+++ b/tests/unit/core/test-arraylist.cpp
@@ -489,8 +489,6 @@ TEST(Arraylist, New)
arraylist_new (-1, 0, 0, NULL, NULL, NULL, NULL));
POINTERS_EQUAL(NULL,
arraylist_new (-1, 0, 0, &test_cmp_cb, NULL, NULL, NULL));
- POINTERS_EQUAL(NULL,
- arraylist_new (0, 0, 0, NULL, NULL, NULL, NULL));
/* tests on arraylists */
for (initial_size = 0; initial_size < 2; initial_size++)