-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsfinae.h
186 lines (172 loc) · 6.77 KB
/
sfinae.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Copyright (C) 2015-2016 Felix Salfelder
* Author: Felix Salfelder
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*------------------------------------------------------------------
* identify container types ins template specialization.
*
* TODO: inherited types.
* http://stackoverflow.com/questions/15616730/cannot-trait-an-inherited-class-as-base-with-sfinae
*
*/
#ifndef GALA_SFINAE_H
#define GALA_SFINAE_H
#include <deque>
#include <set>
#ifdef HAVE_STX_BTREE_SET_H
#include <stx/btree_set.h>
#endif
// #endif
#include <boost/container/flat_set.hpp>
#include <vector>
#include <unordered_set>
#include "trace.h"
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
namespace gala{
namespace sfinae{
/*--------------------------------------------------------------------------*/
struct any{
int dummy; // required for size!=0?
bool operator==(const any&)const{ unreachable(); return true; }
typedef int value_type; // dummy.
};
/*--------------------------------------------------------------------------*/
}
}
/*--------------------------------------------------------------------------*/
namespace std {
template<>
struct hash<gala::sfinae::any>{
size_t operator()(const gala::sfinae::any&)const{ unreachable(); return 0; }
};
} // std
/*--------------------------------------------------------------------------*/
namespace gala{
namespace sfinae{
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<class A, class B=any, class T=void, class...>
struct is_set{
static constexpr bool value = false;
};
/*--------------------------------------------------------------------------*/
template<class S, class T>
struct is_set<S, typename std::enable_if < std::is_same<
std::set<typename S::value_type, typename S::key_compare, typename S::allocator_type >, S
>::value, any >::type , T>{ //
typedef T type;
static constexpr bool value = true;
};
#ifdef HAVE_STX_BTREE_SET_H
template<class S, class T>
struct is_set<S, typename std::enable_if < std::is_same<
stx::btree_set<typename S::value_type, typename S::key_compare, typename S::allocator_type >, S
>::value, any >::type , T>{ //
typedef T type;
static constexpr bool value = true;
};
#endif
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<class S, class T>
struct is_set<S, typename std::enable_if < std::is_same<
boost::container::flat_set<typename S::value_type, typename S::key_compare, typename S::allocator_type >, S
>::value, any >::type , T>{ //
typedef T type;
static constexpr bool value = true;
};
/*--------------------------------------------------------------------------*/
template<class S, class T>
struct is_set<S, typename std::enable_if < std::is_same<
boost::container::flat_set<typename S::value_type, typename S::key_compare, typename S::container_type >, S
>::value, any >::type , T>{ //
typedef T type;
static constexpr bool value = true;
};
/*--------------------------------------------------------------------------*/
template<class S, class T>
struct is_set<S, typename std::enable_if < std::is_same<
boost::container::flat_set<typename S::value_type, typename S::key_compare>, S
>::value, any >::type , T>{ //
typedef T type;
static constexpr bool value = true;
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<class A, class B=any, class T=void>
struct is_seq{
static constexpr bool value = false;
};
/*--------------------------------------------------------------------------*/
template<class S, class T>
struct is_seq<S, typename std::enable_if < std::is_same<
std::vector<any, typename S::allocator_type >, S
>::value, any >::type , T>{ //
typedef T type;
static constexpr bool value = true;
};
template<class S, class T>
struct is_seq<S, typename std::enable_if < std::is_same<
std::deque<any, typename S::allocator_type >, S
>::value, any >::type , T>{ //
typedef T type;
static constexpr bool value = true;
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<class A, class B=any, class T=void>
struct is_hash{
static constexpr bool value = false;
};
/*--------------------------------------------------------------------------*/
template<class S, class T>
struct is_hash<S, typename std::enable_if < std::is_same<
std::hash<typename S::value_type>, S
>::value, any >::type , T>{ //
typedef T type;
static constexpr bool value = true;
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<class A, class B=any, class T=void>
struct is_vector{
static constexpr bool value = false;
};
/*--------------------------------------------------------------------------*/
template<class S, class T>
struct is_vector<S, typename std::enable_if < std::is_same<
std::vector<typename S::value_type, typename S::allocator_type >, S
>::value, any >::type , T>{ //
typedef T type;
static constexpr bool value = true;
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// convenience wrappers for non-determined types
/*--------------------------------------------------------------------------*/
template<template<class T, typename... > class C>
struct is_set_tpl : is_set<C<any> > {};
/*--------------------------------------------------------------------------*/
template<template<class T, typename... > class C>
struct is_seq_tpl : is_seq<C<any> > {};
/*--------------------------------------------------------------------------*/
template<template<class T, typename... > class C>
struct is_vec_tpl : is_vector<C<any> > {};
/*--------------------------------------------------------------------------*/
}//sfinae
} // namespace gala
#endif