Rcpp Version 1.0.7
ProtectedProxy.h
Go to the documentation of this file.
1 // Copyright (C) 2013 Romain Francois
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_ProtectedProxy_h
19 #define Rcpp_ProtectedProxy_h
20 
21 namespace Rcpp{
22 
23  template <typename T> T as(SEXP x);
24 
25  template <class XPtrClass>
27  public:
28 
29  class ProtectedProxy : public GenericProxy<ProtectedProxy> {
30  public:
31  ProtectedProxy( XPtrClass& xp_ ): xp(xp_){}
32 
33  template <typename U>
34  ProtectedProxy& operator=( const U& u) {
35  set(Shield<SEXP>(wrap(u)));
36  return *this;
37  }
38 
39  template <typename U>
40  operator U() const {
41  return as<U>( get() );
42  }
43 
44  operator SEXP() const{
45  return get() ;
46  }
47 
48  private:
49  XPtrClass& xp ;
50 
51  inline SEXP get() const {
52  return R_ExternalPtrProtected(xp) ;
53  }
54 
55  inline void set( SEXP x){
56  R_SetExternalPtrProtected( xp, x ) ;
57  }
58 
59  } ;
60 
61  class const_ProtectedProxy : public GenericProxy<const_ProtectedProxy>{
62  public:
63  const_ProtectedProxy( const XPtrClass& xp_ ): xp(xp_){}
64 
65  template <typename U>
66  operator U() const {
67  return as<U>( get() );
68  }
69 
70  operator SEXP() const{
71  return get() ;
72  }
73 
74  private:
75  const XPtrClass& xp ;
76 
77  inline SEXP get() const {
78  return R_ExternalPtrProtected(xp) ;
79  }
80 
81  } ;
82 
83  ProtectedProxy prot(){
84  return ProtectedProxy( static_cast<XPtrClass&>(*this) ) ;
85  }
86 
87  const_ProtectedProxy prot() const{
88  return const_ProtectedProxy( static_cast<const XPtrClass&>(*this) ) ;
89  }
90 
91 
92  } ;
93 
94 }
95 
96 #endif
Rcpp::ProtectedProxyPolicy::const_ProtectedProxy::xp
const XPtrClass & xp
Definition: ProtectedProxy.h:75
Rcpp::ProtectedProxyPolicy::ProtectedProxy::ProtectedProxy
ProtectedProxy(XPtrClass &xp_)
Definition: ProtectedProxy.h:31
Rcpp::ProtectedProxyPolicy::const_ProtectedProxy::get
SEXP get() const
Definition: ProtectedProxy.h:77
Rcpp::ProtectedProxyPolicy::const_ProtectedProxy::const_ProtectedProxy
const_ProtectedProxy(const XPtrClass &xp_)
Definition: ProtectedProxy.h:63
Rcpp::Shield
Definition: Shield.h:35
Rcpp
Rcpp API.
Definition: algo.h:28
Rcpp::ProtectedProxyPolicy::ProtectedProxy::set
void set(SEXP x)
Definition: ProtectedProxy.h:55
Rcpp::ProtectedProxyPolicy::prot
ProtectedProxy prot()
Definition: ProtectedProxy.h:83
Rcpp::ProtectedProxyPolicy::const_ProtectedProxy
Definition: ProtectedProxy.h:61
Rcpp::wrap
SEXP wrap(const Date &date)
Definition: Date.h:38
Rcpp::ProtectedProxyPolicy
Definition: ProtectedProxy.h:26
Rcpp::ProtectedProxyPolicy::prot
const_ProtectedProxy prot() const
Definition: ProtectedProxy.h:87
Rcpp::ProtectedProxyPolicy::ProtectedProxy::get
SEXP get() const
Definition: ProtectedProxy.h:51
Rcpp::GenericProxy
Definition: GenericProxy.h:24
Rcpp::ProtectedProxyPolicy::ProtectedProxy::xp
XPtrClass & xp
Definition: ProtectedProxy.h:49
Rcpp::ProtectedProxyPolicy::ProtectedProxy::operator=
ProtectedProxy & operator=(const U &u)
Definition: ProtectedProxy.h:34
Rcpp::ProtectedProxyPolicy::ProtectedProxy
Definition: ProtectedProxy.h:29
Rcpp::as
T as(SEXP x)
Definition: as.h:151