Lumiera  0.pre.03
»edit your freedom«
ref-array-impl.hpp
Go to the documentation of this file.
1 /*
2  REF-ARRAY-IMPL.hpp - some implementations of the ref-array interface
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
41 #ifndef LIB_REF_ARRAY_IMPL_H
42 #define LIB_REF_ARRAY_IMPL_H
43 
44 
45 #include "lib/ref-array.hpp"
46 #include "include/logging.h"
47 
48 #include <vector>
49 using std::vector;
50 
51 
52 
53 namespace lib {
54 
59  template<class B, class IM = B>
61  : public RefArray<B>
62  {
63  typedef vector<IM> const& Tab;
64  Tab table_;
65 
66  public:
67 
68  RefArrayVectorWrapper (Tab toWrap)
69  : table_(toWrap)
70  { }
71 
72  virtual size_t size() const
73  {
74  return table_.size();
75  }
76 
77  virtual B const& operator[] (size_t i) const
78  {
79  REQUIRE (i < size());
80  return table_[i];
81  }
82  };
83 
84 
89  template<class B, class IM = B>
91  : public vector<IM>,
92  public RefArrayVectorWrapper<B,IM>
93  {
95  typedef vector<IM> Vect;
96  typedef typename Vect::size_type Size_t;
97  typedef typename Vect::value_type Val_t;
98 
99  public:
100  RefArrayVector() : Vect(), Wrap((Vect&)*this) {}
101  RefArrayVector(Size_t n, Val_t const& v = Val_t()) : Vect(n,v), Wrap((Vect&)*this) {}
102  RefArrayVector(Vect const& ref) : Vect(ref), Wrap((Vect&)*this) {}
103 
104  using Vect::size;
105  using Wrap::operator[];
106  };
107 
108 
109 
116  template<class B, size_t n, class IM = B>
118  : public RefArray<B>
119  {
120  char storage_[n*sizeof(IM)];
121  IM* array_;
122 
123  public:
124  RefArrayTable()
125  : array_ (reinterpret_cast<IM*> (&storage_))
126  {
127  size_t i=0;
128  try
129  {
130  while (i<n)
131  new(&array_[i++]) IM();
132  }
133  catch(...) { cleanup(i); throw; }
134  }
135 
136  template<class FAC>
137  RefArrayTable(FAC& factory)
138  : array_ (reinterpret_cast<IM*> (&storage_))
139  {
140  size_t i=0;
141  try
142  {
143  while (i<n)
144  factory(&array_[i++]);
145  }
146  catch(...) { cleanup(i-1); throw; } // destroy finished part, without the failed object
147  }
148 
149  ~RefArrayTable() { cleanup(); }
150 
151  private:
152  void cleanup(size_t top=n) noexcept
153  {
154  while (top) array_[--top].~IM();
155  }
156 
157 
158  public: //-----RefArray-Interface------------
159 
160  virtual size_t size() const
161  {
162  return n;
163  }
164 
165  virtual B const& operator[] (size_t i) const
166  {
167  REQUIRE (i < size());
168  return array_[i];
169  }
170 
171  };
172 
173 
174 } // namespace lib
175 #endif
Abstraction: Array of const references.
Definition: ref-array.hpp:48
This header is for including and configuring NoBug.
Implementation namespace for support and library code.
RefArray implementation based on a fixed size array, i.e.
Abstraction interface: array-like access by subscript.
RefArrayTable(FAC &factory)
This variation of the wrapper actually is a vector, but can act as a RefArray.
Wrap a vector holding objects of a subtype and provide array-like access using the interface type...