1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        wx/meta/if.h
3 // Purpose:     declares wxIf<> metaprogramming construct
4 // Author:      Vaclav Slavik
5 // Created:     2008-01-22
6 // Copyright:   (c) 2008 Vaclav Slavik
7 // Licence:     wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9 
10 #ifndef _WX_META_IF_H_
11 #define _WX_META_IF_H_
12 
13 #include "wx/defs.h"
14 
15 // NB: This code is intentionally written without partial templates
16 //     specialization, because some older compilers (notably VC6) don't
17 //     support it.
18 
19 namespace wxPrivate
20 {
21 
22 template <bool Cond>
23 struct wxIfImpl
24 
25 // broken VC6 needs not just an incomplete template class declaration but a
26 // "skeleton" declaration of the specialized versions below as it apparently
27 // tries to look up the types in the generic template definition at some moment
28 // even though it ends up by using the correct specialization in the end -- but
29 // without this skeleton it doesn't recognize Result as a class at all below
30 #if defined(__VISUALC__) && !wxCHECK_VISUALC_VERSION(7)
31 {
32     template<typename TTrue, typename TFalse> struct Result {};
33 }
34 #endif // VC++ <= 6
35 ;
36 
37 // specialization for true:
38 template <>
39 struct wxIfImpl<true>
40 {
41     template<typename TTrue, typename TFalse> struct Result
42     {
43         typedef TTrue value;
44     };
45 };
46 
47 // specialization for false:
48 template<>
49 struct wxIfImpl<false>
50 {
51     template<typename TTrue, typename TFalse> struct Result
52     {
53         typedef TFalse value;
54     };
55 };
56 
57 } // namespace wxPrivate
58 
59 // wxIf<> template defines nested type "value" which is the same as
60 // TTrue if the condition Cond (boolean compile-time constant) was met and
61 // TFalse if it wasn't.
62 //
63 // See wxVector<T> in vector.h for usage example
64 template<bool Cond, typename TTrue, typename TFalse>
65 struct wxIf
66 {
67     typedef typename wxPrivate::wxIfImpl<Cond>
68                      ::template Result<TTrue, TFalse>::value
69             value;
70 };
71 
72 #endif // _WX_META_IF_H_
73