CXXR (C++ R)
StdFrame.hpp
Go to the documentation of this file.
1 /*CXXR $Id: StdFrame.hpp 1397 2013-08-16 18:25:59Z 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  *
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 STDFRAME_HPP
43 #define STDFRAME_HPP
44 
45 #include <tr1/unordered_map>
46 #include <boost/serialization/access.hpp>
47 #include <boost/serialization/base_object.hpp>
48 #include <boost/serialization/nvp.hpp>
49 #include <boost/serialization/split_member.hpp>
50 #include <boost/serialization/unordered_map.hpp>
51 
52 #include "CXXR/Allocator.hpp"
53 #include "CXXR/Frame.hpp"
54 
55 namespace CXXR {
58  class StdFrame : public Frame {
59  private:
60  typedef
61  std::tr1::unordered_map<const Symbol*, Binding,
62  std::tr1::hash<const Symbol*>,
63  std::equal_to<const Symbol*>,
64  CXXR::Allocator<std::pair<const Symbol* const,
65  Binding> >
66  > map;
67  public:
77  explicit StdFrame(std::size_t initial_capacity = 15);
78  // Why 15? Because if the implementation uses a prime number
79  // hash table sizing policy, this will result in the
80  // allocation of a hash table array comprising 31 buckets. On
81  // a 32-bit architecture, this will fit well into two 64-byte
82  // cache lines.
83 
84  // Virtual functions of Frame (qv):
85 #ifdef __GNUG__
86  __attribute__((hot,fastcall))
87 #endif
88  Binding* binding(const Symbol* symbol);
89 
90  const Binding* binding(const Symbol* symbol) const;
91  BindingRange bindingRange() const;
92  StdFrame* clone() const;
93  void lockBindings();
94  std::size_t size() const;
95  private:
96  friend class boost::serialization::access;
97 
98  map m_map;
99 
100  // Declared private to ensure that StdFrame objects are
101  // created only using 'new':
102  ~StdFrame() {}
103 
104  // Not (yet) implemented. Declared to prevent
105  // compiler-generated versions:
106  StdFrame& operator=(const StdFrame&);
107 
108  template<class Archive>
109  void load(Archive& ar, const unsigned int version);
110 
111  template<class Archive>
112  void save(Archive& ar, const unsigned int version) const;
113 
114  template<class Archive>
115  void serialize(Archive& ar, const unsigned int version) {
116  boost::serialization::split_member(ar, *this, version);
117  }
118 
119  // Virtual functions of Frame (qv):
120  void v_clear();
121  bool v_erase(const Symbol* symbol);
122  Binding* v_obtainBinding(const Symbol* symbol);
123  };
124 
125  // ***** Implementation of non-inlined templated members *****
126 
127  template<class Archive>
128  void StdFrame::load(Archive& ar, const unsigned int version)
129  {
130  ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(Frame);
131  size_t numberOfBindings;
132  ar >> BOOST_SERIALIZATION_NVP(numberOfBindings);
133  for (size_t i = 0; i < numberOfBindings; ++i) {
134  GCStackRoot<Symbol> symbol;
135  GCNPTR_SERIALIZE(ar, symbol);
136  Binding* binding = obtainBinding(symbol);
137  ar >> boost::serialization::make_nvp("binding", *binding);
138  }
139  }
140 
141  template<class Archive>
142  void StdFrame::save(Archive& ar, const unsigned int version) const
143  {
144  ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(Frame);
145  size_t numberOfBindings = size();
146  ar << BOOST_SERIALIZATION_NVP(numberOfBindings);
147  for (map::const_iterator it = m_map.begin();
148  it != m_map.end(); ++it) {
149  const Symbol* symbol = (*it).first;
150  const Binding& binding = (*it).second;
151  GCNPTR_SERIALIZE(ar, symbol);
152  ar << BOOST_SERIALIZATION_NVP(binding);
153  }
154  }
155 } // namespace CXXR
156 
157 BOOST_CLASS_EXPORT_KEY(CXXR::StdFrame)
158 
159 #endif // STDFRAME_HPP