Line data Source code
1 : // Copyright (c) 2022-2023 Made to Order Software Corp. All Rights Reserved 2 : // 3 : // https://snapwebsites.org/project/snapdev 4 : // contact@m2osw.com 5 : // 6 : // This program is free software: you can redistribute it and/or modify 7 : // it under the terms of the GNU General Public License as published by 8 : // the Free Software Foundation, either version 3 of the License, or 9 : // (at your option) any later version. 10 : // 11 : // This program is distributed in the hope that it will be useful, 12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 : // GNU General Public License for more details. 15 : // 16 : // You should have received a copy of the GNU General Public License 17 : // along with this program. If not, see <https://www.gnu.org/licenses/>. 18 : #pragma once 19 : 20 : /** \file 21 : * \brief Safely assert. 22 : * 23 : * This function defines an assert which is safe, in that, any side effect 24 : * in the list of parameters at the call point. For example, if you have 25 : * a `++i` in the list of parameters, that `++` operation will not be lost 26 : * even when compiling in release mode. 27 : */ 28 : 29 : // self 30 : // 31 : #include <snapdev/not_used.h> 32 : 33 : 34 : // C++ 35 : // 36 : #include <iostream> 37 : 38 : 39 : 40 : namespace snapdev 41 : { 42 : 43 : /** \brief A debug/non-debug assert() which can include side effects. 44 : * 45 : * This function is safe to use without any special protection. The 46 : * expression to verify will be optimized out, except for any part which 47 : * may have side effects. 48 : * 49 : * \param[in] test_result The value to be tested, if true, the assert passes. 50 : * \param[in] ... args Arguments to pass to std::cerr for a user defined 51 : * message. 52 : */ 53 : template<typename ... Args> 54 1 : void SAFE_ASSERT(bool test_result, Args && ... args) 55 : { 56 : #ifdef _DEBUG 57 1 : if(!test_result) 58 : { 59 0 : (std::cerr << ... << args); 60 0 : std::cerr << std::endl; 61 0 : std::terminate(); 62 : } 63 : #else 64 : NOT_USED(test_result, args...); 65 : #endif 66 1 : } 67 : 68 : 69 : 70 : } // snap namespacedev 71 : // vim: ts=4 sw=4 et