Changeset 403


Ignore:
Timestamp:
06/14/15 14:31:00 (10 years ago)
Author:
epyon
Message:
  • got rid of exceptions
  • assert enhancements
  • lots of minor cleanup
Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/base/assert.hh

    r402 r403  
    1010 * @brief Assert functions
    1111 */
     12// TODO: assert levels
     13// TODO: assert should not depend on CORE!
    1214
    1315#ifndef NV_BASE_COMMON_HH
     
    2325#endif
    2426
    25 #if NV_COMPILER == NV_MSVC
    26 void nv_internal_assert( const wchar_t * message, const wchar_t* file, unsigned line );
    27 #       if NV_DEBUG
    28 #       define NV_ASSERT_IMPL(cond) (void)( (!!(cond)) || (nv_internal_assert(NV_WIDE(#cond), NV_WIDE(__FILE__), __LINE__), 0) )
     27#ifdef assert
     28#undef assert
     29#endif
     30
     31namespace nv
     32{
     33        namespace detail
     34        {
     35                NV_NORETURN void abort( const char * msg, const char * file, unsigned int line, const char * function );
     36                NV_NORETURN void assert_abort( const char * msg, const char * file, unsigned int line, const char * function );
     37#if NV_DEBUG
     38#       if NV_COMPILER == NV_MSVC
     39                NV_NORETURN void assert_fail( const wchar_t * message, const wchar_t* file, unsigned line );
    2940#       else
    30 #       define NV_ASSERT_IMPL(cond)
    31 #       endif
    32 #else
    33 void nv_internal_assert( const char * assertion, const char * file, unsigned int line, const char * function );
    34 #       if NV_DEBUG
    35 #       define NV_ASSERT_IMPL(cond) ((cond) ? static_cast<void>(0) : nv_internal_assert(NV_STRINGIZE(cond), __FILE__, __LINE__, __PRETTY_FUNCTION__))
    36 #       else
    37 #       define NV_ASSERT_IMPL(cond)
     41                NV_NORETURN void assert_fail( const char * assertion, const char * file, unsigned int line, const char * function );
    3842#       endif
    3943#endif
     44        }
    4045
    41 // TODO: assert levels
     46        NV_NORETURN void exit( int ret_val );
     47}
     48
     49
     50
     51#define NV_ABORT( msg ) ::nv::detail::abort( msg, __FILE__, __LINE__, NV_FUNC_SIG )
     52
    4253#if NV_DEBUG
    43 #define NV_ASSERT(cond, msg) NV_ASSERT_IMPL( (cond) && "assertion failed:" msg )
    44 #define NV_DEBUG_ASSERT(cond, msg) NV_ASSERT_IMPL( (cond) && "assertion failed:" msg )
     54#       if NV_COMPILER == NV_MSVC
     55#       define NV_ASSERT_COND(cond) (void)( (!!(cond)) || (::nv::detail::assert_fail(NV_WIDE(#cond), NV_WIDE(__FILE__), __LINE__), 0) )
     56#       else
     57#       define NV_ASSERT_COND(cond) ((cond) ? static_cast<void>(0) : ::nv::detail::assert_fail(NV_STRINGIZE(cond), __FILE__, __LINE__, __PRETTY_FUNCTION__))
     58#       endif
     59#define NV_ASSERT(cond, msg) NV_ASSERT_COND( (cond) && "assertion failed:" msg )
     60#define NV_ASSERT_DEBUG(cond, msg) NV_ASSERT_COND( (cond) && "assertion failed:" msg )
     61#define NV_ASSERT_ALWAYS(cond, msg) NV_ASSERT_COND( (cond) && "assertion failed:" msg )
    4562#else 
    4663#define NV_ASSERT(cond, msg) ((void)0)
    47 #define NV_DEBUG_ASSERT(cond, msg) ((void)0)
     64#define NV_ASSERT_DEBUG(cond, msg) ((void)0)
     65#define NV_ASSERT_ALWAYS(cond, msg) ((cond) ? static_cast<void>(0) : ::nv::detail::assert_abort(NV_STRINGIZE(cond) " " msg, __FILE__, __LINE__, NV_FUNC_SIG ))
    4866#endif
    4967
  • trunk/nv/base/common.hh

    r402 r403  
    128128
    129129#if NV_COMPILER == NV_MSVC
     130#define NV_FUNC_SIG __FUNCSIG__
    130131#define NV_DEPRECATED(func) __declspec(deprecated) func
     132#define NV_NORETURN __declspec(noreturn)
    131133#define NV_NOALIAS __declspec(noalias)
    132134#define NV_RESTRICT __declspec(restrict)
     
    139141#endif 
    140142#elif NV_COMPILER == NV_GNUC || NV_COMPILER == NV_CLANG
     143#define NV_FUNC_SIG __PRETTY_FUNCTION__
    141144#define NV_DEPRECATED(func) func __attribute__ ((deprecated))
     145#define NV_NORETURN __attribute__ ((__noreturn__))
    142146#define NV_RESTRICT __restrict__
    143147#define NV_RESTRICT_VAR __restrict__
     
    149153
    150154#define NV_UNUSED(x) (void)(x)
    151 #define NV_THROW(eobj, ...) { \
    152         NV_LOG_ERROR( __FILE__ " line " NV_STRINGIZE(__LINE__) " - exception thrown - " #eobj ); \
    153         throw eobj( __VA_ARGS__ ); \
    154 }
    155155
    156156// MSVC and GCC is too stupid to notice fully covered enums, clang
  • trunk/nv/core/library.hh

    r402 r403  
    1515
    1616#include <nv/common.hh>
    17 #include <nv/stl/exception.hh>
    1817#include <nv/stl/string.hh>
    1918#include <string>
     
    113112        };  // class Library
    114113
    115         class library_error : public runtime_error
    116         {
    117                 /// Library name
    118                 std::string m_name;
    119         public:
    120                 /**
    121                  * Constructor
    122                  */
    123                 library_error( const std::string& message, const std::string& name )
    124                         : runtime_error( "Library (" + name + ") : " + message + " [ " + library::get_error() + " ]"), m_name( name )
    125                 {
    126                 }
    127 
    128                 /**
    129                  * Returns library name
    130                  */
    131                 const std::string& get_name()
    132                 {
    133                         return m_name;
    134                 }
    135         };
    136 
    137114} // namespace nv
    138115
  • trunk/nv/stl/algorithm/raw.hh

    r402 r403  
    3434        T* raw_copy( const T* first, const T* last, T* out )
    3535        {
    36                 NV_DEBUG_ASSERT( last - first > 0, "raw_copy range fail!" );
     36                NV_ASSERT_DEBUG( last - first > 0, "raw_copy range fail!" );
    3737                return static_cast<T*>( nvmemcpy( out, first, detail::byte_distance( first, last ) ) ) + ( last - first );
    3838        }
     
    4747        T* raw_alias_copy( const T* first, const T* last, T* out )
    4848        {
    49                 NV_DEBUG_ASSERT( last - first > 0, "raw_alias_copy range fail!" );
     49                NV_ASSERT_DEBUG( last - first > 0, "raw_alias_copy range fail!" );
    5050                return static_cast<T*>( nvmemmove( out, first, detail::byte_distance( first, last ) ) ) + ( last - first );
    5151        }
     
    6060        T* raw_zero( T* first, T* last )
    6161        {
    62                 NV_DEBUG_ASSERT( last - first > 0, "raw_zero range fail!" );
     62                NV_ASSERT_DEBUG( last - first > 0, "raw_zero range fail!" );
    6363                return static_cast<T*>( nvmemset( first, 0, detail::byte_distance( first, last ) ) ) + ( last - first );
    6464        }
     
    7373        T* raw_fill( T* first, T* last, unsigned char value )
    7474        {
    75                 NV_DEBUG_ASSERT( last - first > 0, "raw_fill range fail!" );
     75                NV_ASSERT_DEBUG( last - first > 0, "raw_fill range fail!" );
    7676                return static_cast<T*>( nvmemset( first, value, detail::byte_distance( first, last ) ) ) + ( last - first );
    7777        }
  • trunk/nv/stl/string.hh

    r402 r403  
    2828#include <nv/stl/type_traits/primary.hh>
    2929#include <nv/stl/memory.hh>
    30 #include <nv/stl/exception.hh>
    3130#include <nv/stl/algorithm.hh>
    3231#include <nv/stl/functional/hash.hh>
  • trunk/nv/stl/unordered_map.hh

    r401 r403  
    2020namespace nv
    2121{
    22         template < typename Pair >
    23         struct use_first
    24         {
    25                 typedef Pair                      argument_type;
    26                 typedef typename Pair::first_type result_type;
    27                 const result_type& operator()( const Pair& arg ) const
    28                 {
    29                         return arg.first;
    30                 }
    31         };
    32        
     22
    3323        template <
    3424                typename Key,
  • trunk/src/core/library.cc

    r402 r403  
    4444        {
    4545                m_handle = nullptr;
    46                 NV_THROW( library_error, "Can't load library!", name.data() );
     46                NV_LOG_CRITICAL( "library \"", name, "\" : failed to load!" );
     47                NV_ABORT( "Can't load library!" );
    4748        }
    4849}
     
    7071        return true;
    7172    }
    72     NV_LOG_NOTICE( "library : loading '", m_name, "'..." );
     73    NV_LOG_NOTICE( "library \"", string_view( m_name ), "\" : loading..." );
    7374
    7475        std::string name = m_name;
     
    8485    if ( m_handle == NULL )
    8586    {
    86                 NV_LOG_NOTICE( "library : '", name, "' failed to open." );
     87                NV_LOG_NOTICE( "library \"", string_view( name ), "\" : failed to open!" );
    8788                return false;
    8889    }
    89     NV_LOG_NOTICE( "library : '", name, "' loaded." );
     90    NV_LOG_NOTICE( "library \"", string_view( name ), "\" : loaded." );
    9091        return true;
    9192}
     
    9697    if ( !result )
    9798    {
    98         NV_THROW( library_error, "Can't find symbol " + std::string(symbol.data(),symbol.size()) + "!", m_name );
     99                NV_LOG_CRITICAL( "library \"", string_view( m_name ), "\" : can't find symbol \"", symbol, "\"" );
     100                NV_ABORT( "Library symbol load failed!" );
    99101    }
    100102        return result;
     
    115117    if ( ! NV_LIB_CLOSE( m_handle ) )
    116118    {
    117         NV_LOG_ERROR( "library : can't close library '", m_name, "'!" );
     119        NV_LOG_ERROR( "library \"", string_view( m_name ), "\" : can't close library!" );
    118120    }
    119121    m_handle = nullptr;
  • trunk/src/gfx/texture_font.cc

    r399 r403  
    1111
    1212using namespace nv;
     13
     14#define NV_CHECK_FREETYPE_ERROR( error, ... ) \
     15if ( error != 0 ) { \
     16        NV_LOG_CRITICAL( "freetype : ", __VA_ARGS__ ); \
     17        NV_ABORT( "freetype : freetype library error!" ); \
     18}
    1319
    1420texture_glyph::texture_glyph()
     
    4551         
    4652        error = FT_Init_FreeType( (FT_Library*)(&m_rlibrary) );
    47         if ( error ) NV_THROW( std::runtime_error, "FT_Error" );
     53        NV_CHECK_FREETYPE_ERROR( error, "error on FT_Init_FreeType, code - ", error );
    4854
    4955        error = FT_New_Face( (FT_Library)(m_rlibrary), filename, 0, (FT_Face*)(&m_rface) );
    50         if ( error ) NV_THROW( std::runtime_error, "FT_Error" );
     56        NV_CHECK_FREETYPE_ERROR( error, "error on FT_New_Face, code - ", error );
    5157
    5258        error = FT_Set_Char_Size( (FT_Face)(m_rface), (int)(size*64), 0, 72*64, 72 );
    53         if ( error ) NV_THROW( std::runtime_error, "FT_Error" );
     59        NV_CHECK_FREETYPE_ERROR( error, "error on FT_Set_Char_Size, code - ", error );
    5460
    5561    FT_Set_Transform( (FT_Face)(m_rface), &matrix, NULL );
     
    123129                FT_UInt glyph_index = FT_Get_Char_Index( face, c );
    124130                FT_Error error = FT_Load_Glyph( face, glyph_index, flags );
    125                 if ( error )
    126                 {
    127                         NV_LOG_ERROR( "FT_Error while loading glyphs, error: ", error, " code: ", c );
    128                         NV_THROW( std::runtime_error, "FT_Error while loading glyphs" );
    129                 }
     131                NV_CHECK_FREETYPE_ERROR( error, "error on FT_Load_Glyph, gylph '", c ,"' code - ", error );
    130132
    131133                FT_GlyphSlot slot   = face->glyph;
     
    141143                if ( r.pos.x < 0 )
    142144                {
    143                         NV_LOG_ERROR( "Atlas full while loading glyphs, r.pos.x: ", r.pos.x, " code: ", c );
    144                         NV_THROW( std::runtime_error, "Atlas full while loading glyphs" );
     145                        NV_LOG_CRITICAL( "texture_font : atlas full while loading glyphs, gylph '", c, "' r.pos.x = ", r.pos.x );
     146                        NV_ABORT( "texture_font : atlas full while loading gylphs!" );
    145147                }
    146148                if (depth == 4)
  • trunk/src/gl/gl_context.cc

    r398 r403  
    379379void gl_context::set_viewport( const ivec4& viewport )
    380380{
    381         if ( viewport.z < 0 || viewport.w < 0 )
    382         {
    383                 NV_THROW( logic_error, "viewport width and height must be greater than zero!");
    384         }
    385 
     381        NV_ASSERT_ALWAYS( viewport.z > 0 && viewport.w > 0, "viewport dimensions must be greater than zero!" );
    386382        m_viewport = viewport;
    387383        glViewport( viewport.x, viewport.y, viewport.z, viewport.w );
     
    456452        }
    457453
    458         if ( scissor.dim.x < 0 || scissor.dim.y < 0 )
    459         {
    460                 NV_THROW( logic_error, "scissor_test.rect width and height must be greater than zero!" );
    461         }
    462454
    463455        if ( scissor.enabled )
    464456        {
     457                NV_ASSERT_ALWAYS( scissor.dim.x > 0 && scissor.dim.y > 0, "scissor_test.rect dimension equal to zero!" );
     458
    465459                if ( m_render_state.scissor_test.dim != scissor.dim || m_render_state.scissor_test.pos != scissor.pos )
    466460                {
     
    514508void gl_context::apply_depth_range( const depth_range& range )
    515509{
    516         if ( range.near < 0.0 || range.near > 1.0 )
    517         {
    518                 NV_THROW( logic_error, "render_state.depth_range.near must be between zero and one!");
    519         }
    520         if ( range.far < 0.0 || range.far > 1.0 )
    521         {
    522                 NV_THROW( logic_error, "render_state.depth_range.far must be between zero and one!");
    523         }
     510        NV_ASSERT_ALWAYS( range.near >= 0.0 && range.near <= 1.0, "render_state.depth_range.near must be between zero and one!" );
     511        NV_ASSERT_ALWAYS( range.far  >= 0.0 && range.far  <= 1.0, "render_state.depth_range.far must be between zero and one!" );
    524512
    525513        if ((m_render_state.depth_range.far  != range.far) ||
  • trunk/src/gl/gl_device.cc

    r399 r403  
    238238                if ( fatal )
    239239                {
    240                         NV_LOG_ERROR( "Uniform '", name, "' not found in program!" );
    241                         NV_THROW( runtime_error, ( "Uniform '"+name+"' not found!" ) );
     240                        NV_LOG_CRITICAL( "gl_device : uniform '", string_view( name ), "' not found in program!" );
     241                        NV_ABORT( "gl_device : uniform not found!" );
    242242                }
    243243        }
     
    257257                if ( fatal )
    258258                {
    259                         NV_LOG_ERROR( "Attribute '", name, "' not found in program!" );
    260                         NV_THROW( runtime_error, ( "Attribute '"+ name + "' not found!" ) );
     259                        NV_LOG_CRITICAL( "gl_device : attribute '", string_view( name ), "' not found in program!" );
     260                        NV_ABORT( "gl_device : attribute not found!" );
    261261                }
    262262        }
  • trunk/src/lua/lua_state.cc

    r399 r403  
    1515
    1616// stack_guard
     17
     18#define NV_LUA_ABORT( func, ... ) \
     19        NV_LOG_CRITICAL( "lua::" func " : ", __VA_ARGS__ ) \
     20        NV_ABORT( "lua::" func " : critical error!" )
     21
    1722
    1823lua::stack_guard::stack_guard( lua::state* aL )
     
    424429        if ( lua_isnil( m_state, -1 ) )
    425430        {
    426                 NV_THROW( runtime_error, lua_name.to_string() + " type not registered!" );
     431                NV_LUA_ABORT( "state::register_object", lua_name, " type not registered!" );
    427432        }
    428433        deep_pointer_copy( -1, o );
     
    436441        if ( lua_isnil( m_state, -1 ) )
    437442        {
    438                 NV_THROW( runtime_error, storage.to_string() + " storage not registered!" );
     443                NV_LUA_ABORT( "state::register_proto", "\"", storage, "\" storage not registered!" );
    439444        }
    440445        lua_getfield( m_state, -1, id.data() );
    441446        if ( lua_isnil( m_state, -1 ) )
    442447        {
    443                 NV_THROW( runtime_error, id.to_string() + " not found in " + storage.to_string() + " storage!" );
     448                NV_LUA_ABORT( "state::register_proto", "\"", id, "\" not found in \"", storage, "\"!" );
    444449        }
    445450        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
     
    452457        if ( lua_isnil( m_state, -1 ) )
    453458        {
    454                 NV_THROW( runtime_error, lua_name.to_string() + " type not registered!" );
     459                NV_LUA_ABORT( "state::register_native_object_method", "\"", lua_name, "\" type not registered!" );
    455460        }
    456461        lua_pushcfunction( m_state, f );
     
    502507        if ( has_functions || has_metatable )
    503508        {
    504                 lua_pushstring( m_state, "__ptr" );
     509                lua_pushliteral( m_state, "__ptr" );
    505510                lua_pushlightuserdata( m_state, obj );
    506511                lua_rawset( m_state, -3 );
     
    569574        if ( lua_isnil( m_state, -1 ) )
    570575        {
    571                 NV_THROW( runtime_error, name.to_string() + " type not registered!" );
     576                NV_LUA_ABORT( "state::register_singleton", "\"", name, "\" type not registered!" );
    572577        }
    573578        deep_pointer_copy( -1, o );
  • trunk/src/stl/assert.cc

    r402 r403  
    99#include "nv/base/assert.hh"
    1010#undef NV_BASE_COMMON_HH
     11#include "nv/core/logging.hh"
    1112
     13extern "C" {
    1214#if NV_COMPILER == NV_MSVC
     15        NV_NORETURN void __cdecl exit( _In_ int _Code );
     16#else
     17        void exit( int status_code ) NV_NORETURN;
     18#endif
     19}
    1320
    14 #       if NV_DEBUG
    15 
     21#if NV_DEBUG
     22#       if NV_COMPILER == NV_MSVC
    1623extern "C" {
    1724        void __cdecl _wassert( const wchar_t * _Message, const wchar_t *_File, unsigned _Line );
    1825}
    1926
    20 void nv_internal_assert( const wchar_t * message, const wchar_t* file, unsigned line )
     27void nv::detail::assert_fail( const wchar_t * message, const wchar_t* file, unsigned line )
    2128{
    2229        _wassert( message, file, line );
    2330}
    24 #       else 
    25         void nv_internal_assert( const wchar_t *, const wchar_t*, unsigned ) {}
    26 #       endif  // NV_DEBUG
    27 #else // NV_COMPILER
    28 
    29 #       if NV_DEBUG
     31#       else // NV_COMPILER
    3032#       if NV_COMPILER == NV_CLANG
    3133extern "C" {
    32         extern void __assert(const char *, const char *, unsigned int, const char *)
    33                 __attribute__ ((__noreturn__));
     34        extern void __assert(const char *, const char *, unsigned int, const char *) NV_NORETURN;
    3435}
     36#define NV_ASSERT_IMPL __assert
    3537#       else
    3638extern "C" {
    37         extern void __assert_fail(const char *, const char *, unsigned int, const char *)
    38                 __attribute__ ((__noreturn__));
     39        extern void __assert_fail(const char *, const char *, unsigned int, const char *) NV_NORETURN;
    3940}
     41#define NV_ASSERT_IMPL __assert_fail
    4042#       endif
    41 __attribute__( ( __noreturn__ ) ) void nv_internal_assert( const char * assertion, const char * file, unsigned int line, const char * function )
     43NV_NORETURN void nv::detail::assert_fail( const char * assertion, const char * file, unsigned int line, const char * function )
    4244{
    43 #       if NV_COMPILER == NV_CLANG
    44         __assert(assertion, file, line, function );
    45 #       else
    46         __assert_fail(assertion, file, line, function );
    47 #       endif
    48 }
    49 #       else
    50 void nv_internal_assert( const char * , const char * , unsigned int , const char * )
    51 {
     45        NV_ASSERT_IMPL (assertion, file, line, function );
    5246}
    5347#       endif
    5448
    55 #endif // NV_COMPILER
     49#endif // NV_DEBUG
    5650
     51NV_NORETURN void nv::detail::abort( const char * msg, const char * file, unsigned int line, const char * function )
     52{
     53        NV_LOG_CRITICAL( "Abort called : ", msg );
     54        NV_LOG_CRITICAL( "  in ", file, ":", line, " (", function, ")" );
     55        NV_LOG_CRITICAL( "Aborting..." );
     56        exit( 1 );
     57}
     58
     59NV_NORETURN void nv::detail::assert_abort( const char * msg, const char * file, unsigned int line, const char * function )
     60{
     61        NV_LOG_CRITICAL( "Assertion failed: (", msg, ")" );
     62        NV_LOG_CRITICAL( "  in ", file, ":", line, " (", function, ")" );
     63        NV_LOG_CRITICAL( "Aborting..." );
     64        exit( 1 );
     65}
Note: See TracChangeset for help on using the changeset viewer.