|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +// WARNING! Do not edit this file manually, your changes will be overwritten. |
| 5 | + |
| 6 | +#pragma once |
| 7 | + |
| 8 | +#ifndef ALIENOBJECT_H |
| 9 | +#define ALIENOBJECT_H |
| 10 | + |
| 11 | +#include "ValidationSchema.h" |
| 12 | + |
| 13 | +namespace graphql::validation::object { |
| 14 | +namespace implements { |
| 15 | + |
| 16 | +template <class I> |
| 17 | +concept AlienIs = std::is_same_v<I, Sentient> || std::is_same_v<I, HumanOrAlien>; |
| 18 | + |
| 19 | +} // namespace implements |
| 20 | + |
| 21 | +namespace methods::AlienHas { |
| 22 | + |
| 23 | +template <class TImpl> |
| 24 | +concept getNameWithParams = requires (TImpl impl, service::FieldParams params) |
| 25 | +{ |
| 26 | + { service::FieldResult<std::string> { impl.getName(std::move(params)) } }; |
| 27 | +}; |
| 28 | + |
| 29 | +template <class TImpl> |
| 30 | +concept getName = requires (TImpl impl) |
| 31 | +{ |
| 32 | + { service::FieldResult<std::string> { impl.getName() } }; |
| 33 | +}; |
| 34 | + |
| 35 | +template <class TImpl> |
| 36 | +concept getHomePlanetWithParams = requires (TImpl impl, service::FieldParams params) |
| 37 | +{ |
| 38 | + { service::FieldResult<std::optional<std::string>> { impl.getHomePlanet(std::move(params)) } }; |
| 39 | +}; |
| 40 | + |
| 41 | +template <class TImpl> |
| 42 | +concept getHomePlanet = requires (TImpl impl) |
| 43 | +{ |
| 44 | + { service::FieldResult<std::optional<std::string>> { impl.getHomePlanet() } }; |
| 45 | +}; |
| 46 | + |
| 47 | +template <class TImpl> |
| 48 | +concept beginSelectionSet = requires (TImpl impl, const service::SelectionSetParams params) |
| 49 | +{ |
| 50 | + { impl.beginSelectionSet(params) }; |
| 51 | +}; |
| 52 | + |
| 53 | +template <class TImpl> |
| 54 | +concept endSelectionSet = requires (TImpl impl, const service::SelectionSetParams params) |
| 55 | +{ |
| 56 | + { impl.endSelectionSet(params) }; |
| 57 | +}; |
| 58 | + |
| 59 | +} // namespace methods::AlienHas |
| 60 | + |
| 61 | +class Alien |
| 62 | + : public service::Object |
| 63 | +{ |
| 64 | +private: |
| 65 | + service::AwaitableResolver resolveName(service::ResolverParams&& params) const; |
| 66 | + service::AwaitableResolver resolveHomePlanet(service::ResolverParams&& params) const; |
| 67 | + |
| 68 | + service::AwaitableResolver resolve_typename(service::ResolverParams&& params) const; |
| 69 | + |
| 70 | + struct Concept |
| 71 | + { |
| 72 | + virtual ~Concept() = default; |
| 73 | + |
| 74 | + virtual void beginSelectionSet(const service::SelectionSetParams& params) const = 0; |
| 75 | + virtual void endSelectionSet(const service::SelectionSetParams& params) const = 0; |
| 76 | + |
| 77 | + virtual service::FieldResult<std::string> getName(service::FieldParams&& params) const = 0; |
| 78 | + virtual service::FieldResult<std::optional<std::string>> getHomePlanet(service::FieldParams&& params) const = 0; |
| 79 | + }; |
| 80 | + |
| 81 | + template <class T> |
| 82 | + struct Model |
| 83 | + : Concept |
| 84 | + { |
| 85 | + Model(std::shared_ptr<T>&& pimpl) noexcept |
| 86 | + : _pimpl { std::move(pimpl) } |
| 87 | + { |
| 88 | + } |
| 89 | + |
| 90 | + service::FieldResult<std::string> getName(service::FieldParams&& params) const final |
| 91 | + { |
| 92 | + if constexpr (methods::AlienHas::getNameWithParams<T>) |
| 93 | + { |
| 94 | + return { _pimpl->getName(std::move(params)) }; |
| 95 | + } |
| 96 | + else if constexpr (methods::AlienHas::getName<T>) |
| 97 | + { |
| 98 | + return { _pimpl->getName() }; |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + throw std::runtime_error(R"ex(Alien::getName is not implemented)ex"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + service::FieldResult<std::optional<std::string>> getHomePlanet(service::FieldParams&& params) const final |
| 107 | + { |
| 108 | + if constexpr (methods::AlienHas::getHomePlanetWithParams<T>) |
| 109 | + { |
| 110 | + return { _pimpl->getHomePlanet(std::move(params)) }; |
| 111 | + } |
| 112 | + else if constexpr (methods::AlienHas::getHomePlanet<T>) |
| 113 | + { |
| 114 | + return { _pimpl->getHomePlanet() }; |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + throw std::runtime_error(R"ex(Alien::getHomePlanet is not implemented)ex"); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + void beginSelectionSet(const service::SelectionSetParams& params) const final |
| 123 | + { |
| 124 | + if constexpr (methods::AlienHas::beginSelectionSet<T>) |
| 125 | + { |
| 126 | + _pimpl->beginSelectionSet(params); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + void endSelectionSet(const service::SelectionSetParams& params) const final |
| 131 | + { |
| 132 | + if constexpr (methods::AlienHas::endSelectionSet<T>) |
| 133 | + { |
| 134 | + _pimpl->endSelectionSet(params); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private: |
| 139 | + const std::shared_ptr<T> _pimpl; |
| 140 | + }; |
| 141 | + |
| 142 | + Alien(std::unique_ptr<Concept>&& pimpl) noexcept; |
| 143 | + |
| 144 | + // Interfaces which this type implements |
| 145 | + friend Sentient; |
| 146 | + |
| 147 | + // Unions which include this type |
| 148 | + friend HumanOrAlien; |
| 149 | + |
| 150 | + template <class I> |
| 151 | + static constexpr bool implements() noexcept |
| 152 | + { |
| 153 | + return implements::AlienIs<I>; |
| 154 | + } |
| 155 | + |
| 156 | + service::TypeNames getTypeNames() const noexcept; |
| 157 | + service::ResolverMap getResolvers() const noexcept; |
| 158 | + |
| 159 | + void beginSelectionSet(const service::SelectionSetParams& params) const final; |
| 160 | + void endSelectionSet(const service::SelectionSetParams& params) const final; |
| 161 | + |
| 162 | + const std::unique_ptr<Concept> _pimpl; |
| 163 | + |
| 164 | +public: |
| 165 | + template <class T> |
| 166 | + Alien(std::shared_ptr<T> pimpl) noexcept |
| 167 | + : Alien { std::unique_ptr<Concept> { std::make_unique<Model<T>>(std::move(pimpl)) } } |
| 168 | + { |
| 169 | + } |
| 170 | +}; |
| 171 | + |
| 172 | +} // namespace graphql::validation::object |
| 173 | + |
| 174 | +#endif // ALIENOBJECT_H |
0 commit comments