CXXR (C++ R) API
FunctionBase.h
Go to the documentation of this file.
1 /*CXXR $Id: FunctionBase.h 1351 2013-03-08 15:12:28Z 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) 1995, 1996 Robert Gentleman and Ross Ihaka
20  * Copyright (C) 1999-2007 The R Development Core Team.
21  *
22  * This program is free software; you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation; either version 2.1 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program; if not, a copy is available at
34  * http://www.r-project.org/Licenses/
35  */
36 
42 #ifndef FUNCTIONBASE_H
43 #define FUNCTIONBASE_H
44 
45 #include "CXXR/RObject.h"
46 
47 #ifdef __cplusplus
48 
49 #include <boost/serialization/nvp.hpp>
50 
51 #include "CXXR/SEXP_downcast.hpp"
52 
53 namespace CXXR {
54  class ArgList;
55  class Expression;
56  class Environment;
57 
60  class FunctionBase : public RObject {
61  public:
75  virtual RObject* apply(ArgList* arglist, Environment* env,
76  const Expression* call) const = 0;
77 
82  static void enableTracing(bool on)
83  {
84  s_tracing_enabled = on;
85  }
86 
95  static bool isA(const RObject* obj)
96  {
97  // We could of course use dynamic_cast here, but the
98  // following is probably faster:
99  if (!obj) return false;
100  SEXPTYPE st = obj->sexptype();
101  return st == CLOSXP || st == BUILTINSXP || st == SPECIALSXP;
102  }
103 
111  void maybeTrace(const Expression* call) const
112  {
113  if (traced() && tracingEnabled())
114  reportCall(call);
115  }
116 
121  static const char* staticTypeName()
122  {
123  return "(function type)";
124  }
125 
131  void setTracing(bool on)
132  {
133  m_traced = on;
134  }
135 
141  bool traced() const
142  {
143  return m_traced;
144  }
145 
150  static bool tracingEnabled()
151  {
152  return s_tracing_enabled;
153  }
154  protected:
158  explicit FunctionBase(SEXPTYPE stype)
159  : RObject(stype), m_traced(false)
160  {}
161 
166  FunctionBase(const FunctionBase& pattern)
167  : RObject(pattern), m_traced(false)
168  {}
169 
170  virtual ~FunctionBase() {}
171  private:
172  friend class boost::serialization::access;
173 
174  static bool s_tracing_enabled;
175  bool m_traced;
176 
177  static void reportCall(const Expression* call);
178 
179  // Fields not serialised here are set up by the constructor.
180  template <class Archive>
181  void serialize(Archive& ar, const unsigned int version)
182  {
183  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(RObject);
184  }
185  };
186 } // namespace CXXR
187 
188 extern "C" {
189 #endif /* __cplusplus */
190 
199 #ifndef __cplusplus
200  int RTRACE(SEXP x);
201 #else
202  inline int RTRACE(SEXP x)
203  {
204  using namespace CXXR;
205  if (!x) return 0;
206  const FunctionBase& f = *SEXP_downcast<const FunctionBase*>(x);
207  return f.traced();
208  }
209 #endif
210 
219 #ifndef __cplusplus
220  void SET_RTRACE(SEXP x, int v);
221 #else
222  inline void SET_RTRACE(SEXP x, int v)
223  {
224  using namespace CXXR;
225  FunctionBase* f = SEXP_downcast<FunctionBase*>(x);
226  f->setTracing(v != 0);
227  }
228 #endif
229 
230 #ifdef __cplusplus
231 }
232 #endif
233 
234 #endif /* FUNCTIONBASE_H */