Merge branch 'ldionne-ldionne-cmake' into dev
[libcds.git] / cds / details / throw_exception.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef CDSLIB_DETAILS_THROW_EXCEPTION_H
32 #define CDSLIB_DETAILS_THROW_EXCEPTION_H
33
34 #include <cds/details/defs.h>
35 #if !defined( CDS_EXCEPTION_ENABLED ) && !defined( CDS_USER_DEFINED_THROW_HANDLER )
36 #   include <stdio.h>
37 #endif
38
39 namespace cds {
40
41 #if !defined( CDS_USER_DEFINED_THROW_EXCEPTION )
42 #if defined( CDS_EXCEPTION_ENABLED )
43     /// Function to throw an exception
44     /**
45         If you compile your code with exception enabled, \p %throw_exception() function
46         throws the \p exception.
47
48         If exception is disabled, \p %throw_exception() prints an exception message to
49         standard output and call \p abort().
50
51         You can supply your own \p %cds::throw_exception() function;
52         for that you should specify \p -DCDS_USER_DEFINED_THROW_EXCEPTION
53         in compiler command line.
54
55         @note \p %throw_exception() never returns. If the user-defined \p %throw_exception() returns,
56         the behavior is undefined.
57     */
58     template <typename E>
59     CDS_NORETURN static inline void throw_exception(
60         E&& exception,       ///< Exception to throw
61         char const* file,   ///< Source filename
62         int line            ///< File line
63     )
64     {
65         CDS_UNUSED( file );
66         CDS_UNUSED( line );
67
68         throw exception;
69     }
70 #else
71     template <typename E>
72     CDS_NORETURN static inline void throw_exception( E&& exception, char const* file, int line )
73     {
74         printf( "file %s, line %d: %s\n", file, line, exception.what());
75         abort();
76     }
77 #endif
78 //#else
79     // User-provided cds::throw_exception()
80 #endif
81
82 #define CDS_THROW_EXCEPTION( exception ) ::cds::throw_exception( exception, __FILE__, __LINE__ )
83
84 } // namespace cds
85
86
87 #endif // #ifndef CDSLIB_DETAILS_THROW_EXCEPTION_H
88