Skip to content

Commit b2afbaf

Browse files
committed
Add make_hero utility functions to learn sample
1 parent 7807c53 commit b2afbaf

File tree

6 files changed

+62
-52
lines changed

6 files changed

+62
-52
lines changed

samples/learn/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 3.15)
55

66
add_subdirectory(schema)
77
add_library(star_wars STATIC
8+
HeroData.cpp
89
DroidData.cpp
910
HumanData.cpp
1011
QueryData.cpp

samples/learn/DroidData.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
#include "CharacterObject.h"
5-
64
#include "DroidData.h"
75

86
#include "HumanData.h"
@@ -55,22 +53,7 @@ std::optional<std::vector<std::shared_ptr<object::Character>>> Droid::getFriends
5553
friends_.end(),
5654
result.begin(),
5755
[](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);
7457
});
7558
result.erase(std::remove_if(result.begin(),
7659
result.end(),

samples/learn/HeroData.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

samples/learn/HeroData.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ class Human;
1717
using SharedHero = std::variant<std::shared_ptr<Human>, std::shared_ptr<Droid>>;
1818
using WeakHero = std::variant<std::weak_ptr<Human>, std::weak_ptr<Droid>>;
1919

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+
2029
} // namespace graphql::learn
2130

2231
#endif // HERODATA_H

samples/learn/HumanData.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
#include "CharacterObject.h"
5-
64
#include "HumanData.h"
75

86
#include "DroidData.h"
@@ -54,22 +52,7 @@ std::optional<std::vector<std::shared_ptr<object::Character>>> Human::getFriends
5452
friends_.end(),
5553
result.begin(),
5654
[](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);
7356
});
7457
result.erase(std::remove_if(result.begin(),
7558
result.end(),

samples/learn/QueryData.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,7 @@ std::shared_ptr<object::Character> Query::getHero(std::optional<Episode> episode
2323

2424
if (const auto itr = heroes_.find(episode); itr != heroes_.end())
2525
{
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);
4227
}
4328

4429
return result;

0 commit comments

Comments
 (0)