root/core/private_/manage_container_args.hpp

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. cont
  2. lock
  3. unlock
  4. decay
  5. begin
  6. end
  7. resize_output

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

/* [<][>][^][v][top][bottom][index][help] */