-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathis.h
More file actions
75 lines (62 loc) · 2.35 KB
/
is.h
File metadata and controls
75 lines (62 loc) · 2.35 KB
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
#ifndef Rcpp__is__h
#define Rcpp__is__h
namespace Rcpp{
namespace internal{
// simple implementation, for most default types
template <typename T> bool is__simple( SEXP x) ;
template <typename T>
struct Is {
inline bool test(SEXP /* x */){
return false ;
}
} ;
template <> struct Is<RObject> ;
template <> struct Is<DataFrame> ;
template <> struct Is<S4> ;
template <> struct Is<Reference> ;
template <> struct Is<Formula> ;
template <> struct Is<Function> ;
template <int RTYPE> struct TypeofIs{
inline bool test(SEXP x){
return TYPEOF(x) == RTYPE ;
}
} ;
template <int RTYPE> struct IsMatrixTypeofIs{
inline bool test(SEXP x){
return TYPEOF(x) == RTYPE && Rf_isMatrix(x) ;
}
} ;
template <typename T>
struct PrimitiveIs{
inline bool test(SEXP x){
return Rf_length(x) == 1 &&
TYPEOF(x) == Rcpp::traits::r_sexptype_traits<T>::rtype ;
}
};
template <int RTYPE> struct Is< Vector<RTYPE> > : TypeofIs<RTYPE>{} ;
template <int RTYPE> struct Is< Matrix<RTYPE> > : IsMatrixTypeofIs<RTYPE>{} ;
template <> struct Is<Environment> : TypeofIs<ENVSXP> {} ;
template <> struct Is<Pairlist> : TypeofIs<LISTSXP> {} ;
template <> struct Is<Promise> : TypeofIs<PROMSXP> {} ;
template <> struct Is<Symbol> : TypeofIs<SYMSXP> {} ;
template <> struct Is<WeakReference> : TypeofIs<WEAKREFSXP> {} ;
template <> struct Is<Language> : TypeofIs<LANGSXP> {} ;
template <typename T>
struct is_type{
typedef typename std::conditional<
Rcpp::traits::is_primitive<T>::value,
typename PrimitiveIs<T>::type,
typename Is<T>::type
>::type type ;
} ;
}
/** identify if an x can be seen as the T type
*
* example:
* bool is_list = is<List>( x ) ;
*/
template <typename T> bool is( SEXP x ){
return typename internal::is_type<T>::type().test(x) ;
}
} // Rcpp
#endif