[ci] Enable IRC notifications from travis
[xapian.git] / xapian-core / common / msvcignoreinvalidparam.h
blob3587ba5a98e3bf70d7650e92edd744edcf8dcf03
1 /** @file msvcignoreinvalidparam.h
2 * @brief Work around MSVC's unhelpful non-standard invalid parameter handling.
3 */
4 /* Copyright (C) 2006,2007,2008,2015,2018 Olly Betts
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 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef XAPIAN_INCLUDED_MSVCIGNOREINVALIDPARAM_H
22 #define XAPIAN_INCLUDED_MSVCIGNOREINVALIDPARAM_H
24 // __STDC_SECURE_LIB__ doesn't appear to be publicly documented, but including
25 // in this check appears to be a good idea. We cribbed this test from the
26 // python sources - see, for example,
27 // https://github.com/python/cpython/commit/74c3ea0a0f6599da7dd9a502b5a66aeb9512d8c3
28 #if defined _MSC_VER && _MSC_VER >= 1400 && defined __STDC_SECURE_LIB__
29 # include <cstdlib> // For _set_invalid_parameter_handler(), etc.
30 # include <crtdbg.h> // For _CrtSetReportMode, etc.
32 /** A dummy invalid parameter handler which ignores the error. */
33 static void dummy_handler(const wchar_t*,
34 const wchar_t*,
35 const wchar_t*,
36 unsigned int,
37 uintptr_t)
41 // Recent versions of MSVC call an "_invalid_parameter_handler" if a
42 // CRT function receives an invalid parameter. However, there are cases
43 // where this is totally reasonable. To avoid the application dying,
44 // you just need to instantiate the MSVCIgnoreInvalidParameter class in
45 // the scope where you want MSVC to ignore invalid parameters.
46 class MSVCIgnoreInvalidParameter {
47 _invalid_parameter_handler old_handler;
48 int old_report_mode;
50 public:
51 MSVCIgnoreInvalidParameter() {
52 // Install a dummy handler to avoid the program dying.
53 old_handler = _set_invalid_parameter_handler(dummy_handler);
54 // Make sure that no dialog boxes appear.
55 old_report_mode = _CrtSetReportMode(_CRT_ASSERT, 0);
58 ~MSVCIgnoreInvalidParameter() {
59 // Restore the previous settings.
60 _set_invalid_parameter_handler(old_handler);
61 _CrtSetReportMode(_CRT_ASSERT, old_report_mode);
64 #else
65 // Mingw seems to be free of this insanity, so for mingw, older MSVC versions,
66 // and other platforms define a dummy class to allow MSVCIgnoreInvalidParameter
67 // to be used unconditionally.
68 struct MSVCIgnoreInvalidParameter {
69 // Provide an explicit constructor so this isn't a POD struct - this seems
70 // to prevent GCC warning about an unused variable whenever we instantiate
71 // this class.
72 MSVCIgnoreInvalidParameter() { }
74 #endif
76 #endif