Line data Source code
1 : /** Copyright 2025 Alexander G. Lopez
2 :
3 : Licensed under the Apache License, Version 2.0 (the "License");
4 : you may not use this file except in compliance with the License.
5 : You may obtain a copy of the License at
6 :
7 : http://www.apache.org/licenses/LICENSE-2.0
8 :
9 : Unless required by applicable law or agreed to in writing, software
10 : distributed under the License is distributed on an "AS IS" BASIS,
11 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : See the License for the specific language governing permissions and
13 : limitations under the License. */
14 : /** C23 provided headers. */
15 : #include <stddef.h>
16 :
17 : /** CCC provided headers. */
18 : #include "ccc/types.h"
19 :
20 : /** @internal */
21 : static char const *const result_messages[CCC_PRIVATE_RESULT_COUNT] = {
22 : [CCC_RESULT_OK] = "",
23 : [CCC_RESULT_FAIL]
24 : = "An operation ran on a container but the desired result could not be "
25 : "returned, meaning no valid value can be given to the user.",
26 : [CCC_RESULT_NO_ALLOCATION_FUNCTION]
27 : = "A container performed an operation requiring new allocation of "
28 : "memory, but no allocation function was provided upon initialization.",
29 : [CCC_RESULT_ALLOCATOR_ERROR]
30 : = "A container performed an operation requiring new allocation of memory, "
31 : "but the allocator function provided on initialization failed.",
32 : [CCC_RESULT_ARGUMENT_ERROR]
33 : = "A container function received bad arguments such as NULL pointers, out "
34 : "of range values, or arguments that cannot be processed in the context "
35 : "of an operation.",
36 : };
37 :
38 : /*============================ Interface ===============================*/
39 :
40 : CCC_Tribool
41 10875 : CCC_entry_occupied(CCC_Entry const *const entry) {
42 10875 : if (!entry) {
43 1 : return CCC_TRIBOOL_ERROR;
44 : }
45 10874 : return (entry->status & CCC_ENTRY_OCCUPIED) != 0;
46 10875 : }
47 :
48 : CCC_Tribool
49 1587 : CCC_entry_insert_error(CCC_Entry const *const entry) {
50 1587 : if (!entry) {
51 1 : return CCC_TRIBOOL_ERROR;
52 : }
53 1586 : return (entry->status & CCC_ENTRY_INSERT_ERROR) != 0;
54 1587 : }
55 :
56 : CCC_Tribool
57 2 : CCC_entry_argument_error(CCC_Entry const *const entry) {
58 2 : if (!entry) {
59 1 : return CCC_TRIBOOL_ERROR;
60 : }
61 1 : return (entry->status & CCC_ENTRY_ARGUMENT_ERROR) != 0;
62 2 : }
63 :
64 : void *
65 7453 : CCC_entry_unwrap(CCC_Entry const *const entry) {
66 7453 : if (!entry || (entry->status & CCC_ENTRY_NO_UNWRAP)
67 7453 : || (entry->status & CCC_ENTRY_INSERT_ERROR)) {
68 10 : return NULL;
69 : }
70 7443 : return entry->type;
71 7453 : }
72 :
73 : CCC_Tribool
74 7806 : CCC_handle_occupied(CCC_Handle const *const handle) {
75 7806 : if (!handle) {
76 2 : return CCC_TRIBOOL_ERROR;
77 : }
78 7804 : return (handle->status & CCC_ENTRY_OCCUPIED) != 0;
79 7806 : }
80 :
81 : CCC_Tribool
82 2928 : CCC_handle_insert_error(CCC_Handle const *const handle) {
83 2928 : if (!handle) {
84 2 : return CCC_TRIBOOL_ERROR;
85 : }
86 2926 : return (handle->status & CCC_ENTRY_INSERT_ERROR) != 0;
87 2928 : }
88 :
89 : CCC_Tribool
90 4 : CCC_handle_argument_error(CCC_Handle const *const handle) {
91 4 : if (!handle) {
92 2 : return CCC_TRIBOOL_ERROR;
93 : }
94 2 : return (handle->status & CCC_ENTRY_ARGUMENT_ERROR) != 0;
95 4 : }
96 :
97 : CCC_Handle_index
98 675 : CCC_handle_unwrap(CCC_Handle const *const handle) {
99 675 : if (!handle) {
100 1 : return 0;
101 : }
102 674 : return handle->status & CCC_ENTRY_NO_UNWRAP ? 0 : handle->index;
103 675 : }
104 :
105 : void *
106 28 : CCC_range_begin(CCC_Range const *const range) {
107 28 : return range ? range->begin : NULL;
108 : }
109 :
110 : void *
111 64 : CCC_range_end(CCC_Range const *const range) {
112 64 : return range ? range->end : NULL;
113 : }
114 :
115 : void *
116 28 : CCC_range_reverse_begin(CCC_Range_reverse const *const range) {
117 28 : return range ? range->reverse_begin : NULL;
118 : }
119 :
120 : void *
121 68 : CCC_range_reverse_end(CCC_Range_reverse const *const range) {
122 68 : return range ? range->reverse_end : NULL;
123 : }
124 :
125 : CCC_Handle_index
126 18 : CCC_handle_range_begin(CCC_Handle_range const *const range) {
127 18 : return range ? range->begin : 0;
128 : }
129 :
130 : CCC_Handle_index
131 56 : CCC_handle_range_end(CCC_Handle_range const *const range) {
132 56 : return range ? range->end : 0;
133 : }
134 :
135 : CCC_Handle_index
136 18 : CCC_handle_range_reverse_begin(CCC_Handle_range_reverse const *const range) {
137 18 : return range ? range->reverse_begin : 0;
138 : }
139 :
140 : CCC_Handle_index
141 60 : CCC_handle_range_reverse_end(CCC_Handle_range_reverse const *const range) {
142 60 : return range ? range->reverse_end : 0;
143 : }
144 :
145 : char const *
146 2 : CCC_result_message(CCC_Result const result) {
147 2 : if (result >= CCC_PRIVATE_RESULT_COUNT) {
148 1 : return "error: invalid result provided no message exists";
149 : }
150 1 : return result_messages[result];
151 2 : }
152 :
153 : CCC_Entry_status
154 55 : CCC_entry_status(CCC_Entry const *entry) {
155 55 : if (!entry) {
156 1 : return CCC_ENTRY_ARGUMENT_ERROR;
157 : }
158 54 : return entry->status;
159 55 : }
160 :
161 : CCC_Handle_status
162 27 : CCC_handle_status(CCC_Handle const *handle) {
163 27 : if (!handle) {
164 2 : return CCC_ENTRY_ARGUMENT_ERROR;
165 : }
166 25 : return handle->status;
167 27 : }
168 :
169 : char const *
170 6 : CCC_handle_status_message(CCC_Handle_status const status) {
171 6 : return CCC_entry_status_message(status);
172 : }
173 :
174 : char const *
175 12 : CCC_entry_status_message(CCC_Entry_status const status) {
176 12 : switch (status) {
177 : case CCC_ENTRY_VACANT:
178 2 : return "vacant with no errors";
179 : break;
180 : case CCC_ENTRY_OCCUPIED:
181 2 : return "occupied and non-NULL with no errors";
182 : break;
183 : case CCC_ENTRY_INSERT_ERROR:
184 2 : return "insert error has occurred or will occur on next insert";
185 : break;
186 : case CCC_ENTRY_ARGUMENT_ERROR:
187 2 : return "could not proceed due to bad arguments to a function";
188 : break;
189 : case CCC_ENTRY_NO_UNWRAP:
190 2 : return "unwrap prohibited in order to protect container integrity";
191 : break;
192 : default:
193 2 : return "error: encountered an unknown combination of entry/handle "
194 : "flags";
195 : break;
196 : }
197 12 : }
|