Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
vector-impl.hxx
Go to the documentation of this file.
1 // file : odb/vector-impl.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : GNU GPL v2; see accompanying LICENSE file
4 
5 #ifndef ODB_VECTOR_IMPL_HXX
6 #define ODB_VECTOR_IMPL_HXX
7 
8 #include <odb/pre.hxx>
9 #include <odb/details/config.hxx> // ODB_CXX11
10 
11 #include <new>
12 #include <cstddef> // std::size_t
13 
14 #include <odb/transaction.hxx>
15 #include <odb/details/export.hxx>
16 
17 namespace odb
18 {
19  // Change tracking vector implementation details.
20  //
21  class LIBODB_EXPORT vector_impl
22  {
23  public:
25  {
29  state_erased
30  };
31 
33  {
36  state_changed // Container has changed but individual changes
37  // were not tracked.
38  };
39 
40  vector_impl ();
41  ~vector_impl ();
42 
43  // The copy constructor will copy the state. The idea is that the
44  // copy keeps tracking changes, just like the original.
45  //
46  vector_impl (const vector_impl&);
47 
48 #ifdef ODB_CXX11
50 #endif
51 
52  void
53  swap (vector_impl& x);
54 
55  // Allocate enough memory to store the specified number of
56  // elements.
57  //
58  void
59  reserve (std::size_t);
60 
61  // Reduce capacity to size.
62  //
63  void
64  shrink_to_fit ();
65 
66  // Capacity (each entry takes 2 bits).
67  //
68  std::size_t
69  capacity () const;
70 
71  // (Re)start tracking changes for a vector with n elements.
72  //
73  void
74  start (std::size_t);
75 
76  // Stop tracking changes.
77  //
78  void
79  stop ();
80 
81  // Mark the container as changed without tracking the changes.
82  // This state is useful as a fallback mechnism for situations
83  // where the change information has been discarded (e.g., after
84  // its state has been updated in the database) but the container
85  // should remain changed (e.g., after the transaction is rolled
86  // back).
87  //
88  void
89  change ();
90 
91  // Get the state of the container.
92  //
93  container_state_type
94  state () const;
95 
96  // Shortcut for state() == state_tracking.
97  //
98  bool
99  tracking () const;
100 
101  // Note that the returned size can be greater than the actual,
102  // parallel vector size. In this case the difference is the
103  // erased elements at the back.
104  //
105  std::size_t
106  size () const;
107 
108  // Get the change state of the specified element.
109  //
110  element_state_type
111  state (std::size_t) const;
112 
113  // Change notifications.
114  //
115  void
116  push_back (std::size_t n = 1);
117 
118  void
119  pop_back (std::size_t n = 1);
120 
121  void
122  insert (std::size_t, std::size_t n = 1);
123 
124  void
125  erase (std::size_t, std::size_t n = 1);
126 
127  void
128  modify (std::size_t, std::size_t n = 1);
129 
130  void
131  clear ();
132 
133  void
134  assign (std::size_t n);
135 
136  void
137  resize (std::size_t n);
138 
139  private:
140  // Assignment does not make sense (it is changing of the content).
141  //
142  vector_impl& operator= (const vector_impl&);
143 
144  private:
145  void
146  realloc (std::size_t);
147 
148  void
149  set (std::size_t, element_state_type);
150 
151  static const unsigned char mask_[4];
152  static const unsigned char shift_[4];
153 
154  container_state_type state_;
155 
156  // Size, tail, and capacity are in 2-bit blocks. Size is the number
157  // of elements we have in data. Tail is the position of the first
158  // erased element at the back. If there are no erased elements, then
159  // tail is equal size. Capacity is the number of elements we can
160  // store in data.
161  //
162  std::size_t size_;
163  std::size_t tail_;
164  std::size_t capacity_;
165  unsigned char* data_;
166  };
167 
168  // Base class that provides a change tracking interface and
169  // handles the rollback callback. The only function that's
170  // missing is _start() which needs to know the number of
171  // elements currently in the vector.
172  //
173  class LIBODB_EXPORT vector_base
174  {
175  public:
176  void
177  _stop () const {impl_.stop ();}
178 
179  bool
180  _tracking () const {return impl_.tracking ();}
181 
182  void
183  _arm (transaction& t) const;
184 
185  vector_impl&
186  _impl () const {return impl_;}
187 
188  private:
189  // Assignment is changing of the content.
190  //
191  vector_base& operator= (const vector_base&);
192 
193  protected:
194  vector_base (): tran_ (0) {}
195  ~vector_base () {if (tran_ != 0) tran_->callback_unregister (this);}
196  vector_base (const vector_base&);
197 
198 #ifdef ODB_CXX11
200 #endif
201 
202  void
203  swap (vector_base&);
204 
205  static void
206  rollback (unsigned short, void* key, unsigned long long);
207 
208  private:
209  void
210  swap_tran (vector_base&);
211 
212  protected:
214  mutable transaction* tran_;
215  };
216 }
217 
218 #include <odb/vector-impl.ixx>
219 
220 #include <odb/post.hxx>
221 
222 #endif // ODB_VECTOR_IMPL_HXX
Definition: vector-impl.hxx:28
Definition: vector-impl.hxx:26
Definition: vector-impl.hxx:35
Definition: transaction.hxx:22
transaction * tran_
Definition: vector-impl.hxx:214
vector_impl & _impl() const
Definition: vector-impl.hxx:186
Definition: vector-impl.hxx:27
container_state_type
Definition: vector-impl.hxx:32
void _stop() const
Definition: vector-impl.hxx:177
~vector_base()
Definition: vector-impl.hxx:195
Definition: vector-impl.hxx:21
bool _tracking() const
Definition: vector-impl.hxx:180
vector_impl impl_
Definition: vector-impl.hxx:213
Definition: vector-impl.hxx:173
Definition: vector-impl.hxx:34
void swap(lazy_ptr< T > &, lazy_ptr< T > &)
element_state_type
Definition: vector-impl.hxx:24
vector_base()
Definition: vector-impl.hxx:194