Skip to content

Commit 880c682

Browse files
committed
init
1 parent 8f62532 commit 880c682

File tree

6 files changed

+230
-0
lines changed

6 files changed

+230
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,6 @@ MigrationBackup/
348348

349349
# Ionide (cross platform F# VS Code tools) working folder
350350
.ionide/
351+
352+
353+
.vscode/

CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 2.8.12)
2+
project(tpl)
3+
4+
if (MSVC_VERSION GREATER_EQUAL "1900")
5+
include(CheckCXXCompilerFlag)
6+
CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported)
7+
if (_cpp_latest_flag_supported)
8+
add_compile_options("/std:c++latest")
9+
endif()
10+
endif()
11+
12+
add_subdirectory(example)

example/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include_directories(${PROJECT_SOURCE_DIR}/include)
2+
3+
add_executable(demo ${PROJECT_SOURCE_DIR}/example/demo.cpp)

example/demo.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <tpl/tpltasks.hpp>
2+
#include <ppltasks.h>
3+
#include <iostream>
4+
#include <string>
5+
#include <memory>
6+
7+
tpl::task<std::string> write_to_string()
8+
{
9+
// Create a shared pointer to a string that is
10+
// assigned to and read by multiple tasks.
11+
// By using a shared pointer, the string outlives
12+
// the tasks, which can run in the background after
13+
// this function exits.
14+
auto s = std::make_shared<std::string>("Value 1");
15+
16+
return tpl::create_task([s]
17+
{
18+
// Print the current value.
19+
std::cout << "Current value: " << *s << std::endl;
20+
// Assign to a new value.
21+
*s = "Value 2";
22+
return *s;
23+
});
24+
/*
25+
.then([s]
26+
{
27+
// Print the current value.
28+
std::cout << "Current value: " << *s << std::endl;
29+
// Assign to a new value and return the string.
30+
*s = "Value 3";
31+
return *s;
32+
});
33+
*/
34+
}
35+
36+
int main()
37+
{
38+
// Create a chain of tasks that work with a string.
39+
auto t = write_to_string();
40+
41+
// Wait for the tasks to finish and print the result.
42+
std::cout << "Final value: " << t.get() << std::endl;
43+
}
44+
45+
/* Output:
46+
Current value: Value 1
47+
Current value: Value 2
48+
Final value: Value 3
49+
*/

include/tpl/tpltasks.hpp

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#ifndef TPL_TASKS_H_
2+
#define TPL_TASKS_H_
3+
4+
#include <stdexcept>
5+
#include <memory>
6+
#include <type_traits>
7+
8+
namespace tpl
9+
{
10+
enum task_status
11+
{
12+
/// <summary>
13+
/// The tasks queued to the <c>task_group</c> object have not completed. Note that this value is not presently returned by
14+
/// the Concurrency Runtime.
15+
/// </summary>
16+
not_complete,
17+
18+
/// <summary>
19+
/// The tasks queued to the <c>task_group</c> or <c>structured_task_group</c> object completed successfully.
20+
/// </summary>
21+
completed,
22+
23+
/// <summary>
24+
/// The <c>task_group</c> or <c>structured_task_group</c> object was canceled. One or more tasks may not have executed.
25+
/// </summary>
26+
canceled
27+
};
28+
29+
class cancellation_token
30+
{
31+
public:
32+
33+
};
34+
35+
struct task_impl_base
36+
{
37+
enum TaskInternalState
38+
{
39+
// Tracks the state of the task, rather than the task collection on which the task is scheduled
40+
Created,
41+
Started,
42+
PendingCancel,
43+
Completed,
44+
Canceled
45+
};
46+
47+
task_impl_base()
48+
{
49+
}
50+
51+
virtual ~task_impl_base()
52+
{
53+
}
54+
55+
task_status wait()
56+
{
57+
58+
}
59+
60+
};
61+
62+
template<typename ReturnType>
63+
struct task_impl : public task_impl_base
64+
{
65+
66+
};
67+
68+
template<typename ReturnType>
69+
struct task_ptr
70+
{
71+
typedef std::shared_ptr<task_impl<ReturnType>> PtrType;
72+
/*
73+
static PtrType make(CancellationTokenState * ct, scheduler_ptr scheduler_arg)
74+
{
75+
return std::make_shared<task_impl<ReturnType>>(ct, scheduler_arg);
76+
}
77+
*/
78+
};
79+
80+
template<typename ReturnType>
81+
class task
82+
{
83+
public:
84+
task() : impl_(nullptr)
85+
{
86+
}
87+
88+
task(const task& other) : impl_(other.impl_) {}
89+
90+
task(task&& other): impl_(std::move(other.impl_)) {}
91+
92+
task& operator=(const task& other)
93+
{
94+
if (this != &other)
95+
{
96+
impl_ = other.impl_;
97+
}
98+
return *this;
99+
}
100+
101+
task& operator=(task&& other)
102+
{
103+
if (this != &other)
104+
{
105+
impl_ = std::move(other.impl_);
106+
}
107+
return *this;
108+
}
109+
110+
template<typename FunctionType>
111+
task(FunctionType fn)
112+
{
113+
114+
}
115+
116+
task_status wait()
117+
{
118+
if (impl_)
119+
{
120+
return impl_.wait();
121+
}
122+
else
123+
{
124+
throw std::runtime_error("Task implement no found");
125+
}
126+
}
127+
128+
ReturnType get()
129+
{
130+
return ReturnType{};
131+
}
132+
133+
typename task_ptr<ReturnType>::PtrType impl_;
134+
};
135+
136+
template<typename ReturnType>
137+
task<ReturnType> create_task(const task<ReturnType>& t)
138+
{
139+
task<ReturnType> created_task(t);
140+
return created_task;
141+
}
142+
143+
namespace details
144+
{
145+
template<typename ParamType>
146+
struct TaskTypeFromParam
147+
{
148+
#if __cplusplus <= 201103L
149+
typedef std::result_of<ParamType> type;
150+
#else
151+
typedef std::invoke_result<ParamType> type;
152+
#endif
153+
};
154+
}
155+
156+
template<typename ParamType>
157+
task<typename details::TaskTypeFromParam<ParamType>::type> create_task(ParamType param) // , task_options _TaskOptions = task_options())
158+
{
159+
return task<typename details::TaskTypeFromParam<ParamType>::type>();
160+
}
161+
}
162+
163+
#endif // TPL_TASKS_H_

license.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)