Rcpp Version 1.0.9
SingleLogicalResult.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // SingleLogicalResult.h: Rcpp R/C++ interface class library --
4 //
5 // Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__sugar__SingleLogicalResult_h
23 #define Rcpp__sugar__SingleLogicalResult_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <bool>
30 
31 template <>
32 class forbidden_conversion<true>{} ;
33 
34 template <bool x>
37  public:
38  void touch(){}
39 };
40 
41 template <bool NA,typename T>
43 public:
44  const static int UNRESOLVED = -5 ;
45 
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() ;
67  }
68 
69  inline operator SEXP(){
70  return get_sexp() ;
71  }
72 
73  inline operator bool(){
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 } ;
100 
101 }
102 }
103 
104 
105 
106 #endif
bool is_na< LGLSXP >(int x)
Definition: is_na.h:57
Rcpp API.
Definition: algo.h:28