CXXR (C++ R)
RConverters.h
1 /*CXXR $Id: RConverters.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) 1998-2006 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  * Application Routines, typically implemented in ../appl/
37  * ---------------------------------------------- ========
38  */
39 
40 #ifndef R_CCONVERTERS_H
41 #define R_CCONVERTERS_H
42 
43 #include <Rinternals.h>
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 #define freeCConverter RC_freeCConverter
50 #define R_addToCConverter RC_addToCConverter
51 #define R_converterMatchClass RC_converterMatchClass
52 #define R_converterMatchClass RC_converterMatchClass
53 #define R_getToCConverterByDescription RC_getToCConverterByDescription
54 #define R_getToCConverterByIndex RC_getToCConverterByIndex
55 #define R_getToCConverterByIndex RC_getToCConverterByIndex
56 #define R_removeToCConverter RC_removeToCConverter
57 
58  /* Context information controlling how the conversion is performed, passed
59  to RObjToCPtr in dotcode.c and the different user level converters. */
60  typedef struct {
61  int naok;
62  int narg;
63  int dup;
64  int Fort;
65 
66  char const * name ;
67 
68  SEXP classes;
69  } R_CConvertInfo;
70 
71 
72  /* Typedefs for structs defined below with future/cross-referencing. */
73  typedef struct RtoCConverter R_toCConverter;
74  typedef struct RFromCConvertInfo R_FromCConvertInfo;
75 
76 
77  /* The matching routine which determines whether the converter can process the given SEXP. */
78  typedef Rboolean (*R_ToCPredicate)(SEXP obj, R_CConvertInfo *info, R_toCConverter *el);
79 
80  /* The converter routine that returns the value to be passed to the C routine.
81  (We may have to make the return type a union to handle the different types.) */
82  typedef void* (*R_ToCConverter)(SEXP obj, R_CConvertInfo *info, R_toCConverter *el);
83 
84  /* The reverse converter from the C argument to the R object that is returned via the .C() call. */
85  typedef SEXP (*R_FromCConverter)(void *value, SEXP arg, R_FromCConvertInfo *info,
86  R_toCConverter *el);
87 
88 
89  /* The definition of the converter element which are stored as a linked list. */
90  struct RtoCConverter {
91  R_ToCPredicate matcher; /* check if converter applies to R object */
92  R_ToCConverter converter; /* convert the R object to C value */
93  R_FromCConverter reverse; /* convert the C value back to an R object. */
94  char *description; /* user-readable string describing the converter. */
95  void *userData; /* additional information used in (any of) the matcher,
96  converter, and reverse routines to parameterize them. */
97  Rboolean active; /* allows the converter to be in the list but ignored temporarily. */
98 
99  R_toCConverter *next; /* next element in the linked list. */
100  };
101 
102 
103  /* Information used to convert C values to R objects at the end of do_dotCode() */
104  struct RFromCConvertInfo {
105  const char *functionName; /* the name of the routine being called (S's name for it). */
106 
107  int argIndex; /* the pariticular argument being processed. */
108 
109  /* We provide all of the arguments and the corresponding C values.
110  This gives the full context of the call to the reverse converter */
111  SEXP allArgs;
112  void **cargs;
113  int nargs;
114  };
115 
116 
117 
118  /* Internal mechanism for employing the converter mechanism, used in do_dotCode() in dotcode.c */
119  void *Rf_convertToC(SEXP s, R_CConvertInfo *info, int *success, R_toCConverter **converter);
120 
121  /* Converter management facilities. */
122  R_toCConverter *R_addToCConverter(R_ToCPredicate match, R_ToCConverter converter,
123  R_FromCConverter reverse,
124  void *userData, char *desc);
125  R_toCConverter *R_getToCConverterByIndex(int which);
126  R_toCConverter *R_getToCConverterByDescription(const char *desc);
127  void R_removeToCConverter(R_toCConverter *el);
128 
129  Rboolean R_converterMatchClass(SEXP obj, R_CConvertInfo *inf, R_toCConverter *el);
130  void freeCConverter(R_toCConverter *el);
131 
132  /* The routines corresponding to the .Internal() providing access to the
133  management facilities of the converter list.
134  */
135  SEXP do_getNumRtoCConverters(SEXP call, SEXP op, SEXP args, SEXP env);
136  SEXP do_getRtoCConverterDescriptions(SEXP call, SEXP op, SEXP args, SEXP env);
137  SEXP do_getRtoCConverterStatus(SEXP call, SEXP op, SEXP args, SEXP env);
138  SEXP do_setToCConverterActiveStatus(SEXP call, SEXP op, SEXP args, SEXP env);
139 
140 
141 #ifdef __cplusplus
142 }
143 #endif
144 
145 #endif /* R_CCONVERTERS_H */