tools

various tools
git clone git://deadbeef.fr/tools.git
Log | Files | Refs | README | LICENSE

commit 409b0d2605e1a2ebf26a3b8e298a3116f0087eab
parent 577223ec046fa94c1c468ad7b5bea977114a625c
Author: Morel BĂ©renger <berengermorel76@gmail.com>
Date:   Fri, 26 Jun 2020 15:08:42 +0200

log.hpp => utils.hpp + improve empty_array

Diffstat:
Dsrc/log.hpp | 41-----------------------------------------
Msrc/optparser.cpp | 2+-
Asrc/utils.hpp | 44++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 45 insertions(+), 42 deletions(-)

diff --git a/src/log.hpp b/src/log.hpp @@ -1,41 +0,0 @@ -#ifndef LOG_HPP -#define LOG_HPP - -#define xstr(s) str(s) -#define str(s) #s -#define HEADER_LOG __FILE__ "[" xstr(__LINE__) "]" - -#ifndef NDEBUG -#define BUG_CHECK( cond, err ) \ - do \ - { \ - if( cond ) \ - { \ - fputs( HEADER_LOG ": " #err " caused by " #cond, stderr ); \ - abort(); \ - } \ - } while( 0 ) -#else -#define BUG_CHECK( cond, err ) if( cond ) return err -#endif - -#define syserr( str ) \ - do { \ - fputs( HEADER_LOG ": ", stderr ); \ - fputs( strerror( errno ), stderr ); \ - fputs( ": " str "\n", stderr ); \ - } while( 0 ) - -//ok, this should not be here, since not related to any logging stuff -//otoh, I think this file should be renamed utils, or alike... -//it's only defines anyway... -#define ALLOC( type ) static_cast<type*>( malloc( sizeof( type ) ) ) -#define REDEF( type, dst, src ) type& dst = *static_cast<type*>( src ) - -template <typename T> -inline bool empty_array( T* arr ) -{ - return arr && *arr; -} - -#endif diff --git a/src/optparser.cpp b/src/optparser.cpp @@ -7,7 +7,7 @@ #include <limits.h> #include "optparser.hpp" -#include "log.hpp" +#include "utils.hpp" // those strings must be defined in same order than the enum // Trick: use vim's block insertion to check them diff --git a/src/utils.hpp b/src/utils.hpp @@ -0,0 +1,44 @@ +#ifndef UTILS_HPP +#define UTILS_HPP + +#define xstr(s) str(s) +#define str(s) #s +#define HEADER_LOG __FILE__ "[" xstr(__LINE__) "]" + +#ifndef NDEBUG +//abort-like macro, but more flexible +#define BUG_CHECK( cond, err ) \ + do \ + { \ + if( cond ) \ + { \ + fputs( HEADER_LOG ": " #err " caused by " #cond, stderr ); \ + abort(); \ + } \ + } while( 0 ) +#else +#define BUG_CHECK( cond, err ) if( cond ) return err +#endif + +//display a system error with an optional +#define syserr( str ) \ + do { \ + fputs( HEADER_LOG ": ", stderr ); \ + fputs( strerror( errno ), stderr ); \ + fputs( ": " str "\n", stderr ); \ + } while( 0 ) + +//ok, this should not be here, since not related to any logging stuff +//otoh, I think this file should be renamed utils, or alike... +//it's only defines anyway... +#define ALLOC( type ) static_cast<type*>( malloc( sizeof( type ) ) ) +#define REDEF( type, dst, src ) type& dst = *static_cast<type*>( src ) + +//simply check if an array is nullptr or starts with a value considered invalid +template <typename T, T INVALID=T()> +inline bool empty_array( T* arr ) +{ + return arr && *arr == INVALID; +} + +#endif