libBLOC
Barry Initial commit b3106d3 (2 years, 11 months ago)
diff --git a/list/list.c b/list/list.c
new file mode 100644
index 0000000..888ff12
--- /dev/null
+++ b/list/list.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+/*
+ * This file implements object Lists. Objects can be a part of multiple lists
+ * that can be managed automatically by the library. This prevents subsystems
+ * from having to implement their own object lists for sub-objects. Lists may
+ * be created with a specific Object Type, and will not accept objects that are
+ * not of that type. If a list does not have a type, it will accept objects of
+ * any type. Lists are very little more than the default Iterable object.
+ */
+
+#include <BLOC/object.h>
+#include <BLOC/list.h>
+#include "../assert.h"
+#include "../object.h"
+#include "list.h"
+
+static void _list_new(void *);
+static void _list_delete(void *);
+
+/* List object type */
+struct ObjectType listType = {
+ .name = "List",
+ .size = sizeof(List),
+ .new = _list_new,
+ .delete = _list_delete,
+};
+
+/* Create a List object */
+static void
+_list_new(void *obj)
+{
+ List *list = obj;
+ iterable_init(&list->header);
+}
+
+/* Destroy a List object */
+static void
+_list_delete(void *obj)
+{
+ List *list = obj;
+ struct IterableEntry *entry, *next;
+ for (entry = list->header.start; entry; entry = next) {
+ next = entry->next;
+ obj_put(entry->obj);
+ free(entry);
+ }
+}
+
+/* Create an object list */
+List *
+list_create(struct ObjectType *type)
+{
+ List *list = obj_new(&listType);
+ list->type = type;
+ return list;
+}
+
+/* Count the entries in a list */
+unsigned int
+list_count(List *list)
+{
+ return list->header.entries;
+}