Rcpp Version 1.1.2
Loading...
Searching...
No Matches
Promise.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// Promise.h: Rcpp R/C++ interface class library -- promises (PROMSXP)
4//
5// Copyright (C) 2010 - 2025 Dirk Eddelbuettel and Romain François
6// Copyright (C) 2026 Dirk Eddelbuettel, Romain François and Iñaki Ucar
7//
8// This file is part of Rcpp.
9//
10// Rcpp is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 2 of the License, or
13// (at your option) any later version.
14//
15// Rcpp is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
22
23#ifndef Rcpp_Promise_h
24#define Rcpp_Promise_h
25
26namespace Rcpp{
27
28 RCPP_API_CLASS(Promise_Impl) {
29 public:
30 RCPP_GENERATE_CTOR_ASSIGN(Promise_Impl)
31
32 Promise_Impl( SEXP x){
33 if( TYPEOF(x) != PROMSXP) {
34 const char* fmt = "Not a promise: [type = %s].";
35 throw not_compatible(fmt, Rf_type2char(TYPEOF(x)));
36 }
37
38 Storage::set__(x) ;
39 }
40
44 int seen() const {
45 return PRSEEN(Storage::get__());
46 }
47
51 SEXP value() const{
52 if (!was_evaluated()) throw unevaluated_promise();
53 return PRVALUE(Storage::get__());
54 }
55
56 bool was_evaluated() const {
57#if R_VERSION < R_Version(4,6,0)
58 return PRVALUE(Storage::get__()) != R_UnboundValue ;
59#else
60 SEXP env = environment();
61 R_BindingType_t bt = R_GetBindingType(Storage::get__(), env);
62 return bt != R_BindingTypeUnbound;
63#endif
64 }
65
69 ExpressionVector expression() const {
70 return ExpressionVector(PRCODE( Storage::get__() )) ;
71 }
72
76 Environment environment() const {
77 return Environment( PRENV(Storage::get__() ) ) ;
78 }
79
80 inline void update(SEXP data){}
81
82 } ;
83
84 typedef Promise_Impl<PreserveStorage> Promise ;
85
86} // namespace
87
88#endif
#define RCPP_GENERATE_CTOR_ASSIGN(__CLASS__)
Definition interface.h:21
#define RCPP_API_CLASS(__CLASS__)
Definition interface.h:49
Rcpp API.
Definition algo.h:28
Promise_Impl< PreserveStorage > Promise
Definition Promise.h:84
void update(SEXP)
Vector< EXPRSXP > ExpressionVector
Environment_Impl< PreserveStorage > Environment