template<typename AgentType>
struct nlohmann::adl_serializer< fpmas::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 :
...
public:
...
nlohmann::json& j,
const UserDefinedAgent* agent
);
const nlohmann::json& j
);
...
};
static void to_json(JsonType &j, const fpmas::api::utils::PtrWrapper< AgentType > &agent_ptr)
Definition: json_serializer.h:332
static fpmas::api::utils::PtrWrapper< AgentType > from_json(const JsonType &j)
Definition: json_serializer.h:321
The from_json method is assumed to return an heap allocated agent (initialized with a new statement).
- Example
private:
int count;
std::string message;
public:
Agent1(int count, std::string message)
: count(count), message(message) {}
static void to_json(nlohmann::json& j,
const Agent1* agent) {
j["c"] = agent->count;
j["m"] = agent->message;
}
static Agent1*
from_json(
const nlohmann::json& j) {
return new Agent1(
j.at("c").get<int>(),
j.at("m").get<std::string>()
);
}
void act() override;
};
- Note
- Notice that its still possible to define adl_serializer specialization without using the internal to_json / from_json methods.
- Example
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;}
void act() override;
};
template<>
struct adl_serializer<
fpmas::api::utils::PtrWrapper<Agent1>> {
json& j,
) {
j["c"] = agent_ptr->getCount();
j["m"] = agent_ptr->getMessage();
}
json& j
) {
return {new Agent1(
j.at("c").get<int>(),
j.at("m").get<std::string>()
)};
}
};
}
Definition: ptr_wrapper.h:21