CXXR (C++ R) API
StringVector.h
Go to the documentation of this file.
1 /*CXXR $Id: StringVector.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-2006 The R Development Core Team.
21  * Andrew Runnalls (C) 2008
22  *
23  * This program is free software; you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation; either version 2.1 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31  * GNU Lesser General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program; if not, a copy is available at
35  * http://www.r-project.org/Licenses/
36  */
37 
44 #ifndef STRINGVECTOR_H
45 #define STRINGVECTOR_H
46 
47 #include "CXXR/String.h"
48 
49 #ifdef __cplusplus
50 
51 #include <iostream>
52 
53 #include "CXXR/FixedVector.hpp"
54 #include "CXXR/SEXP_downcast.hpp"
55 
56 namespace CXXR {
57  // Template specializations:
58  namespace ElementTraits {
59  template <>
60  struct NAFunc<RHandle<String> > {
61  const RHandle<String>& operator()() const
62  {
63  static RHandle<String> na(String::NA());
64  return na;
65  }
66  };
67 
68  template <>
69  struct IsNA<RHandle<String> > {
70  bool operator()(const RHandle<String>& t) const
71  {
72  typedef RHandle<String> T;
73  return t == NA<T>();
74  }
75  };
76  }
77 
78  // Make the default handle for a String point to a blank string:
79  template <>
80  inline RHandle<String>::RHandle()
81  : GCEdge<String>(String::blank())
82  {}
83 
84  template <>
85  inline const char* FixedVector<RHandle<String>, STRSXP>::staticTypeName()
86  {
87  return "character";
88  }
89 
97 
110  inline StringVector* asStringVector(const std::string& str,
111  cetype_t encoding = CE_NATIVE)
112  {
113  GCStackRoot<String> cs(String::obtain(str, encoding));
114  return CXXR_NEW(StringVector(1, cs));
115  }
116 
121  void strdump(std::ostream& os, const StringVector& sv,
122  std::size_t margin = 0);
123 } // namespace CXXR
124 
125 BOOST_CLASS_EXPORT_KEY(CXXR::StringVector)
126 
127 extern "C" {
128 #endif /* __cplusplus */
129 
135 #ifndef __cplusplus
136  Rboolean Rf_isString(SEXP s);
137 #else
138  inline Rboolean Rf_isString(SEXP s)
139  {
140  return Rboolean(s && TYPEOF(s) == STRSXP);
141  }
142 #endif
143 
152 void SET_STRING_ELT(SEXP x, int i, SEXP v);
153 
165 #ifndef __cplusplus
166 SEXP STRING_ELT(SEXP x, int i);
167 #else
168 inline SEXP STRING_ELT(SEXP x, int i)
169 {
170  using namespace CXXR;
171  return (*SEXP_downcast<StringVector*>(x, false))[i];
172 }
173 #endif
174 
175 #ifdef __cplusplus
176 }
177 #endif
178 
179 #endif /* STRINGVECTOR_H */