williamr@2
|
1 |
// (C) Copyright Jeremy Siek 2000.
|
williamr@2
|
2 |
// Distributed under the Boost Software License, Version 1.0. (See
|
williamr@2
|
3 |
// accompanying file LICENSE_1_0.txt or copy at
|
williamr@2
|
4 |
// http://www.boost.org/LICENSE_1_0.txt)
|
williamr@2
|
5 |
|
williamr@2
|
6 |
// The ct_if implementation that avoids partial specialization is
|
williamr@2
|
7 |
// based on the IF class by Ulrich W. Eisenecker and Krzysztof
|
williamr@2
|
8 |
// Czarnecki.
|
williamr@2
|
9 |
|
williamr@2
|
10 |
#ifndef BOOST_CT_IF_HPP
|
williamr@2
|
11 |
#define BOOST_CT_IF_HPP
|
williamr@2
|
12 |
|
williamr@2
|
13 |
#include <boost/config.hpp>
|
williamr@2
|
14 |
|
williamr@2
|
15 |
/*
|
williamr@2
|
16 |
There is a bug in the Borland compiler with regards to using
|
williamr@2
|
17 |
integers to specialize templates. This made it hard to use ct_if in
|
williamr@2
|
18 |
the graph library. Changing from 'ct_if' to 'ct_if_t' fixed the
|
williamr@2
|
19 |
problem.
|
williamr@2
|
20 |
*/
|
williamr@2
|
21 |
|
williamr@2
|
22 |
#include <boost/type_traits/integral_constant.hpp> // true_type and false_type
|
williamr@2
|
23 |
|
williamr@2
|
24 |
namespace boost {
|
williamr@2
|
25 |
|
williamr@2
|
26 |
struct ct_if_error { };
|
williamr@2
|
27 |
|
williamr@2
|
28 |
template <class A, class B>
|
williamr@2
|
29 |
struct ct_and { typedef false_type type; };
|
williamr@2
|
30 |
template <> struct ct_and<true_type,true_type> { typedef true_type type; };
|
williamr@2
|
31 |
|
williamr@2
|
32 |
template <class A> struct ct_not { typedef ct_if_error type; };
|
williamr@2
|
33 |
template <> struct ct_not<true_type> { typedef false_type type; };
|
williamr@2
|
34 |
template <> struct ct_not<false_type> { typedef true_type type; };
|
williamr@2
|
35 |
|
williamr@2
|
36 |
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
williamr@2
|
37 |
|
williamr@2
|
38 |
// agurt, 15/sep/02: in certain cases Borland has problems with
|
williamr@2
|
39 |
// choosing the right 'ct_if' specialization even though 'cond'
|
williamr@2
|
40 |
// _does_ equal '1'; the easiest way to fix it is to make first
|
williamr@2
|
41 |
// 'ct_if' non-type template parameter boolean.
|
williamr@2
|
42 |
#if !defined(__BORLANDC__)
|
williamr@2
|
43 |
template <bool cond, class A, class B>
|
williamr@2
|
44 |
struct ct_if { typedef ct_if_error type; };
|
williamr@2
|
45 |
template <class A, class B>
|
williamr@2
|
46 |
struct ct_if<true, A, B> { typedef A type; };
|
williamr@2
|
47 |
template <class A, class B>
|
williamr@2
|
48 |
struct ct_if<false, A, B> { typedef B type; };
|
williamr@2
|
49 |
#else
|
williamr@2
|
50 |
template <bool cond, class A, class B>
|
williamr@2
|
51 |
struct ct_if { typedef A type; };
|
williamr@2
|
52 |
template <class A, class B>
|
williamr@2
|
53 |
struct ct_if<false, A, B> { typedef B type; };
|
williamr@2
|
54 |
#endif
|
williamr@2
|
55 |
|
williamr@2
|
56 |
template <class cond, class A, class B>
|
williamr@2
|
57 |
struct ct_if_t { typedef ct_if_error type; };
|
williamr@2
|
58 |
template <class A, class B>
|
williamr@2
|
59 |
struct ct_if_t<true_type, A, B> { typedef A type; };
|
williamr@2
|
60 |
template <class A, class B>
|
williamr@2
|
61 |
struct ct_if_t<false_type, A, B> { typedef B type; };
|
williamr@2
|
62 |
|
williamr@2
|
63 |
#else
|
williamr@2
|
64 |
|
williamr@2
|
65 |
namespace detail {
|
williamr@2
|
66 |
|
williamr@2
|
67 |
template <int condition, class A, class B> struct IF;
|
williamr@2
|
68 |
template <int condition> struct SlectSelector;
|
williamr@2
|
69 |
struct SelectFirstType;
|
williamr@2
|
70 |
struct SelectSecondType;
|
williamr@2
|
71 |
|
williamr@2
|
72 |
struct SelectFirstType {
|
williamr@2
|
73 |
template<class A, class B>
|
williamr@2
|
74 |
struct Template { typedef A type; };
|
williamr@2
|
75 |
};
|
williamr@2
|
76 |
|
williamr@2
|
77 |
struct SelectSecondType {
|
williamr@2
|
78 |
template<class A, class B>
|
williamr@2
|
79 |
struct Template { typedef B type; };
|
williamr@2
|
80 |
};
|
williamr@2
|
81 |
|
williamr@2
|
82 |
template<int condition>
|
williamr@2
|
83 |
struct SlectSelector {
|
williamr@2
|
84 |
typedef SelectFirstType type;
|
williamr@2
|
85 |
};
|
williamr@2
|
86 |
|
williamr@2
|
87 |
template <>
|
williamr@2
|
88 |
struct SlectSelector<0> {
|
williamr@2
|
89 |
typedef SelectSecondType type;
|
williamr@2
|
90 |
};
|
williamr@2
|
91 |
|
williamr@2
|
92 |
} // namespace detail
|
williamr@2
|
93 |
|
williamr@2
|
94 |
template<int condition, class A, class B>
|
williamr@2
|
95 |
struct ct_if
|
williamr@2
|
96 |
{
|
williamr@2
|
97 |
typedef typename detail::SlectSelector<condition>::type Selector;
|
williamr@2
|
98 |
typedef typename Selector::template Template<A, B>::type type;
|
williamr@2
|
99 |
};
|
williamr@2
|
100 |
|
williamr@2
|
101 |
template <class cond, class A, class B>
|
williamr@2
|
102 |
struct ct_if_t {
|
williamr@2
|
103 |
typedef typename ct_if<cond::value, A, B>::type type;
|
williamr@2
|
104 |
};
|
williamr@2
|
105 |
|
williamr@2
|
106 |
#endif
|
williamr@2
|
107 |
|
williamr@2
|
108 |
} // namespace boost
|
williamr@2
|
109 |
|
williamr@2
|
110 |
#endif // BOOST_CT_IF_HPP
|
williamr@2
|
111 |
|