if.h 2.0 KB

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