1 /*
2  * Exec.hpp
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 
16 #ifndef CORE_EXEC_HPP
17 #define CORE_EXEC_HPP
18 
19 #include <vector>
20 
21 #include <boost/function.hpp>
22 
23 namespace rstudio {
24 namespace core {
25 
26 class Error;
27 
28 class ExecBlock
29 {
30 public:
31    typedef boost::function<core::Error()> Function;
32 
33 public:
ExecBlock()34    ExecBlock() {}
35 
36     // COPYING: via compiler (copyable members)
37 
38    // add to the block
39    ExecBlock& add(Function function);
40 
41    // easy init style (based on idiom in boost::program_options)
42    class EasyInit;
addFunctions()43    EasyInit addFunctions() { return EasyInit(this); }
44 
45    // execute the block
46    core::Error execute() const;
47 
48    // allow an ExecBlock to act as a boost::function<core::Error()>
49    core::Error operator()() const;
50 
51 public:
52    // easy init helper class
53    class EasyInit
54    {
55    public:
EasyInit(ExecBlock * pExecBlock)56       EasyInit(ExecBlock* pExecBlock) : pExecBlock_(pExecBlock) {}
operator ()(Function function)57       EasyInit& operator()(Function function)
58       {
59          pExecBlock_->add(function);
60          return *this;
61       }
62    private:
63       ExecBlock* pExecBlock_;
64    };
65 private:
66    std::vector<Function> functions_;
67 };
68 
69 
70 } // namespace core
71 } // namespace rstudio
72 
73 #endif // CORE_EXEC_HPP
74 
75