CXXR (C++ R) API
UncachedString.h
Go to the documentation of this file.
1 /*CXXR $Id: UncachedString.h 1254 2012-01-03 15:54: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-12 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  *
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 
41 #ifndef UNCACHEDSTRING_H
42 #define UNCACHEDSTRING_H
43 
44 #include "CXXR/String.h"
45 
46 #ifdef __cplusplus
47 
48 #include <string>
49 
50 namespace CXXR {
63  class UncachedString : public String {
64  public:
75  explicit UncachedString(size_t sz, cetype_t encoding = CE_NATIVE)
76  : String(sz, encoding), m_databytes(sz + 1), m_data(m_short_string)
77  {
78  allocData(sz);
79  setCString(m_data);
80  }
81 
92  explicit UncachedString(const std::string& str,
93  cetype_t encoding = CE_NATIVE);
94 
102  char* ptr()
103  {
104  invalidateHash();
105  return m_data;
106  }
107 
112  static const char* staticTypeName()
113  {
114  return "char (uncached)";
115  }
116 
117  // Virtual function of RObject:
118  const char* typeName() const;
119  private:
120  // Max. strlen stored internally:
121  static const size_t s_short_strlen = 7;
122 
123  size_t m_databytes; // includes trailing null byte
124  char* m_data; // pointer to the string's data block.
125 
126  // If there are fewer than s_short_strlen+1 chars in the
127  // string (including the trailing null), it is stored here,
128  // internally to the UncachedString object, rather than via a separate
129  // allocation from CXXR::MemoryBank. We put this last, so that it
130  // will be adjacent to any trailing redzone.
131  char m_short_string[s_short_strlen + 1];
132 
133  // Not implemented yet. Declared to prevent
134  // compiler-generated versions:
136  UncachedString& operator=(const UncachedString&);
137 
138  // Declared private to ensure that UncachedString objects are
139  // allocated only using 'new'.
140  ~UncachedString()
141  {
142  if (m_data != m_short_string)
143  MemoryBank::deallocate(m_data, m_databytes);
144  }
145 
146  // Initialise m_data, if necessary by allocating a data block
147  // from MemoryBank:
148  void allocData(size_t sz);
149  };
150 } // namespace CXXR
151 
152 extern "C" {
153 
154 #endif /* __cplusplus */
155 
165 #ifndef __cplusplus
166  char *CHAR_RW(SEXP x);
167 #else
168  inline char *CHAR_RW(SEXP x)
169  {
170  using namespace CXXR;
171  return SEXP_downcast<UncachedString*>(x)->ptr();
172  }
173 #endif
174 
175 #ifdef __cplusplus
176 } // extern "C"
177 #endif
178 
179 #endif /* UNCACHEDSTRING_H */