|
| 1 | +// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- |
| 2 | +// |
| 3 | +// lgrow.h: Rcpp R/C++ interface class library -- grow a (LANGSXP) pairlist |
| 4 | +// |
| 5 | +// Copyright (C) 2010 - 2025 Dirk Eddelbuettel, Romain Francois, and Kevin Ushey |
| 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_lgrow_h |
| 23 | +#define Rcpp_lgrow_h |
| 24 | + |
| 25 | +#include <RcppCommon.h> |
| 26 | +#include <Rcpp/Named.h> |
| 27 | + |
| 28 | +namespace Rcpp { |
| 29 | + |
| 30 | +inline SEXP lgrow(SEXP head, SEXP tail) { |
| 31 | + return Rf_lcons(head, tail); |
| 32 | +} |
| 33 | + |
| 34 | +namespace internal { |
| 35 | + |
| 36 | +// for Named objects |
| 37 | +template <typename T> |
| 38 | +inline SEXP lgrow__dispatch(Rcpp::traits::true_type, const T& head, SEXP tail) { |
| 39 | + Shield<SEXP> y(wrap(head.object)); |
| 40 | + Shield<SEXP> x(Rf_lcons(y, tail)); |
| 41 | + SEXP headNameSym = Rf_install(head.name.c_str()); |
| 42 | + SET_TAG(x, headNameSym); |
| 43 | + return x; |
| 44 | +} |
| 45 | + |
| 46 | +// for all other objects |
| 47 | +template <typename T> |
| 48 | +inline SEXP lgrow__dispatch(Rcpp::traits::false_type, const T& head, SEXP tail) { |
| 49 | + return lgrow(wrap(head), tail); |
| 50 | +} |
| 51 | + |
| 52 | +} // internal |
| 53 | + |
| 54 | +template <typename T> |
| 55 | +SEXP lgrow(const T& head, SEXP tail) { |
| 56 | + Shield<SEXP> y(tail); |
| 57 | + return internal::lgrow__dispatch(typename traits::is_named<T>::type(), head, y); |
| 58 | +} |
| 59 | + |
| 60 | +inline SEXP lgrow(const char* head, SEXP tail) { |
| 61 | + Shield<SEXP> y(tail); |
| 62 | + return lgrow(Rf_mkString(head), y); |
| 63 | +} |
| 64 | + |
| 65 | +template <typename T1> |
| 66 | +SEXP langlist(const T1& t1) { |
| 67 | + return lgrow(t1, R_NilValue); |
| 68 | +} |
| 69 | + |
| 70 | +template <typename T, typename... TArgs> |
| 71 | +SEXP langlist(const T& t1, const TArgs&... args) { |
| 72 | + return lgrow(t1, langlist(args...)); |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace Rcpp |
| 76 | + |
| 77 | +#endif |
0 commit comments