Skip to content

Commit a13ebbb

Browse files
committed
Allow specify schema in capnproto's stream interface
1 parent afc0255 commit a13ebbb

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,12 @@ if (REFLECTCPP_CAPNPROTO)
241241
if (NOT TARGET CapnProto)
242242
find_package(CapnProto CONFIG REQUIRED)
243243
endif ()
244-
target_link_libraries(reflectcpp PUBLIC CapnProto::kj CapnProto::capnp CapnProto::capnpc CapnProto::kj-gzip)
244+
if (NOT TARGET CapnProto::capnpc)
245+
# in some old versions, there is no alias target CapnProto::capnpc, fallback to capnc
246+
target_link_libraries(reflectcpp PUBLIC CapnProto::kj CapnProto::capnp capnpc CapnProto::kj-gzip)
247+
else()
248+
target_link_libraries(reflectcpp PUBLIC CapnProto::kj CapnProto::capnp CapnProto::capnpc CapnProto::kj-gzip)
249+
endif()
245250
endif ()
246251

247252
if (REFLECTCPP_CBOR)

include/rfl/capnproto/read.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ auto read(std::istream& _stream) {
7878
return read<T, Ps...>(bytes.data(), bytes.size());
7979
}
8080

81+
template <class T, class... Ps>
82+
auto read(std::istream& _stream, const Schema<T>& _schema) {
83+
std::istreambuf_iterator<char> begin(_stream), end;
84+
auto bytes = std::vector<char>(begin, end);
85+
return read<T, Ps...>(bytes.data(), bytes.size(), _schema);
86+
}
87+
8188
} // namespace rfl::capnproto
8289

8390
#endif

include/rfl/capnproto/write.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ std::ostream& write(const auto& _obj, std::ostream& _stream) {
6767
return _stream;
6868
}
6969

70+
template <class... Ps>
71+
std::ostream& write(const auto& _obj, const auto& _schema, std::ostream& _stream) {
72+
auto buffer = write<Ps...>(_obj, _schema);
73+
_stream.write(buffer.data(), buffer.size());
74+
return _stream;
75+
}
76+
7077
} // namespace rfl::capnproto
7178

7279
#endif

0 commit comments

Comments
 (0)