libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
manage_container_args.hpp
Go to the documentation of this file.
1 #ifndef LIBJMMCG_CORE_PRIVATE_MANAGE_CONTAINER_ARGS_HPP
2 #define LIBJMMCG_CORE_PRIVATE_MANAGE_CONTAINER_ARGS_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2015 by J.M.McGuiness, coder@hussar.me.uk
6 **
7 ** This library is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU Lesser General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2.1 of the License, or (at your option) any later version.
11 **
12 ** This library 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 GNU
15 ** Lesser General Public License for more details.
16 **
17 ** You should have received a copy of the GNU Lesser General Public
18 ** License along with this library; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 #include "../../core/config.h"
23 #include "../../core/rw_locking.hpp"
24 
25 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace ppd { namespace private_ {
26 
27  template<class Colln>
29  typedef Colln container_type;
31  typedef typename container_type::size_type size_type;
32 
33  explicit constexpr input_safe_colln(container_type const &c) noexcept(true) FORCE_INLINE
34  : cont(c) {
35  }
36 
37  void __fastcall lock() const noexcept(true) FORCE_INLINE {
38  cont.pop_lock().lock();
39  }
40 
41  void __fastcall unlock() const noexcept(true) FORCE_INLINE {
42  cont.pop_lock().unlock();
43  }
44 
45  iterator __fastcall begin() const noexcept(true) FORCE_INLINE {
46  return cont.colln().begin();
47  }
48  iterator __fastcall end() const noexcept(true) FORCE_INLINE {
49  return cont.colln().end();
50  }
51  typename container_type::size_type __fastcall size() const noexcept(true) FORCE_INLINE {
52  return cont.colln().size();
53  }
54 
55  private:
56  container_type const &cont;
57  };
58 
59  template<class Colln, class Iter=typename Colln::const_iterator>
61  typedef Colln container_type;
62  typedef Iter iterator;
63  typedef typename container_type::size_type size_type;
64 
65  constexpr input_safe_range(iterator const &b, iterator const &e) noexcept(true) FORCE_INLINE
66  : beg(b), en(e) {
67  }
68 
69  static constexpr void __fastcall lock() noexcept(true) FORCE_INLINE {
70  }
71 
72  static constexpr void __fastcall unlock() noexcept(true) FORCE_INLINE {
73  }
74 
75  iterator __fastcall begin() noexcept(true) FORCE_INLINE {
76  return beg;
77  }
78  iterator __fastcall end() const noexcept(true) FORCE_INLINE {
79  return en;
80  }
81  constexpr typename iterator::difference_type __fastcall size() const noexcept(true) FORCE_INLINE {
82  return std::distance(beg, en);
83  }
84 
85  private:
86  iterator const beg;
87  iterator const en;
88  };
89 
90  template<class Colln, class Iter=typename Colln::iterator>
92  typedef Colln container_type;
93  typedef Iter iterator;
94  typedef typename container_type::size_type size_type;
95 
96  constexpr output_safe_range(iterator const &b, iterator const &e) noexcept(true) FORCE_INLINE
97  : beg(b), en(e) {
98  }
99 
100  static constexpr void __fastcall lock() noexcept(true) FORCE_INLINE {
101  }
102 
103  static constexpr void __fastcall unlock() noexcept(true) FORCE_INLINE {
104  }
105 
106  iterator __fastcall begin() noexcept(true) FORCE_INLINE {
107  return beg;
108  }
109  iterator __fastcall end() noexcept(true) FORCE_INLINE {
110  return en;
111  }
112  constexpr typename iterator::difference_type __fastcall size() const noexcept(true) FORCE_INLINE {
113  return std::distance(beg, en);
114  }
115 
116  private:
117  iterator const beg;
118  iterator const en;
119  };
120 
121  template<class Colln>
123  public:
124  typedef Colln container_type;
127  typedef typename container_type::size_type size_type;
128 
129  explicit __stdcall output_safe_colln_rw_lk(container_type &c) noexcept(true) FORCE_INLINE
130  : lk(c.push_lock()), cont(c) {
131  }
132 
133  void __fastcall lock() noexcept(true) FORCE_INLINE {
134  lk.lock();
135  }
136  void __fastcall unlock() noexcept(true) FORCE_INLINE {
137  lk.unlock();
138  }
139 
140  void __fastcall decay() noexcept(true) FORCE_INLINE {
141  lk.decay();
142  }
143 
144  iterator __fastcall begin() noexcept(true) FORCE_INLINE {
145  return cont.colln().begin();
146  }
147  iterator __fastcall end() noexcept(true) FORCE_INLINE {
148  return cont.colln().end();
149  }
150  typename container_type::size_type __fastcall size() const noexcept(true) FORCE_INLINE {
151  return cont.colln().size();
152  }
153  void resize_output(typename container_type::size_type sz) noexcept(false) FORCE_INLINE {
154  cont.resize_noinit_nolk(sz);
155  }
156 
157  private:
158  atomic_t lk;
159  container_type &cont;
160  };
161 
162  template<class Colln>
164  typedef Colln container_type;
166  typedef typename container_type::size_type size_type;
167 
168  explicit constexpr output_safe_colln_simple_lk(container_type &c) noexcept(true) FORCE_INLINE
169  : cont(c) {
170  }
171 
172  void __fastcall lock() const noexcept(true) FORCE_INLINE {
173  cont.push_lock().lock();
174  }
175 
176  void __fastcall unlock() const noexcept(true) FORCE_INLINE {
177  cont.push_lock().unlock();
178  }
179 
180  iterator __fastcall begin() noexcept(true) FORCE_INLINE {
181  return cont.colln().begin();
182  }
183  iterator __fastcall end() noexcept(true) FORCE_INLINE {
184  return cont.colln().end();
185  }
186  typename container_type::size_type __fastcall size() const noexcept(true) FORCE_INLINE {
187  return cont.colln().size();
188  }
189  void resize_output(size_type const sz) noexcept(true) FORCE_INLINE {
190  cont.resize_noinit_nolk(sz);
191  }
192 
193  private:
194  container_type &cont;
195  };
196 
197  template<class CollnIn>
198  struct one_container {
199  typedef input_safe_colln<CollnIn> input_t;
200  typedef typename input_t::iterator in_iterator;
201  typedef typename input_t::size_type size_type;
202 
204 
205  explicit constexpr one_container(CollnIn const &c_in) noexcept(true) FORCE_INLINE
206  : input1(c_in) {
207  }
208 
209  void __fastcall lock() noexcept(true) FORCE_INLINE {
210  input1.lock();
211  }
212 
213  void __fastcall unlock() noexcept(true) FORCE_INLINE {
214  input1.unlock();
215  }
216  static constexpr void resize_output(size_type const) noexcept(true) FORCE_INLINE {
217  }
218  };
219 
220  template<class Colln>
223  typedef input_t output_t;
224  typedef typename input_t::iterator in_iterator;
225  typedef typename output_t::size_type size_type;
226 
228 
229  explicit constexpr one_output_container_rw_lk(Colln &c) noexcept(true) FORCE_INLINE
230  : input1(c) {
231  }
232 
233  void __fastcall lock() noexcept(true) FORCE_INLINE {
234  input1.lock();
235  }
236 
237  void __fastcall unlock() noexcept(true) FORCE_INLINE {
238  input1.unlock();
239  }
240  void resize_output(size_type const sz) noexcept(true) FORCE_INLINE {
241  input1.resize_output(sz);
242  input1.decay();
243  }
244  };
245 
246  template<class Colln>
249  typedef input_t output_t;
250  typedef typename input_t::iterator in_iterator;
251  typedef typename output_t::size_type size_type;
252 
254 
255  explicit constexpr one_output_container_simple_lk(Colln &c) noexcept(true) FORCE_INLINE
256  : input1(c) {
257  }
258 
259  void __fastcall lock() noexcept(true) FORCE_INLINE {
260  input1.lock();
261  }
262 
263  void __fastcall unlock() noexcept(true) FORCE_INLINE {
264  input1.unlock();
265  }
266  void resize_output(size_type const sz) noexcept(true) FORCE_INLINE {
267  input1.resize_output(sz);
268  input1.decay();
269  }
270  };
271 
272  template<class CollnIn, class CollnOut>
273  struct two_containers {
274  typedef input_safe_colln<CollnIn> input_t;
276  typedef typename input_t::iterator in_iterator;
277  typedef typename output_t::iterator out_iterator;
278  typedef typename output_t::size_type size_type;
279 
282 
283  constexpr two_containers(CollnIn const &c_in, CollnOut &c_out) noexcept(true) FORCE_INLINE
284  : input1(c_in), output(c_out) {
285  }
286 
287  void __fastcall lock() noexcept(true) FORCE_INLINE {
288  input1.lock();
289  output.lock();
290  }
291 
292  void __fastcall unlock() noexcept(true) FORCE_INLINE {
293  input1.unlock();
294  output.unlock();
295  }
296  void resize_output(size_type const sz) noexcept(false) FORCE_INLINE {
297  output.resize_output(sz);
298  output.decay();
299  assert(output.size()>=input1.size());
300  }
301  };
302 
303  template<class CollnIn, class CollnOut, class IterIn, class IterOut>
304  struct two_ranges {
305  typedef input_safe_range<CollnIn, IterIn> input_t;
306  typedef output_safe_range<CollnOut, IterOut> output_t;
307  typedef IterIn in_iterator;
308  typedef IterOut out_iterator;
309  typedef typename output_t::size_type size_type;
310 
313 
314  __stdcall two_ranges(in_iterator const &b1, in_iterator const &e1, out_iterator const &b2) noexcept(true) FORCE_INLINE
315  : input1(b1, e1), output(b2, std::next(b2, std::distance(b1, e1))) {
316  }
317 
318  static constexpr void __fastcall lock() noexcept(true) FORCE_INLINE {
319  }
320 
321  static constexpr void __fastcall unlock() noexcept(true) FORCE_INLINE {
322  }
323  static constexpr void resize_output(size_type const) noexcept(true) {
324  }
325  };
326 
327  template<class CollnIn, class CollnOut, class IterIn, class IterOut>
328  struct two_out_ranges {
329  typedef output_safe_range<CollnIn, IterIn> input_t;
330  typedef output_safe_range<CollnOut, IterOut> output_t;
331  typedef IterIn in_iterator;
332  typedef IterOut out_iterator;
333  typedef typename output_t::size_type size_type;
334 
337 
338  __stdcall two_out_ranges(in_iterator const &b1, in_iterator const &e1, out_iterator const &b2) noexcept(true) FORCE_INLINE
339  : input1(b1, e1), output(b2, std::next(b2, std::distance(b1, e1))) {
340  }
341 
342  static constexpr void __fastcall lock() noexcept(true) FORCE_INLINE {
343  }
344 
345  static constexpr void __fastcall unlock() noexcept(true) FORCE_INLINE {
346  }
347  static constexpr void resize_output(size_type const) noexcept(true) FORCE_INLINE {
348  }
349  };
350 
351  template<class CollnIn1, class CollnIn2, class CollnOut>
353  typedef input_safe_colln<CollnIn1> input1_t;
354  typedef input_safe_colln<CollnIn2> input2_t;
356  typedef typename input1_t::iterator in_iterator;
357  typedef typename input2_t::iterator in2_iterator;
358  typedef typename output_t::iterator out_iterator;
359  typedef typename output_t::size_type size_type;
360 
364 
365  constexpr three_containers(CollnIn1 const &c1_in, CollnIn2 const &c2_in, CollnOut &c_out) noexcept(true) FORCE_INLINE
366  : input1(c1_in), input2(c2_in), output(c_out) {
367  }
368 
369  void __fastcall lock() noexcept(true) FORCE_INLINE {
370  input1.lock();
371  input2.lock();
372  output.lock();
373  }
374 
375  void __fastcall unlock() noexcept(true) FORCE_INLINE {
376  input1.unlock();
377  input2.unlock();
378  output.unlock();
379  }
380  void resize_output(size_type const sz) noexcept(false) FORCE_INLINE {
381  output.resize_output(sz);
382  output.decay();
383  assert(output.size()>=input1.size());
384  }
385  };
386 
387 } } } }
388 
389 #endif