NEST  2.6.0,not_revisioned_source_dir@0
template_util.h
Go to the documentation of this file.
1 // Copyright 2005 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // ----
31 //
32 // Template metaprogramming utility functions.
33 //
34 // This code is compiled directly on many platforms, including client
35 // platforms like Windows, Mac, and embedded systems. Before making
36 // any changes here, make sure that you're not breaking any platforms.
37 //
38 //
39 // The names choosen here reflect those used in tr1 and the boost::mpl
40 // library, there are similar operations used in the Loki library as
41 // well. I prefer the boost names for 2 reasons:
42 // 1. I think that portions of the Boost libraries are more likely to
43 // be included in the c++ standard.
44 // 2. It is not impossible that some of the boost libraries will be
45 // included in our own build in the future.
46 // Both of these outcomes means that we may be able to directly replace
47 // some of these with boost equivalents.
48 //
49 #ifndef BASE_TEMPLATE_UTIL_H_
50 #define BASE_TEMPLATE_UTIL_H_
51 
52 #include <sparseconfig.h>
53 _START_GOOGLE_NAMESPACE_
54 
55 // Types small_ and big_ are guaranteed such that sizeof(small_) <
56 // sizeof(big_)
57 typedef char small_;
58 
59 struct big_ {
60  char dummy[2];
61 };
62 
63 // Identity metafunction.
64 template <class T>
65 struct identity_ {
66  typedef T type;
67 };
68 
69 // integral_constant, defined in tr1, is a wrapper for an integer
70 // value. We don't really need this generality; we could get away
71 // with hardcoding the integer type to bool. We use the fully
72 // general integer_constant for compatibility with tr1.
73 
74 template<class T, T v>
76  static const T value = v;
77  typedef T value_type;
79 };
80 
81 template <class T, T v> const T integral_constant<T, v>::value;
82 
83 
84 // Abbreviations: true_type and false_type are structs that represent boolean
85 // true and false values. Also define the boost::mpl versions of those names,
86 // true_ and false_.
89 typedef true_type true_;
91 
92 // if_ is a templatized conditional statement.
93 // if_<cond, A, B> is a compile time evaluation of cond.
94 // if_<>::type contains A if cond is true, B otherwise.
95 template<bool cond, typename A, typename B>
96 struct if_{
97  typedef A type;
98 };
99 
100 template<typename A, typename B>
101 struct if_<false, A, B> {
102  typedef B type;
103 };
104 
105 
106 // type_equals_ is a template type comparator, similar to Loki IsSameType.
107 // type_equals_<A, B>::value is true iff "A" is the same type as "B".
108 //
109 // New code should prefer base::is_same, defined in base/type_traits.h.
110 // It is functionally identical, but is_same is the standard spelling.
111 template<typename A, typename B>
112 struct type_equals_ : public false_ {
113 };
114 
115 template<typename A>
116 struct type_equals_<A, A> : public true_ {
117 };
118 
119 // and_ is a template && operator.
120 // and_<A, B>::value evaluates "A::value && B::value".
121 template<typename A, typename B>
122 struct and_ : public integral_constant<bool, (A::value && B::value)> {
123 };
124 
125 // or_ is a template || operator.
126 // or_<A, B>::value evaluates "A::value || B::value".
127 template<typename A, typename B>
128 struct or_ : public integral_constant<bool, (A::value || B::value)> {
129 };
130 
131 
132 _END_GOOGLE_NAMESPACE_
133 
134 #endif // BASE_TEMPLATE_UTIL_H_
Definition: template_util.h:75
integral_constant< bool, true > true_type
Definition: template_util.h:87
_START_GOOGLE_NAMESPACE_ typedef char small_
Definition: template_util.h:57
A type
Definition: template_util.h:97
Definition: template_util.h:128
true_type true_
Definition: template_util.h:89
T type
Definition: template_util.h:66
Definition: template_util.h:65
static const T value
Definition: template_util.h:76
char dummy[2]
Definition: template_util.h:60
T value_type
Definition: template_util.h:77
integral_constant< T, v > type
Definition: template_util.h:78
Definition: template_util.h:96
Definition: template_util.h:112
Definition: template_util.h:122
const Name A("A")
Definition: nest_names.h:40
Definition: template_util.h:59
B type
Definition: template_util.h:102
false_type false_
Definition: template_util.h:90
integral_constant< bool, false > false_type
Definition: template_util.h:88