CXXR (C++ R)

UncachedString.h

Go to the documentation of this file.
00001 /*CXXR $Id: UncachedString.h 1254 2012-01-03 15:54:03Z arr $
00002  *CXXR
00003  *CXXR This file is part of CXXR, a project to refactor the R interpreter
00004  *CXXR into C++.  It may consist in whole or in part of program code and
00005  *CXXR documentation taken from the R project itself, incorporated into
00006  *CXXR CXXR (and possibly MODIFIED) under the terms of the GNU General Public
00007  *CXXR Licence.
00008  *CXXR 
00009  *CXXR CXXR is Copyright (C) 2008-12 Andrew R. Runnalls, subject to such other
00010  *CXXR copyrights and copyright restrictions as may be stated below.
00011  *CXXR 
00012  *CXXR CXXR is not part of the R project, and bugs and other issues should
00013  *CXXR not be reported via r-bugs or other R project channels; instead refer
00014  *CXXR to the CXXR website.
00015  *CXXR */
00016 
00017 /*
00018  *  R : A Computer Language for Statistical Data Analysis
00019  *  Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
00020  *  Copyright (C) 1999-2006   The R Development Core Team.
00021  *
00022  *  This program is free software; you can redistribute it and/or modify
00023  *  it under the terms of the GNU General Public License as published by
00024  *  the Free Software Foundation; either version 2.1 of the License, or
00025  *  (at your option) any later version.
00026  *
00027  *  This program is distributed in the hope that it will be useful,
00028  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00029  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00030  *  GNU Lesser General Public License for more details.
00031  *
00032  *  You should have received a copy of the GNU General Public License
00033  *  along with this program; if not, a copy is available at
00034  *  http://www.r-project.org/Licenses/
00035  */
00036 
00041 #ifndef UNCACHEDSTRING_H
00042 #define UNCACHEDSTRING_H
00043 
00044 #include "CXXR/String.h"
00045 
00046 #ifdef __cplusplus
00047 
00048 #include <string>
00049 
00050 namespace CXXR {
00063     class UncachedString : public String {
00064     public:
00075         explicit UncachedString(size_t sz, cetype_t encoding = CE_NATIVE)
00076             : String(sz, encoding), m_databytes(sz + 1), m_data(m_short_string)
00077         {
00078             allocData(sz);
00079             setCString(m_data);
00080         }
00081 
00092         explicit UncachedString(const std::string& str,
00093                                 cetype_t encoding = CE_NATIVE);
00094 
00102         char* ptr()
00103         {
00104             invalidateHash();
00105             return m_data;
00106         }
00107 
00112         static const char* staticTypeName()
00113         {
00114             return "char (uncached)";
00115         }
00116 
00117         // Virtual function of RObject:
00118         const char* typeName() const;
00119     private:
00120         // Max. strlen stored internally:
00121         static const size_t s_short_strlen = 7;
00122 
00123         size_t m_databytes;  // includes trailing null byte
00124         char* m_data;  // pointer to the string's data block.
00125 
00126         // If there are fewer than s_short_strlen+1 chars in the
00127         // string (including the trailing null), it is stored here,
00128         // internally to the UncachedString object, rather than via a separate
00129         // allocation from CXXR::MemoryBank.  We put this last, so that it
00130         // will be adjacent to any trailing redzone.
00131         char m_short_string[s_short_strlen + 1];
00132 
00133         // Not implemented yet.  Declared to prevent
00134         // compiler-generated versions:
00135         UncachedString(const UncachedString&);
00136         UncachedString& operator=(const UncachedString&);
00137 
00138         // Declared private to ensure that UncachedString objects are
00139         // allocated only using 'new'.
00140         ~UncachedString()
00141         {
00142             if (m_data != m_short_string)
00143                 MemoryBank::deallocate(m_data, m_databytes);
00144         }
00145 
00146         // Initialise m_data, if necessary by allocating a data block
00147         // from MemoryBank:
00148         void allocData(size_t sz);
00149     };
00150 }  // namespace CXXR
00151 
00152 extern "C" {
00153 
00154 #endif /* __cplusplus */
00155 
00165 #ifndef __cplusplus
00166     char *CHAR_RW(SEXP x);
00167 #else
00168     inline char *CHAR_RW(SEXP x)
00169     {
00170         using namespace CXXR;
00171         return SEXP_downcast<UncachedString*>(x)->ptr();
00172     }
00173 #endif
00174 
00175 #ifdef __cplusplus
00176 }  // extern "C"
00177 #endif
00178 
00179 #endif /* UNCACHEDSTRING_H */