fpmas 1.5
Static Public Member Functions | List of all members
fpmas::io::datapack::Serializer< api::utils::PtrWrapper< AgentType > > Struct Template Reference

#include <datapack_serializer.h>

Static Public Member Functions

template<typename PackType >
static std::size_t size (const PackType &p, const api::utils::PtrWrapper< AgentType > &agent_ptr)
 
template<typename PackType >
static void to_datapack (PackType &pack, const api::utils::PtrWrapper< AgentType > &agent_ptr)
 
template<typename PackType >
static api::utils::PtrWrapper< AgentType > from_datapack (const PackType &pack)
 

Detailed Description

template<typename AgentType>
struct fpmas::io::datapack::Serializer< api::utils::PtrWrapper< AgentType > >

Generic Agent pointer serialization.

This partial specialization can be used to easily define serialization rules directly from the Agent implementation, using the following static method definitions :

class UserDefinedAgent : fpmas::model::AgentBase<UserDefinedAgent> {
...
public:
...
static void to_datapack(
const UserDefinedAgent* agent
);
static UserDefinedAgent* from_datapack(
);
...
};
Definition: datapack.h:275
Definition: model.h:702
static api::utils::PtrWrapper< AgentType > from_datapack(const PackType &pack)
Definition: datapack_serializer.h:399
static void to_datapack(PackType &pack, const api::utils::PtrWrapper< AgentType > &agent_ptr)
Definition: datapack_serializer.h:387

The from_datapack method is assumed to return an heap allocated agent (initialized with a new statement).

Example
class Agent1 : public fpmas::model::AgentBase<Agent1> {
private:
int count;
std::string message;
public:
Agent1(int count, std::string message)
: count(count), message(message) {}
static void to_datapack(fpmas::io::datapack::ObjectPack& o, const Agent1* agent) {
using namespace fpmas::io::datapack;
o.allocate(pack_size<int>() + pack_size(agent->message));
o.write(agent->count);
o.write(agent->message);
}
using namespace fpmas::io::datapack;
int count;
std::string message;
o.read(count);
o.read(message);
return new Agent1(count, message);
}
};
void read(T &item) const
Definition: datapack.h:533
void allocate(std::size_t size)
Definition: datapack.h:444
void write(const T &item)
Definition: datapack.h:495
Note
Notice that its still possible to define adl_serializer specialization without using the internal to_json / from_json methods.
Example
class Agent1 : public fpmas::model::AgentBase<Agent1> {
private:
int count;
std::string message;
public:
Agent1(int count, std::string message)
: count(count), message(message) {}
int getCount() const {return count;}
std::string getMessage() const {return message;}
};
namespace fpmas { namespace io { namespace datapack {
template<>
struct Serializer<fpmas::api::utils::PtrWrapper<Agent1>> {
static void to_datapack(
) {
o.allocate(pack_size<int>() + pack_size(agent->message));
o.write(agent->count);
o.write(agent->message);
}
) {
int count;
std::string message;
o.read(count);
o.read(message);
return new Agent1(count, message);
}
};
}}}
Definition: ptr_wrapper.h:21
BasicObjectPack< Serializer > ObjectPack
Definition: datapack.h:1364
Definition: fpmas.cpp:3

Member Function Documentation

◆ size()

template<typename AgentType >
template<typename PackType >
static std::size_t fpmas::io::datapack::Serializer< api::utils::PtrWrapper< AgentType > >::size ( const PackType &  p,
const api::utils::PtrWrapper< AgentType > &  agent_ptr 
)
inlinestatic

Returns the buffer size required to serialize the AgentType pointed by agent_ptr to p, as specified by the AgentType::size(p) method.

◆ to_datapack()

template<typename AgentType >
template<typename PackType >
static void fpmas::io::datapack::Serializer< api::utils::PtrWrapper< AgentType > >::to_datapack ( PackType &  pack,
const api::utils::PtrWrapper< AgentType > &  agent_ptr 
)
inlinestatic

Calls AgentType::to_datapack(pack, agent_ptr.get()).

Parameters
packdestination BasicObjectPack
agent_ptragent pointer to serialized

◆ from_datapack()

template<typename AgentType >
template<typename PackType >
static api::utils::PtrWrapper< AgentType > fpmas::io::datapack::Serializer< api::utils::PtrWrapper< AgentType > >::from_datapack ( const PackType &  pack)
inlinestatic

Returns a PtrWrapper initialized with AgentType::from_datapack(pack).

Parameters
packsource BasicObjectPack
Returns
unserialized agent pointer

The documentation for this struct was generated from the following file: