Belle II Software development
AbstractNSMCallback.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#ifndef _Belle2_AbstractNSMCallback_hh
9#define _Belle2_AbstractNSMCallback_hh
10
11#include "daq/slc/nsm/Callback.h"
12#include "daq/slc/nsm/NSMMessage.h"
13#include "daq/slc/nsm/NSMNode.h"
14#include "daq/slc/nsm/NSMCommand.h"
15
16#include <string>
17#include <vector>
18
19namespace Belle2 {
24
25 class NSMCommunicator;
26 typedef std::map<std::string, NSMNode> NSMNodeMap;
27 typedef std::map<std::string, NSMNodeMap> NSMNodeMapMap;
28
29 class AbstractNSMCallback : public Callback {
30
31 friend class NSMCallback;
32
33 public:
34 AbstractNSMCallback(int timeout);
35 virtual ~AbstractNSMCallback() override {}
36
37 public:
38 int addDB(const DBObject& obj);
39 bool get(const NSMNode& node, const std::string& name, int& val, int timeout = 5);
40 bool get(const NSMNode& node, const std::string& name, float& val, int timeout = 5);
41 bool get(const NSMNode& node, const std::string& name, std::string& val, int timeout = 5);
42 bool get(const NSMNode& node, const std::string& name, std::vector<int>& val, int timeout = 5);
43 bool get(const NSMNode& node, const std::string& name, std::vector<float>& val, int timeout = 5);
44 bool set(const NSMNode& node, const std::string& name, int val, int timeout = 5);
45 bool set(const NSMNode& node, const std::string& name, float val, int timeout = 5);
46 bool set(const NSMNode& node, const std::string& name, const std::string& val, int timeout = 5);
47 bool set(const NSMNode& node, const std::string& name, const std::vector<int>& val, int timeout = 5);
48 bool set(const NSMNode& node, const std::string& name, const std::vector<float>& val, int timeout = 5);
49 bool get(const NSMNode& node, NSMVHandler* handler, int timeout = 5);
50 bool get(const DBObject& obj);
51 bool get(const std::string& name, int& val) { return get("", name, val); }
52 bool get(const std::string& name, float& val) { return get("", name, val); }
53 bool get(const std::string& name, std::string& val) { return get("", name, val); }
54 bool get(const std::string& name, std::vector<int>& val) { return get("", name, val); }
55 bool get(const std::string& name, std::vector<float>& val) { return get("", name, val); }
56 bool set(const std::string& name, int val) { return set("", name, val); }
57 bool set(const std::string& name, float val) { return set("", name, val); }
58 bool set(const std::string& name, const std::string& val) { return set("", name, val); }
59 bool set(const std::string& name, const std::vector<int>& val) { return set("", name, val); }
60 bool set(const std::string& name, const std::vector<float>& val) { return set("", name, val); }
61 bool get(const std::string& node, const std::string& name, int& val) { return get_t(node, name, val); }
62 bool get(const std::string& node, const std::string& name, float& val) { return get_t(node, name, val); }
63 bool get(const std::string& node, const std::string& name, std::string& val) { return get_t(node, name, val); }
64 bool get(const std::string& node, const std::string& name, std::vector<int>& val) { return get_t(node, name, val); }
65 bool get(const std::string& node, const std::string& name, std::vector<float>& val) { return get_t(node, name, val); }
66 bool set(const std::string& node, const std::string& name, int val) { return set_t(node, name, val); }
67 bool set(const std::string& node, const std::string& name, float val) { return set_t(node, name, val); }
68 bool set(const std::string& node, const std::string& name, const std::string& val) { return set_t(node, name, val); }
69 bool set(const std::string& node, const std::string& name, const std::vector<int>& val) { return set_t(node, name, val); }
70 bool set(const std::string& node, const std::string& name, const std::vector<float>& val) { return set_t(node, name, val); }
71 int wait(double timeout = 5);
72 static NSMCommunicator& wait(const NSMNode& node, const NSMCommand& cmd,
73 double timeout = 5);
74 // cppcheck-suppress missingOverride ; this is the base declaration
75 virtual void notify(const NSMVar& var) = 0;
76 bool try_wait();
77
78 public:
79 NSMNode& getNode() { return m_node; }
80 const NSMNode& getNode() const { return m_node; }
81 void setNode(const NSMNode& node) { m_node = node; }
82 int getTimeout() const { return m_timeout; }
83 void setTimeout(int timeout) { m_timeout = timeout; }
84
85 protected:
86 virtual bool perform(NSMCommunicator& com) = 0;
87 static void readVar(const NSMMessage& msg, NSMVar& var);
88
89 private:
90 NSMNode m_node;
91 int m_timeout;
92 NSMNodeMapMap m_node_v_m;
93
94 public:
95 bool get(const NSMNode& node, NSMVar& var, int timeout = 5) ;
96 bool set(const NSMNode& node, const NSMVar& var, int timeout = 5);
97
98 private:
99 template<typename T>
100 bool get_t(const NSMNode& node, const std::string& name,
101 T& val, int timeout, NSMVar::Type type = NSMVar::NONE, int len = 0)
102 {
103 NSMVar var(name);
104 var.setNode(node.getName());
105 if (get(node, var, timeout)) {
106 if (type == NSMVar::NONE ||
107 (type == var.getType() &&
108 ((len == 0 && var.getLength() == 0) ||
109 (len > 0 && var.getLength() > 0)))) {
110 var >> val;
111 return true;
112 }
113 }
114 return false;
115 }
116 template<typename T>
117 bool get_t(const std::string& node, const std::string& name, T& val)
118 {
119 NSMVHandler* handler = getHandler_p(node, name);
120 if (handler) {
121 NSMVar var;
122 handler->handleGet(var);
123 var >> val;
124 return true;
125 }
126 return false;
127 }
128 template<typename T>
129 bool set_t(const std::string& node, const std::string& name, const T& val)
130 {
131 NSMVHandler* handler = getHandler_p(node, name);
132 if (handler) {
133 handler->set(val);
134 if (node.size() == 0) {
135 notify(handler->get());
136 }
137 return true;
138 }
139 return false;
140 }
141 };
142
144};
145
146#endif
Abstract base class for different kinds of events.