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 <sparsehash/internal/sparseconfig.h>
53_START_GOOGLE_NAMESPACE_
54
55// Types small_ and big_ are guaranteed such that sizeof(small_) <
56// sizeof(big_)
57typedef char small_;
58
59struct big_ {
60 char dummy[2];
61};
62
63// Identity metafunction.
64template <class T>
65struct 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
74template<class T, T v>
75struct integral_constant {
76 static const T value = v;
77 typedef T value_type;
78 typedef integral_constant<T, v> type;
79};
80
81template <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_.
87typedef integral_constant<bool, true> true_type;
88typedef integral_constant<bool, false> false_type;
89typedef true_type true_;
90typedef false_type false_;
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.
95template<bool cond, typename A, typename B>
96struct if_{
97 typedef A type;
98};
99
100template<typename A, typename B>
101struct 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.
111template<typename A, typename B>
112struct type_equals_ : public false_ {
113};
114
115template<typename A>
116struct type_equals_<A, A> : public true_ {
117};
118
119// and_ is a template && operator.
120// and_<A, B>::value evaluates "A::value && B::value".
121template<typename A, typename B>
122struct 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".
127template<typename A, typename B>
128struct or_ : public integral_constant<bool, (A::value || B::value)> {
129};
130
131
132_END_GOOGLE_NAMESPACE_
133
134#endif // BASE_TEMPLATE_UTIL_H_
135