|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 // 00003 // Environment.h: Rcpp R/C++ interface class library -- access R environments 00004 // 00005 // Copyright (C) 2009 - 2011 Dirk Eddelbuettel and Romain Francois 00006 // 00007 // This file is part of Rcpp. 00008 // 00009 // Rcpp is free software: you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 2 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // Rcpp is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00021 00022 #ifndef Rcpp_Environment_h 00023 #define Rcpp_Environment_h 00024 00025 #include <RcppCommon.h> 00026 #include <Rcpp/exceptions.h> 00027 #include <Rcpp/Evaluator.h> 00028 #include <Rcpp/Symbol.h> 00029 #include <Rcpp/Language.h> 00030 00031 #include <Rcpp/RObject.h> 00032 00033 namespace Rcpp{ 00034 00035 class Environment: public RObject{ 00036 public: 00037 00042 class Binding { 00043 public: 00050 Binding( Environment& env, const std::string& name) ; 00051 00055 bool active() const ; 00056 00060 bool locked() const ; 00061 00065 bool exists() const ; 00066 00070 void lock( ) ; 00071 00075 void unlock() ; 00076 00077 /* lvalue uses */ 00078 00091 Binding& operator=(const Binding& rhs) ; 00092 00103 Binding& operator=(SEXP rhs) ; 00104 00121 template <typename WRAPPABLE> 00122 Binding& operator=(const WRAPPABLE& rhs){ 00123 env.assign( name, rhs ) ; 00124 return *this ; 00125 } 00126 00127 /* rvalue */ 00135 template <typename T> 00136 operator T() const{ 00137 SEXP x = env.get(name) ; 00138 return as<T>(x) ; 00139 } 00140 00141 00142 private: 00146 Environment& env ; 00147 00151 std::string name ; 00152 } ; 00153 00161 const Binding operator[]( const std::string& name) const ; 00162 00166 Binding operator[](const std::string& name) ; 00167 00168 friend class Binding ; 00169 00170 Environment() ; 00171 00177 Environment(SEXP x); 00178 00182 Environment(const Environment& other); 00183 00187 Environment& operator=(const Environment& other); 00188 00194 Environment( const std::string& name ); 00195 00202 Environment( int pos ); 00203 00207 ~Environment() ; 00208 00217 SEXP ls(bool all) const ; 00218 00226 SEXP get(const std::string& name) const ; 00227 00235 SEXP find( const std::string& name) const; 00236 00245 bool exists( const std::string& name ) const ; 00246 00258 bool assign( const std::string& name, SEXP x ) const; 00259 00268 template <typename WRAPPABLE> 00269 bool assign( const std::string& name, const WRAPPABLE& x) const { 00270 return assign( name, wrap( x ) ) ; 00271 } 00272 00277 bool isLocked() const ; 00278 00282 bool remove( const std::string& name ); 00283 00289 void lock(bool bindings) ; 00290 00297 void lockBinding(const std::string& name); 00298 00305 void unlockBinding(const std::string& name); 00306 00315 bool bindingIsLocked(const std::string& name) const; 00316 00326 bool bindingIsActive(const std::string& name) const; 00327 00331 bool is_user_database() const ; 00332 00336 static Environment global_env(); 00337 00341 static Environment empty_env(); 00342 00346 static Environment base_env(); 00347 00351 static Environment base_namespace(); 00352 00356 static Environment Rcpp_namespace(); 00357 00365 static Environment namespace_env(const std::string& ); 00366 00370 Environment parent() const; 00371 00375 Environment new_child(bool hashed) ; 00376 00377 }; 00378 00379 } // namespace Rcpp 00380 00381 #endif