1 // Copyright (C) 2013 Romain Francois and Kevin Ushey 2 // 3 // This file is part of Rcpp. 4 // 5 // Rcpp is free software: you can redistribute it and/or modify it 6 // under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 2 of the License, or 8 // (at your option) any later version. 9 // 10 // Rcpp is distributed in the hope that it will be useful, but 11 // WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 17 18 #ifndef Rcpp__protection_Shield_h 19 #define Rcpp__protection_Shield_h 20 21 namespace Rcpp{ 22 Rcpp_protect(SEXP x)23 inline SEXP Rcpp_protect(SEXP x){ 24 if( x != R_NilValue ) PROTECT(x) ; 25 return x ; 26 } 27 Rcpp_unprotect(int i)28 inline void Rcpp_unprotect(int i){ 29 // Prefer this function over UNPROTECT() in Rcpp so that all 30 // balance checks errors by rchk are contained at one location (#892) 31 UNPROTECT(i); 32 } 33 34 template <typename T> 35 class Shield{ 36 public: Shield(SEXP t_)37 Shield( SEXP t_) : t(Rcpp_protect(t_)){} ~Shield()38 ~Shield(){ 39 if( t != R_NilValue ) Rcpp_unprotect(1) ; 40 } 41 SEXP()42 operator SEXP() const { return t; } 43 SEXP t ; 44 45 private: 46 Shield( const Shield& ) ; 47 Shield& operator=( const Shield& ) ; 48 } ; 49 50 51 52 } 53 54 #endif 55