Skip to content

Commit

Permalink
WIP: strings in reflection tables
Browse files Browse the repository at this point in the history
Working on cctbx/dxtbx#465

This could never have worked. Now trying to make something which does work.
This does not work. This is at least some stubs / breadcrumbs on how it
could work
  • Loading branch information
graeme-winter committed Jan 19, 2022
1 parent 7df0b0d commit 8113975
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion array_family/reflection_table_msgpack_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,64 @@ MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
}
};

/**
* Overload of above for type std::string
*/
template <>
struct pack<scitbx::af::const_ref<std::string> > {
template <typename Stream>
msgpack::packer<Stream>& operator()(
msgpack::packer<Stream>& o,
const scitbx::af::const_ref<std::string>& v) const {
typedef typename scitbx::af::const_ref<std::string>::const_iterator iterator;
std::stringstream buffer;
for (iterator it = v.begin(); it != v.end(); ++it) {
// N.B. + 1 because NULL terminated
uint32_t s = it->size();
buffer.write((const char*)&s, sizeof(uint32_t));
buffer.write(it->c_str(), (uint32_t)s);
std::cout << "Writing string of length " << s << std::endl;
}
std::string buffer_string = buffer.str();
std::cout << "Total size " << buffer_string.size() << std::endl;
o.pack_bin(buffer_string.size());
o.pack_bin_body(buffer_string.c_str(), buffer_string.size());
return o;
}
};

/**
* Unpacking thereof...
*/

scitbx::af::shared<std::string> extract_strings(msgpack::object const& o) {
// packed strings are different as the sizes are not uniform ->
// we need to pick out the strings one at a time and then push
// them onto the back of an array - N.B. these are packed as a
// uint32_t size followed by the bytes...

scitbx::af::shared<std::string> result;

size_t s;

std::size_t binary_size = o.via.bin.size;

std::cout << "Object size is " << binary_size << std::endl;
char* ptr = (char*)o.via.bin.ptr;

while (binary_size > 0) {
s = (size_t) * ((uint32_t*)ptr);
std::cout << "Reading string of length " << s << std::endl;
ptr += sizeof(uint32_t);
binary_size -= sizeof(uint32_t);
result.push_back(std::string((const char*)ptr, s));
ptr += s + 1;
binary_size -= s + 1;
}

return result;
}

/**
* Pack a shared<Shoebox<>> into a msgpack array.
*
Expand Down Expand Up @@ -663,7 +721,7 @@ MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
} else if (name == "double") {
v = extract<double>(o.via.array.ptr[1]);
} else if (name == "std::string") {
v = extract<std::string>(o.via.array.ptr[1]);
v = extract_strings(o.via.array.ptr[1]);
} else if (name == "vec2<double>") {
v = extract<scitbx::vec2<double> >(o.via.array.ptr[1]);
} else if (name == "vec3<double>") {
Expand Down

0 comments on commit 8113975

Please sign in to comment.