CXXR (C++ R) API
HandleVector.hpp
Go to the documentation of this file.
1 /*CXXR $Id: HandleVector.hpp 1030 2011-01-04 14:00:23Z 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-11 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  *
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; either version 2.1 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, a copy is available at
32  * http://www.r-project.org/Licenses/
33  */
34 
40 #ifndef HANDLEVECTOR_HPP
41 #define HANDLEVECTOR_HPP 1
42 
43 #include <algorithm>
44 
45 #include "localization.h"
46 #include "R_ext/Error.h"
47 #include "CXXR/Allocator.hpp"
48 #include "CXXR/GCRoot.h"
49 #include "CXXR/VectorBase.h"
50 
51 namespace CXXR {
65  template <typename T, SEXPTYPE ST>
66  class HandleVector : public VectorBase {
67  private:
68  typedef std::vector<Handle<T>, Allocator<Handle<T> > > Vector;
69  public:
70  typedef typename Vector::const_iterator const_iterator;
71 
80  explicit HandleVector(size_t sz, T* init = 0)
81  : VectorBase(ST, sz), m_data(sz, Handle<T>(init))
82  {}
83 
93  : VectorBase(pattern), m_data(pattern.m_data.size())
94  {
95  for (unsigned int i = 0; i < m_data.size(); ++i) {
96  // Use copy constructor to apply object copying logic:
97  Handle<T> handle(pattern.m_data[i]);
98  m_data[i] = handle;
99  }
100  }
101 
109  Handle<T>& operator[](unsigned int index)
110  {
111  return m_data[index];
112  }
113 
121  const T* operator[](unsigned int index) const
122  {
123  return m_data[index];
124  }
125 
131  const_iterator begin() const
132  {
133  return m_data.begin();
134  }
135 
141  const_iterator end() const
142  {
143  return m_data.end();
144  }
145 
155  inline static const char* staticTypeName();
156 
157  // Virtual function of RObject:
158  const char* typeName() const;
159 
160  // Virtual function of GCNode:
161  void visitReferents(const_visitor* v) const;
162  protected:
168 
169  // Virtual function of GCNode:
170  void detachReferents();
171  private:
172  Vector m_data;
173 
174  // Not implemented. Declared to prevent
175  // compiler-generated version:
176  HandleVector& operator=(const HandleVector&);
177 
178  friend class ElementProxy;
179  };
180 
181  template <typename T, SEXPTYPE ST>
182  const char* HandleVector<T, ST>::typeName() const
183  {
185  }
186 
187  template <typename T, SEXPTYPE ST>
189  {
191  m_data.clear();
192  }
193 
194  template <typename T, SEXPTYPE ST>
196  {
198  typename Vector::const_iterator end = m_data.end();
199  for (typename Vector::const_iterator it = m_data.begin();
200  it != end; ++it) {
201  T* node = *it;
202  if (node)
203  (*v)(node);
204  }
205  }
206 } // namespace CXXR
207 
208 #endif // HANDLEVECTOR_HPP