fw4spl
Aggregator.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWJOBS_AGGREGATOR_HPP__
8 #define __FWJOBS_AGGREGATOR_HPP__
9 
10 #include "fwJobs/config.hpp"
11 #include "fwJobs/IJob.hpp"
12 
13 #include <fwCore/macros.hpp>
14 
15 #include <boost/multi_index_container.hpp>
16 #include <boost/multi_index/hashed_index.hpp>
17 #include <boost/multi_index/ordered_index.hpp>
18 #include <boost/multi_index/random_access_index.hpp>
19 #include <boost/multi_index/identity.hpp>
20 #include <boost/multi_index/member.hpp>
21 
22 #include <cstdint>
23 #include <functional>
24 #include <string>
25 
26 namespace fwJobs
27 {
28 
32 class FWJOBS_CLASS_API Aggregator : public IJob
33 {
34 public:
35 
40  typedef WPTR (::fwJobs::Aggregator) wptr;
41 
43  typedef ::boost::multi_index_container< ::fwJobs::IJob::sptr,
44  ::boost::multi_index::indexed_by<
45  ::boost::multi_index::random_access<>,
46  ::boost::multi_index::hashed_unique<
47  ::boost::multi_index::identity< ::fwJobs::IJob::sptr >
48  >
49  >
60  FWJOBS_API static sptr New(const std::string& name = "");
61 
63  FWJOBS_API Aggregator();
64 
70  FWJOBS_API Aggregator(const std::string& name);
71 
80  FWJOBS_API void add(const ::fwJobs::IJob::sptr & iJob, double weight = 1.);
81 
87  FWJOBS_API IJobSeq getSubJobs();
88 
89 
90 protected:
91 
97  FWJOBS_API SharedFuture runImpl();
98 
99 private:
100 
102  IJobSeq m_jobs;
103 
105  struct JobInfo
106  {
107  std::uint64_t doneWork;
108  std::uint64_t totalWork;
109  std::uint64_t lastValue;
110 
111  JobInfo() : doneWork(0), totalWork(0), lastValue(0)
112  {
113  }
114 
115  JobInfo(std::uint64_t d, std::uint64_t t) :
116  doneWork(d),
117  totalWork(t),
118  lastValue(0)
119  {
120  }
121 
122  JobInfo( const IJob &iJob ) :
123  doneWork(iJob.getDoneWorkUnits()),
124  totalWork(iJob.getTotalWorkUnits()),
125  lastValue(0)
126  {
127  }
128 
129  double progress() const
130  {
131  return ( 0 == totalWork ) ? 1. : double(doneWork)/totalWork;
132  }
133  };
134 
136  typedef std::map< fwJobs::IJob*, JobInfo > JobInfoMap;
137 
139  JobInfoMap m_jobInfo;
140 };
141 
142 } //namespace fwJobs
143 
144 #endif //__FWJOBS_AGGREGATOR_HPP__
#define SPTR(_cls_)
std::weak_ptr< ::fwJobs::Aggregator > wptr
Aggregator container type.
Definition: Aggregator.hpp:40
This class is an interface for class managing job.
Definition: IJob.hpp:28
#define WPTR(_cls_)
std::shared_future< void > SharedFuture
Future type.
Definition: IJob.hpp:109
STL namespace.
This file defines fwCore base macros.
::boost::multi_index_container< ::fwJobs::IJob::sptr,::boost::multi_index::indexed_by< ::boost::multi_index::random_access<>,::boost::multi_index::hashed_unique< ::boost::multi_index::identity< ::fwJobs::IJob::sptr > > > > IJobSeq
Aggregator container type.
Definition: Aggregator.hpp:50
std::shared_ptr< ::fwJobs::Aggregator > sptr
Aggregator container type.
Definition: Aggregator.hpp:39
FWJOBS_API std::uint64_t getDoneWorkUnits() const
Getter on the number of done work units.
Definition: IJob.cpp:71
This class manages an aggregation of IJob.
Definition: Aggregator.hpp:32
FWJOBS_API std::uint64_t getTotalWorkUnits() const
Getter on the total number of work units.
Definition: IJob.cpp:79
This namespace fwJobs provides jobs management.