Rcpp Version 1.0.9
NamesProxy.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_proxy_NamesProxy_h
19 #define Rcpp_proxy_NamesProxy_h
20 
21 namespace Rcpp{
22 
23 template <typename CLASS>
25 public:
26 
27  class NamesProxy : public GenericProxy<NamesProxy> {
28  public:
29  NamesProxy( CLASS& v) : parent(v){} ;
30 
31  /* lvalue uses */
33  if( this != &rhs) set( rhs.get() ) ;
34  return *this ;
35  }
36 
37  template <typename T>
38  NamesProxy& operator=(const T& rhs);
39 
40  template <typename T> operator T() const;
41 
42  private:
43  CLASS& parent;
44 
45  SEXP get() const {
46  return RCPP_GET_NAMES(parent.get__()) ;
47  }
48 
49  void set(SEXP x) {
50  Shield<SEXP> safe_x(x);
51 
52  /* check if we can use a fast version */
53  if( TYPEOF(x) == STRSXP && parent.size() == Rf_length(x) ){
54  Rf_namesgets(parent, x);
55  } else {
56  /* use the slower and more flexible version (callback to R) */
57  SEXP namesSym = Rf_install( "names<-" );
58  Shield<SEXP> call(Rf_lang3(namesSym, parent, x));
59  Shield<SEXP> new_vec(Rcpp_fast_eval(call, R_GlobalEnv));
60  parent.set__(new_vec);
61  }
62 
63  }
64 
65  } ;
66 
67  class const_NamesProxy : public GenericProxy<const_NamesProxy>{
68  public:
69  const_NamesProxy( const CLASS& v) : parent(v){} ;
70 
71  template <typename T> operator T() const;
72 
73  private:
74  const CLASS& parent;
75 
76  SEXP get() const {
77  return RCPP_GET_NAMES(parent.get__()) ;
78  }
79 
80  } ;
81 
83  return NamesProxy( static_cast<CLASS&>(*this) ) ;
84  }
85 
87  return const_NamesProxy(static_cast<const CLASS&>(*this) ) ;
88  }
89 
90 
91 } ;
92 
93 }
94 
95 #endif
NamesProxy & operator=(const NamesProxy &rhs)
Definition: NamesProxy.h:32
NamesProxy & operator=(const T &rhs)
NamesProxy names()
Definition: NamesProxy.h:82
const_NamesProxy names() const
Definition: NamesProxy.h:86
#define RCPP_GET_NAMES(x)
Definition: macros.h:45
Rcpp API.
Definition: algo.h:28
SEXP Rcpp_fast_eval(SEXP expr, SEXP env)
Definition: Rcpp_eval.h:68