1+
2+ // PreserveStorage.h: Rcpp R/C++ interface class library -- helper class
3+ //
4+ // Copyright (C) 2013 - 2020 Romain Francois
5+ // Copyright (C) 2021 Romain Francois and Iñaki Ucar
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+
122#ifndef Rcpp_PreserveStorage_h
223#define Rcpp_PreserveStorage_h
324
@@ -7,15 +28,20 @@ namespace Rcpp{
728 class PreserveStorage {
829 public:
930
10- PreserveStorage () : data(R_NilValue){}
31+ PreserveStorage () : data(R_NilValue), token(R_NilValue) {}
1132
1233 ~PreserveStorage (){
13- Rcpp_ReleaseObject (data ) ;
34+ Rcpp_ReleaseObject (token ) ;
1435 data = R_NilValue;
36+ token = R_NilValue;
1537 }
1638
1739 inline void set__ (SEXP x){
18- data = Rcpp_ReplaceObject (data, x) ;
40+ if (data != x) {
41+ data = x;
42+ Rcpp_ReleaseObject (token);
43+ token = Rcpp_PreserveObject (data);
44+ }
1945
2046 // calls the update method of CLASS
2147 // this is where to react to changes in the underlying SEXP
@@ -28,7 +54,9 @@ namespace Rcpp{
2854
2955 inline SEXP invalidate__ (){
3056 SEXP out = data ;
57+ Rcpp_ReleaseObject (token);
3158 data = R_NilValue ;
59+ token = R_NilValue ;
3260 return out ;
3361 }
3462
@@ -48,6 +76,7 @@ namespace Rcpp{
4876
4977 private:
5078 SEXP data ;
79+ SEXP token ;
5180 } ;
5281
5382}
0 commit comments