CXXR (C++ R) API
Rdefines.h
1 /*CXXR $Id: Rdefines.h 1348 2013-02-25 17:49:03Z arr $
2  *CXXR
3  *CXXR This file is part of CXXR, a project to refactor the R interpreter
4  *CXXR into C++. It may consist in whole or in part of program code and
5  *CXXR documentation taken from the R project itself, incorporated into
6  *CXXR CXXR (and possibly MODIFIED) under the terms of the GNU General Public
7  *CXXR Licence.
8  *CXXR
9  *CXXR CXXR is Copyright (C) 2008-13 Andrew R. Runnalls, subject to such other
10  *CXXR copyrights and copyright restrictions as may be stated below.
11  *CXXR
12  *CXXR CXXR is not part of the R project, and bugs and other issues should
13  *CXXR not be reported via r-bugs or other R project channels; instead refer
14  *CXXR to the CXXR website.
15  *CXXR */
16 
17 /*
18  * R : A Computer Language for Statistical Data Analysis
19  * Copyright (C) 1999-2007 The R Core Team.
20  *
21  * This program is free software; you can redistribute it and/or modify
22  * it under the terms of the GNU Lesser General Public License as published by
23  * the Free Software Foundation; either version 2.1 of the License, or
24  * (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29  * GNU Lesser General Public License for more details.
30  *
31  * You should have received a copy of the GNU Lesser General Public License
32  * along with this program; if not, a copy is available at
33  * http://www.r-project.org/Licenses/
34  */
35 
36 #ifndef R_DEFINES_H
37 #define R_DEFINES_H
38 
39 #if !defined(R_R_H) && !defined(R_S_H)
40 /* user forgot to include R.h or S.h */
41 # include <R_ext/Memory.h>
42 # include <R_ext/RS.h>
43 #endif
44 
45 #include <Rinternals.h>
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 /*
52  * Much is from John Chambers' "Programming With Data".
53  * Some of this is from Doug Bates.
54  *
55  * It is presented here to support a joint programming style which
56  * will work in both R and S. In particular it helps with:
57  *
58  * 1. S/R <-> CORBA code.
59  * 2. S/R <-> Java Code.
60  *
61  * And to hide some internal nastiness.
62  */
63 
64 
65 /*
66  * Added some macros defined in S.h from Splus 5.1
67  */
68 
69 #define NULL_USER_OBJECT R_NilValue
70 
71 #define AS_LOGICAL(x) coerceVector(x,LGLSXP)
72 #define AS_INTEGER(x) coerceVector(x,INTSXP)
73 #define AS_NUMERIC(x) coerceVector(x,REALSXP)
74 #define AS_CHARACTER(x) coerceVector(x,STRSXP)
75 #define AS_COMPLEX(x) coerceVector(x,CPLXSXP)
76 #define AS_VECTOR(x) coerceVector(x,VECSXP)
77 #define AS_LIST(x) coerceVector(x,VECSXP)
78 #define AS_RAW(x) coerceVector(x,RAWSXP)
79 
80 #define IS_LOGICAL(x) Rf_isLogical(x)
81 #define IS_INTEGER(x) Rf_isInteger(x)
82 #define IS_NUMERIC(x) Rf_isReal(x)
83 #define IS_CHARACTER(x) Rf_isString(x)
84 #define IS_COMPLEX(x) Rf_isComplex(x)
85 /* NB: is this right? It means atomic or VECSXP or EXPRSXP */
86 #define IS_VECTOR(x) Rf_isVector(x)
87 /* And this cannot be right: isVectorList(x)? */
88 #define IS_LIST(x) IS_VECTOR(x)
89 #define IS_RAW(x) (TYPEOF(x) == RAWSXP)
90 
91 #define NEW_LOGICAL(n) Rf_allocVector(LGLSXP,n)
92 #define NEW_INTEGER(n) Rf_allocVector(INTSXP,n)
93 #define NEW_NUMERIC(n) Rf_allocVector(REALSXP,n)
94 #define NEW_CHARACTER(n) Rf_allocVector(STRSXP,n)
95 #define NEW_COMPLEX(n) Rf_allocVector(CPLXSXP,n)
96 #define NEW_LIST(n) Rf_allocVector(VECSXP,n)
97 #define NEW_STRING(n) NEW_CHARACTER(n)
98 #define NEW_RAW(n) Rf_allocVector(RAWSXP,n)
99 
100 #define LOGICAL_POINTER(x) LOGICAL(x)
101 #define INTEGER_POINTER(x) INTEGER(x)
102 #define NUMERIC_POINTER(x) REAL(x)
103 #define COMPLEX_POINTER(x) COMPLEX(x)
104 #define RAW_POINTER(x) RAW(x)
105 
106 /* The following are not defined in `Programming with Data' but are
107  defined in S.h in Svr4 */
108 
109 /*
110  * Note that LIST_DATA and RAW_DATA are missing.
111  * This is consistent with Svr4.
112  */
113 
114 #define LOGICAL_DATA(x) (LOGICAL(x))
115 #define INTEGER_DATA(x) (INTEGER(x))
116 #define DOUBLE_DATA(x) (REAL(x))
117 #define NUMERIC_DATA(x) (REAL(x))
118 #define COMPLEX_DATA(x) (COMPLEX(x))
119 
120 #define LOGICAL_VALUE(x) asLogical(x)
121 #define INTEGER_VALUE(x) asInteger(x)
122 #define NUMERIC_VALUE(x) asReal(x)
123 #define CHARACTER_VALUE(x) CHAR(asChar(x))
124 #define STRING_VALUE(x) CHAR(asChar(x))
125 #define LIST_VALUE(x) error("the 'value' of a list object is not defined")
126 #define RAW_VALUE(x) error("the 'value' of a raw object is not defined")
127 
128 #define SET_ELEMENT(x, i, val) SET_VECTOR_ELT(x, i, val)
129 #define GET_ATTR(x,what) getAttrib(x, what)
130 #define GET_CLASS(x) getAttrib(x, R_ClassSymbol)
131 #define GET_DIM(x) getAttrib(x, R_DimSymbol)
132 #define GET_DIMNAMES(x) getAttrib(x, R_DimNamesSymbol)
133 #define GET_COLNAMES(x) GetColNames(x)
134 #define GET_ROWNAMES(x) GetRowNames(x)
135 #define GET_LEVELS(x) getAttrib(x, R_LevelsSymbol)
136 #define GET_TSP(x) getAttrib(x, R_TspSymbol)
137 #define GET_NAMES(x) getAttrib(x, R_NamesSymbol)
138 #define SET_ATTR(x, what, n) setAttrib(x, what, n)
139 #define SET_CLASS(x, n) setAttrib(x, R_ClassSymbol, n)
140 #define SET_DIM(x, n) setAttrib(x, R_DimSymbol, n)
141 #define SET_DIMNAMES(x, n) setAttrib(x, R_DimNamesSymbol, n)
142 #define SET_LEVELS(x, l) setAttrib(x, R_LevelsSymbol, l)
143 #define SET_NAMES(x, n) setAttrib(x, R_NamesSymbol, n)
144 #define GET_LENGTH(x) length(x)
145 #define SET_LENGTH(x, n) (x = lengthgets(x, n))
146 
147 #define GET_SLOT(x, what) R_do_slot(x, what)
148 #define SET_SLOT(x, what, value) R_do_slot_assign(x, what, value)
149 
150 #define MAKE_CLASS(what) R_do_MAKE_CLASS(what)
151 /* NEW_OBJECT is recommended; NEW is for green book compatibility */
152 #define NEW_OBJECT(class_def) R_do_new_object(class_def)
153 #define NEW(class_def) R_do_new_object(class_def)
154 
155 #define s_object SEXPREC
156 #define S_EVALUATOR
157 #ifndef TRUE
158 #define TRUE 1
159 #endif
160 #ifndef FALSE
161 #define FALSE 0
162 #endif
163 
164 #define COPY_TO_USER_STRING(x) mkChar(x)
165 #define CREATE_STRING_VECTOR(x) mkChar(x)
166 
167 #define CREATE_FUNCTION_CALL(name, argList) createFunctionCall(name, argList)
168 
169 #define EVAL(x) eval(x,R_GlobalEnv)
170 
171 #ifdef __cplusplus
172 }
173 #endif
174 
175 #endif