atrri

Refractive indices varying with wavelength
git clone git://git.meso-star.fr/atrri.git
Log | Files | Refs | README | LICENSE

commit fb6bc1c7da39f0258f0f6413e33a04020e5a14c8
parent b74074bff5a8e11a9f9c827f110877e439b8a644
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Tue, 12 Jan 2021 15:48:00 +0100

Begin the implementation of AtrRI API

Diffstat:
Acmake/CMakeLists.txt | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/atrri.c | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/atrri.h | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/atrri_c.h | 32++++++++++++++++++++++++++++++++
Asrc/atrri_log.c | 125+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/atrri_log.h | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 505 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -0,0 +1,97 @@ +# Copyright (C) 2021 CNRS +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +cmake_minimum_required(VERSION 2.8) +project(atrri C) +enable_testing() + +set(ATRRI_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) +option(NO_TEST "Do not build tests" OFF) + +################################################################################ +# Check dependencies +################################################################################ +find_package(RCMake 0.4 REQUIRED) +find_package(RSys 0.10 REQUIRED) + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR}) +include(rcmake) +include(rcmake_runtime) + +include_directories(${RSys_INCLUDE_DIR}) + +################################################################################ +# Configure and define targets +################################################################################ +set(VERSION_MAJOR 0) +set(VERSION_MINOR 0) +set(VERSION_PATCH 0) +set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) + +set(ATRRI_FILES_SRC + atrri.c + atrri_log.c) +set(ATRRI_FILES_INC + atrri_c.h + atrri_log.h) +set(ATRRI_FILES_INC_API + atrri.h) + +set(ATRRI_FILES_DOC COPYING README.md) + +# Prepend each file in the `ATRRI_FILES_<SRC|INC>' list by `ATRRI_SOURCE_DIR' +rcmake_prepend_path(ATRRI_FILES_SRC ${ATRRI_SOURCE_DIR}) +rcmake_prepend_path(ATRRI_FILES_INC ${ATRRI_SOURCE_DIR}) +rcmake_prepend_path(ATRRI_FILES_INC_API ${ATRRI_SOURCE_DIR}) +rcmake_prepend_path(ATRRI_FILES_DOC ${PROJECT_SOURCE_DIR}/../) + +add_library(atrri SHARED ${ATRRI_FILES_SRC} ${ATRRI_FILES_INC} ${ATRRI_FILES_INC_API}) +target_link_libraries(atrri RSys) + +set_target_properties(atrri PROPERTIES + DEFINE_SYMBOL ATRRI_SHARED_BUILD + VERSION ${VERSION} + SOVERSION ${VERSION_MAJOR}) + +rcmake_setup_devel(atrri AtrRI ${VERSION} astoria/atrri_version.h) + +################################################################################ +# Add tests +################################################################################ +if(NOT NO_TEST) + function(build_test _name) + add_executable(${_name} ${ATRRI_SOURCE_DIR}/${_name}.c) + target_link_libraries(${_name} atrri RSys ${ARGN}) + endfunction() + + function(new_test _name) + build_test(${_name} ${ARGN}) + add_test(${_name} ${_name}) + endfunction() + + #new_test(test_atrri) + #new_test(test_atrri_load) +endif() + +################################################################################ +# Define output & install directories +################################################################################ +install(TARGETS atrri + ARCHIVE DESTINATION bin + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin) +install(FILES ${ATRRI_FILES_INC_API} DESTINATION include/astoria) +install(FILES ${ATRRI_FILES_DOC} DESTINATION share/doc/atrri) + diff --git a/src/atrri.c b/src/atrri.c @@ -0,0 +1,102 @@ +/* Copyright (C) 2021 CNRS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "atrri.h" +#include "atrri_c.h" +#include "atrri_log.h" + +/******************************************************************************* + * Local functions + ******************************************************************************/ +static void +release_atrri(ref_T* ref) +{ + struct atrri* atrri; + ASSERT(ref); + atrri = CONTAINER_OF(ref, struct atrri, ref); + if(atrri->logger == &atrri->logger__) logger_release(&atrri->logger__); + MEM_RM(atrri->allocator, atrri); +} + +/******************************************************************************* + * Exported functions + ******************************************************************************/ +res_T +atrri_create + (struct logger* logger, /* NULL <=> use default logger */ + struct mem_allocator* mem_allocator, /* NULL <=> use default allocator */ + const int verbose, /* Verbosity level */ + struct atrri** out_atrri) +{ + struct atrri* atrri = NULL; + struct mem_allocator* allocator = NULL; + res_T res = RES_OK; + + if(!out_atrri) { + res = RES_BAD_ARG; + goto error; + } + + allocator = mem_allocator ? mem_allocator : &mem_default_allocator; + atrri = MEM_CALLOC(allocator, 1, sizeof(*atrri)); + if(!atrri) { + if(verbose) { + #define ERR_STR "Could not allocate the AtrRI device.\n" + if(logger) { + logger_print(logger, LOG_ERROR, ERR_STR); + } else { + fprintf(stderr, MSG_ERROR_PREFIX ERR_STR); + } + #undef ERR_STR + } + res = RES_MEM_ERR; + goto error; + } + ref_init(&atrri->ref); + atrri->allocator = allocator; + atrri->verbose = verbose; + if(logger) { + atrri->logger = logger; + } else { + setup_log_default(atrri); + } + +exit: + if(out_atrri) *out_atrri = atrri; + return res; +error: + if(atrri) { + ATRRI(ref_put(atrri)); + atrri = NULL; + } + goto exit; +} + +res_T +atrri_ref_get(struct atrri* atrri) +{ + if(!atrri) return RES_BAD_ARG; + ref_get(&atrri->ref); + return RES_OK; +} + +res_T +atrri_ref_put(struct atrri* atrri) +{ + if(!atrri) return RES_BAD_ARG; + ref_put(&atrri->ref, release_atrri); + return RES_OK; +} + diff --git a/src/atrri.h b/src/atrri.h @@ -0,0 +1,80 @@ +/* Copyright (C) 2021 CNRS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef ATRRI_H +#define ATRRI_H + +#include <rsys/rsys.h> + +/* Library symbol management */ +#if defined(ATRRI_SHARED_BUILD) /* Build shared library */ + #define ATRRI_API extern EXPORT_SYM +#elif defined(ATRRI_STATIC) /* Use/build static library */ + #define ATRRI_API extern LOCAL_SYM +#else /* Use shared library */ + #define ATRRI_API extern IMPORT_SYM +#endif + +/* Helper macro that asserts if the invocation of the atrri function `Func' + * returns an error. One should use this macro on sth function calls for + * which no explicit error checking is performed */ +#ifndef NDEBUG + #define ATRRI(Func) ASSERT(atrri_ ## Func == RES_OK) +#else + #define ATRRI(Func) atrri_ ## Func +#endif + +/* Forward declaration of external data types */ +struct logger; +struct mem_allocator; + +/* Forware declaration of opaque data type */ +struct atrri; + +BEGIN_DECLS + +/******************************************************************************* + * AtrRI API + ******************************************************************************/ +ATRRI_API res_T +atrri_create + (struct logger* logger, /* NULL <=> use default logger */ + struct mem_allocator* allocator, /* NULL <=> use default allocator */ + const int verbose, /* Verbosity level */ + struct atrri** atrri); + +ATRRI_API res_T +atrri_ref_get + (struct atrri* atrri); + +ATRRI_API res_T +atrri_ref_put + (struct atrri* attrtp); + +ATRRI_API res_T +atrri_load + (struct atrri* atrri, + const char* path); + +ATRRI_API res_T +atrri_load_stream + (struct atrri* atrri, + FILE* stream, + const char* stream_name); /* Can be NULL */ + +END_DECLS + +#endif /* ATRRI_H */ + diff --git a/src/atrri_c.h b/src/atrri_c.h @@ -0,0 +1,32 @@ +/* Copyright (C) 2021 CNRS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef ATRRI_C_H +#define ATRRI_C_H + +#include <rsys/logger.h> +#include <rsys/ref_count.h> + +struct mem_allocator; + +struct atrri { + struct mem_allocator* allocator; + struct logger* logger; + struct logger logger__; /* Default logger */ + int verbose; + ref_T ref; +}; + +#endif /* ATRRI_C_H */ diff --git a/src/atrri_log.c b/src/atrri_log.c @@ -0,0 +1,125 @@ +/* Copyright (C) 2021 CNRS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "atrri_c.h" +#include "atrri_log.h" + +#include <rsys/cstr.h> +#include <rsys/logger.h> + +#include <stdarg.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE void +log_msg + (const struct atrri* atrri, + const enum log_type stream, + const char* msg, + va_list vargs) +{ + ASSERT(atrri && msg); + if(atrri->verbose) { + res_T res; (void)res; + res = logger_vprint(atrri->logger, stream, msg, vargs); + ASSERT(res == RES_OK); + } +} + +static void +print_info(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_INFO_PREFIX"%s", msg); +} + +static void +print_err(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_ERROR_PREFIX"%s", msg); +} + +static void +print_warn(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_WARNING_PREFIX"%s", msg); +} + +/******************************************************************************* + * Local functions + ******************************************************************************/ +res_T +setup_log_default(struct atrri* atrri) +{ + res_T res = RES_OK; + ASSERT(atrri); + + res = logger_init(atrri->allocator, &atrri->logger__); + if(res != RES_OK) { + if(atrri->verbose) { + fprintf(stderr, + MSG_ERROR_PREFIX + "Could not setup the AtrRI default logger -- %s.\n", + res_to_cstr(res)); + } + goto error; + } + logger_set_stream(&atrri->logger__, LOG_OUTPUT, print_info, NULL); + logger_set_stream(&atrri->logger__, LOG_ERROR, print_err, NULL); + logger_set_stream(&atrri->logger__, LOG_WARNING, print_warn, NULL); + atrri->logger = &atrri->logger__; + +exit: + return res; +error: + goto exit; +} + +void +log_info(const struct atrri* atrri, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(atrri && msg); + + va_start(vargs_list, msg); + log_msg(atrri, LOG_OUTPUT, msg, vargs_list); + va_end(vargs_list); +} + +void +log_err(const struct atrri* atrri, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(atrri && msg); + + va_start(vargs_list, msg); + log_msg(atrri, LOG_ERROR, msg, vargs_list); + va_end(vargs_list); +} + +void +log_warn(const struct atrri* atrri, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(atrri && msg); + + va_start(vargs_list, msg); + log_msg(atrri, LOG_WARNING, msg, vargs_list); + va_end(vargs_list); +} + diff --git a/src/atrri_log.h b/src/atrri_log.h @@ -0,0 +1,69 @@ +/* Copyright (C) 2021 CNRS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef ATRRI_LOG_H +#define ATRRI_LOG_H + +#include <rsys/rsys.h> + +#define MSG_INFO_PREFIX "AtrRI:\x1b[1m\x1b[32minfo\x1b[0m: " +#define MSG_ERROR_PREFIX "AtrRI:\x1b[1m\x1b[31merror\x1b[0m: " +#define MSG_WARNING_PREFIX "AtrRI:\x1b[1m\x1b[33mwarning\x1b[0m: " + +struct atrri; +struct logger; + +extern LOCAL_SYM res_T +setup_log_default + (struct atrri* atrri); + +/* Conditionally log a message on the LOG_OUTPUT stream of the atrri logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_info + (const struct atrri* atrri, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_ERROR stream of the atrri logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_err + (const struct atrri* atrri, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_WARNING stream of the atrri logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_warn + (const struct atrri* atrri, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +#endif /* ATRRI_LOG_H */ +