Skip to content

Commit

Permalink
Add base for exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Feb 26, 2021
1 parent 44a43d6 commit 5deea0c
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 0 deletions.
52 changes: 52 additions & 0 deletions source/reflect/include/reflect/reflect_exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Reflect Library by Parra Studios
* A library for provide reflection and metadata representation.
*
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <vic798@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef REFLECT_EXCEPTION_H
#define REFLECT_EXCEPTION_H 1

#include <reflect/reflect_api.h>

#ifdef __cplusplus
extern "C" {
#endif

struct exception_type;

typedef struct exception_type * exception;

REFLECT_API exception exception_create(const char * message, const char * label, int code, const char * stacktrace);

REFLECT_API const char * exception_message(exception ex);

REFLECT_API const char * exception_label(exception ex);

REFLECT_API int exception_code(exception ex);

REFLECT_API const char * exception_stacktrace(exception ex);

REFLECT_API int exception_thrown(exception ex);

REFLECT_API void exception_destroy(exception ex);

#ifdef __cplusplus
}
#endif

#endif /* REFLECT_EXCEPTION_H */
126 changes: 126 additions & 0 deletions source/reflect/source/reflect_exception.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Reflect Library by Parra Studios
* A library for provide reflection and metadata representation.
*
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <vic798@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <reflect/reflect_exception.h>

#include <threading/threading_thread_id.h>

#include <log/log.h>

#include <stdlib.h>
#include <string.h>

struct exception_type
{
char * message;
char * label;
int code;
char * stacktrace;
uint64_t id;
};

exception exception_create(const char * message, const char * label, int code, const char * stacktrace)
{
exception ex = malloc(sizeof(struct exception_type));

if (ex == NULL)
{
return NULL;
}

// TODO: Copy

ex->id = thread_id_get_current();

return ex;
}

const char * exception_message(exception ex)
{
if (ex == NULL)
{
return NULL;
}

return ex->message;
}

const char * exception_label(exception ex)
{
if (ex == NULL)
{
return NULL;
}

return ex->label;
}

int exception_code(exception ex)
{
if (ex == NULL)
{
return 0;
}

return ex->code;
}

const char * exception_stacktrace(exception ex)
{
if (ex == NULL)
{
return NULL;
}

return ex->stacktrace;
}

int exception_thrown(exception ex)
{
if (ex == NULL)
{
return 1;
}

return ex->thrown;
}

void exception_destroy(exception ex)
{
if (ex != NULL)
{
if (ex->message != NULL)
{
free(ex->message);
}

if (ex->label != NULL)
{
free(ex->label);
}

if (ex->stacktrace != NULL)
{
free(ex->stacktrace);
}

free(ex);
}
}

0 comments on commit 5deea0c

Please sign in to comment.