| 40 | |
| 41 | template <bool NA,typename T> |
| 42 | class SingleLogicalResult { |
| 43 | public: |
| 44 | const static int UNRESOLVED = -5 ; |
| 45 | |
| 46 | SingleLogicalResult() : result(UNRESOLVED) {} ; |
| 47 | |
| 48 | void apply(){ |
| 49 | if( result == UNRESOLVED ){ |
| 50 | static_cast<T&>(*this).apply() ; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | inline bool is_true(){ |
| 55 | apply() ; |
| 56 | return result == TRUE ; |
| 57 | } |
| 58 | |
| 59 | inline bool is_false(){ |
| 60 | apply() ; |
| 61 | return result == FALSE ; |
| 62 | } |
| 63 | |
| 64 | inline bool is_na(){ |
| 65 | apply() ; |
| 66 | return Rcpp::traits::is_na<LGLSXP>( result ) ; |
| 67 | } |
| 68 | |
| 69 | inline operator SEXP(){ |
| 70 | return get_sexp() ; |
| 71 | } |
| 72 | |
| 73 | inline operator bool(){ |
| 74 | conversion_to_bool_is_forbidden<!NA> x ; |
| 75 | x.touch() ; |
| 76 | return is_true() ; |
| 77 | } |
| 78 | |
| 79 | inline int size(){ return 1 ; } |
| 80 | |
| 81 | inline int get(){ |
| 82 | apply(); |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | inline SEXP get_sexp(){ |
| 87 | apply() ; |
| 88 | return Rf_ScalarLogical( result ) ; |
| 89 | } |
| 90 | |
| 91 | protected: |
| 92 | int result ; |
| 93 | inline void set(int x){ result = x ;} |
| 94 | inline void reset(){ set(UNRESOLVED) ; } |
| 95 | inline void set_true(){ set(TRUE); } |
| 96 | inline void set_false(){ set(FALSE); } |
| 97 | inline void set_na(){ set(NA_LOGICAL); } |
| 98 | inline bool is_unresolved(){ return result == UNRESOLVED ; } |
| 99 | } ; |