Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1d144a9

Browse files
committed
support custom params for actor's constructor for each spawn
1 parenta4a2825 commit1d144a9

File tree

2 files changed

+57
-34
lines changed

2 files changed

+57
-34
lines changed

‎lib/acto.h

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -440,45 +440,35 @@ object_t* make_instance(actor_ref context,
440440

441441
}// namespace core
442442

443-
namespacedetail {
444-
445-
template<typename Impl>
446-
inline core::object_t*make_instance(actor_ref context,
447-
const actor_thread thread_opt,
448-
std::unique_ptr<Impl> impl) {
449-
static_assert(std::is_base_of<::acto::actor, Impl>::value,
450-
"implementation should be derived from the acto::actor class");
451-
452-
returncore::make_instance(std::move(context), thread_opt,std::move(impl));
453-
}
454-
455-
}// namespace detail
456-
457443
template<typename T,typename... P>
458-
inline actor_refspawn(P&&... p) {
459-
returnactor_ref(detail::make_instance<T>(actor_ref(), actor_thread::shared,
444+
inline std::enable_if_t<std::is_base_of<::acto::actor, T>::value, actor_ref>
445+
spawn(P&&... p) {
446+
returnactor_ref(core::make_instance(actor_ref(), actor_thread::shared,
460447
std::make_unique<T>(std::forward<P>(p)...)),
461448
false);
462449
}
463450

464-
template<typename T>
465-
inline actor_refspawn(actor_ref context) {
466-
returnactor_ref(detail::make_instance<T>(std::move(context),
467-
actor_thread::shared, std::make_unique<T>()),
451+
template<typename T,typename... P>
452+
inline std::enable_if_t<std::is_base_of<::acto::actor, T>::value, actor_ref>
453+
spawn(actor_ref context, P&&... p) {
454+
returnactor_ref(core::make_instance(std::move(context), actor_thread::shared,
455+
std::make_unique<T>(std::forward<P>(p)...)),
468456
false);
469457
}
470458

471-
template<typename T>
472-
inline actor_refspawn(const actor_thread thread_opt) {
473-
returnactor_ref(
474-
detail::make_instance<T>(actor_ref(), thread_opt, std::make_unique<T>()),
459+
template<typename T,typename... P>
460+
inline std::enable_if_t<std::is_base_of<::acto::actor, T>::value, actor_ref>
461+
spawn(const actor_thread thread_opt, P&&... p) {
462+
returnactor_ref(core::make_instance(actor_ref(), thread_opt,
463+
std::make_unique<T>(std::forward<P>(p)...)),
475464
false);
476465
}
477466

478-
template<typename T>
479-
inline actor_refspawn(actor_ref context,const actor_thread thread_opt) {
480-
returnactor_ref(detail::make_instance<T>(
481-
std::move(context), thread_opt, std::make_unique<T>()),
467+
template<typename T,typename... P>
468+
inline std::enable_if_t<std::is_base_of<::acto::actor, T>::value, actor_ref>
469+
spawn(actor_ref context,const actor_thread thread_opt, P&&... p) {
470+
returnactor_ref(core::make_instance(std::move(context), thread_opt,
471+
std::make_unique<T>(std::forward<P>(p)...)),
482472
false);
483473
}
484474

‎test/tests.cpp

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,53 @@
22
#include"catch.hpp"
33

44
#include<iostream>
5+
#include<vector>
56

67
TEST_CASE("Finalize library") {
78
acto::shutdown();
89
}
910

1011
TEST_CASE("Spawn actor") {
11-
classA :publicacto::actor { };
12+
structA : acto::actor {
13+
structM {
14+
int x;
15+
};
1216

13-
auto a = acto::spawn<A>();
17+
A() {
18+
}
1419

15-
REQUIRE(a);
20+
A(int x)
21+
: x_(x) {
22+
actor::handler<M>(
23+
[this](acto::actor_ref,const M& m) {REQUIRE(m.x == x_); });
24+
}
1625

17-
acto::destroy(a);
26+
private:
27+
int x_;
28+
};
29+
30+
{
31+
auto a = acto::spawn<A>();
32+
33+
std::vector<acto::actor_ref> actors = {a,
34+
acto::spawn<A>(acto::actor_thread::shared),
35+
acto::spawn<A>(acto::actor_thread::shared,3), acto::spawn<A>(a),
36+
acto::spawn<A>(a,5), acto::spawn<A>(a, acto::actor_thread::shared),
37+
acto::spawn<A>(a, acto::actor_thread::shared,7)};
38+
// Validate actors.
39+
for (constauto& x : actors) {
40+
REQUIRE(x);
41+
}
42+
// Check values.
43+
actors[2].send(A::M{3});
44+
actors[4].send(A::M{5});
45+
actors[6].send(A::M{7});
46+
// Cleanup actors.
47+
for (auto& x : actors) {
48+
acto::destroy(x);
49+
}
50+
}
51+
// Shutdown.
1852
acto::shutdown();
1953
}
2054

@@ -37,8 +71,7 @@ TEST_CASE("Process binded actors at exit") {
3771
};
3872

3973
A() {
40-
actor::handler<M>(
41-
[](acto::actor_ref,const M& m) { (*m.counter)++; });
74+
actor::handler<M>([](acto::actor_ref,const M& m) { (*m.counter)++; });
4275
}
4376
};
4477

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp