github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/fflib/include/ff/functionflow/para/paracontainer.h (about)

     1  /***********************************************
     2    The MIT License (MIT)
     3  
     4    Copyright (c) 2012 Athrun Arthur <athrunarthur@gmail.com>
     5  
     6    Permission is hereby granted, free of charge, to any person obtaining a copy
     7    of this software and associated documentation files (the "Software"), to deal
     8    in the Software without restriction, including without limitation the rights
     9    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10    copies of the Software, and to permit persons to whom the Software is
    11    furnished to do so, subject to the following conditions:
    12  
    13    The above copyright notice and this permission notice shall be included in
    14    all copies or substantial portions of the Software.
    15  
    16    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    22    THE SOFTWARE.
    23   *************************************************/
    24  #ifndef FF_PARA_PARA_CONTAINER_H_
    25  #define FF_PARA_PARA_CONTAINER_H_
    26  #include "ff/functionflow/common/common.h"
    27  #include "ff/functionflow/para/para.h"
    28  #include "ff/functionflow/para/para_helper.h"
    29  #include "ff/functionflow/para/paras_with_lock.h"
    30  #include "ff/functionflow/runtime/rtcmn.h"
    31  #include <cmath>
    32  
    33  namespace ff {
    34  
    35  namespace internal {
    36  class wait_all;
    37  class wait_any;
    38  
    39  }  // end namespace internal
    40  
    41  class paracontainer {
    42   public:
    43    typedef void ret_type;
    44  
    45   public:
    46     paracontainer() : m_pEntities(new ::ff::internal::paras_with_lock()) {}
    47  
    48     para<void> &operator[](int index) {
    49       std::lock_guard<ff::spinlock> _l(m_pEntities->lock);
    50       return (*m_pEntities).entities[index];
    51    }
    52    size_t size() const {
    53      std::lock_guard<ff::spinlock> _l(m_pEntities->lock);
    54      return m_pEntities->entities.size();
    55    }
    56    ~paracontainer() {}
    57  
    58    template <typename Func_t>
    59    void add(const Func_t& f) {
    60      para<void> p;
    61      p(f);
    62      add(p);
    63    }
    64  
    65    void add(const para<void>& p) {
    66      std::lock_guard<ff::spinlock> _l(m_pEntities->lock);
    67      m_pEntities->entities.push_back(p);
    68    }
    69  
    70    void clear() {
    71      std::lock_guard<ff::spinlock> _l(m_pEntities->lock);
    72      m_pEntities->entities.clear();
    73    }
    74  
    75   protected:
    76     typedef std::shared_ptr<::ff::internal::paras_with_lock> Entities_t;
    77  
    78     friend ::ff::internal::wait_all all(paracontainer &pg);
    79     friend ::ff::internal::wait_any any(paracontainer &pg);
    80     std::shared_ptr<::ff::internal::paras_with_lock> &all_entities() {
    81       return m_pEntities;
    82    };
    83  
    84    std::shared_ptr<::ff::internal::paras_with_lock> m_pEntities;
    85  };  // end class paracontainer
    86  
    87  }  // end namespace ff
    88  
    89  #endif