Coverage Report

Created: 2025-10-08 19:34

/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-2025 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
18
/*****************************************************
19
 *
20
 * :: struct and accessors
21
 *
22
 *****************************************************/
23
24
struct Tox_Event_Group_Self_Join {
25
    uint32_t group_number;
26
};
27
28
static void tox_event_group_self_join_set_group_number(Tox_Event_Group_Self_Join *_Nonnull group_self_join, uint32_t group_number)
29
100
{
30
100
    assert(group_self_join != nullptr);
31
100
    group_self_join->group_number = group_number;
32
100
}
33
uint32_t tox_event_group_self_join_get_group_number(const Tox_Event_Group_Self_Join *group_self_join)
34
2
{
35
2
    assert(group_self_join != nullptr);
36
2
    return group_self_join->group_number;
37
2
}
38
39
static void tox_event_group_self_join_construct(Tox_Event_Group_Self_Join *_Nonnull group_self_join)
40
150
{
41
150
    *group_self_join = (Tox_Event_Group_Self_Join) {
42
150
        0
43
150
    };
44
150
}
45
static void tox_event_group_self_join_destruct(Tox_Event_Group_Self_Join *_Nonnull group_self_join, const Memory *_Nonnull mem)
46
150
{
47
150
    return;
48
150
}
49
50
bool tox_event_group_self_join_pack(
51
    const Tox_Event_Group_Self_Join *event, Bin_Pack *bp)
52
38
{
53
38
    return bin_pack_u32(bp, event->group_number);
54
38
}
55
56
static bool tox_event_group_self_join_unpack_into(Tox_Event_Group_Self_Join *_Nonnull event, Bin_Unpack *_Nonnull bu)
57
49
{
58
49
    assert(event != nullptr);
59
49
    return bin_unpack_u32(bu, &event->group_number);
60
49
}
61
62
/*****************************************************
63
 *
64
 * :: new/free/add/get/size/unpack
65
 *
66
 *****************************************************/
67
68
const Tox_Event_Group_Self_Join *tox_event_get_group_self_join(const Tox_Event *event)
69
0
{
70
0
    return event->type == TOX_EVENT_GROUP_SELF_JOIN ? event->data.group_self_join : nullptr;
71
0
}
72
73
Tox_Event_Group_Self_Join *tox_event_group_self_join_new(const Memory *mem)
74
152
{
75
152
    Tox_Event_Group_Self_Join *const group_self_join =
76
152
        (Tox_Event_Group_Self_Join *)mem_alloc(mem, sizeof(Tox_Event_Group_Self_Join));
77
78
152
    if (group_self_join == nullptr) {
79
2
        return nullptr;
80
2
    }
81
82
150
    tox_event_group_self_join_construct(group_self_join);
83
150
    return group_self_join;
84
152
}
85
86
void tox_event_group_self_join_free(Tox_Event_Group_Self_Join *group_self_join, const Memory *mem)
87
151
{
88
151
    if (group_self_join != nullptr) {
89
150
        tox_event_group_self_join_destruct(group_self_join, mem);
90
150
    }
91
151
    mem_delete(mem, group_self_join);
92
151
}
93
94
static Tox_Event_Group_Self_Join *tox_events_add_group_self_join(Tox_Events *_Nonnull events, const Memory *_Nonnull mem)
95
102
{
96
102
    Tox_Event_Group_Self_Join *const group_self_join = tox_event_group_self_join_new(mem);
97
98
102
    if (group_self_join == nullptr) {
99
1
        return nullptr;
100
1
    }
101
102
101
    Tox_Event event;
103
101
    event.type = TOX_EVENT_GROUP_SELF_JOIN;
104
101
    event.data.group_self_join = group_self_join;
105
106
101
    if (!tox_events_add(events, &event)) {
107
1
        tox_event_group_self_join_free(group_self_join, mem);
108
1
        return nullptr;
109
1
    }
110
100
    return group_self_join;
111
101
}
112
113
bool tox_event_group_self_join_unpack(
114
    Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem)
115
50
{
116
50
    assert(event != nullptr);
117
50
    assert(*event == nullptr);
118
50
    *event = tox_event_group_self_join_new(mem);
119
120
50
    if (*event == nullptr) {
121
1
        return false;
122
1
    }
123
124
49
    return tox_event_group_self_join_unpack_into(*event, bu);
125
50
}
126
127
static Tox_Event_Group_Self_Join *tox_event_group_self_join_alloc(void *_Nonnull user_data)
128
103
{
129
103
    Tox_Events_State *state = tox_events_alloc(user_data);
130
103
    assert(state != nullptr);
131
132
103
    if (state->events == nullptr) {
133
1
        return nullptr;
134
1
    }
135
136
102
    Tox_Event_Group_Self_Join *group_self_join = tox_events_add_group_self_join(state->events, state->mem);
137
138
102
    if (group_self_join == nullptr) {
139
2
        state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
140
2
        return nullptr;
141
2
    }
142
143
100
    return group_self_join;
144
102
}
145
146
/*****************************************************
147
 *
148
 * :: event handler
149
 *
150
 *****************************************************/
151
152
void tox_events_handle_group_self_join(
153
    Tox *tox, uint32_t group_number,
154
    void *user_data)
155
103
{
156
103
    Tox_Event_Group_Self_Join *group_self_join = tox_event_group_self_join_alloc(user_data);
157
158
103
    if (group_self_join == nullptr) {
159
3
        return;
160
3
    }
161
162
100
    tox_event_group_self_join_set_group_number(group_self_join, group_number);
163
100
}