File tree Expand file tree Collapse file tree 6 files changed +62
-52
lines changed Expand file tree Collapse file tree 6 files changed +62
-52
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 3.15)
5
5
6
6
add_subdirectory (schema )
7
7
add_library (star_wars STATIC
8
+ HeroData.cpp
8
9
DroidData.cpp
9
10
HumanData.cpp
10
11
QueryData.cpp
Original file line number Diff line number Diff line change 1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT License.
3
3
4
- #include " CharacterObject.h"
5
-
6
4
#include " DroidData.h"
7
5
8
6
#include " HumanData.h"
@@ -55,22 +53,7 @@ std::optional<std::vector<std::shared_ptr<object::Character>>> Droid::getFriends
55
53
friends_.end (),
56
54
result.begin (),
57
55
[](const auto & wpFriend) noexcept {
58
- return std::visit (
59
- [](const auto & hero) noexcept {
60
- using hero_t = std::decay_t <decltype (hero)>;
61
-
62
- if constexpr (std::is_same_v<std::weak_ptr<Human>, hero_t >)
63
- {
64
- return std::make_shared<object::Character>(
65
- std::make_shared<object::Human>(hero.lock ()));
66
- }
67
- else if constexpr (std::is_same_v<std::weak_ptr<Droid>, hero_t >)
68
- {
69
- return std::make_shared<object::Character>(
70
- std::make_shared<object::Droid>(hero.lock ()));
71
- }
72
- },
73
- wpFriend);
56
+ return make_hero (wpFriend);
74
57
});
75
58
result.erase (std::remove_if (result.begin (),
76
59
result.end (),
Original file line number Diff line number Diff line change
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ #include " HeroData.h"
5
+
6
+ #include " DroidData.h"
7
+ #include " HumanData.h"
8
+
9
+ namespace graphql ::learn {
10
+
11
+ std::shared_ptr<object::Character> make_hero (const SharedHero& hero) noexcept
12
+ {
13
+ return std::visit (
14
+ [](const auto & hero) noexcept {
15
+ using hero_t = std::decay_t <decltype (hero)>;
16
+
17
+ if constexpr (std::is_same_v<std::shared_ptr<Human>, hero_t >)
18
+ {
19
+ return std::make_shared<object::Character>(std::make_shared<object::Human>(hero));
20
+ }
21
+ else if constexpr (std::is_same_v<std::shared_ptr<Droid>, hero_t >)
22
+ {
23
+ return std::make_shared<object::Character>(std::make_shared<object::Droid>(hero));
24
+ }
25
+ },
26
+ hero);
27
+ }
28
+
29
+ std::shared_ptr<object::Character> make_hero (const WeakHero& hero) noexcept
30
+ {
31
+ return std::visit (
32
+ [](const auto & hero) noexcept {
33
+ using hero_t = std::decay_t <decltype (hero)>;
34
+
35
+ if constexpr (std::is_same_v<std::weak_ptr<Human>, hero_t >)
36
+ {
37
+ return std::make_shared<object::Character>(
38
+ std::make_shared<object::Human>(hero.lock ()));
39
+ }
40
+ else if constexpr (std::is_same_v<std::weak_ptr<Droid>, hero_t >)
41
+ {
42
+ return std::make_shared<object::Character>(
43
+ std::make_shared<object::Droid>(hero.lock ()));
44
+ }
45
+ },
46
+ hero);
47
+ }
48
+
49
+ } // namespace graphql::learn
Original file line number Diff line number Diff line change @@ -17,6 +17,15 @@ class Human;
17
17
using SharedHero = std::variant<std::shared_ptr<Human>, std::shared_ptr<Droid>>;
18
18
using WeakHero = std::variant<std::weak_ptr<Human>, std::weak_ptr<Droid>>;
19
19
20
+ namespace object {
21
+
22
+ class Character ;
23
+
24
+ } // namespace object
25
+
26
+ std::shared_ptr<object::Character> make_hero (const SharedHero& hero) noexcept ;
27
+ std::shared_ptr<object::Character> make_hero (const WeakHero& hero) noexcept ;
28
+
20
29
} // namespace graphql::learn
21
30
22
31
#endif // HERODATA_H
Original file line number Diff line number Diff line change 1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT License.
3
3
4
- #include " CharacterObject.h"
5
-
6
4
#include " HumanData.h"
7
5
8
6
#include " DroidData.h"
@@ -54,22 +52,7 @@ std::optional<std::vector<std::shared_ptr<object::Character>>> Human::getFriends
54
52
friends_.end (),
55
53
result.begin (),
56
54
[](const auto & wpFriend) noexcept {
57
- return std::visit (
58
- [](const auto & hero) noexcept {
59
- using hero_t = std::decay_t <decltype (hero)>;
60
-
61
- if constexpr (std::is_same_v<std::weak_ptr<Human>, hero_t >)
62
- {
63
- return std::make_shared<object::Character>(
64
- std::make_shared<object::Human>(hero.lock ()));
65
- }
66
- else if constexpr (std::is_same_v<std::weak_ptr<Droid>, hero_t >)
67
- {
68
- return std::make_shared<object::Character>(
69
- std::make_shared<object::Droid>(hero.lock ()));
70
- }
71
- },
72
- wpFriend);
55
+ return make_hero (wpFriend);
73
56
});
74
57
result.erase (std::remove_if (result.begin (),
75
58
result.end (),
Original file line number Diff line number Diff line change @@ -23,22 +23,7 @@ std::shared_ptr<object::Character> Query::getHero(std::optional<Episode> episode
23
23
24
24
if (const auto itr = heroes_.find (episode); itr != heroes_.end ())
25
25
{
26
- result = std::visit (
27
- [](const auto & hero) noexcept {
28
- using hero_t = std::decay_t <decltype (hero)>;
29
-
30
- if constexpr (std::is_same_v<std::shared_ptr<Human>, hero_t >)
31
- {
32
- return std::make_shared<object::Character>(
33
- std::make_shared<object::Human>(hero));
34
- }
35
- else if constexpr (std::is_same_v<std::shared_ptr<Droid>, hero_t >)
36
- {
37
- return std::make_shared<object::Character>(
38
- std::make_shared<object::Droid>(hero));
39
- }
40
- },
41
- itr->second );
26
+ result = make_hero (itr->second );
42
27
}
43
28
44
29
return result;
You can’t perform that action at this time.
0 commit comments