forked from microsoft/cppgraphqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DroidData.cpp
83 lines (67 loc) · 2.09 KB
/
DroidData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "DroidData.h"
#include "HumanData.h"
namespace graphql::learn {
Droid::Droid(std::string&& id, std::optional<std::string>&& name, std::vector<Episode>&& appearsIn,
std::optional<std::string>&& primaryFunction) noexcept
: id_ { std::move(id) }
, name_ { std::move(name) }
, appearsIn_ { std::move(appearsIn) }
, primaryFunction_ { std::move(primaryFunction) }
{
}
void Droid::addFriends(
std::vector<std::variant<std::shared_ptr<Human>, std::shared_ptr<Droid>>> friends) noexcept
{
friends_.resize(friends.size());
std::transform(friends.begin(),
friends.end(),
friends_.begin(),
[](const auto& spFriend) noexcept {
return std::visit(
[](const auto& hero) noexcept {
return WeakHero {
std::weak_ptr<typename std::decay_t<decltype(hero)>::element_type> { hero }
};
},
spFriend);
});
}
const response::IdType& Droid::getId() const noexcept
{
return id_;
}
const std::optional<std::string>& Droid::getName() const noexcept
{
return name_;
}
std::optional<std::vector<std::shared_ptr<object::Character>>> Droid::getFriends() const noexcept
{
std::vector<std::shared_ptr<object::Character>> result(friends_.size());
std::transform(friends_.begin(),
friends_.end(),
result.begin(),
[](const auto& wpFriend) noexcept {
return make_hero(wpFriend);
});
result.erase(std::remove(result.begin(), result.end(), std::shared_ptr<object::Character> {}),
result.end());
return result.empty() ? std::nullopt : std::make_optional(std::move(result));
}
std::optional<std::vector<std::optional<Episode>>> Droid::getAppearsIn() const noexcept
{
std::vector<std::optional<Episode>> result(appearsIn_.size());
std::transform(appearsIn_.begin(),
appearsIn_.end(),
result.begin(),
[](const auto& entry) noexcept {
return std::make_optional(entry);
});
return result.empty() ? std::nullopt : std::make_optional(std::move(result));
}
const std::optional<std::string>& Droid::getPrimaryFunction() const noexcept
{
return primaryFunction_;
}
} // namespace graphql::learn