Rcpp Version 0.9.10
SingleLogicalResult.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
00002 //
00003 // SingleLogicalResult.h: Rcpp R/C++ interface class library -- 
00004 //
00005 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
00006 //
00007 // This file is part of Rcpp.
00008 //
00009 // Rcpp is free software: you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 2 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // Rcpp is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
00021 
00022 #ifndef Rcpp__sugar__SingleLogicalResult_h
00023 #define Rcpp__sugar__SingleLogicalResult_h
00024 
00025 namespace Rcpp{
00026 namespace sugar{  
00027 
00028 template <bool> 
00029 class forbidden_conversion ;
00030 
00031 template <> 
00032 class forbidden_conversion<true>{} ;
00033 
00034 template <bool x>
00035 class conversion_to_bool_is_forbidden : 
00036         conversion_to_bool_is_forbidden<x>{
00037         public:
00038                 void touch(){}
00039 }; 
00040 
00041 template <bool NA,typename T>
00042 class SingleLogicalResult {
00043 public:
00044         const static int UNRESOLVED = -5 ;
00045         
00046         SingleLogicalResult() : result(UNRESOLVED) {} ;
00047         
00048         void apply(){
00049                 if( result == UNRESOLVED ){
00050                         static_cast<T&>(*this).apply() ;
00051                 }
00052         }
00053         
00054         inline bool is_true(){
00055                 apply() ;
00056                 return result == TRUE ;
00057         }
00058         
00059         inline bool is_false(){
00060                 apply() ;
00061                 return result == FALSE ;
00062         }
00063         
00064         inline bool is_na(){
00065                 apply() ;
00066                 return Rcpp::traits::is_na<LGLSXP>( result ) ;
00067         }
00068         
00069         operator SEXP(){
00070                 apply() ;
00071                 return Rf_ScalarLogical( result ) ;
00072         }
00073         
00074         operator bool(){
00075                 conversion_to_bool_is_forbidden<!NA> x ;
00076                 x.touch() ;
00077                 return is_true() ;
00078         }
00079         
00080         inline int size(){ return 1 ; }
00081         
00082         inline int get(){
00083                 apply();
00084                 return result;
00085         }
00086         
00087 protected:
00088         int result ;
00089         inline void set(int x){ result = x ;}
00090         inline void reset(){ set(UNRESOLVED) ; }
00091         inline void set_true(){ set(TRUE); }
00092         inline void set_false(){ set(FALSE); }
00093         inline void set_na(){ set(NA_LOGICAL); }
00094         inline bool is_unresolved(){ return result == UNRESOLVED ; }
00095 } ;
00096 
00097 }
00098 }
00099 
00100 
00101 
00102 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines