]> git.saurik.com Git - wxWidgets.git/blob - include/wx/meta/if.h
do the VC6 hacks only when compiling with VC6
[wxWidgets.git] / include / wx / meta / if.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/meta/if.h
3 // Purpose: declares wxIf<> metaprogramming construct
4 // Author: Vaclav Slavik
5 // Created: 2008-01-22
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_META_IF_H_
12 #define _WX_META_IF_H_
13
14 #include "wx/defs.h"
15
16 // NB: This code is intentionally written without partial templates
17 // specialization, because some older compilers (notably VC6) don't
18 // support it.
19
20 namespace wxPrivate
21 {
22
23 template <bool Cond>
24 struct wxIfImpl
25
26 // broken VC6 needs not just an incomplete template class declaration but a
27 // "skeleton" declaration of the specialized versions below as it apparently
28 // tries to look up the types in the generic template definition at some moment
29 // even though it ends up by using the correct specialization in the end -- but
30 // without this skeleton it doesn't recognize Result as a class at all below
31 #if defined(__VISUALC__) && !wxCHECK_VISUALC_VERSION(7)
32 {
33 template<typename TTrue, typename TFalse> struct Result
34 {
35 // unfortunately we also need to define value here because otherwise
36 // Result::value is not recognized as a class neither and it has to be
37 // complete too -- at least make it unusable because it really, really
38 // should never be used
39 class value
40 {
41 private:
42 value();
43 ~value();
44 };
45 };
46 }
47 #endif // VC++ <= 6
48 ;
49
50 // specialization for true:
51 template <>
52 struct wxIfImpl<true>
53 {
54 template<typename TTrue, typename TFalse> struct Result
55 {
56 #if defined(__VISUALC__) && !wxCHECK_VISUALC_VERSION(7)
57 struct value : TTrue { };
58 #else
59 typedef TTrue value;
60 #endif
61 };
62 };
63
64 // specialization for false:
65 template<>
66 struct wxIfImpl<false>
67 {
68 template<typename TTrue, typename TFalse> struct Result
69 {
70 #if defined(__VISUALC__) && !wxCHECK_VISUALC_VERSION(7)
71 struct value : TFalse { };
72 #else
73 typedef TFalse value;
74 #endif
75 };
76 };
77
78 } // namespace wxPrivate
79
80 // wxIf<> template defines nested type "value" which is the same as
81 // TTrue if the condition Cond (boolean compile-time constant) was met and
82 // TFalse if it wasn't.
83 //
84 // See wxVector<T> in vector.h for usage example
85 template<bool Cond, typename TTrue, typename TFalse>
86 struct wxIf
87 {
88 #if defined(__VISUALC__) && !wxCHECK_VISUALC_VERSION(7)
89 // notice that value can't be a typedef, VC6 refuses to use it as a base
90 // class in this case
91 struct value : wxPrivate::wxIfImpl<Cond>::Result<TTrue, TFalse>::value { };
92 #else // !VC6++
93 typedef typename wxPrivate::wxIfImpl<Cond>
94 ::template Result<TTrue, TFalse>::value
95 value;
96 #endif
97 };
98
99 #endif // _WX_META_IF_H_