1 //=============================================================================
2 //
3 //   File : KviCaster.h
4 //   Creation date : Mon April 18 2016 02:00:36 by Matt Ullman
5 //
6 //   This file is part of the KVIrc IRC client distribution
7 //   Copyright (C) 2016 Matt Ullman (staticfox at staticfox dot net)
8 //
9 //   This program is FREE software. You can redistribute it and/or
10 //   modify it under the terms of the GNU General Public License
11 //   as published by the Free Software Foundation; either version 2
12 //   of the License, or (at your option) any later version.
13 //
14 //   This program is distributed in the HOPE that it will be USEFUL,
15 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 //   See the GNU General Public License for more details.
18 //
19 //   You should have received a copy of the GNU General Public License
20 //   along with this program. If not, write to the Free Software Foundation,
21 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 //=============================================================================
24 
25 #include <typeinfo>
26 
27 #define STRINGIFY_(x) #x
28 #define STRINGIFY(x) STRINGIFY_(x)
29 
30 #if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
31 #define KVI_CAST_FUNC __FUNCTION__
32 #else
33 #define KVI_CAST_FUNC __func__
34 #endif
35 
36 #define KVI_DYNAMIC(expr, expr2)          \
37 	KviCast::dynamic<expr>(expr2,         \
38 	    __FILE__ ":" STRINGIFY(__LINE__), \
39 	    KVI_CAST_FUNC,                    \
40 	    #expr)
41 
42 namespace KviCast
43 {
44 	template <typename T, typename P>
dynamic(P p,const char * file,const char * func,const char * expr)45 	T dynamic(P p, const char * file, const char * func, const char * expr)
46 	{
47 		T newPointer = dynamic_cast<T>(p);
48 
49 		KVI_ASSERT(newPointer != nullptr);
50 
51 		if(newPointer == nullptr)
52 		{
53 			qDebug("KviCast::dynamic: Conversion from %s to %s failed: %s in %s (%s).",
54 			    typeid(P).name(), typeid(T).name(), func, file, expr);
55 		}
56 
57 		return newPointer;
58 	}
59 }
60