xapian-core  1.4.27
msvcignoreinvalidparam.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2007,2008,2015,2018 Olly Betts
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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef XAPIAN_INCLUDED_MSVCIGNOREINVALIDPARAM_H
22 #define XAPIAN_INCLUDED_MSVCIGNOREINVALIDPARAM_H
23 
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.
31 
33 static void dummy_handler(const wchar_t*,
34  const wchar_t*,
35  const wchar_t*,
36  unsigned int,
37  uintptr_t) noexcept
38 {
39 }
40 
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.
47  _invalid_parameter_handler old_handler;
48  int old_report_mode;
49 
50  public:
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);
56  }
57 
59  // Restore the previous settings.
60  _set_invalid_parameter_handler(old_handler);
61  _CrtSetReportMode(_CRT_ASSERT, old_report_mode);
62  }
63 };
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.
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.
73 };
74 #endif
75 
76 #endif