Skip to content

Commit 6fc53bb

Browse files
Fixed Cap'n Proto
1 parent 0c4de4f commit 6fc53bb

3 files changed

Lines changed: 21 additions & 16 deletions

File tree

include/rfl/capnproto/Reader.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ class RFL_API Reader {
166166
for (auto entry : entries) {
167167
auto s = entry.template as<capnp::DynamicStruct>();
168168
const auto key = s.get("key").as<capnp::Text>();
169-
_map_reader.read(std::string_view(key.cStr(), key.size()), InputVarType{s.get("value")});
169+
_map_reader.read(std::string_view(key.cStr(), key.size()),
170+
InputVarType{s.get("value")});
170171
}
171172
return std::nullopt;
172173
} catch (std::exception& e) {

include/rfl/capnproto/Writer.hpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
#include <string_view>
1111
#include <type_traits>
1212

13-
// #include "../Box.hpp"
1413
#include "../Bytestring.hpp"
15-
// #include "../Ref.hpp"
16-
// #include "../Result.hpp"
1714
#include "../Vectorstring.hpp"
1815
#include "../always_false.hpp"
19-
#include "../internal/is_literal.hpp"
20-
// #include "../internal/ptr_cast.hpp"
2116
#include "../common.hpp"
17+
#include "../internal/is_literal.hpp"
2218

2319
namespace rfl::capnproto {
2420

@@ -193,7 +189,7 @@ class RFL_API Writer {
193189
auto entries =
194190
OutputArrayType{_parent->val_.get("entries").as<capnp::DynamicList>()};
195191
auto new_entry = add_object_to_array(2, &entries);
196-
new_entry.val_.set("key", { _name.data(), _name.size() });
192+
add_value_to_object("key", _name, &new_entry);
197193
return add_value_to_object("value", _var, &new_entry);
198194
}
199195

@@ -202,20 +198,22 @@ class RFL_API Writer {
202198
const T& _var,
203199
OutputObjectType* _parent) const {
204200
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
205-
_parent->val_.set({ _name.data(), _name.size() }, { _var.c_str(), _var.size() });
201+
_parent->val_.set(to_kj_string_ptr(_name), _var.c_str());
206202

207203
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
208204
rfl::Bytestring>()) {
209205
const auto array_ptr = kj::ArrayPtr<const kj::byte>(
210206
internal::ptr_cast<const unsigned char*>(_var.data()), _var.size());
211-
_parent->val_.set({ _name.data(), _name.size() }, capnp::Data::Reader(array_ptr));
207+
_parent->val_.set(to_kj_string_ptr(_name),
208+
capnp::Data::Reader(array_ptr));
212209

213210
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
214211
std::is_same<std::remove_cvref_t<T>, bool>()) {
215-
_parent->val_.set({ _name.data(), _name.size() }, _var);
212+
_parent->val_.set(to_kj_string_ptr(_name), _var);
216213

217214
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
218-
_parent->val_.set({ _name.data(), _name.size() }, static_cast<std::int64_t>(_var));
215+
_parent->val_.set(to_kj_string_ptr(_name),
216+
static_cast<std::int64_t>(_var));
219217

220218
} else if constexpr (internal::is_literal_v<T>) {
221219
return add_value_to_object(_name, _var.value(), _parent);
@@ -262,6 +260,11 @@ class RFL_API Writer {
262260

263261
void end_object(OutputObjectType* /*_obj*/) const {}
264262

263+
private:
264+
kj::StringPtr to_kj_string_ptr(const std::string_view& _str) const {
265+
return kj::StringPtr(_str.begin(), _str.end());
266+
}
267+
265268
private:
266269
capnp::DynamicStruct::Builder* root_;
267270
};

src/rfl/capnproto/Writer.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ Writer::OutputArrayType Writer::add_array_to_map(const std::string_view& _name,
4747
Writer::OutputArrayType Writer::add_array_to_object(
4848
const std::string_view& _name, const size_t _size,
4949
OutputObjectType* _parent) const {
50-
return OutputArrayType{_parent->val_.init({ _name.data(), _name.size() }, SafeSizeToUInt(_size))
51-
.as<capnp::DynamicList>()};
50+
return OutputArrayType{
51+
_parent->val_.init(to_kj_string_ptr(_name), SafeSizeToUInt(_size))
52+
.as<capnp::DynamicList>()};
5253
}
5354

5455
Writer::OutputArrayType Writer::add_array_to_union(
@@ -111,7 +112,7 @@ Writer::OutputObjectType Writer::add_object_to_object(
111112
const std::string_view& _name, const size_t /*_size*/,
112113
OutputObjectType* _parent) const {
113114
return OutputObjectType{
114-
_parent->val_.get({ _name.data(), _name.size() }).as<capnp::DynamicStruct>()};
115+
_parent->val_.get(to_kj_string_ptr(_name)).as<capnp::DynamicStruct>()};
115116
}
116117

117118
Writer::OutputObjectType Writer::add_object_to_union(
@@ -142,7 +143,7 @@ Writer::OutputUnionType Writer::add_union_to_map(const std::string_view& _name,
142143
Writer::OutputUnionType Writer::add_union_to_object(
143144
const std::string_view& _name, OutputObjectType* _parent) const {
144145
return OutputUnionType{
145-
_parent->val_.get({ _name.data(), _name.size() }).as<capnp::DynamicStruct>()};
146+
_parent->val_.get(to_kj_string_ptr(_name)).as<capnp::DynamicStruct>()};
146147
}
147148

148149
Writer::OutputUnionType Writer::add_union_to_union(
@@ -171,7 +172,7 @@ Writer::OutputVarType Writer::add_null_to_map(const std::string_view& _name,
171172

172173
Writer::OutputVarType Writer::add_null_to_object(
173174
const std::string_view& _name, OutputObjectType* _parent) const {
174-
_parent->val_.set({ _name.data(), _name.size() }, capnp::VOID);
175+
_parent->val_.set(to_kj_string_ptr(_name), capnp::VOID);
175176
return OutputVarType{};
176177
}
177178

0 commit comments

Comments
 (0)