Skip to content

Commit 117120b

Browse files
committed
feat: replace response::Writer with response::ValueVisitor
1 parent 11d7d0c commit 117120b

File tree

6 files changed

+141
-168
lines changed

6 files changed

+141
-168
lines changed

doc/json.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private:
5858
virtual void end_object() const = 0;
5959

6060
virtual void start_array() const = 0;
61-
virtual void end_arrary() const = 0;
61+
virtual void end_array() const = 0;
6262

6363
virtual void write_null() const = 0;
6464
virtual void write_string(const std::string& value) const = 0;

include/graphqlservice/GraphQLResponse.h

Lines changed: 3 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ class [[nodiscard("unnecessary construction")]] ValueVisitor final
500500
template <class T>
501501
ValueVisitor(std::shared_ptr<T> writer) noexcept
502502
: _concept { std::static_pointer_cast<Concept>(
503-
std::make_shared<Model<T>>(std::move(writer))) }
503+
std::make_shared<Model<T>>(std::move(writer))) }
504504
{
505505
}
506506

@@ -635,6 +635,8 @@ class [[nodiscard("unnecessary construction")]] ValueTokenStream final
635635
ValueTokenStream() noexcept = default;
636636
~ValueTokenStream() = default;
637637

638+
GRAPHQLRESPONSE_EXPORT explicit ValueTokenStream(Value&& value);
639+
638640
ValueTokenStream(ValueTokenStream&&) noexcept = default;
639641
ValueTokenStream& operator=(ValueTokenStream&&) noexcept = default;
640642

@@ -662,102 +664,6 @@ class [[nodiscard("unnecessary construction")]] ValueTokenStream final
662664
std::list<ValueToken> _tokens;
663665
};
664666

665-
class [[nodiscard("unnecessary construction")]] Writer final
666-
{
667-
private:
668-
struct Concept
669-
{
670-
virtual ~Concept() = default;
671-
672-
virtual void start_object() const = 0;
673-
virtual void add_member(const std::string& key) const = 0;
674-
virtual void end_object() const = 0;
675-
676-
virtual void start_array() const = 0;
677-
virtual void end_arrary() const = 0;
678-
679-
virtual void write_null() const = 0;
680-
virtual void write_string(const std::string& value) const = 0;
681-
virtual void write_bool(bool value) const = 0;
682-
virtual void write_int(int value) const = 0;
683-
virtual void write_float(double value) const = 0;
684-
};
685-
686-
template <class T>
687-
struct Model : Concept
688-
{
689-
explicit Model(std::unique_ptr<T> pimpl) noexcept
690-
: _pimpl { std::move(pimpl) }
691-
{
692-
}
693-
694-
void start_object() const final
695-
{
696-
_pimpl->start_object();
697-
}
698-
699-
void add_member(const std::string& key) const final
700-
{
701-
_pimpl->add_member(key);
702-
}
703-
704-
void end_object() const final
705-
{
706-
_pimpl->end_object();
707-
}
708-
709-
void start_array() const final
710-
{
711-
_pimpl->start_array();
712-
}
713-
714-
void end_arrary() const final
715-
{
716-
_pimpl->end_arrary();
717-
}
718-
719-
void write_null() const final
720-
{
721-
_pimpl->write_null();
722-
}
723-
724-
void write_string(const std::string& value) const final
725-
{
726-
_pimpl->write_string(value);
727-
}
728-
729-
void write_bool(bool value) const final
730-
{
731-
_pimpl->write_bool(value);
732-
}
733-
734-
void write_int(int value) const final
735-
{
736-
_pimpl->write_int(value);
737-
}
738-
739-
void write_float(double value) const final
740-
{
741-
_pimpl->write_float(value);
742-
}
743-
744-
private:
745-
std::unique_ptr<T> _pimpl;
746-
};
747-
748-
const std::shared_ptr<const Concept> _concept;
749-
750-
public:
751-
template <class T>
752-
Writer(std::unique_ptr<T> writer) noexcept
753-
: _concept { std::static_pointer_cast<const Concept>(
754-
std::make_shared<Model<T>>(std::move(writer))) }
755-
{
756-
}
757-
758-
GRAPHQLRESPONSE_EXPORT void write(Value value) const;
759-
};
760-
761667
} // namespace graphql::response
762668

763669
#endif // GRAPHQLRESPONSE_H

include/graphqlservice/Response.ixx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ using response::AwaitableValue;
3131
using response::ValueVisitor;
3232
using response::ValueToken;
3333
using response::ValueTokenStream;
34-
35-
using response::Writer;
3634
// clang-format on
3735

3836
} // namespace graphql::response

src/GraphQLResponse.cpp

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ bool IdType::operator==(const IdType& rhs) const noexcept
101101

102102
return (std::holds_alternative<ByteData>(_data)
103103
? internal::Base64::compareBase64(std::get<ByteData>(_data),
104-
std::get<OpaqueString>(rhs._data))
104+
std::get<OpaqueString>(rhs._data))
105105
: internal::Base64::compareBase64(std::get<ByteData>(rhs._data),
106-
std::get<OpaqueString>(_data)))
106+
std::get<OpaqueString>(_data)))
107107
== internal::Base64::Comparison::EqualTo;
108108
}
109109

@@ -146,7 +146,7 @@ bool IdType::operator<(const IdType& rhs) const noexcept
146146
return (std::holds_alternative<ByteData>(_data)
147147
? (internal::Base64::compareBase64(std::get<ByteData>(_data),
148148
std::get<OpaqueString>(rhs._data))
149-
< internal::Base64::Comparison::EqualTo)
149+
< internal::Base64::Comparison::EqualTo)
150150
: (internal::Base64::compareBase64(std::get<ByteData>(rhs._data),
151151
std::get<OpaqueString>(_data)))
152152
> internal::Base64::Comparison::EqualTo);
@@ -1802,111 +1802,127 @@ void ValueTokenStreamVisitor::add_value(Value&& value)
18021802
}
18031803
}
18041804

1805-
void ValueTokenStream::append(ValueTokenStream&& other)
1806-
{
1807-
_tokens.splice(_tokens.end(), std::move(other._tokens));
1808-
}
1809-
1810-
void ValueTokenStream::visit(const std::shared_ptr<ValueVisitor>& visitor) &&
1805+
ValueTokenStream::ValueTokenStream(Value&& value)
18111806
{
1812-
for (auto& token : _tokens)
1813-
{
1814-
std::move(token).visit(visitor);
1815-
}
1816-
1817-
visitor->complete();
1818-
}
1819-
1820-
Value ValueTokenStream::value() &&
1821-
{
1822-
auto visitor = std::make_shared<ValueTokenStreamVisitor>();
1823-
1824-
std::move(*this).visit(std::make_shared<ValueVisitor>(visitor));
1825-
1826-
return visitor->value();
1827-
}
1828-
1829-
void Writer::write(Value response) const
1830-
{
1831-
switch (response.type())
1807+
switch (value.type())
18321808
{
18331809
case Type::Map:
18341810
{
1835-
auto members = response.release<MapType>();
1811+
auto members = value.release<MapType>();
18361812

1837-
_concept->start_object();
1813+
push_back(ValueToken::StartObject {});
1814+
push_back(ValueToken::Reserve { members.size() });
18381815

18391816
for (auto& entry : members)
18401817
{
1841-
_concept->add_member(entry.first);
1842-
write(std::move(entry.second));
1818+
push_back(ValueToken::AddMember { std::move(entry.first) });
1819+
append(ValueTokenStream { std::move(entry.second) });
18431820
}
18441821

1845-
_concept->end_object();
1822+
push_back(ValueToken::EndObject {});
18461823
break;
18471824
}
18481825

18491826
case Type::List:
18501827
{
1851-
auto elements = response.release<ListType>();
1828+
auto elements = value.release<ListType>();
18521829

1853-
_concept->start_array();
1830+
push_back(ValueToken::StartArray {});
1831+
push_back(ValueToken::Reserve { elements.size() });
18541832

18551833
for (auto& entry : elements)
18561834
{
1857-
write(std::move(entry));
1835+
append(ValueTokenStream { std::move(entry) });
18581836
}
18591837

1860-
_concept->end_arrary();
1838+
push_back(ValueToken::EndArray {});
18611839
break;
18621840
}
18631841

18641842
case Type::String:
1865-
case Type::EnumValue:
1866-
case Type::ID:
18671843
{
1868-
auto value = response.release<StringType>();
1844+
auto stringValue = value.release<StringType>();
18691845

1870-
_concept->write_string(value);
1846+
push_back(ValueToken::StringValue { std::move(stringValue) });
18711847
break;
18721848
}
18731849

18741850
case Type::Null:
18751851
{
1876-
_concept->write_null();
1852+
push_back(ValueToken::NullValue {});
18771853
break;
18781854
}
18791855

18801856
case Type::Boolean:
18811857
{
1882-
_concept->write_bool(response.get<BooleanType>());
1858+
push_back(ValueToken::BoolValue { value.get<BooleanType>() });
18831859
break;
18841860
}
18851861

18861862
case Type::Int:
18871863
{
1888-
_concept->write_int(response.get<IntType>());
1864+
push_back(ValueToken::IntValue { value.get<IntType>() });
18891865
break;
18901866
}
18911867

18921868
case Type::Float:
18931869
{
1894-
_concept->write_float(response.get<FloatType>());
1870+
push_back(ValueToken::FloatValue { value.get<FloatType>() });
1871+
break;
1872+
}
1873+
1874+
case Type::EnumValue:
1875+
{
1876+
auto enumValue = value.release<StringType>();
1877+
1878+
push_back(ValueToken::EnumValue { std::move(enumValue) });
1879+
break;
1880+
}
1881+
1882+
case Type::ID:
1883+
{
1884+
auto idValue = value.release<IdType>();
1885+
1886+
push_back(ValueToken::IdValue { std::move(idValue) });
18951887
break;
18961888
}
18971889

18981890
case Type::Scalar:
18991891
{
1900-
write(response.release<ScalarType>());
1892+
append(ValueTokenStream { value.release<ScalarType>() });
19011893
break;
19021894
}
19031895

19041896
default:
19051897
{
1906-
_concept->write_null();
1898+
push_back(ValueToken::NullValue {});
19071899
break;
19081900
}
19091901
}
19101902
}
19111903

1904+
void ValueTokenStream::append(ValueTokenStream&& other)
1905+
{
1906+
_tokens.splice(_tokens.end(), std::move(other._tokens));
1907+
}
1908+
1909+
void ValueTokenStream::visit(const std::shared_ptr<ValueVisitor>& visitor) &&
1910+
{
1911+
for (auto& token : _tokens)
1912+
{
1913+
std::move(token).visit(visitor);
1914+
}
1915+
1916+
visitor->complete();
1917+
}
1918+
1919+
Value ValueTokenStream::value() &&
1920+
{
1921+
auto visitor = std::make_shared<ValueTokenStreamVisitor>();
1922+
1923+
std::move(*this).visit(std::make_shared<ValueVisitor>(visitor));
1924+
1925+
return visitor->value();
1926+
}
1927+
19121928
} // namespace graphql::response

0 commit comments

Comments
 (0)