salsa  0.4.0
Node.hh
1 #pragma once
2 
3 // TODO? #include <Object.hh>
4 #include <json/json.h>
5 #include <memory>
6 #include <salsa.hh>
7 #include <string>
8 
9 #include "NodeInfo.pb.h"
10 #include <Publisher.hh>
11 
12 namespace Salsa {
21 
22 class Node : public std::enable_shared_from_this<Node> // TODO ? public Object
23 {
24 public:
25  Node(std::string name = "", std::string uuid = "");
26  virtual ~Node();
27 
28  virtual void print() const;
29  virtual void json(Json::Value & root);
30  virtual void publish();
31 
33  std::string name() const { return mpNodeInfo->name(); }
35  std::string uuid() const { return mpNodeInfo->uuid(); }
37  std::weak_ptr<Node> parent() const { return mpParent; }
39  std::vector<std::shared_ptr<Node>> nodes() const { return mChildNodes; }
40 
42  void name(std::string n) { mpNodeInfo->set_name(n); }
44  void uuid(std::string uuid) { mpNodeInfo->set_uuid(uuid); }
46  void parent(std::weak_ptr<Node> node) { mpParent = node; }
47 
49  void add(std::shared_ptr<Node> node)
50  {
51  mChildNodes.push_back(node);
52  node->parent(shared_from_this());
53  }
55  std::shared_ptr<Node> find(std::string name) const;
56 
58  void removeByUUID(std::string uuid);
59 
61  void add(std::shared_ptr<Publisher> pPublisher) { mPublishers.push_back(pPublisher); }
63  std::vector<std::shared_ptr<Publisher>> publishers() const { return mPublishers; }
65  NodeInfo * nodeInfo() const { return mpNodeInfo; }
66 
67 protected:
68  NodeInfo * mpNodeInfo{new NodeInfo()};
69  std::weak_ptr<Node> mpParent;
70  std::vector<std::shared_ptr<Node>> mChildNodes = {};
71  std::vector<std::shared_ptr<Publisher>> mPublishers = {};
72 };
73 } // namespace Salsa
std::shared_ptr< Node > find(std::string name) const
Find node by name.
Definition: Node.cc:113
std::vector< std::shared_ptr< Publisher > > mPublishers
List of publishers.
Definition: Node.hh:71
void add(std::shared_ptr< Publisher > pPublisher)
Adds publisher to the node.
Definition: Node.hh:61
std::weak_ptr< Node > mpParent
Parent node.
Definition: Node.hh:69
Base Node class.
Definition: Node.hh:22
Node(std::string name="", std::string uuid="")
Definition: Node.cc:5
virtual void publish()
Definition: Node.cc:150
void add(std::shared_ptr< Node > node)
Adds node to the list of nodes.
Definition: Node.hh:49
virtual void print() const
Definition: Node.cc:91
void uuid(std::string uuid)
Sets node uuid.
Definition: Node.hh:44
void removeByUUID(std::string uuid)
Remove node by uuid.
Definition: Node.cc:130
NodeInfo * mpNodeInfo
Node Info.
Definition: Node.hh:68
std::weak_ptr< Node > parent() const
Returns parent node.
Definition: Node.hh:37
std::string uuid() const
Returns node UUID.
Definition: Node.hh:35
void parent(std::weak_ptr< Node > node)
Sets parent.
Definition: Node.hh:46
virtual void json(Json::Value &root)
Definition: Node.cc:34
virtual ~Node()
Definition: Node.cc:17
std::vector< std::shared_ptr< Node > > mChildNodes
List of nodes.
Definition: Node.hh:70
std::vector< std::shared_ptr< Node > > nodes() const
Returns nodes.
Definition: Node.hh:39
void name(std::string n)
Sets node name.
Definition: Node.hh:42
std::vector< std::shared_ptr< Publisher > > publishers() const
Returns publishers.
Definition: Node.hh:63
std::string name() const
Returns node name.
Definition: Node.hh:33
NodeInfo * nodeInfo() const
Returns Node Info.
Definition: Node.hh:65