DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::serialization Namespace Reference

Classes

struct  is_serializable
 

Enumerations

enum  SerializationType { kMsgPack }
 Serialization methods that are available. More...
 

Functions

SerializationType from_string (const std::string s)
 Convert string to SerializationType.
 
constexpr uint8_t serialization_type_byte (SerializationType stype)
 
template<class T >
std::vector< uint8_t > serialize (const T &obj, SerializationType stype)
 Serialize object obj using serialization method stype.
 
template<class T , typename CharType = unsigned char>
deserialize (const std::vector< CharType > &v)
 Deserialize vector of bytes v into an instance of class T.
 

Enumeration Type Documentation

◆ SerializationType

Serialization methods that are available.

Enumerator
kMsgPack 

Definition at line 163 of file Serialization.hpp.

Function Documentation

◆ deserialize()

template<class T , typename CharType = unsigned char>
T dunedaq::serialization::deserialize ( const std::vector< CharType > & v)

Deserialize vector of bytes v into an instance of class T.

Definition at line 221 of file Serialization.hpp.

222{
223 // The first byte in the array indicates the serialization format;
224 // the rest is the actual message
225 switch (v[0]) {
226 case serialization_type_byte(kMsgPack): {
227 try {
228 // The lambda function here is of type `unpack_reference_func`
229 // as described at
230 // https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_unpacker#memory-management
231 // . It is called for every STR, BIN and EXT field in the
232 // MsgPack data. If the function returns false, the object is
233 // copied into MsgPack's "zone", otherwise a pointer to the
234 // original buffer is stored. Our input buffer is going to exist
235 // at least until the end of this function, so it's safe to
236 // return true (ie, store a pointer in the MsgPack object; no
237 // copy) everywhere. Doing so results in a factor ~2 speedup in
238 // deserializing Fragment, which is just a large BIN field
239 msgpack::object_handle oh = msgpack::unpack(
240 const_cast<char*>(reinterpret_cast<const char*>(v.data() + 1)),
241 v.size() - 1,
242 [](msgpack::type::object_type /*typ*/, std::size_t /*length*/, void* /*user_data*/) -> bool { return true; });
243 msgpack::object obj = oh.get();
244 return obj.as<T>();
245 } catch (msgpack::type_error& e) {
247 } catch (msgpack::unpack_error& e) {
248 throw CannotDeserializeMessage(ERS_HERE, e);
249 }
250 }
251 default:
252 throw UnknownSerializationTypeByte(ERS_HERE, (char)v[0]); // NOLINT
253 }
254}
#define ERS_HERE
constexpr uint8_t serialization_type_byte(SerializationType stype)
Unknown serialization CannotDeserializeMessage

◆ from_string()

SerializationType dunedaq::serialization::from_string ( const std::string s)
inline

Convert string to SerializationType.

Definition at line 172 of file Serialization.hpp.

173{
174 if (s == "msgpack")
175 return kMsgPack;
176 throw UnknownSerializationTypeString(ERS_HERE, s);
177}

◆ serialization_type_byte()

uint8_t dunedaq::serialization::serialization_type_byte ( SerializationType stype)
constexpr

Definition at line 180 of file Serialization.hpp.

181{
182 switch (stype) {
183 case kMsgPack:
184 return 'M';
185 default:
187 }
188}
UnknownSerializationTypeEnum

◆ serialize()

template<class T >
std::vector< uint8_t > dunedaq::serialization::serialize ( const T & obj,
SerializationType stype )

Serialize object obj using serialization method stype.

Definition at line 195 of file Serialization.hpp.

196{
197 switch (stype) {
198 case kMsgPack: {
199 // Serialize into the sbuffer and then copy to a
200 // std::vector. Seems like it would be more efficient to
201 // write directly to the vector (by creating a class that
202 // implements `void write(char* buf, size_t len)`), but my
203 // tests aren't any faster than this
204 msgpack::sbuffer buf;
205 msgpack::pack(buf, obj);
206 std::vector<uint8_t> ret(buf.size() + 1); // NOLINT(build/unsigned)
207 ret[0] = serialization_type_byte(stype);
208 std::copy(buf.data(), buf.data() + buf.size(), ret.begin() + 1); // NOLINT
209 return ret;
210 }
211 default:
213 }
214}