Skip to content

Commit 5deea0c

Browse files
committed
Add base for exceptions.
1 parent 44a43d6 commit 5deea0c

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Reflect Library by Parra Studios
3+
* A library for provide reflection and metadata representation.
4+
*
5+
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#ifndef REFLECT_EXCEPTION_H
22+
#define REFLECT_EXCEPTION_H 1
23+
24+
#include <reflect/reflect_api.h>
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
struct exception_type;
31+
32+
typedef struct exception_type * exception;
33+
34+
REFLECT_API exception exception_create(const char * message, const char * label, int code, const char * stacktrace);
35+
36+
REFLECT_API const char * exception_message(exception ex);
37+
38+
REFLECT_API const char * exception_label(exception ex);
39+
40+
REFLECT_API int exception_code(exception ex);
41+
42+
REFLECT_API const char * exception_stacktrace(exception ex);
43+
44+
REFLECT_API int exception_thrown(exception ex);
45+
46+
REFLECT_API void exception_destroy(exception ex);
47+
48+
#ifdef __cplusplus
49+
}
50+
#endif
51+
52+
#endif /* REFLECT_EXCEPTION_H */
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Reflect Library by Parra Studios
3+
* A library for provide reflection and metadata representation.
4+
*
5+
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include <reflect/reflect_exception.h>
22+
23+
#include <threading/threading_thread_id.h>
24+
25+
#include <log/log.h>
26+
27+
#include <stdlib.h>
28+
#include <string.h>
29+
30+
struct exception_type
31+
{
32+
char * message;
33+
char * label;
34+
int code;
35+
char * stacktrace;
36+
uint64_t id;
37+
};
38+
39+
exception exception_create(const char * message, const char * label, int code, const char * stacktrace)
40+
{
41+
exception ex = malloc(sizeof(struct exception_type));
42+
43+
if (ex == NULL)
44+
{
45+
return NULL;
46+
}
47+
48+
// TODO: Copy
49+
50+
ex->id = thread_id_get_current();
51+
52+
return ex;
53+
}
54+
55+
const char * exception_message(exception ex)
56+
{
57+
if (ex == NULL)
58+
{
59+
return NULL;
60+
}
61+
62+
return ex->message;
63+
}
64+
65+
const char * exception_label(exception ex)
66+
{
67+
if (ex == NULL)
68+
{
69+
return NULL;
70+
}
71+
72+
return ex->label;
73+
}
74+
75+
int exception_code(exception ex)
76+
{
77+
if (ex == NULL)
78+
{
79+
return 0;
80+
}
81+
82+
return ex->code;
83+
}
84+
85+
const char * exception_stacktrace(exception ex)
86+
{
87+
if (ex == NULL)
88+
{
89+
return NULL;
90+
}
91+
92+
return ex->stacktrace;
93+
}
94+
95+
int exception_thrown(exception ex)
96+
{
97+
if (ex == NULL)
98+
{
99+
return 1;
100+
}
101+
102+
return ex->thrown;
103+
}
104+
105+
void exception_destroy(exception ex)
106+
{
107+
if (ex != NULL)
108+
{
109+
if (ex->message != NULL)
110+
{
111+
free(ex->message);
112+
}
113+
114+
if (ex->label != NULL)
115+
{
116+
free(ex->label);
117+
}
118+
119+
if (ex->stacktrace != NULL)
120+
{
121+
free(ex->stacktrace);
122+
}
123+
124+
free(ex);
125+
}
126+
}

0 commit comments

Comments
 (0)