fpmas 1.6
distributed_node.h
Go to the documentation of this file.
1#ifndef FPMAS_DISTRIBUTED_NODE_H
2#define FPMAS_DISTRIBUTED_NODE_H
3
10#include "fpmas/graph/node.h"
11#include "fpmas/io/json.h"
12#include "fpmas/io/datapack.h"
13
14namespace fpmas { namespace graph {
15
17 using api::graph::DistributedId;
18
22 template<typename T>
24 public Node<DistributedId, api::graph::DistributedEdge<T>>,
28
29 private:
31 int _location;
32 T _data;
33 Mutex* _mutex = nullptr;
34
35 public:
44 DistributedNode(const DistributedId& id, const T& data)
45 : NodeBase(id), _data(data) {
46 }
47
57 : NodeBase(id), _data(std::move(data)) {
58 }
59
60 int location() const override {return _location;}
61 void setLocation(int location) override {this->_location = location;}
62
63 api::graph::LocationState state() const override {return _state;}
64 void setState(api::graph::LocationState state) override {this->_state=state;}
65
66
67 T& data() override {return _data;}
68 const T& data() const override {return _data;}
69 void setMutex(Mutex* mutex) override {_mutex=mutex;}
70 Mutex* mutex() override {return _mutex;}
71 const Mutex* mutex() const override {return _mutex;}
72
74 if(_mutex!=nullptr)
75 delete _mutex;
76 }
77 };
78
82 template<typename T>
84}}
85
86namespace nlohmann {
88
94 template<typename T>
95 struct adl_serializer<NodePtrWrapper<T>> {
104 static void to_json(json& j, const NodePtrWrapper<T>& node) {
105 j["id"] = node->getId();
106 j["data"] = node->data();
107 j["weight"] = node->getWeight();
108 }
109
118 static NodePtrWrapper<T> from_json(const json& j) {
119 auto node = new fpmas::graph::DistributedNode<T> {
120 j.at("id").get<DistributedId>(),
121 std::move(j.at("data").get<T>())
122 };
123 node->setWeight(j.at("weight").get<float>());
124 return node;
125 }
126 };
127}
128
129namespace fpmas { namespace io {
130 namespace json {
132
143 template<typename T>
155 static void to_json(light_json& j, const NodePtrWrapper<T>& node) {
156 j["id"] = node->getId();
157 j["data"] = node->data();
158 }
172 static void from_json(const light_json& j, NodePtrWrapper<T>& node_ptr) {
173 node_ptr = {new fpmas::graph::DistributedNode<T>(
174 j["id"].get<fpmas::graph::DistributedId>(),
175 j["data"].get<T>()
176 )};
177 }
178 };
179 }
180
181 namespace datapack {
183
191 template<typename T>
196 static std::size_t size(
197 const ObjectPack& p, const NodePtrWrapper<T>& node) {
198 return p.size<DistributedId>() + p.size(node->data())
199 + p.size<float>();
200 }
207 static void to_datapack(ObjectPack& pack, const NodePtrWrapper<T>& node) {
208 pack.put(node->getId());
209 pack.put(node->data());
210 pack.put(node->getWeight());
211 }
212
219 static NodePtrWrapper<T> from_datapack(const ObjectPack& pack) {
220 DistributedId id = pack.get<DistributedId>();
221 T data = pack.get<T>();
222 auto node = new fpmas::graph::DistributedNode<T>(
223 std::move(id), std::move(data)
224 );
225 node->setWeight(pack.get<float>());
226 return node;
227 }
228 };
229
244 template<typename T>
253 static std::size_t size(
254 const LightObjectPack& p, const NodePtrWrapper<T>& node) {
255 return p.size<DistributedId>() + p.size(node->data());
256 }
257
264 static void to_datapack(LightObjectPack& pack, const NodePtrWrapper<T>& node) {
265 pack.put(node->getId());
266 pack.put(node->data());
267 }
268
275 static NodePtrWrapper<T> from_datapack(const LightObjectPack& pack) {
276 // Call order guaranteed, DO NOT CALL gets FROM THE CONSTRUCTOR
277 DistributedId id = pack.get<DistributedId>();
278 T data = pack.get<T>();
279 auto node = new fpmas::graph::DistributedNode<T>(
280 std::move(id), std::move(data)
281 );
282 return node;
283 }
284 };
285 }
286}}
287#endif
Definition: distributed_id.h:89
Definition: distributed_node.h:28
Definition: mutex.h:26
Definition: ptr_wrapper.h:21
Definition: distributed_node.h:25
void setState(api::graph::LocationState state) override
Definition: distributed_node.h:64
void setLocation(int location) override
Definition: distributed_node.h:61
void setMutex(Mutex *mutex) override
Definition: distributed_node.h:69
DistributedNode(const DistributedId &id, T &&data)
Definition: distributed_node.h:56
int location() const override
Definition: distributed_node.h:60
const Mutex * mutex() const override
Definition: distributed_node.h:71
Mutex * mutex() override
Definition: distributed_node.h:70
api::graph::LocationState state() const override
Definition: distributed_node.h:63
T & data() override
Definition: distributed_node.h:67
DistributedNode(const DistributedId &id, const T &data)
Definition: distributed_node.h:44
const T & data() const override
Definition: distributed_node.h:68
Definition: node.h:22
Definition: datapack.h:301
std::size_t size() const
Definition: datapack.h:367
void put(const T &item)
Definition: datapack.h:447
T get() const
Definition: datapack.h:459
LocationState
Definition: location_state.h:15
@ LOCAL
Definition: location_state.h:21
api::utils::PtrWrapper< api::graph::DistributedNode< T > > NodePtrWrapper
Definition: distributed_node.h:83
nlohmann::basic_json< std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, double, std::allocator, light_serializer > light_json
Definition: json.h:14
Definition: fpmas.cpp:3
static NodePtrWrapper< T > from_datapack(const LightObjectPack &pack)
Definition: distributed_node.h:275
static void to_datapack(LightObjectPack &pack, const NodePtrWrapper< T > &node)
Definition: distributed_node.h:264
static std::size_t size(const LightObjectPack &p, const NodePtrWrapper< T > &node)
Definition: distributed_node.h:253
Definition: datapack.h:1411
static std::size_t size(const ObjectPack &p, const NodePtrWrapper< T > &node)
Definition: distributed_node.h:196
static void to_datapack(ObjectPack &pack, const NodePtrWrapper< T > &node)
Definition: distributed_node.h:207
static NodePtrWrapper< T > from_datapack(const ObjectPack &pack)
Definition: distributed_node.h:219
Definition: datapack.h:55
static void from_json(const light_json &j, NodePtrWrapper< T > &node_ptr)
Definition: distributed_node.h:172
static void to_json(light_json &j, const NodePtrWrapper< T > &node)
Definition: distributed_node.h:155
static void to_json(json &j, const NodePtrWrapper< T > &node)
Definition: distributed_node.h:104
static NodePtrWrapper< T > from_json(const json &j)
Definition: distributed_node.h:118