Rcpp Version 0.10.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
RObject.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // RObject.h: Rcpp R/C++ interface class library -- general R object wrapper
4 //
5 // Copyright (C) 2009 - 2012 Dirk Eddelbuettel and Romain Francois
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 
22 #ifndef Rcpp_RObject_h
23 #define Rcpp_RObject_h
24 
25 #define R_NO_REMAP
26 #include <R.h>
27 #include <Rcpp/macros/macros.h>
28 #include <Rinternals.h>
29 #include <vector>
30 #include <string>
31 
32 namespace Rcpp{
33 
34  class RObject {
35  public:
36 
40  RObject() ;
41 
47  RObject(SEXP x) ;
48 
52  RObject( const RObject& other ) ;
53 
57  RObject& operator=( const RObject& other ) ;
58 
62  RObject& operator=( SEXP other ) ;
63 
68  virtual ~RObject() ;
69 
73  inline operator SEXP() const { return m_sexp ; }
74 
78  inline bool inherits(const char* clazz) const { return ::Rf_inherits( m_sexp, clazz) ; }
79 
80  /* attributes */
81 
85  std::vector<std::string> attributeNames() const ;
86 
90  bool hasAttribute( const std::string& attr) const ;
91 
106  public:
107 
112  AttributeProxy( const RObject& v, const std::string& attr_name) ;
113 
119 
126  template <typename T> AttributeProxy& operator=(const T& rhs) ;
127 
132  template <typename T> operator T() const ;
133 
134  inline operator SEXP() const { return get() ; }
135 
136  private:
137  const RObject& parent;
138  std::string attr_name ;
139 
140  SEXP get() const ;
141  void set(SEXP x) const ;
142  } ;
143 
147  class SlotProxy {
148  public:
158  SlotProxy( const RObject& v, const std::string& name) ;
159 
166  SlotProxy& operator=(const SlotProxy& rhs) ;
167 
173  template <typename T> SlotProxy& operator=(const T& rhs) ;
174 
180  template <typename T> operator T() const ;
181  inline operator SEXP() const { return get() ; }
182 
183  private:
184  const RObject& parent;
185  std::string slot_name ;
186 
187  SEXP get() const ;
188  void set(SEXP x ) const;
189  } ;
190  friend class SlotProxy ;
191 
208  AttributeProxy attr( const std::string& name) const ;
209 
213  inline bool isNULL() const{ return Rf_isNull(m_sexp) ; }
214 
218  inline int sexp_type() const { return TYPEOF(m_sexp) ; }
219 
223  inline SEXP asSexp() const { return m_sexp ; }
224 
228  inline bool isObject() const { return ::Rf_isObject(m_sexp) ;}
229 
233  inline bool isS4() const { return ::Rf_isS4(m_sexp) ; }
234 
240  bool hasSlot(const std::string& name) const ;
241 
247  SlotProxy slot(const std::string& name) const ;
248 
249  protected:
250 
256  void setSEXP(SEXP x) ;
257 
262  SEXP m_sexp ;
263 
264  };
265 
266 } // namespace Rcpp
267 
268 #endif