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)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
34 #ifndef LIB_REF_ARRAY_IMPL_H
35 #define LIB_REF_ARRAY_IMPL_H
36 
37 
38 #include "lib/ref-array.hpp"
39 #include "include/logging.h"
40 
41 #include <vector>
42 using std::vector;
43 
44 
45 
46 namespace lib {
47 
52  template<class B, class IM = B>
54  : public RefArray<B>
55  {
56  typedef vector<IM> const& Tab;
57  Tab table_;
58 
59  public:
60 
61  RefArrayVectorWrapper (Tab toWrap)
62  : table_(toWrap)
63  { }
64 
65  virtual size_t size() const
66  {
67  return table_.size();
68  }
69 
70  virtual B const& operator[] (size_t i) const
71  {
72  REQUIRE (i < size());
73  return table_[i];
74  }
75  };
76 
77 
82  template<class B, class IM = B>
84  : public vector<IM>,
85  public RefArrayVectorWrapper<B,IM>
86  {
88  typedef vector<IM> Vect;
89  typedef typename Vect::size_type Size_t;
90  typedef typename Vect::value_type Val_t;
91 
92  public:
93  RefArrayVector() : Vect(), Wrap((Vect&)*this) {}
94  RefArrayVector(Size_t n, Val_t const& v = Val_t()) : Vect(n,v), Wrap((Vect&)*this) {}
95  RefArrayVector(Vect const& ref) : Vect(ref), Wrap((Vect&)*this) {}
96 
97  using Vect::size;
98  using Wrap::operator[];
99  };
100 
101 
102 
109  template<class B, size_t n, class IM = B>
111  : public RefArray<B>
112  {
113  char storage_[n*sizeof(IM)];
114  IM* array_;
115 
116  public:
117  RefArrayTable()
118  : array_ (reinterpret_cast<IM*> (&storage_))
119  {
120  size_t i=0;
121  try
122  {
123  while (i<n)
124  new(&array_[i++]) IM();
125  }
126  catch(...) { cleanup(i); throw; }
127  }
128 
129  template<class FAC>
130  RefArrayTable(FAC& factory)
131  : array_ (reinterpret_cast<IM*> (&storage_))
132  {
133  size_t i=0;
134  try
135  {
136  while (i<n)
137  factory(&array_[i++]);
138  }
139  catch(...) { cleanup(i-1); throw; } // destroy finished part, without the failed object
140  }
141 
142  ~RefArrayTable() { cleanup(); }
143 
144  private:
145  void cleanup(size_t top=n) noexcept
146  {
147  while (top) array_[--top].~IM();
148  }
149 
150 
151  public: //-----RefArray-Interface------------
152 
153  virtual size_t size() const
154  {
155  return n;
156  }
157 
158  virtual B const& operator[] (size_t i) const
159  {
160  REQUIRE (i < size());
161  return array_[i];
162  }
163 
164  };
165 
166 
167 } // namespace lib
168 #endif
Abstraction: Array of const references.
Definition: ref-array.hpp:42
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...