Coverage Report

Created: 2026-10-24 08:16

/work/toxcore/events/group_self_join.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2023-2026 The TokTok team.
3
 */
4
5
#include "events_alloc.h"
6
7
#include <assert.h>
8
9
#include "../attributes.h"
10
#include "../bin_pack.h"
11
#include "../bin_unpack.h"
12
#include "../ccompat.h"
13
#include "../mem.h"
14
#include "../tox.h"
15
#include "../tox_event.h"
16
#include "../tox_events.h"
17
#include "../tox_struct.h"
18
19
/*****************************************************
20
 *
21
 * :: struct and accessors
22
 *
23
 *****************************************************/
24
25
struct Tox_Event_Group_Self_Join {
26
    uint32_t group_number;
27
};
28
29
static void tox_event_group_self_join_set_group_number(Tox_Event_Group_Self_Join *_Nonnull group_self_join, uint32_t group_number)
30
22
{
31
22
    assert(group_self_join != nullptr);
32
22
    group_self_join->group_number = group_number;
33
22
}
34
uint32_t tox_event_group_self_join_get_group_number(const Tox_Event_Group_Self_Join *group_self_join)
35
6
{
36
6
    assert(group_self_join != nullptr);
37
6
    return group_self_join->group_number;
38
6
}
39
40
static void tox_event_group_self_join_construct(Tox_Event_Group_Self_Join *_Nonnull group_self_join)
41
64
{
42
64
    *group_self_join = (Tox_Event_Group_Self_Join) {
43
64
        0
44
64
    };
45
64
}
46
static void tox_event_group_self_join_destruct(Tox_Event_Group_Self_Join *_Nonnull group_self_join, const Memory *_Nonnull mem)
47
64
{
48
64
    return;
49
64
}
50
51
bool tox_event_group_self_join_pack(
52
    const Tox_Event_Group_Self_Join *event, Bin_Pack *bp)
53
38
{
54
38
    return bin_pack_u32(bp, event->group_number);
55
38
}
56
57
static bool tox_event_group_self_join_unpack_into(Tox_Event_Group_Self_Join *_Nonnull event, Bin_Unpack *_Nonnull bu)
58
42
{
59
42
    assert(event != nullptr);
60
42
    return bin_unpack_u32(bu, &event->group_number);
61
42
}
62
63
/*****************************************************
64
 *
65
 * :: new/free/add/get/size/unpack
66
 *
67
 *****************************************************/
68
69
const Tox_Event_Group_Self_Join *tox_event_get_group_self_join(const Tox_Event *event)
70
0
{
71
0
    return event->type == TOX_EVENT_GROUP_SELF_JOIN ? event->data.group_self_join : nullptr;
72
0
}
73
74
Tox_Event_Group_Self_Join *tox_event_group_self_join_new(const Memory *mem)
75
65
{
76
65
    Tox_Event_Group_Self_Join *const group_self_join =
77
65
        (Tox_Event_Group_Self_Join *)mem_alloc(mem, sizeof(Tox_Event_Group_Self_Join));
78
79
65
    if (group_self_join == nullptr) {
80
1
        return nullptr;
81
1
    }
82
83
64
    tox_event_group_self_join_construct(group_self_join);
84
64
    return group_self_join;
85
65
}
86
87
void tox_event_group_self_join_free(Tox_Event_Group_Self_Join *group_self_join, const Memory *mem)
88
65
{
89
65
    if (group_self_join != nullptr) {
90
64
        tox_event_group_self_join_destruct(group_self_join, mem);
91
64
    }
92
65
    mem_delete(mem, group_self_join);
93
65
}
94
95
static Tox_Event_Group_Self_Join *_Nullable tox_events_add_group_self_join(Tox_Events *_Nonnull events, const Memory *_Nonnull mem)
96
22
{
97
22
    Tox_Event_Group_Self_Join *const group_self_join = tox_event_group_self_join_new(mem);
98
99
22
    if (group_self_join == nullptr) {
100
0
        return nullptr;
101
0
    }
102
103
22
    Tox_Event event;
104
22
    event.type = TOX_EVENT_GROUP_SELF_JOIN;
105
22
    event.data.group_self_join = group_self_join;
106
107
22
    if (!tox_events_add(events, &event)) {
108
0
        tox_event_group_self_join_free(group_self_join, mem);
109
0
        return nullptr;
110
0
    }
111
22
    return group_self_join;
112
22
}
113
114
bool tox_event_group_self_join_unpack(
115
    Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem)
116
43
{
117
43
    assert(event != nullptr);
118
43
    assert(*event == nullptr);
119
43
    *event = tox_event_group_self_join_new(mem);
120
121
43
    if (*event == nullptr) {
122
1
        return false;
123
1
    }
124
125
42
    return tox_event_group_self_join_unpack_into(*event, bu);
126
43
}
127
128
static Tox_Event_Group_Self_Join *_Nullable tox_event_group_self_join_alloc(Tox_Events_State *_Nonnull state)
129
22
{
130
22
    if (state->events == nullptr) {
131
0
        return nullptr;
132
0
    }
133
134
22
    Tox_Event_Group_Self_Join *group_self_join = tox_events_add_group_self_join(state->events, state->mem);
135
136
22
    if (group_self_join == nullptr) {
137
0
        state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
138
0
        return nullptr;
139
0
    }
140
141
22
    return group_self_join;
142
22
}
143
144
/*****************************************************
145
 *
146
 * :: event handler
147
 *
148
 *****************************************************/
149
150
void tox_events_handle_group_self_join(
151
    Tox *_Nonnull tox,
152
    uint32_t group_number,
153
    void *_Nullable user_data)
154
22
{
155
22
    Tox_Events_State *state = tox_events_alloc(user_data);
156
22
    Tox_Event_Group_Self_Join *group_self_join = tox_event_group_self_join_alloc(state);
157
158
22
    if (group_self_join == nullptr) {
159
0
        return;
160
0
    }
161
162
22
    tox_event_group_self_join_set_group_number(group_self_join, group_number);
163
22
}
164
165
void tox_events_handle_group_self_join_dispatch(Tox *tox, const Tox_Event_Group_Self_Join *event, void *user_data)
166
0
{
167
0
    if (tox->group_self_join_callback == nullptr) {
168
0
        return;
169
0
    }
170
171
0
    tox_unlock(tox);
172
0
    tox->group_self_join_callback(tox, event->group_number, user_data);
173
0
    tox_lock(tox);
174
0
}