1 | /******************************************************************************* |
2 | Copyright (c) The Taichi Authors (2016- ). All Rights Reserved. |
3 | The use of this software is governed by the LICENSE file. |
4 | *******************************************************************************/ |
5 | |
6 | #pragma once |
7 | |
8 | #include <vector> |
9 | #include <string> |
10 | |
11 | #include "taichi/common/interface.h" |
12 | |
13 | namespace taichi { |
14 | |
15 | class Task : public Unit { |
16 | public: |
17 | virtual std::string run(const std::vector<std::string> ¶meters) { |
18 | TI_ASSERT_INFO(parameters.size() == 0, "No parameters supported." ); |
19 | return this->run(); |
20 | } |
21 | |
22 | virtual std::string run() { |
23 | return this->run(std::vector<std::string>()); |
24 | } |
25 | |
26 | ~Task() override { |
27 | } |
28 | }; |
29 | |
30 | TI_INTERFACE(Task) |
31 | |
32 | template <typename T> |
33 | inline std::enable_if_t< |
34 | std::is_same<std::result_of_t<T(const std::vector<std::string> &)>, |
35 | void>::value || |
36 | std::is_same<std::result_of_t<T(const std::vector<std::string> &)>, |
37 | const char *>::value, |
38 | std::string> |
39 | task_invoke(const T &func, const std::vector<std::string> ¶ms) { |
40 | func(params); |
41 | return "" ; |
42 | } |
43 | |
44 | template <typename T> |
45 | inline std::enable_if_t< |
46 | std::is_same<std::result_of_t<T(const std::vector<std::string> &)>, |
47 | std::string>::value, |
48 | std::string> |
49 | task_invoke(const T &func, const std::vector<std::string> ¶ms) { |
50 | return func(params); |
51 | } |
52 | |
53 | template <typename T> |
54 | inline std::enable_if_t<std::is_same<std::result_of_t<T()>, void>::value, |
55 | std::string> |
56 | task_invoke(const T &func, const std::vector<std::string> ¶ms) { |
57 | func(); |
58 | return "" ; |
59 | } |
60 | |
61 | template <typename T> |
62 | inline std::enable_if_t< |
63 | std::is_same<std::result_of_t<T()>, std::string>::value || |
64 | std::is_same<std::result_of_t<T()>, const char *>::value, |
65 | std::string> |
66 | task_invoke(const T &func, const std::vector<std::string> ¶ms) { |
67 | return func(); |
68 | } |
69 | |
70 | #define TI_REGISTER_TASK(task) \ |
71 | class Task_##task : public taichi::Task { \ |
72 | std::string run(const std::vector<std::string> ¶meters) override { \ |
73 | return taichi::task_invoke<decltype(task)>(task, parameters); \ |
74 | } \ |
75 | }; \ |
76 | TI_IMPLEMENTATION(Task, Task_##task, #task) |
77 | |
78 | } // namespace taichi |
79 | |